Pages: [1]   Go Down
Author Topic: Frequency Generator  (Read 554 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am using the Arduino Uno to generate different frequency square waves to modulate an LED. I am using the toggle on compare match mode of the 16MHz clock on the ATMega 328 to do this. The datasheet for the ATMega 328 has a nice formula for calculating the OCRnA register value needed to generate the desired output frequency. When I calculate the register value and hardcode the OCR1A register, for 500Hz the register value is 1999, it outputs the correct frequency. What I would like to do is just to have a int variable frequency where I can simply enter 500 and then it will calculate the OCR1A value but when I try to implement this the frequency that is outputted is random. I can't seem to figure out why this is. Here is a little bit of my code:
For 500Hz
//This works
OCR1A =1999 ;

//This doesn't work
int freq = 500;
int eq = ((16*10^6)/(2*8*500))-1; //This is the formula from the datasheet and
                                              // what I used to get 1999 above
OCR1A = eq;

Can anyone see what I am doing wrong? Thanks in advance.
Logged

Offline Offline
Edison Member
*
Karma: 48
Posts: 1632
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't crosspost!

Pete
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

See:

http://arduino.cc/forum/index.php/topic,100017
Logged


Pages: [1]   Go Up
Jump to: