Fast system tick on AVR

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Fast system tick on AVR

Post by mikenick42 »

I'm trying to get make a PWM dimmer for an AC source and am having trouble with the best way to do so.
I can use chVTGetSystemTime() to check the ticks between zero crossings and get ~130 at 16MHz and ~65 with the internal oscillator. I want it to have small steps, so
I've tried changing CH_CFG_ST_FREQUENCY in chconf.h to a larger number which I thought would change the value of AVR_PRESCALER in st_lld.c, but it doesn't seem to be working. Is there something I'm missing? Should I be using the 16-bit timer instead of the default?

Thanks,
Mike
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Fast system tick on AVR

Post by tfAteba »

Hi Mike,

Wich arduino board do you use and wich version of ChibiOS do you work on?
mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: Fast system tick on AVR

Post by mikenick42 »

Hi Theo,

I'm using the stable 16.1 branch on an Uno.

Thanks,
Mike
User avatar
tfAteba
Posts: 551
Joined: Fri Oct 16, 2015 11:03 pm
Has thanked: 91 times
Been thanked: 50 times

Re: Fast system tick on AVR

Post by tfAteba »

Hi Mike,

Sorry I read your post and I can t understand your need! can you re-explain it to me please?

thanks.
mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: Fast system tick on AVR

Post by mikenick42 »

Hi Theo,

After some more reading today, I suspect that I'm coming at this the wrong way.

I'm trying to use PWM to control a mains power source, similar to what's shown in Figure 4 here: https://electronics.stackexchange.com/a/233804

Using the default settings in ChibiOS I get ~67 system ticks between zero crosses, but I would like to have finer control over the system than that. I had been trying to increase the system tick frequency but couldn't get it to work.

Now I'm thinking a better strategy would be to use timer1 set at a high frequency and use the GPT driver. I had tried something similar to this with an earlier version of ChibiOS but didn't have much luck.

Thanks,
Mike
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Fast system tick on AVR

Post by Giovanni »

Hi,

Implementing a PWM using system tick is, in general, not a good idea, it is too slow for most applications. Note that in HAL there is a specific PWM driver, you need that (not sure if it is implemented for AVR however).

Giovanni
mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: Fast system tick on AVR

Post by mikenick42 »

Hi Giovanni,

I've used the hal PWM drivers before and they're definitely nice. But for this project I'm driving an SSR that has an AC load. So once I enable the output line, the load won't turn back off until there's a zero-crossing, so I don't think traditional PWM would work.

I think I want basically what you describe here: http://www.chibios.org/dokuwiki/doku.ph ... resolution
and am trying to figure out if it's possible with the AVR architecture.

Thanks,
Mike
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Fast system tick on AVR

Post by Giovanni »

Hi,

I am not the one maintaining the AVR port but I remember that there is a problem with a small timer counter, on STM32 it is 32 bits wide. Is on AVR 8 bits wide? if so high resolution would be problematic.

Giovanni
mikenick42
Posts: 28
Joined: Tue Mar 10, 2015 4:49 pm
Been thanked: 1 time

Re: Fast system tick on AVR

Post by mikenick42 »

Hi Giovanni,

There are a couple of 8bit counters and one 16bit counter.
It looks like in tickless mode the 16bit counter is used, and in periodic mode an 8bit counter is used.

st_lld.c has the divider for tickless mode hard-coded to 1024. I played with that quite a while ago and I think had some success. It seems like that's where I need to be looking.

On another note, the comment in line 189 of os/hal/ports/AVR/st_lld.c should say "Freerunning mode" rather than "Periodic mode."

Thanks for the help,
Mike
Post Reply