Page 1 of 1

Custom interrupt for Nano Timer1

Posted: Wed Oct 02, 2024 4:41 am
by viktor_au
Hi

In the article - Arduino Uno SPWM Inverter,
https://yopiediy.xyz/
https://yopiediy.xyz/arduino-uno-invert ... sine-wave/
Author used Timer1 to create the SPWM by using one Lut (table).
The Time1 code:
TCCR1A = 0b10110000;
/* 0b10xxxxxx Clear OC1A/OC1B on compare match when up-counting. Set on compare match when down counting
0bxx11xxxx Set OC1A/OC1B on compare match when up-counting. Clear on compare match when down counting.
0bxxxxxx00 WGM1 1:0 for waveform 8 (phase freq. correct). */
TCCR1B = 0b00010001;
/* 000xxxxx
xxx10xxx WGM1 3:2 for waveform mode 8.
xxxxx001 no prescale on the counter.
*/
TIMSK1 = 0b00000001;
/* xxxxxxx1 TOV1 Flag interrupt enable. */
ICR1 = 800; /* Counter TOP value (at 16MHz XTAL, SPWM carrier freq. 10kHz, 200 samples/cycle).*/
sei(); /* Enable global interrupts.*/
DDRB = 0b00011110; /* Pin 9, 10, 11, 12 as outputs.*/

Question
How can I set the custom code for Timer1 (in Nano)?
PS
I found one example (attached), fixed by Ben years ago.
Would be great if somebody can help me.
Thanks

Re: Custom interrupt for Nano Timer1

Posted: Wed Oct 02, 2024 5:52 am
by viktor_au
Have tried the next code, but due to my C-code errors, it does not work

Re: Custom interrupt for Nano Timer1

Posted: Wed Oct 02, 2024 8:46 am
by viktor_au
I am not sure where should be the interrupt title in this code...
ISR(TIMER1_CAPT_vect) // input capture vector
{
FCM_%n(); // Call Flowcode Macro
}

Doesn't look like interrupt calls the Timer1 macro

Re: Custom interrupt for Nano Timer1

Posted: Thu Oct 03, 2024 9:13 am
by mnf
See viewtopic.php?p=107449#p107449 - where I posted an Arduino PW sine wave chart..

Might do what you need?

Martin

Re: Custom interrupt for Nano Timer1

Posted: Thu Oct 03, 2024 10:36 pm
by viktor_au
Hi Martin
The ATmega328 Timer1 can be configured (if directly accessed) into the Phase Correct Pulse Width Modulator (PWM) mode.
So far I have problems with the custom code for Timer1
TCCR1A = 0b10110000;
/* 10xxxxxx Clear OC1A/OC1B on compare match when up-counting. Set OC1A/OC1B on compare match when down counting
xx11xxxx Set OC1A/OC1B on compare match when up-counting. Clear OC1A/OC1B on compare match when down counting.
xxxxxx00 WGM1 1:0 for waveform 8 (phase freq. correct).
*/
TCCR1B = 0b00010001;
/* 000xxxxx
xxx10xxx WGM1 3:2 for waveform mode 8.
xxxxx001 no prescale on the counter.
*/
TIMSK1 = 0b00000001;
/* xxxxxxx1 TOV1 Flag interrupt enable. */
ICR1 = 800; /* Counter TOP value (at 16MHz XTAL, SPWM carrier freq. 10kHz, 200 samples/cycle).*/
sei(); /* Enable global interrupts.*/
DDRB = 0b00011110;