hussong1555
Member level 1
Does anyone have any sample "C" code for setting up and using PWM with PIC18F45k20?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
//PWM output of 32kHz 50% duty cycle at PORTC2
//Developed by: Tahmid
//1st February, 2010
//System clock: 4MHz
//Clock source: XT Crystal
void main(void){
TRISC = 0; //PORTC OUTPUT
PORTC = 0;
PR2 = 30; //~32kHz
CCPR1L = 15; //Duty cycle 50%
CCP1CON = 0x0C; //CCP1CON = 00001100, Single output, Active High
T2CON = 4; //TMR2 on, prescale = 0
while (1);
}
CCPR1L = 27; //90% duty cycle
delay_ms(3000); //3second delay
CCP1CON = 0; //PWM off
//PWM output of 32kHz 50% duty cycle at PORTC2
//Developed by: Tahmid
//1st February, 2010
//System clock: 4MHz
//Clock source: XT Crystal
void main(void){
TRISC = 0; //PORTC OUTPUT
PORTC = 0;
PR2 = 30; //~32kHz
CCPR1L = 27; //Duty cycle 90%
CCP1CON = 0x0C; //CCP1CON = 00001100, Single output, Active High
T2CON = 4; //TMR2 on, prescale = 0
delay_ms(3000); //3second delay
while (PIR1.TMR2IF == 0); //Wait for timer overflow
CCP1CON = 0; //PWM off
}
void MSDelay(unsigned int itime){
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
MSDelay(3000);//3second delay
PR2 = [Fosc / (Fpwm * 4 * N)] - 1
CCPR1L = (DC/100)*PR2
Hi all,Tahmid said:For C18, I'd make a delay routine like:
And then call the function like:Code:void MSDelay(unsigned int itime){ unsigned int i, j; for(i=0;i<itime;i++) for(j=0;j<135;j++); }
Code:MSDelay(3000);//3second delay