In: Computer Science
Make a C program to blink/toggle a LED every second with the
Atmega128 chip.
Hint: Because the delay function has a maximum delay limit, you
will need an internal counter to accumulate delays to one
second.
a) Write code that uses the timer interrupt for implementation.
b) Write code that uses stopwatch on the second toggle in Atmel Studio
C program to blink/toggle a LED every second.
The very first step towards programming a microcontroller is Blinking a LED with a delay. Atmega32 is a very popular high performance 8 bit AVR Microcontroller. For this example project we need to use two registers DDR and PORT. DDR stands for Data Direction Register, it determines the direction (Input/Output) of each pins on the microcontroller. HIGH at DDR register makes corresponding pin Output while LOW at DDR register makes corresponding pin Input. PORT register is the output register which determines the status of each pin of a particular port. HIGH at PORT register makes corresponding pin Logic HIGH (5V) while LOW at PORT register makes corresponding pin Logic LOW (0V).
1.Download and Install Atmel Studio. You can download Atmel Studio from Atmel’s Website.
2.Open Atmel Studio
3. Select New Project
4. Select GCC C Executable Project, give a project name, solution name, location in which project is to be saved and click OK.
5. Selecting Microcontroller
Choose the microcontroller that you are going to use, here we are using Atmega32. Then Click OK.
6. Enter the Program
7. Then click on Build >> Build Solution or Press F7 to generate the hex file.
C program:
#ifndef F_CPU #define F_CPU 16000000UL // 16 MHz clock speed #endif #include <avr/io.h> #include <util/delay.h> int main(void) { DDRC = 0xFF; //Nakes PORTC as Output while(1) //infinite loop { PORTC = 0xFF; //Turns ON All LEDs _delay_ms(1000); //1 second delay PORTC= 0x00; //Turns OFF All LEDs _delay_ms(1000); //1 second delay } }
You have seen that PORT registers are used to write data to ports. Similarly to read data from ports PIN registers are used. It stand for Port Input Register. eg : PIND, PINB
You may like to set or reset individual pins of PORT or DDR registers or to know the status of a specific bit of PIN register. There registers are not bit addressable, so we can’t do it directly but we can do it through program. To make 3ed bit (PC2) of DDRC register low we can use DDRC &= ~(1<<PC2). (1<<PC2) generates the binary number 00000100, which is complemented 11111011 and ANDed with DDRC register, which makes the 3ed bit 0. Similarly DDRC |= (1<<PC2) can be used set the 3ed bit (PC2) of DDRC register and to read 3ed bit (PC2) we can use PINC & (1<<PC2). Similarly we can set or reset each bit of DDR or PORT registers and able to know the logic state of a particular bit of PIN register.
CODE THAT USES THE TIME INTERRUPT FOR IMPLEMENTATION
I am able to blink LED 1 sec on, 1sec off with below code. I would like to keep the LED on only for i.e. 50ms. How to achieve this?
int timer1_counter;
void setup() {
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
timer1_counter = 10000; //34286; // preload timer
65536-16MHz/256/2Hz
TCNT1 = timer1_counter; // preload timer
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow
interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_OVF_vect) // interrupt service routine
{
TCNT1 = timer1_counter; // preload timer
digitalWrite(LED_GREEN_PIN, digitalRead(LED_GREEN_PIN) ^ 1);
}