Professional Documents
Culture Documents
Selection of C programming
Easier and less time consuming
Easy to modify and update
Direct available functions can be used from libraries
C codes are portable to other microcontrollers
Produces hex file of larger size limitation
C data types for AVR in C
Type Storage size Value range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
-32,768 to 32,767 or -
int 2 or 4 bytes
2,147,483,648 to 2,147,483,647
0 to 65,535 or 0 to
unsigned int 2 or 4 bytes
4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
-2,147,483,648 to
long 4 bytes
2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
Write an AVR C program to send values 00-
FF to Port B
#include <avr/io.h> //standard AVR header
int main(void)
{
unsigned char z;
DDRB = 0xFF; //PORTB is output
for(z = 0; z <= 255; z++)
PORTB = z;
}
Bit level Operations in C
There are basically 6 types of Bitwise operators.
Bitwise OR operator denoted by ‘|‘
Bitwise AND operator denoted by ‘&‘
Bitwise Complement or Negation Operator denoted by
‘~‘
Bitwise Right Shift & Left Shift denoted by ‘>>‘ and
‘<<‘ respectively
Bitwise XOR operator denoted by ‘^‘
Number system
Hexadecimal Numbers in C/C++ program have a ’0x’ prefix
and for Binary we have a ’0b’ prefix.
Without these prefix any number will be considered as a
Decimal number by the compiler.
Consider:
int n = 0x1A2; Here its is implied that n will be 0x000001A2
since n is a 32bit number. Here number will be automatically
padded with Zeros form right hand side.
int x = 0b0010; Here too zeros (28 0s) will be padded on the
left to make x 32bit.
int n = (1<<5); Here the 5th bit will be 1 and rest all will
be made zeros by the compiler.
In short 0s are padded towards the left as required.
}
Write an AVR C code to send values -5 to +5 on
Port B
#include <avr/io.h> //standard AVR header
int main(void)
{
char num[] = {-4,-3,-2,-1,0,1,2,3,4};
unsigned char z;
DDRB = 0xFF; //PORTB is output
for(z = 0; z <=8; z++)
PORTB = num[z];
}
Write an AVR C program to get a byte of data from
Port B, and then send it to Port C.
#include <avr/io.h> //standard AVR header
int main(void)
{
unsigned char temp;
DDRB = 0x00; //Port B is input
DDRC = 0xFF; //Port C is output
while(1)
{
temp = PINB;
PORTC = temp;
}
}