AVR Sample Code For LED Flashing
AVR Sample Code For LED Flashing
Sample code for AVR ATmega16. //LED Flashing #include <avr/io.h> #include <util/delay.h> #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif int main(void) { sbi(DDRD,1); while(1) { sbi(PORTD,1); _delay_ms(4000); cbi(PORTD,1); _delay_ms(4000); } return 0; }
Embedded C code for Two 7 Segment displays
Sample code for Two 7 Segment displays using delay subroutine. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.
PORTD DDRD
PORTB DDRB
#define ZERO #define ONE #define TWO #define THREE #define FOUR #define FIVE #define SIX #define SEVEN #define EIGHT #define NINE #define DOT
0x77 0x41 0x3B 0x6B 0x4D 0x6E 0x7E 0x43 0x7F 0x6F 0x80
4 5 6 7
//(left most)
//(right most)
#endif
DISPLAY_DDR=0xFF; DISPLAY_PORT=0x00;
DISPLAY_CTRL_DDR=0xff; DISPLAY_CTRL_PORT=0x00;
_delay_ms(4000);
while(1) {
cbi(DISPLAY_CTRL_PORT,DIGIT1);
Sample code for Two 7 Segment displays using delay subroutine. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.
PORTD DDRD
PORTB DDRB
#define ZERO #define ONE #define TWO #define THREE #define FOUR #define FIVE #define SIX #define SEVEN #define EIGHT #define NINE #define DOT
0x77 0x41 0x3B 0x6B 0x4D 0x6E 0x7E 0x43 0x7F 0x6F 0x80
4 5 6 7
//(left most)
//(right most)
DISPLAY_DDR=0xFF; DISPLAY_PORT=0x00;
DISPLAY_CTRL_DDR=0xff; DISPLAY_CTRL_PORT=0x00;
_delay_ms(4000);
while(1) {
for(i=0;i<1000;i++) {
switch(d1) { case 0: disp1=~ZERO; break; case 1: disp1=~ONE; break; case 2: disp1=~TWO; break; case 3: disp1=~THREE; break; case 4: disp1=~FOUR; break; case 5: disp1=~FIVE; break;
case 6: disp1=~SIX; break; case 7: disp1=~SEVEN; break; case 8: disp1=~EIGHT; break; case 9: disp1=~NINE; break; }
switch(d2) { case 0: disp2=~ZERO; break; case 1: disp2=~ONE; break; case 2: disp2=~TWO; break;
case 3: disp2=~THREE; break; case 4: disp2=~FOUR; break; case 5: disp2=~FIVE; break; case 6: disp2=~SIX; break; case 7: disp2=~SEVEN; break; case 8: disp2=~EIGHT; break; case 9: disp2=~NINE; break; }
switch(d3) {
case 0: disp3=~ZERO; break; case 1: disp3=~ONE; break; case 2: disp3=~TWO; break; case 3: disp3=~THREE; break; case 4: disp3=~FOUR; break; case 5: disp3=~FIVE; break; case 6: disp3=~SIX; break; case 7: disp3=~SEVEN; break; case 8: disp3=~EIGHT;
} return 0; }
Sample code for Single 7 Segment display delay subroutine. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.
PORTD DDRD
#define THREE #define FOUR #define FIVE #define SIX #define SEVEN #define EIGHT #define NINE #define DOT
DISPLAY_PORT=ONE; _delay_ms(1000);
DISPLAY_PORT=TWO; _delay_ms(1000);
DISPLAY_PORT=THREE;
_delay_ms(1000);
DISPLAY_PORT=FOUR; _delay_ms(1000);
DISPLAY_PORT=FIVE; _delay_ms(1000);
DISPLAY_PORT=SIX; _delay_ms(1000);
DISPLAY_PORT=SEVEN; _delay_ms(1000);
DISPLAY_PORT=EIGHT; _delay_ms(1000);
DISPLAY_PORT=NINE; _delay_ms(1000);
Sample code for Simple Keypad. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.
void initports(void);
while(1) {
if(bit_is_clear(PINA,0)) { cbi(PORTB,0); sbi(PORTB,1); sbi(PORTB,2); } else { } } else { } if(bit_is_clear(PINA,1)) { _delay_ms(5); if(bit_is_clear(PINA,1)) { sbi(PORTB,0); cbi(PORTB,1); sbi(PORTB,2); } else
{ } } else { } if(bit_is_clear(PINA,2)) { _delay_ms(5); if(bit_is_clear(PINA,2)) { sbi(PORTB,0); sbi(PORTB,1); cbi(PORTB,2); } else { } } else { } } return 0; }
void initports(void) { cbi(DDRA,0); cbi(DDRA,1); cbi(DDRA,2); //set A.0 as input //set A.1 as input //set A.2 as input
PORTB = 0xFF;
return; }