EmbeddedRelated.com
Forums

problem to activate correctly pic18f442 PWM module

Started by El75 March 18, 2004
Dear Sirs, 

I work with PIC18F442,Fosc =16 MHz and want to generate PWM 4 KHz with
Duty cycle of 50% through CCP2 Module.

The duty cycle, I've received, is too small and the PWM period is not
correct (3.76 KHz). An other one problem is that the duty cycle is not
permanent. Probably I didn't understand correctly how to initialize.
What's wrong with this ?


void Start_Buzzer(void) 
{ 

TMR2 = 0; 
CCP2CON=0x7E; /* Init PWM module */ 


PR2 = 0x3e; /* update the PWM period */ 


/* update the PWM duty cycle to give 50% */ 

/* CCPR2L:<DC2B1:DC2B0> should be PR2/2 = 0x1F */ 

DC2B0 = 1; 

DC2B1 = 1; 

CCPR2L = 0x07; /* 0000 0111 11 giving together 0x1f*/ 


/* update the timer 2 prescaler to 16 and start the Timer0 */ 
T2CON = 0b00000110; 

TRISC1 = 0; 
T_buzzer = 25; /* 25 * 4msec =100msec of Buzzer On*/ 


} 

Of course I could do it in other manner without PWM, but I want to
understand what is going on here.

Thank you in advance and best regards.
>I work with PIC18F442,Fosc =16 MHz and want to generate PWM 4 KHz with >Duty cycle of 50% through CCP2 Module.
>CCP2CON=0x7E; /* Init PWM module */
I think this should be 0x3E. I'm wondering if it has to do with the order in which you set the PWM up. Your order is different than the one in the datasheet. It might make a difference, or maybe not.
>Another one problem is that the duty cycle is not >permanent.
This is weird. It sounds like something is trashing your Timer2.