Hello World Project AVR
When we start learning 'C' or any new stuff the first program we write is generally known as the 'Hello world Program'. In ANSI 'C' we write a program to echo 'Hello world' on screen but in embedded 'C' the hello world program is 'Blinking LEDs'. On-Off a LED connected to a port pin. It is the simplest program to start your journey with AVR Microcontrollers. So lets have a look...
Schematic Diagram -
/*********************************************************************** Example - Blink an LED connected to PORTC_5 Note - Connect a LED at PORTC_5 and flash it at a particular interval. Here LED is connected in sinking mode Logic1 -> LED Off Logic0 -> LED On ************************************************************************/ #include<avr/io.h> // Header file for basic avr input/output #include<util/delay.h> // header file for delay generation #define BV(x) (1<<x) // See text below int main(void) { DDRC=0xFF; // PORTC declared as output PORTC=0xFF; // PORTC is initially high to off the led initially while(1==1) // infinite loop as 1 is always equals 1 { PORTC=~(BV(5)); // led glow here, Making 5th bit of PORTC LOW _delay_ms(1000); // one second delay PORTC=BV(5); // led do not glow here _delay_ms(1000); } return 0; }
Note - Although we have connected the LED in sink mode, AVR has sufficient current output on I/O ports that it can drive an 5mm RED LED in sourcing mode also. So you can connect the LED reverse also and the same code will work.
What is BV(x) 1<<x ???
Its just a wise use of bitwise operator (shift left). What (1<<x) actually does is it shift 1 (in binary) to left by 'x' times.
Suppose you need to make Pin number 5 of PORTC=1, you can make it by writing PORTC= 1<<5;
devesh@electroons.comBest viewed at Firefox 1024x768 resolution