Question on the Arduino Uno adjusting high frequency PWM

Posted by Qq11011 on Thu 21 Mar 2013 11:34 AM — 2 posts, 15,542 views.

#0
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



Australia Forum Administrator #1
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.