Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Forum ➜ Problems ➜ Question on the Arduino Uno adjusting high frequency PWM

Question on the Arduino Uno adjusting high frequency PWM

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Qq11011   (1 post)  Bio
Date Thu 21 Mar 2013 11:34 AM (UTC)
Message
Dear all,

I am writing to ask for help on the arduino uno programming on the high frequency PWM adjusting.

In my project, I am thinking to first receive high frequency PWM input, and read it, then reproduce a new pwm with adjusted duty cycle. The factor adjusting the new PWM duty cycle is based on the Photovaristor value.

The operating freuency is about 150kHz.

below is the program with adjusting duty cycle from input of the photovaristor value, it already able dim the led lamp in 150kHZ.

------------------------------------------
const byte LED = 3; // Timer 2 "B" output: OC2B
int divider =10;
const long frequency = 150000L; // Hz (eg:150000=150khz)
int inputPin = 2; // set the analog input at pin2
int val = 0; // create variable
void setup()
{ Serial.begin(9600);
pinMode (LED, OUTPUT);

TCCR2A = _BV (WGM20) | _BV (WGM21) | _BV (COM2B1); // fast PWM, clear OC2B on compare
TCCR2B = _BV (WGM22) | _BV (CS21); // fast PWM, prescaler of 8
OCR2A = ((F_CPU / 8) / frequency) - 1; // zero relative
//OCR2B = ((OCR2A + 1) / 2) - 1; // 50% duty cycle
} // end of setup

void loop()
{
//divider=1 100% val<500 dark val>500 normal light
//divider=2 50%
//divider=3 33%
//divider=4 25%
//divider=10 10%

val = analogRead(inputPin);
Serial.write("val: ");
Serial.print(val); //print the received input data to serialmonitor
Serial.write(" divider: ");
if (val>10 && val<=200 ){ divider =1; }
if (val>200 && val<=400 ){ divider =2; }
if (val>400 && val<=600 ){ divider =3; }
if (val>600 && val<=800 ){ divider =4; }
if (val>800 && val<=1000 ){ divider =10; }
delay(50);
Serial.print(divider);
Serial.println("");
OCR2B = ((OCR2A + 1) / divider) - 1;
// do other stuff here
}
------------------------------------------

But the problem now is how can I able to read the high frequency PWM.
The input PWM will be 10V at frequency 150kHZ(may change a bit)

Thank You.

QQ11011



Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 21 Mar 2013 09:40 PM (UTC)
Message
Template:Arduino Please post Arduino-related questions to the Arduino Forum or to StackExchange: Arduino. This saves splitting questions and answers between this forum and the Arduino ones.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,609 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.