Ch03 AVR Programming in C
Ch03 AVR Programming in C
Systems
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
Chapter 07: AVR Programming in C
International Islamic University H-10, Islamabad, Pakistan
http://www.iiu.edu.pk
5.
6.
7.
Size
Data Range
unsigned char
8-bit
0 to 255
char
8-bit
-128 to +127
unsigned int
16-bit 0 to 65,535
int
unsigned long
32-bit 0 to 4,294,967,295
long
32-bit -2,147,483,648 to
+2,147,483,648
float
double
Example 7-1
// The following program sends 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;
return 0;
}
// Notice that the program never exits the for
// loop because if you //increment an unsigned
// char variable when it is OxFF, it will become 0.
Address
Usage
Port
Address
Usage
PORTA
$3B
Output
PORTC
$35
Output
DDRA
$3A
Direction
DDRC
$34
Direction
PINA
$39
Input
PINC
$33
Input
PORTB
$38
Output
PORTD
$32
Output
DDRB
$37
Direction
DDRD
$31
Direction
PINB
$36
Input
PIND
$30
Input
of
making a given port an input or output
port.
Setting (writing a one) to a bit in the
DDRx configures the pin as an Output.
Clearing (writing a zero) to a bit in the
DDRx configures the pin as an Input. e.g.
Imagine a person who has 0 dollars, he can
only get money, not give it. Similarly when
DDR contains 0s the port gets data.
DDRC = 0xFF;
// Configure PRTC as output
DDRA = 0x00;
// Configure PRTA for input
pins, we
the PORTx
each of
Out 0
Out 1
PORTA = 0xFF;
//turn-on the pull-up resistors
Example 7-2
// this program sends hex values for ASCII
// characters of 0,1,2,3,4,5,A,B,C,D to Port B.
#include <avr/io.h>
int main(void){
//PORTB is output
//needed if running on a
Example 7-3
// this program toggles all the bits of Port B 200 times.
#include <avr/io.h>
int main(void){
DDRB = 0xFF;
// PORTB is output
PORTB = 0xAA;
// PORTB is 10101010
unsigned char z;
for(z=0; z < 200; z++)
PORTB = ~ PORTB;
while(1);
return 0;
}
Example 7-4
// A program to send values of -4 to +4 to Port B.
#include <avr/io.h>
//standard AVR header
int main(void){
char mynum[] = {-4,-3,-2,-1,0,+1,+2,+3,+4} ;
unsigned char z;
DDRB = 0xFF;
// PORTB
is
output
for( z=0 ; z<=8 ; z++)
PORTB = mynum[z];
while(1);
// stay here
forever
return 0;
}
// Run the above program on your simulator to see how
// PORTB displays values of FCH, FDH, FEH, FFH, 00H, 01H,
// 02H, 03H, and 04H (the hex values for -4, -3, -2, -1,0,
// 1, etc.). numbers.
Example 7-5
// program to toggle all bits of Port B 50,000 times.
#include <avr/io.h>
//standard AVR header
int main(void){
unsigned int z;
DDRB = 0xFF;
//PORTB
is
output
for( z=0 ; z<50000 ; z++){
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
//stay here forever
return 0;
}
// Run the above program on your simulator to see how Port
// B toggles continuously. Notice that the maximum value
// for unsigned int is 65,535.
Example 7-6
// A program to toggle all bits of Port B 100,000 times.
//toggle PB 100,00 times
#include <avr/io.h> // standard AVR header
int main(void){
unsigned long z; // long is used because it should
// store more than 65535
DDRB = 0xFF;
// PORTB is output
for( z=0 ; z<100000 ; z++){
PORTB = 0x55;
PORTB = 0xAA;
}
while(1);
//stay here forever
return 0;
}
Example 7-7
// A program to toggle all the bits of Port B continuously
// with a 100 ms delay. Assume that the system is ATmega32
// with XTAL = 8 MHz.
#include <avr/io.h> // standard AVR header
void delay100ms(void){
// try different numbers on your
unsigned int i;
// compiler and examine the
for(i=0; i<42150; i++); // result.
}
int main(void){
DDRB = 0xFF;
while(1){
PORTB = 0xAA;
delay100ms() ;
PORTB = 0x55;
delay100ms();
}
return 0;
// PORTB is output
Example 7-8
// Write an AVR C program
// continuously with a 10
// function in Win AVR.
#include <util/delay.h>
#include <avr/io.h>
int main(void){
DDRB
=
0xFF;
while(1)
{
PORTB
=
0xAA;
_delay_ms(10);
PORTB = 0x55;
_delay_ms(10);
}
return
0;
is
output
I/O PROGRAMMING IN C:
//
LEDs are connected
Example
7-9 to pins of Port B. Write
an AVR C
I/O PROGRAMMING IN C:
//
Write an AVR 7-10
C program to get a byte of data
Example
from Port
// Port B is input
// Port C is output
I/O PROGRAMMING IN C:
//
Write an AVR 7-11
C program to get a byte of data
Example
from
// Port C. If it is less than 100, send it to Port B;
// otherwise, send it to Port D.
#include <avr/io.h> //standard AVR header
int main(void){
DDRC = 0x00;
//Port C is
input
DDRB = 0xFF;
//Port B is output
DDRD = 0xFF;
//Port D is output
unsigned char temp;
while(1){
temp = PINC;
//read from PINB
if(temp < 100 )
PORTB = temp;
else
PORTD = temp;
}
return
0;
}
//standard AVR
Port A output
Port B output
Port C output
Port D output
// bitwise AND
// bitwise OR
// bitwise XOR
// bitwise NOT
header
Port B
// continuously without disturbing the rest of the
pins of
// Port B.
#include <avr/io.h>
//standard AVR header
int main(void)
{
DDRB = 0xFF;
//PORTB is output
while(1)
{
PORTB = PORTB ^ 0b00010000;
//set bit
4 (5th bit) of PORTB
}
return 0;
}
0
1
2
3
#include <avr/io.h>
// standard AVR header
int main(void){
DDRB = 0xFF; PORTB = 0xAA; // Port B is output
while(1)
PORTB = PORTB ^ 0xFF;
return 0;
}
while (1){
if(PINB & (1<<7))
// if bit 7 of Port B is 1
0x29 to
int main(void){
unsigned char
unsigned
// make
// mask upper
PORTB
// make
it ASCII
// mask
lower
y =
// shift
it
x | 0x30;
y >> 4;
PORTC
y | 0x30;
while(1);
return
}
x, y;
// make
Ports
and C
bits
bits
to
lower
it ASCII
// stay here
0;
output
bits
of '4
// and '7' to packed BCD and display them on PORTB.
#include <avr/io.h> //standard AVR header
int main(void){
unsigned char bcdbyte;
unsigned char w = '4';
unsigned char z = '7';
DDRB
=
0xFF; // make Port B an output
w &= 0x0F;
// mask 3
w <<= 4;
// shift left to make upper BCD digit
z &= 0x0F;
// mask 3
bcdbyte = w | z; // combine to make packed BCD
PORTB = bcdbyte;
while(1);
return 0;
}
byte
7-28.
// If the data is good, send ASCII character 'G' to PORTD.
// Otherwise, send 'B' to PORTD.
#include <avr/io.h>
// standard AVR header
int main(void){
unsigned char mydata[] = {0x25,0x62,0x3F,0x52,0xE8};
unsigned char chksum = 0;
unsigned char x;
DDRD = 0xFF;
// make
Port D an output
for( x=0 ; x<5 ; x++ ) // add them together
chksum = chksum + mydata[x] ;
if(chksum == 0)
PORTD = 'G';
else
PORTD = 'B';
while(1); return 0;
}
hex) to
// decimal and display the digits on PORTB, PORTC, PORTD.
#include <avr/io.h>
//standard AVR header
int main(void){
unsigned char x, binbyte, d1, d2, d3;
DDRB = DDRC = DDRD = 0xFF; //Ports B, C and D are output
binbyte = 0xFD;
//binary (hex) byte
x = binbyte / 10;
// divide by 10
d1 = binbyte % 10;
// find remainder
(LSD)
d2 = x % 10;
// middle digit
d3 = x / 10;
// most-significant digit(MSD)
PORTB = d1;
PORTC = d2;
PORTD = d3;
while(1);
return 0;
}
Function Name
Description
Bytes
Chip
Bytes
Chip
Bytes
ATmega8
512
ATmega 16
512
ATmega32
1024
ATmega64
2048
ATmegal28
4096
ATmega256RZ
4096
ATmega640
4096
ATmegal280
4096
ATmega2560
4096
EEPROM Registers
EEPROM Registers
EEPROM Registers
EEPROM Registers
= 0x5F; //write
EEDR
= a; //write
// EECR |= 0x06;
EECR |= (1<<EEMWE)|(1<<EEWE);
while(EECR&(1<<EEWE)); //wait for last write to finish
while(1);
return
}
0;
register
register
to
PORTB
EEPROM Access in C
// Write an AVR C program to store a' into location
// Ox005F of EEPROM then read from EEPROM.
#include <avr/io.h> //standard AVR header
int main(void){
DDRB = 0xFF;
//make PORTB an output
while(EECR&(1<<EEWE)); //wait for last write to finish
EEAR = 0x5F; //write 0x5F to address register
EEDR = 'a'; //write a to data register
// EECR |= 0x06;
EECR |= (1<<EEMWE)|(1<<EEWE);
while (EECR & (1<<EEWE));
EEAR = 0x5F;
// write 0x5F to address register
EEDR = 0x00;
//start EEPROM read by writing EERE
EECR |= (1<<EERE);
//move data from data register to PORTB
PORTB = EEDR;
while(1);
return 0;
}