MCUdude / MegaCore

Arduino hardware package for ATmega64, ATmega128, ATmega165, ATmega169, ATmega325, ATmega329, ATmega640, ATmega645, ATmega649, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega3250, ATmega3290, ATmega6450, ATmega6490, AT90CAN32, AT90CAN64 and AT90CAN128
Other
386 stars 120 forks source link

Atmega64 timers1 ,3 #130

Closed AnatiliyKsl closed 4 years ago

AnatiliyKsl commented 4 years ago

when timer 1 or 3 is in CTC mode, in the interrupt you have to load the comparison registerOCR1A (OCR3A), otherwise the desired time interval does not work. //****

include <avr/io.h>

include <avr/interrupt.h>

ifndef cbi

define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))

endif

ifndef sbi

define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

endif

//LED control on standard contact

define LED_RED_ON cbi(PORTB,5)

define LED_RED_OFF sbi(PORTB,5)

uint8_t timer3=0; //** //interrupt from timer 1 in coincidence with A ISR(TIMER1_COMPA_vect) { OCR1A=12500;//does not want to work without this line (the interval does not match) if(timer3==0){cbi(PORTB,5);timer3=1;} else {sbi(PORTB,5);timer3=0;} } // void setup() { //set port led DDRB=0b01110111; PORTB=0b01110111; // set timer 1 cli(); OCR1A=12500; TCCR1A =0;// 0b00000000; //TCCR1B = 0b00001011; TCCR1B |=(1<<WGM12);//ctc 0100 TCCR1B |=(1<<CS10); TCCR1B |=(1<<CS11); TCCR3C = 0;//0b00000000; TIMSK |= (1<<OCIE1A); sei(); } void loop() { } //***** this happens both with timer 1 and with timer3 (with the corresponding register settings). This code is checked in AVR STUDIO 7, without line OCR1A = 12500; in the interrupt ISR (TIMER1_COMPA_vect). For many years I wrote programs for Atmega, both in assembler and in C, С++. Such a situation has never happened. When working with Atmega 328, setting a timer and interruption does not cause problems

MCUdude commented 4 years ago

The timer function registers are modified in init(), which is executed before setup().

What you're experiencing is probably related to the settings there:

https://github.com/MCUdude/MegaCore/blob/c33c287a1de7916ed5e421af3e0bc9e388d821e0/avr/cores/MCUdude_corefiles/wiring.c#L487-L652