Ch03 AVR Programming in C
Ch03 AVR Programming in C
and
Microcontroller
Chapter 03: AVR Programming in C
2.
3.
4.
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
16-bit
-32,768 to +32,767
unsigned long
32-bit
0 to 4,294,967,295
long
32-bit
-2,147,483,648 to +2,147,483,648
float
32-bit
1.175e-38 to 3.402e38
double
32-bit
1.175e-38 to 3.402e38
ATmega16/mega32 pinout
Vital Pins:
1. Power
VCC
Ground
2. Crystal
XTAL1
XTAL2
3. Reset
2. I/O pins
. PORTA, PORTB,
PORTC, PORTD
3. Internal ADC pins
.
AREF, AGND,
AVCC
1.
Address
Usage
Port
Address
PORTA
$3B
Output
DDRA
$3A
PINA
Usage
PORTC
$35
Output
Direction
DDRC
$34
Direction
$39
Input
PINC
$33
Input
PORTB
$38
Output
PORTD
$32
Output
DDRB
$37
Direction
DDRD
$31
Direction
PINB
$36
Input
PIND
$30
Input
PORTx
0
1
0
Input & high impedance
Input & Pull-up
1
Out 0
Out 1
Example 3-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
Example 3-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
Example 3-3
// this program toggles all the bits of Port B 200 times.
#include <avr/io.h>
int main(void){
DDRB = 0xFF;
PORTB = 0xAA;
// PORTB is output
// PORTB is 10101010
unsigned char z;
for(z=0; z < 200; z++) // run the next line 200 times
PORTB = ~ PORTB;
while(1);
return 0;
}
// toggle PORTB
Example 3-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 3-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 3-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
Example 3-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;
// PORTB is output
while(1){
PORTB = 0xAA;
delay100ms() ;
PORTB = 0x55;
delay100ms();
}
return 0;
}
Example 3-8
// Write an AVR C program to toggle all the pins of Port B
// continuously with a 10 ms delay. Use a predefined delay
// function in Win AVR.
// #define F_CPU 4000000UL // for 4 MHz write on top
#include <util/delay.h>
// delay loop functions
#include <avr/io.h>
// standard AVR header
int main(void){
DDRB = 0xFF;
while(1)
{
PORTB = 0xAA;
_delay_ms(10);
PORTB = 0x55;
_delay_ms(10);
}
return 0;
}
// PORTB is output
Example 3-9
// LEDs are connected to pins of Port B. Write an AVR C
// program that shows the count from 0 to FFH (0000 0000
// to 1111 1111 in binary) on the LEDs.
#include <avr/io.h>
int main(void)
{
DDRB = 0xFF;
while (1)
PORTB = PORTB + 1;
return 0;
}
Example 3-10
// 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;
}
return 0;
}
Example 3-11
// Write an AVR C program to get a byte of data 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
PORTC = 0xFF;
// activate pullups
unsigned char temp;
while(1){
temp = PINC;
//read from PINB
if(temp < 100 )
PORTB = temp;
else
PORTD = temp;
}
return 0;
}
Example 3-12
// Run the following program on your simulator and examine the results.
#include <avr/io.h>
//standard AVR header
int main(void) {
DDRA = 0xFF; //make Port A output
DDRB = 0xFF; //make Port B output
DDRC = 0xFF; //make Port C output
DDRD = 0xFF; //make Port D output
PORTA = 0x35 & 0x0F;
// bitwise AND
PORTB = 0x04 | 0x68;
// bitwise OR
PORTC = 0x54 ^ 0xF0;
// bitwise XOR
PORTD =~ 0x55;
// bitwise NOT
while(1);
return 0;
}
Example 3-13
// Write an AVR C program to toggle only bit 4 of 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;
}
Example 3-14
// Write an AVR C program to monitor bit 5 of port C. If it is HIGH, send 55H
// to Port B; otherwise, send AAH to Port B.
#include <avr/io.h> // standard AVR header
int main(void){
DDRB = 0xFF;
DDRC = 0x00;
// PORTB is output
// PORTC is input
while(1){
if (PINC & 0b00100000)
PORTB = 0x55;
else
PORTB = 0xAA;
}
return 0;
Example 3-15
// A door sensor is connected to bit 1 of Port B, and an LED is connected to bit 7 of
// Port C. Write an AVR C program to monitor the door sensor and, when it opens,
// turn on the LED.
#include <avr/io.h>
//standard AVR header
int main(void){
DDRB = DDRB & 0b11111101;
//pin 1 of Port B is input
DDRC = DDRC | 0b10000000;
//pin 7 of Port C is output
while(1){
if (PINB & 0b00000010) //check pin 1(2nd pin) of PINB
PORTC = PORTC | 0b10000000;
//set pin 7 (8th pin) of PORTC
else
PORTC = PORTC & 0b01111111;
//clear pin 7 (8th pin) of PORTC
}
return 0;
}
Example 3-16
// The data pins of an LCD are connected to Port B. The information is latched into the
// LCD whenever its Enable pin goes from HIGH to LOW. The enable pin is connected
// to pin 5 of Port C (6th pin). Write a C program to send "The Earth is but One
// Country" to this LCD.
#include <avr/io.h>
//standard AVR header
int main(void){
unsigned char message[] = "The Earth is but One Country";
unsigned char z;
DDRB = 0xFF;
//Port B is output
DDRC = DDRC | 0b00100000;
//pin 5 of Port C is output
for ( z = 0; z < 28; z++){
PORTB = message[z] ;
PORTC = PORTC | 0b00100000;
//pin LCD_EN of Port C is 1
PORTC = PORTC & 0b11011111;
//pin LCD_EN of Port C is 0
}
while (1); return 0;
}
Example 3-17
// Write an AVR C program to read pins 1 and 0 of Port B and issue an ASCII
// character to Port D
#include <avr/io.h>
//standard AVR header
int main(void){
unsigned char z;
DDRB = DDRB & 0b11111100 ;
// make Port B an input
DDRD = 0xFF;
// make Port D an output
while(1){
// repeat forever
z = PINB;
// read PORTB
z = z & 0b00000011; // mask the unused bits
switch(z){
// make decision
case(0): PORTD = '0'; break;
// issue ASCII 0
case(1): PORTD = '1'; break;
/ issue ASCII 1
case(2): PORTD = '2'; break;
// issue ASCII 2
case(3): PORTD = '3'; break;
// issue ASCII 3
}
}
return 0;
}
Example 3-18
// Write an AVR C program to monitor bit 7 of Port B. If it is 1 make bit 4 of Port B
// input; otherwise, change pin 4 of Port B to output.
#include <avr/io.h>
//standard AVR header
int main(void){
DDRB = DDRB & 0b01111111; //bit 7 of Port B is input
// DDRB &= 0b01111111;
while (1){
if(PINB & 0b10000000)
//bit 4 of Port B is input
DDRB = DDRB & 0b11101111;
// DDRB &= 0b11101111;
else
DDRB = DDRB | 0b00010000; //bit 4 of Port B is output
// DDRB |= 0b00010000;
}
return 0;
Example 3-19
// Write an AVR C program to get the status of bit 5 of Port B and send it to bit 7 of port C
// continuously.
#include <avr/io.h>
//standard AVR header
int main(void){
DDRB = DDRB & 0b11011111;
// bit 5 of Port B is input
// DDRB &= 0b11011111;
// using compound Assignment
DDRC = DDRC | 0b10000000;
// bit 7 of Port C is output
// DDRC |= 0b10000000;
// using compound Assignment
while (1){
if(PINB & 0b00100000) //set bit 7 of Port C to 1
PORTC = PORTC | 0b10000000;
PORTC |= 0b10000000;
else
PORTC = PORTC & 0b01111111; //clear bit 7 of Port C to 0
PORTC &= 0b01111111;
}
return 0;
}
Example 3-20
// Write an AVR C program to toggle all the pins of Port B continuously.
#include <avr/io.h> // standard AVR header
int main(void){
DDRB = 0xFF;
// Port B is output
PORTB = 0xAA;
while(1)
{ PORTB = ~ PORTB; }
// toggle PORTB
return 0;
}
#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;
}
Example 3-21
// Write an AVR C program to monitor bit 7 of Port B. If it is 1, make bit 4 of
// Port B input; else, change pin 4 of Port B to output.
#include <avr/io.h>
// standard AVR header
int main(void) {
DDRB = DDRB & ~(1<<7); // bit 7 of Port B is input
while (1){
if(PINB & (1<<7))
// if bit 7 of Port B is 1
else
DDRB = DDRB | (1<<4);
}
return 0;
}
Example 3-22
// Write an AVR C program to get the status of bit 5 of Port B and send it to bit 7 of
// port C continuously.
#include <avr/io.h>
// standard AVR header
int main(void){
DDRB = DDRB & ~(1<<5); // bit 5 of Port B is input
DDRC = DDRC | (1<<7); // bit 7 of Port C is output
while (1){
if(PINB & (1<<5))
PORTC = PORTC | (1<<7); // set bit 7 of Port C to 1
else
PORTC = PORTC & ~(1<<7); // clear bit 7 of Port C to 0
}
return 0;
}
Example 3-23
// A door sensor is connected to the port B pin 1, and an LED is connected to port C
// pin 7. Write an AVR C program to monitor the door sensor and, when it opens, turn
// on the LED.
#include <avr/io.h> // standard AVR header
#define LED
7
#define SENSOR 1
int main(void){
DDRB = DDRB & ~(1<<SENSOR);
// SENSOR pin is input
DDRC = DDRC | (1<< LED);
// LED pin is output
while(1){
if(PINB & (1<<SENSOR))
// check SENSOR pin of PINB
PORTC = PORTC | (1<<LED);
// set LED pin of Port C
else
PORTC = PORTC & ~(1<<LED); // clear LED pin of Port C
}
return 0;
}
int main(void){
unsigned char x, y;
unsigned char mybyte = 0x29;
DDRB = DDRC = 0xFF; // make Ports B and C output
x = mybyte & 0x0F;
PORTB = x | 0x30;
// make it ASCII
y = y >> 4;
PORTC = y | 0x30;
while(1);
return 0;
}
// make it ASCII
// stay here
Example 3-25
// Write an AVR C program to convert ASCII digits 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;
}
Example 3-26
// Write an AVR C program to take two BCD numbers 0x74 from PORTC. Swap these
// numbers to 0x74 and show the result on PORTC
#include <avr/io.h> // standard AVR header
int main(void){
unsigned char c,d,e;
DDRC = 0x00;
// PORTC is input port
DDRB = 0xFF;
// PORTB is output port
while(1){
c = PINC;
// take input 0x74
d = c & 0x0F;
// d = 0x04
e = c & 0xF0;
// e = 0x70
d = d << 4;
// d = 0x40
e = e >> 4;
// d = 0x07
PORTB = d|e;
// PORTB = 0x47
}
return 0;
}
Example 3-27
// Write an AVR C program to calculate the checksum byte for the data given in
// Example 7-26.
#include <avr/io.h>
int main(void){
// standard AVR header
unsigned char mydata[] = { 0x25, 0x62, 0x3F, 0x52};
unsigned char x, sum = 0, chksumbyte;
DDRA = 0xFF;
// make Port A output
DDRB = 0xFF;
// make Port B output
DDRC = 0xFF;
// make Port C output
for(x=0; x<4; x++){
PORTA = mydata[x];
// issue each byte to PORTA
sum = sum + mydata[x] ; // add them together
}
PORTB = sum;
// issue the sum to PORTB
chksumbyte = ~sum + 1;
PORTC = chksumbyte;
return 0;
}
Example 3-28
// Write a C program to perform step (b) of Example 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;
}
Example 3-29
// Write an AVR C program to convert 11111101 (FD 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;
}
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
15
14
13
12
11
10
EEARH
EEAR9
EEAR8
EEARL
EEAR7
EEAR6
EEAR5
EEAR4
EEAR3
EEAR2
EEAR1
EEAR0
Bit
EEPROM Registers
EECR
EERIE
EEMWE
EEWE
EERE
Bit
EEPROM Registers
The When you set EEMWE to one, the hardware clears the bit
to zero after four clock cycles. This prevents unwanted write
operations on EEPROM contents.
Write on EEPROM
2.
3.
4.
5.
Set the EEMWE bit to 1, and Within four clock cycles after
setting EEMWE, set EEWE to one.
Wait until EEWE becomes zero.
Example 3-30
// Write an AVR C program to store a' into location 0x005F of EEPROM.
#include <avr/io.h> // standard AVR header
void EEPROM_write(unsigned int Address, unsigned char Data){
while(EECR & (1<<EEWE));
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
}
int main(void){
EEPROM_write(0x5F, 'a');
while(1);
return 0;
}
Example 3-31
// Write an AVR C program to read the content of location 0x005F of EEPROM
// into PORTB.
#include <avr/io.h>
}
int main(void){
DDRB = 0xFF;
PORTB = EEPROM_read(0x5F);
while(1);
return 0;
}
(1/2)
// Write an AVR C program to store a' into location 0x005F of EEPROM then read
// from EEPROM.
#include <avr/io.h> // standard AVR header
void EEPROM_write (unsigned int Address, unsigned char Data) {
while(EECR & (1<<EEWE));
// Wait for completion of previous write
EEAR = Address; // Set up address register
EEDR = Data;
// Set up data register
EECR |= (1<<EEMWE); // Write logical one to EEMWE
EECR |= (1<<EEWE);
// Start eeprom write by setting EEWE
}
unsigned char EEPROM_read(unsigned int Address) {
while(EECR & (1<<EEWE)); // Wait for completion of previous write
EEAR = Address; // Set up address register
EECR |= (1<<EERE); // Start eeprom read by writing EERE
return EEDR;
// Return data from data register
}
(2/2)