[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Forum
. -> [Folder]  Problems
. . -> [Subject]  Question on the Arduino Uno adjusting high frequency PWM
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Question on the Arduino Uno adjusting high frequency PWM

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


Posted by Qq11011   (1 post)  [Biography] 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



[Go to top] top

Posted by Nick Gammon   Australia  (21,406 posts)  [Biography] 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
[Go to top] 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.


3,542 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]