Lighting an LED with Microcontroller | Atmega328p
21 Jan 2013It is very simple if you have some taste in programming. The code for lighting an LED is given below. What am doing is just enabling a particular port of microcontroller. Here it is PORTD. Before you can use a port you need to set the port as an output port. So that you can output data to that port. If you set the DDR (Data Direction Register) of PORT D (ie, DDRD) as 1, then it will act as output. If it is set to 0 it will act as input.
Code
/*
* My_Learning_Projects.c
*
* Created: 01-01-2013 AM 11:45:20
* Author: ANANTHAKRISHNAN.U.S
*/
#include<avr/io.h>
int main(void)
{
DDRD = 0b00000010; //Data register D1 is set to output
PORTD = 0b00000010; //Make PD1=1
while(1) //Infinite loop
{
}
}
The while(1) loop is an infinite loop for running the microcontroller for ever. Otherwise it will execute all the commands once and then stops. PORT D1 is the 2nd pin.(For atmega328p)