Question

In: Computer Science

Make a C program to blink/toggle a LED every second with the Atmega128 chip. Hint: Because...

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

Solutions

Expert Solution

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
  }
}
  • DDRC = 0xFF makes all pins on PORTC as output pins
  • PORTC = 0xFF makes all pins on PORTC Logic High (5V)
  • PORTC = 0x00 makes all pins on PORTC Logic Low (0V)
  • _delay_ms(1000) provides 1000 milliseconds delay.
  • while(1) makes an infinite loop

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);
}


Related Solutions

Make a C program to blink/toggle a LED every second with the Atmega128 chip. Hint: Because...
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 delay function for implementation b) Write code that uses a stopwatch on the third toggle
1.write a program for the msp430 to make led 1 blink at 50 percent duty cycle....
1.write a program for the msp430 to make led 1 blink at 50 percent duty cycle. 2 modify the program to make led 1 blink 5 times, make a long pause, then blink 5 more times then stop. The time delays should be carried out in subroutines
1.write a program for the msp430FR6989 to make led 1 blink at 50 percent duty cycle....
1.write a program for the msp430FR6989 to make led 1 blink at 50 percent duty cycle. 2 modify the program to make led 1 blink 5 times, make a long pause, then blink 5 more times then stop. The time delays should be carried out in subroutines
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and...
1. IN C LANGUAGE: Write a code that makes the MSP430 blink the red LED and the green LED at the same time. The two LEDs should be on at the same time and then off at the same time
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at...
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5...
For the mspfr6989 write program #2 1(b) Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop. Program #2 – Using both S1 and S2 1 Write a program like 1(b) above, but the LED1 will blink until S2 is pressed. 2 Write a program that simulates a motor coming up to speed. When S1 is pressed, LED1 blinks at 50% duty cycle for 10 cycles (indicating a motor coming up to speed)....
Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
Write a program in C that allows you to determine the endianness of your computer. Hint:...
Write a program in C that allows you to determine the endianness of your computer. Hint: use unsigned char* ptr. Your task is to explain this code. Make sure that you are detailed in your explanation by explaining the significant lines. In particular, correct explanation of lines 12, 13, 15, 18, 21, 22, 24, 25, 26 and 27 will earn 1 point each. Additionally, explain the overall purpose of the logical operations in lines 24-27 for 2 points. Your explanation...
Write the following in C language for Arduino: Write a program that increases the LED brightness...
Write the following in C language for Arduino: Write a program that increases the LED brightness in five steps with each press of the button. Remember you are going to using a digital input and the "digitalRead" command. You are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine...
Write a program for the msp430 to make led1 and led 2 alternate blinking 2. repeat...
Write a program for the msp430 to make led1 and led 2 alternate blinking 2. repeat the program to make each led blink 5 times in a sequence, then 5 times simultaneously
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT