Hi,
I seems to be missing something about setting up a PWM on arduino UNO pin3.
I've been pouring over atmel doc for days trying to untangle all the various cryptic bit and register names and thought I had it sussed. Apparently there's still something I'm missing.
I have a loop like the Serial_Fading example reading a ch from serial, then I call stopPWM and startPWM on getting s or g respectively.
The loop works and I see the debug stop and start messages but not a clean pulse on pin3.
Could someone more familiar with the hieroglyphs ease my pain?
Thanks.
void stopPWM() {
cli();
// disable interrupts
bitWrite(TIMSK2, OCIE2A, 0); // bit 1: o/p compare reg A used by WGM
bitWrite(TIMSK2, TOIE2, 0); // bit 0: overflow event used by WGM
//Hi_off; Lin off;
sei();
Serial.println("stopPWM: off");
} // end stopPWM()
void startPWM() {
cli();
// enable interrupts for WGM
bitWrite(TIMSK2, OCIE2A, 1); // bit 1: o/p compare reg A used by WGM
bitWrite(TIMSK2, TOIE2, 1); // bit 0: overflow event used by WGM
sei();
Serial.println("startPWM: on");
} // end startPWM();
ISR( TIMER2_OVF_vect ){
// if pwd_change
// analogWrite(PWM_pin, new_pwm_value); // this is calculated in P&O funct. ###
// lo off ; pulse(Hin);
// implicit reti();
}
ISR( TIMER2_COMPA_vect ){
// hi off ; pulse(Lin);
}
byte const PWM_pin=3; // ~ = 3.5.6.9.10.11 ; 10.11 on SD ; Pins 11 and 3: controlled by timer2
byte const pwm_min=4;
byte const pwm_max=250; // min, max ensure bootstrap still charges.
void setup_PWM(){
cli();
// http: //letsmakerobots.com/node/28278
pinMode(PWM_pin, OUTPUT); // input still does PWM interrupts but not physical o/p
// TCCR2C; regC 'force comp' bits not needed for PWM modes;
// TCCR2B[2..0]: 3LSB of regB = prescalarbits;
// TCCR2B[7,6] : input capture only, noise filter , edge detection. [5] n/c
// TCCR2[A,B]: 8bit fast pwm: WGM2[3..0] = TCCR2B[4,3] + TCCR2A[1.0] = 0101;
// also GTCCR to sync different counters
// OCR2[A,B] output comp registers
// OC2[A,B] output pins [ arduino pin3,pin11 ]
TCCR2A = 0;
TCCR2B = 0;
TCCR2A |= 0b01; // WGM2[1,0]
TCCR2A |= 0b10000000; // COM2A[1,0]= TCCR2A[7,6] = 10 Clear OC2A on Compare Match, set OC2A at BOTTOM
// TCCR2B |= 0b01001; // WGM2[3,2] + full 16MHz
TCCR2B |= 0b01010; // WGM2[3,2] + div 8 = 2MHz clk -> 8kHz PMW
// enable output compare: OCIE2y bit in the interrupt mask register TIMSK2; ISR(TIMER2_COMPy_vect)
// enable timer overflow: TOIE2 bit in the interrupt mask register TIMSK2; ISR(TIMER2_OVF_vect)
// enable input capture : ICIE2 bit in the interrupt mask register TIMSK2; ISR(TIMER2_CAPT_vect)
bitWrite(TIMSK2, OCIE2A, 1); // bit 1: o/p compare reg A used by WGM
bitWrite(TIMSK2, TOIE2, 1); // bit 0: overflow event used by WGM
bitWrite(TIMSK2, OCIE2B, 0); // bit 2: p/p compare reg B :off
// analogWrite(PWM_pin, pwm_min); // ensure finite width for bootstrap diode pulse.eg 8kHz, 2us pulse.
// TCCR2A |= COM2A1; // redundant repetition
OCR2A = pwm_min; // set pwm duty cycle
sei();
} // end setup_PWM()
void set_PWM(){
// ### if batt_state== charging {do P&O}
// ### else {adj for batt voltage}
// analogWrite(PWM_pin, pwm_min);
OCR2A = pwm_min; // set pwm duty cycle
} // end set_PWM()