avr sample code for led flashing

22
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); //This will turn ON LED _delay_ms(4000); cbi(PORTD,1); //This will turn OFF LED _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.

Upload: sameer-nandan

Post on 04-Jan-2016

11 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 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);         //This will turn ON LED        _delay_ms(4000);

        cbi(PORTD,1);         //This will turn OFF LED        _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.

#include <avr/io.h>

#include <util/delay.h>

Page 2: AVR Sample Code for LED Flashing

#define DISPLAY_PORT PORTD

#define DISPLAY_DDR DDRD

#define DISPLAY_CTRL_PORT PORTB

#define DISPLAY_CTRL_DDR DDRB

#define ZERO 0x77

#define ONE 0x41

#define TWO 0x3B

#define THREE 0x6B

#define FOUR 0x4D

#define FIVE 0x6E

#define SIX 0x7E

#define SEVEN 0x43

#define EIGHT 0x7F

#define NINE 0x6F

#define DOT 0x80

#define DIGIT1 4 //(left most)

#define DIGIT2 5

#define DIGIT3 6

#define DIGIT4 7 //(right most)

#ifndef cbi

Page 3: AVR Sample Code for LED Flashing

#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)

{

int i=0;

int d1,d2,d3;

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++)

{

Page 4: AVR Sample Code for LED Flashing

sbi(DISPLAY_CTRL_PORT,DIGIT1);

cbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=d3;

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

sbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=d2;

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

cbi(DISPLAY_CTRL_PORT,DIGIT2);

sbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=d1;

_delay_us(200);

}

for(i=0;i<1000;i++)

{

sbi(DISPLAY_CTRL_PORT,DIGIT1);

cbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=~SIX;

Page 5: AVR Sample Code for LED Flashing

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

sbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=~FIVE;

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

cbi(DISPLAY_CTRL_PORT,DIGIT2);

sbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=~FOUR;

_delay_us(200);

}

}

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.

#include <avr/io.h>

#include <util/delay.h>

Page 6: AVR Sample Code for LED Flashing

#define DISPLAY_PORT PORTD

#define DISPLAY_DDR DDRD

#define DISPLAY_CTRL_PORT PORTB

#define DISPLAY_CTRL_DDR DDRB

#define ZERO 0x77

#define ONE 0x41

#define TWO 0x3B

#define THREE 0x6B

#define FOUR 0x4D

#define FIVE 0x6E

#define SIX 0x7E

#define SEVEN 0x43

#define EIGHT 0x7F

#define NINE 0x6F

#define DOT 0x80

#define DIGIT1 4 //(left most)

#define DIGIT2 5

#define DIGIT3 6

#define DIGIT4 7 //(right most)

#ifndef cbi

Page 7: AVR Sample Code for LED Flashing

#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)

{

int i=0;

int d1,d2,d3;

int disp1,disp2,disp3;

DISPLAY_DDR=0xFF;

DISPLAY_PORT=0x00;

DISPLAY_CTRL_DDR=0xff;

DISPLAY_CTRL_PORT=0x00;

_delay_ms(4000);

while(1)

{

for(d1=0;d1<10;d1++)

Page 8: AVR Sample Code for LED Flashing

{

for(d2=0;d2<10;d2++)

{

for(d3=0;d3<10;d3++)

{

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:

Page 9: AVR Sample Code for LED Flashing

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:

Page 10: AVR Sample Code for LED Flashing

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;

Page 11: AVR Sample Code for LED Flashing

}

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;

Page 12: AVR Sample Code for LED Flashing

case 7:

disp3=~SEVEN;

break;

case 8:

disp3=~EIGHT;

break;

case 9:

disp3=~NINE;

break;

}

sbi(DISPLAY_CTRL_PORT,DIGIT1);

cbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=disp3;

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

sbi(DISPLAY_CTRL_PORT,DIGIT2);

cbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=disp2;

_delay_us(200);

cbi(DISPLAY_CTRL_PORT,DIGIT1);

Page 13: AVR Sample Code for LED Flashing

cbi(DISPLAY_CTRL_PORT,DIGIT2);

sbi(DISPLAY_CTRL_PORT,DIGIT3);

DISPLAY_PORT=disp1;

_delay_us(200);

}

}

}

}

}

return 0;

}

Embedded C code for Single 7 Segment display

Sample code for Single 7 Segment display delay subroutine. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.

#include <avr/io.h>

#include <util/delay.h>

Page 14: AVR Sample Code for LED Flashing

#define DISPLAY_PORT PORTD

#define DISPLAY_DDR DDRD

#define ZERO 0x77

#define ONE 0x41

#define TWO 0x3B

#define THREE 0x6B

#define FOUR 0x4D

#define FIVE 0x6E

#define SIX 0x7E

#define SEVEN 0x43

#define EIGHT 0x7F

#define NINE 0x6F

#define DOT 0x80

int main(void)

{

DISPLAY_DDR=0xFF;

DISPLAY_PORT=0x00;

while(1)

{

DISPLAY_PORT=ZERO;

_delay_ms(1000);

Page 15: AVR Sample Code for LED Flashing

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;

Page 16: AVR Sample Code for LED Flashing

_delay_ms(1000);

DISPLAY_PORT=DOT;

_delay_ms(1000);

}

return 0;

}

Embedded C code for Simple Keypad

Sample code for Simple Keypad. Following code can be using for AVR series ICs like: ATmega8, ATmega16, ATmega32.

#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

Page 17: AVR Sample Code for LED Flashing

void initports(void);

int main(void)

{

initports();

while(1)

{

if(bit_is_clear(PINA,0))

{

_delay_ms(5); // debounce delay

if(bit_is_clear(PINA,0))

{

cbi(PORTB,0);

sbi(PORTB,1);

sbi(PORTB,2);

}

else

{

}

}

else

{

}

if(bit_is_clear(PINA,1))

{

Page 18: AVR Sample Code for LED Flashing

_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

{

}

Page 19: AVR Sample Code for LED Flashing

}

else

{

}

}

return 0;

}

void initports(void)

{

cbi(DDRA,0); //set A.0 as input

cbi(DDRA,1); //set A.1 as input

cbi(DDRA,2); //set A.2 as input

sbi(DDRB,0); //set B.0 as output

sbi(DDRB,1); //set B.1 as output

sbi(DDRB,2); //set B.2 as output

PORTB = 0xFF; //turn off all LEDs

return;

}

Page 20: AVR Sample Code for LED Flashing