#include <mega328p.h>
interrupt [13] void ext_int(void)
{
PORTB ^= (1<<0);
}
void main(void)
{
DDRB.0 = 1;
PORTB.0 = 0;
TCCR1B = (1<<WGM12)|(1<<CS12);
TCNT1H = 0;
OCR1BH = 31249;
TIMSK1 = (1<<OCIE1B);
#asm ("sei");
while(1) {}
}
There was no difference,in proteus both OCR1A and B workDatasheet says CTC mode uses OCR1A rather than OCR1B. Presonally I don't remember, too long that I did AVR programming.
HiHi,
It may work "by accident" ... if OCR1A still contains the correct value from a previous program
--> power off the AVR completely for more than 10 seconds.
Then check if the timing still is correct.
Show your actual complete code.
Klaus
#include <mega328p.h>
void TCT(void)
{
TCCR1A = (1<<WGM12);
TCCR1B = (1<<CS12);
TCNT1H = 0;
OCR1AH = 200;
TIMSK1 |= (1<<OCIE1B);
#asm("sei");
}
interrupt [13] void ext_int(void)
{
PORTB ^= (1<<0);
}
void main(void)
{
DDRB.0 = 1;
PORTB.1 = 0;
TCT();
while(1) {}
}
WGM12 isn´t part of TCCR1A .. it belongs to TCCR1BTCCR1A = (1<<WGM12);
--> now should be: TCCR1B = (1<<CS12 | 1<<WGM12)TCCR1B = (1<<CS12);
I recommend to use ful 16 bit registerOCR1AH = 200;
CTC uses OCIE1ATIMSK1 |= (1<<OCIE1B);
CTC usues OCIE1Ainterrupt [13] void ext_int(void)
There are 2 problems:Hi
I see some issues: (my code is pseudo code)
(BTW: I´m not familiar with M328, thus I had to read the datasheet. The same you need to do)
WGM12 isn´t part of TCCR1A .. it belongs to TCCR1B
--> set TCCR1A = 0
--> now should be: TCCR1B = (1<<CS12 | 1<<WGM12)
I recommend to use ful 16 bit register
--> OCR1A = ...
CTC uses OCIE1A
--> TIMSK1 |= (1<<OCIE1A);
CTC usues OCIE1A
--> use int 12
*****
Your solution needs interrupt, writing an ISR and processing power.
You could do the same without interrupt, without ISR and without processing power by connecting the LED to an PWM output and using the PWM feature.
This is no problem for low frequency blinking a LED. But it could be used - for example - with LED brightness control... which basically is the same as blinking a LED, but with much higher frequency and variable duty cycle.
Klaus
I´m sorry for this. This is my mistake. You are right, they all have WGM12 in the TCCR1BI also checked ATMEGA328,ATMEGA328P,ATMEGA328PB,ATMEGA328PU and they all have WGM12 in TCCR1B
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
void TCT(void)
{
TCCR1A = 0x00;
TCCR1B = (1<<WGM12)|(1<<CS12);
TCNT1 = 0;
OCR1A = 31249;
TIMSK1 |= (1<<OCIE1A);
asm("sei");
}
ISR (TIMER1_COMPA_vect)
{
PORTB ^= (1<<0);
}
int main()
{
DDRB= (1<<DDB0);
PORTB = (0<<PORTB0);
TCT();
while(1);
}
If you want to check the compiler for mistakes then you have to check the assembler output of the compiler.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?