# Forum: µC & Digital Electronics AVR microcontrollers

Rate this post
 0 ▲ useful ▼ not useful
Hello.
I am beginner in microcontrollers programming.
Now a while I bought a development board Easy AVR6.
I try to make a program : flas an led every 1sec.
The next program should flash the led but my hardware doesn't respond.
Timer count = (Required Delay / Clock Time Period) - 1;
Required delay = 1 sec; Frequency = 8MHz/64= 125KHz; Period = 1/125 =
0,008ms

Timer count = (1000ms / 0,008ms) - 1 = 124.999 clock cycles
So OCR0 must be loaded with 124999, but the OCR1A is an 16-bit register,
so the maximum value is 65535 and this value 124999 is on the 17 bit.
I'm stuck here.

// Frequency = 8MHz; Prescaler = 64;

// Flash an Led every 100 msec;
// Frequency = 16MHz; Prescaler = 64;

void timer1_init()
{
TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);//CTC, Prescaler = 64
TCNT1H = 0;
TCNT1L = 0;
OCR1AH = 59392;
OCR1AL = 71;
TIMSK |= (1 << OCIE1A);
SREG_I_bit = 1;
}

{
PORTC ^= (1 << 0);
}

int main(void)
{
DDRC |= (1 << 0);
timer1_init();
while(1)
{

}
}

Rate this post
 0 ▲ useful ▼ not useful
The solution is to use a variable inside the ISR and increment this.
Once it reaches the desired count, you'll flash the LED and reset the
variable:

 ISR(TIMER1_COMPA_vect) // org IVT_ADDR_TIMER1_COMPA { static uint_8_t limit; limit++; if (limit > 1) { PORTC ^= (1 << 0); limit = 0; } } 
All you have to do now is to adjust the OCR value so that it is exactly
half a second.

Rate this post
 0 ▲ useful ▼ not useful
Sorry, theres a typo:
static uint_8_t limit;

should really be:
static uint8_t limit;

Rate this post
 0 ▲ useful ▼ not useful
I have changed something in my program. Still I am not able.
I want to make a programm who must realize the next task : flash PORTD
leds on every 2 second   (2 second stay ON, 2 second stay OFF), using
the OCR1A.
The frequency = 16MHz, prescaler = 256;
How I do it :
f = 16MHz; Prescaler = 256;
So the f = 16000KHz / 256 = 62,5KHz; T = 1 / 62,5KHz = 0,016 ms = 16
microsec.
Timer 1 is on the 16 bits, so it counts up 65536 when an Overflow flag
is set;
The Maximum Delay = 65536 * 16 microsec = 1048576 microsec = 1 sec.
So I loaded OCR1AH with 32768 approximatly 0,5 sec.
I defined a variable "counter" and I use this variable to count up 4
(0,5 sec * 4 = 2 sec).
My PORTD leds are always ON.
Look at my code :

volatile unsigned int counter;

void timer1_init()
{
TCCR1B |= (1 << WGM12)|(1 << CS12);
TCNT1H = 0;
TCNT1L = 0;
OCR1AH = 32768;
OCR1AL = 0;
TIMSK |= (1 << OCIE1A);
SREG_I_bit = 1;
counter=0;
}

{
counter++;
if(counter >= 4)
{
PORTD = ~PORTD;
counter = 0;
}
}

int main(void)
{
DDRD = 0xFF;
timer1_init();
while(1)
{

}
}

• $formula (LaTeX syntax)$