so what I am trying to do is that I am trying to make the led blink for a certain number of times before it turns off. I want to avoid using delays so I tried
utilizing the interrupt and I also used a placeholder value like x to count the number of times it goes through interrupt, so one it reaches that value it will turn off, however I am having trouble getting it to work, any ideas
/*
* GccApplication3.c
*
* Created: 2015-04-01 6:13:58 AM
* Author: Bangladesh
*/
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define stp_led PB0
#define dir_led PB1
#define led_port PORTB
#define led_ddr DDRB
#define F_CPU 16000000UL
int x = 0;
int main(void)
{
///////////////////////////////////////////////////////
led_ddr |= (1 << stp_led); //enable led as an output pin
led_ddr |= (1 << dir_led);
//led_port |= (1<<stp_led);
led_port |= (1<<dir_led);
///////////////////////////////////////////////////////
TCCR1B |= (1 << WGM12); // configuring timer 1 for ctc mode
OCR1A = 15625;
TIMSK1 |= (1 << OCIE1A); // enable ctc interrupt
TCCR1B |= ((1 << CS12) | (1<< CS10)); //Start at 1024 prescaler
sei(); //Enable global interrupts
//Sets ctc compare value to 1hz at 1mhz AVR clock, with prescaler of 64
while(1)
{
}
} //end of while loop
ISR(TIMER1_COMPA_vect)
{
function_details (10, 1);
}
void function_details (stp,dir){
if (dir == 1){
led_port |= (1 << dir_led);
}
else if (dir == 0){
led_port &= ~ (1 << dir_led);
}
while (1){
led_port ^= (1 << stp_led);
if (x == stp){
led_port &= ~ (1 << stp_led);
return 0;
}
x++;
}
}
if()
statement contains areturn;
, so whenx==stp
, it will break the loop by returning from the function. That said, there are two problems with that. The first is that x is not set to 0 before the function call, so you won't necessarily getstp
blinks as it will start counting from whatever the last x was - in the code above first it will be 10 blinks then INT_MAX-10 after that. Secondly, x should either be local to that function, or set volatile. Now granted the approach is not a good design practice. \$\endgroup\$