Variable Frequency Generator using atmega324a

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

I pretty new to Embedded Programming and I'm working on a stroboscope project and I need some help regarding frequency generation. I have LEDs that I want to toggle ON and OFF at certain frequencies to measure the frequency of a spinning motor. 

What I need help in is figuring out how to use the ATMega324As (@ 1Mhz cpu clock) timers to generate a variable frequency of at least 100Hz-1kHz. Any help is greatly appreciated. 

Thank you

Last Edited: Sun. Oct 29, 2017 - 05:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To get maximum frequency resolution you'll need to use a 16-bit timer. Run it with a prescaler of 1 so that it clocks at the 1MHz CPU clock.

 

That gives you a lower frequency of 1,000,000/65536 = 15.25Hz, and at 1kHz output you will have frequency steps of 1Hz.

 

I suggest you run the counter in Mode 0 and simply vary TCNT to change your frequency. A TCNT value of 10,000 will give you 100Hz and a value of 1,000 will do 1kHz. Use the OVF interrupt to turn on your LED, and either a software delay, or even use another timer, to vary the on-time of the LED.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mode 0 would be 'Normal Mode', correct? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Anviori wrote:

Mode 0 would be 'Normal Mode', correct? 

 

Correct.

 

Thinking about it a bit more you could use OCR1A to turn on your LED automagically with it connected to OC1A and then turn it off in software on the OVF interrupt.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define "variable frequency". 50% duty cycle? Use cTc mode. Not 50%? Use Fast PWWM. Make the tmer do the work. Aww he

E

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And don't try to post from a stup I'd p ad. Mnnot worth the frustration.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Use Fast PWWM.

 

Ah, of course, I'd forgotten mode 15 allows full 16-bit resolution.

 

@OP, with a 16MHz clock (because that's what my board runs at) this generates a 1us wide pulse at 1kHz on D.4

 

DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (1<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);

TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (1<<COM1B0) | (1<<WGM11) | (1<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x3E;
OCR1AL=0x80;
OCR1BH=0x3E;
OCR1BL=0x70;

 

Vary OCR1A to change the frequency and OCR1B to vary the on time of the output.

 

That's all it takes, no interrupts, no nothing.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LEDs plural mentioned. How many? ??? LEDs to measure motor speed???

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A TCNT value of 10,000 will give you 100Hz and a value of 1,000 will do 1kHz

Sorry to be bravely bearing bitterly bad news, but in normal mode 0,  the 16-bit timer TCNT counts all the way up to 65535 (where it sets an interrupt warning flag), and back to 0  in a loop.   

So to generate 100 Hz you need 10000 ticks (1000000/100) which means loading 555536 into the counter  (I might be off by 1)

For 1000 Hz you need to get to the interrupt faster, (1000 ticks til endpoint), by reloading with  64536

 

The higher the freq, the higher this reload # becomes (the divider is getting smaller)...so the steps get bigger & bigger.  Think in reverse, you can divide by 1,2,3,4,5...800,801,802....14567,14568...the bigger dividers give finer frequency resolution

 

LEDs to measure motor speed???

---yes kinda like a strobe light you used on your car to do a tune up, nowadays its leds

 

   

When in the dark remember-the future looks brighter than ever.

Last Edited: Sun. Oct 29, 2017 - 03:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

So to generate 100 Hz you need 10000 ticks (1000000/100) which means loading 555536 into the counter  (I might be off by 1)

For 1000 Hz you need to get to the interrupt faster, (1000 ticks til endpoint), by reloading with  64536

 

You are, of course, correct. I'm so used to putting the numbers into a macro and letting that sort it out.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

The higher the freq, the higher this reload # becomes (the divider is getting smaller)...so the steps get bigger & bigger.  Think in reverse, you can divide by 1,2,3,4,5...800,801,802....14567,14568...the bigger dividers give finer frequency resolution

 

Which was my reason for suggesting a 16-bit timer and keeping the timer clock as high as possible.

 

At first glance you can achieve the 100Hz and 1kHz figures with an 8-bit timer and a prescaler of 64.

 

A count of 156 gives 100Hz(ish) and a count of 15 gives 1kHz(ish). However, the first step down in frequency from 1kHz, by using a count of 16, drops the frequency down to 976Hz which is a big jump.

 

Using a 16-bit timer and no prescaler gives a jump down from 1kHz(exactly) to 999Hz(well, 999.000999000Hz) which is much smoother.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 I'm so used to putting the numbers into a macro and letting that sort it out.

I do the same thing with slot machines crying 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A count of 156 gives 100Hz(ish) and a count of 15 gives 1kHz(ish).

More clear to say:  A division of 156 gives 100Hz(ish) and a division of 15 gives 1kHz(ish). 

The TCNT sets the division ticks, going from reload to max.  ...too bad they don't count down to zero...then they could be the same number.    

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems to me that you will also need an LCD to display the current strobe frequency, and a couple of push button switches to increase and decrease the strobe frequency, as well as a careful look at what LEDs you are using, and how many of them, so you can power them correctly.

 

Have you tackled any of that part of the project yet?

 

JC