Custom interrupt for Nano Timer1

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
viktor_au
Posts: 367
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 45 times
Been thanked: 63 times
Contact:

Custom interrupt for Nano Timer1

Post 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
Attachments
ArdCustomCodeExample_1.fcfx
(8.57 KiB) Downloaded 54 times

viktor_au
Posts: 367
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 45 times
Been thanked: 63 times
Contact:

Re: Custom interrupt for Nano Timer1

Post by viktor_au »

Have tried the next code, but due to my C-code errors, it does not work
Attachments
Ard_SinWave_2.fcfx
(18.1 KiB) Downloaded 62 times

viktor_au
Posts: 367
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 45 times
Been thanked: 63 times
Contact:

Re: Custom interrupt for Nano Timer1

Post 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
Attachments
Ard_Timer1_4.fcfx
(20.23 KiB) Downloaded 52 times

mnf
Valued Contributor
Valued Contributor
Posts: 1208
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 440 times

Re: Custom interrupt for Nano Timer1

Post by mnf »

See viewtopic.php?p=107449#p107449 - where I posted an Arduino PW sine wave chart..

Might do what you need?

Martin

viktor_au
Posts: 367
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 45 times
Been thanked: 63 times
Contact:

Re: Custom interrupt for Nano Timer1

Post 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;

Post Reply