go_away

Author Topic: [solved] PWM Woes / Does this code dig?  (Read 893 times)

0 Members and 1 Guest are viewing this topic.

Offline ksquaredTopic starter

  • Jr. Member
  • **
  • Posts: 41
  • Helpful? 0
[solved] PWM Woes / Does this code dig?
« on: May 31, 2010, 02:39:31 PM »
I was working on using a PWM timer

(here: http://www.societyofrobots.com/member_tutorials/node/232)

to make an L.E.D. flash on and off every half second.  That would mean my duty period should be 1 second at 1 hz. and thus following the equation...

TOP = (Clock_Speed / (Prescaler * Output_PWM_Frequency)) - 1

Assuming the Atmega168 does 8 Mhz without an external crystal (Am I right about THAT!?) that leaves something like 8000000/(1024*1)-1 = 78124.  Since this is a 16 bit timer, I put 65535 for that value.

My code goes something like:

Code: [Select]
TCCR1A = 0;
ICR1 = 65535;  
///*******
//// 1024x prescaling ?
TCCR1A = (1 << WGM11);
TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS12) | (1 << CS10);
DDRB |= _BV(1);      // make port B1 an output pin
TCCR1A |= 2 <<  6;  // enable PWM on port B2 to use non-inverting mode - mode 2
//pin
DDRB |= _BV(2);      // make port B2 an output
TCCR1A |= 2 <<  4;  // enable PWM on port B2 to use non-inverting mode - mode 2

Unfourtunatly, the L.E.D just stays on constantly.  You might find this useful:


http://www.societyofrobots.com/member_tutorials/files/ATMega8.pdf

I already know how bit flags work.  For someone wondering what to do with this, look up how does work...  

Is this becuase I did not specify a comparator value?  Thanks...
Fixed

Code: [Select]
int main (void){
TCCR1A = 0;          // disable all PWM on Timer1 whilst we set it up

TCCR1A = (1<<WGM11);


ICR1 = 15625*2;

TCCR1A = (1 << WGM11);
TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS11) | (1 << CS10);



OCR1A = ICR1 / 2;
OCR1B = ICR1 / 2;

DDRB |= _BV(1);      // make port B1 an output pin
TCCR1A |= 2 <<  6;  // enable PWM on port B2 to use non-inverting mode - mode 2
//pin
DDRB |= _BV(2);      // make port B2 an output
TCCR1A |= 2 <<  4;  // enable PWM on port B2 to use non-inverting mode - mode 2
while (1)

{

}

}
THIS code should do it for a 1 Mhz processor.  Ie, make it flash on one second, off one second.   I was experimenting with the scalers and realized it was not acting at 8 Mhz. If your processor is fast enough it  might not flash at all but dim.  Recalculate your value.  Be sure to have the proper include files.... use that tutorial!
« Last Edit: June 02, 2010, 05:37:35 AM by ksquared »

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,941
  • Helpful? 98
Re: PWM Woes / Does this code dig?
« Reply #1 on: May 31, 2010, 05:26:29 PM »
Just an FYI:
Quote
my duty cycle should be 1 hz
This is the frequency not the duty cycle. The period is 1 second.
Duty cycle is the percent of on time verses the period. So for a 1Hz frequency a 50% duty cycle would be on for 0.5 second and off for 0.5 second.

 


Get Your Ad Here