0% found this document useful (0 votes)
15 views39 pages

07 C Prgramming

Uploaded by

s135670
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views39 pages

07 C Prgramming

Uploaded by

s135670
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

ECCE4227: Embedded Systems

Chapter 7

AVR Programming in C
Topics
 Basic C program
 Data Types
 Accessing I/O registers
 Time Delays
 IO Programming in C
 Logic Operation in C
 Data serialization in C

2
Languages
 High Level Languages
 Easy to develop and
update
 C Language
 Acceptable
performance High Level Languages
(like VB)
 Easy to develop and
C Language
update
 Portable Machine Language
 Low Level Languages
 High performance
 Not portable

3
A simple C program
 Write a program that calculates the sum of
{1,3,…,13,15}

int main ()
{
unsigned int sum = 0;

for (int i = 1; i <= 15; i+=2)


sum += i;

while (1);
return 0;
}

4
Basic C program structure

5
Preprocessor
 #include and #define are the preprocessor
directives
 #include: preprocessor inserts the file specified
 Example #include <avr/io.h> is inserted in the place of the
include line (it defines memory addresses and symbolic
labels for CPU and peripheral function register addresses)
 Include files normally store function prototypes and constant
definition
 #define: simple macro substitution or to form
function-like macros
 Example #define MAX 10
 # define INC (x) (x+1) or #define SQR (a) ((a) * (a))

6
Program variables

 A variable is an addressable storage location


 Each variable must be declared to indicate size and
type of information to be stored, plus a name to be
used to reference the information
 int x,y,z; //declares 3 variables of type “int”
 char a,b; //declares 2 variables of type “char”

 Space for variables may be allocated in registers,


RAM, or ROM/Flash (for constants)

7
Data Types

Use unsigned whenever you can
 unsigned char instead of unsigned int if you can

8
Data types (cont.)
 char lng;
long c; c
lng

 int a1;
unsigned int a;
a1 a

Q1.
Q1.What
Whatisisthe
thedifference
differencebetween
betweenint
intand
andunsigned
unsignedint?
int?

9
Variables values
10

10
Data types
 Precautions must be taken:
 Floating point operations minimized whenever
possible.

Expensive in terms of memory usage and
processing power
 Appropriate data types must be used

Example count from 1 to 100, 8 bit long data
type is enough

11
Choosing optimized data type
unsigned int sum; unsigned char sum;

for (int i = 1; i <= 15; i+=2) for (char i = 1; i <= 15; i+=2)
sum += i; sum += i;

12
Choosing optimized data type

13
Accessing I/O registers
 Example 1: Write an AVR C program to
send value 0xAA to PORTD.

#include <avr/io.h>

int main ()
{
DDRD = 0xFF;
PORTD = 0xAA;

while (1);
return 0;
}

14
Accessing I/O registers
 Example 2: Write an AVR C program to
calculate PINB + PINC and send the result
to PORTD.
#include <avr/io.h>

int main ()
{
DDRB = 0x00;
DDRC = 0x00;
DDRD = 0xFF;

while (1)
PORTD = PINB + PINC;
return 0;
}

15
Accessing I/O registers
 Example 3: Write an AVR C program that
outputs 0 to 9 continuously on PORTD.
#include <avr/io.h>

int main ()
{
DDRD = 0xFF;// Port D is output

while(1)
for (unsigned char i = 0; i <= 9; i++)
PORTD = i;

return 0;
}

16
Accessing I/O registers Examples

17
Accessing I/O registers Examples

18
Accessing I/O registers Examples

19
Accessing I/O registers Examples

20
Accessing I/O registers Examples

// read from PINC

21
Time Delays in C

You can use for loop to introduce time delay

void delay(void)
{
unsigned int i;
for(i = 0; i < 42150; i++)
{

}
}

If you use for loop


 The clock frequency can change your delay

duration !
 The compiler has direct effect on delay duration!

22
Time Delays in C

You can use predefined functions of compilers to make
time delays

In Atmel Studio:

First you should include:


#define F_CPU 8000000UL
#include <util/delay.h>

and then you can use


_delay_us(200); //200 microseconds
_delay_ms(100); //100 milliseconds

 It is compiler dependent

23
I/O programming in C
Byte size I/O programming in C
DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}

 PORTX.y to access bit y of PORTX.y (Ex.


PORTB.0=1)
 Different compilers have different syntax for bit
manipulations! It is better to use
Bit-wise logical
operators 24
Bit-wise logical operators

1110 1110
1111 1111 ~ 1110
& 0000 | 0000 1011
0001 0001 --------------
-------------- -------------- 0001
0000 1110 0100
0001 1111 25
Shift operations in C
 data >> number of bits to be shifted right
 data << number of bits to be shifted left

1110 0000 >> 0000 0001 <<2


3 --------------
-------------- 0000 0100
0001 1100

26
Setting a bit in a Byte to 1
 We can use | operator to set a bit of a byte
to 1
xxxx xxxx
xxxx OR xxxx
| 0001 | 1 << 4
0000 -------------
-------------
xxx1 xxxx
PORTB |= (1<<4); //set bit 4 (5th bit) of PORTB
xxx1 xxxx
PORTB |= 0x10; //set bit 4 (5th bit) of PORTB

27
Clearing a bit in a Byte to 0
 We can use | operator to set a bit of a byte
to 1
xxxx xxxx
xxxx OR xxxx
& 1110 & ~(1 <<
1111 4)
------------- -------------
PORTB &= ~(1<<4); //clear bit 4 (5th bit) of PORTB
xxx0 xxxx xxx0 xxxx
PORTB &= 0xEF; //clear bit 4 (5th bit) of PORTB

28
Inverting a bit in a Byte to 0
 We can use ^ operator to toggle a bit of a
byte
xxxx xxxx
xxxx OR xxxx
^ 0001 ^ (1 << 4)
0000 -------------
-------------
xxxx’ xxxx
PORTB ^= (1<<4); //Toggle bit 4 (5th bit) of PORTB
xxxx’ xxxx
PORTB ^= 0x10; //Toggle bit 4 (5th bit) of PORTB

29
Checking a bit in a Byte
 We can use & operator to see if a bit in a byte is 1
or 0

xxxx xxxx
xxxx OR xxxx
& 0001 & (1 << 4)
0000 -------------
-------------
if( (PINC & (1<<4)) != 0) 000x
//check 0000
bit 4 (5th bit)
000x 0000
if((PINC & 0x10) != 0) //check bit 4 (5th bit)

if((PINC & 0x10) == 0x10) //check bit 4 (5th bit)


30
Examples

31
Examples

32
Examples

33
Examples

34
Examples

35
Examples

36
Data serialization in C

37
Data serialization in C

38
RECOMMENDED PROBLEMS
PROBLEMS HIGHLIGHTED IN RED HAVE HIGHER PRIORITY

CHAPTER-7 [AVR programming in C]

Problems:
S7.1 (1, 2, 3) S7.2 (7, 8, 9, 10,11) S7.3 (13, 14, 15, 16,
17) S7.5 (20).

39

You might also like