ancymuhammed
Newbie level 5
pic pwm
I want a program to generate a pwm wave of 40 KHz
I want a program to generate a pwm wave of 40 KHz
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.
//Program to generate 40kHz output at RC2(CCP1) pin
//Microcontroller: Microchip PIC18452
//Language: C
//Compiler: mikroC v8.20
//Programmer: Tahmid
void main (void){
TRISC = 0;
PORTC = 0;
ADCON1 = 7;
T2CON = 0;
TMR2 = 0;
PWM1_Init(40000); //40kHz
PWM1_Change_Duty(128); //50% duty cycle
// Choose Duty cycle as such:
// PWM_Change_Duty(x);
// x = ( (Duty Cycle in %) / 100) * 255
PWM1_Start(); //Start PWM
while (1){ //Loop forever
// Whatever else might be needed to be done while PWM is running
}
}
'Program to generate 40kHz output at RC2(CCP1) pin
'Microcontroller: Microchip PIC18452
'Language: BASIC
'Compiler: mikroBASIC PRO for PIC v3.20
'Programmer: Tahmid
program PWM40kHz
main:
TRISC = 0
PORTC = 0
ADCON1 = 7
PWM1_Init(40000) '40kHz
PWM1_Set_Duty(128) '50% duty cycle
' Choose Duty cycle as such:
' PWM_Set_Duty(x)
' x = ( (Duty Cycle in %) / 100) * 255
PWM1_Start() 'Start PWM
while (1) 'Loop forever
' Whatever else might be needed to be done while PWM is running
wend
end.
Hi,
Here is a sample code in mikroC:
Code://Program to generate 40kHz output at RC2(CCP1) pin //Microcontroller: Microchip PIC18452 //Language: C //Compiler: mikroC v8.20 //Programmer: Tahmid void main (void){ TRISC = 0; PORTC = 0; ADCON1 = 7; T2CON = 0; TMR2 = 0; PWM1_Init(40000); //40kHz PWM1_Change_Duty(128); //50% duty cycle // Choose Duty cycle as such: // PWM_Change_Duty(x); // x = ( (Duty Cycle in %) / 100) * 255 PWM1_Start(); //Start PWM while (1){ //Loop forever // Whatever else might be needed to be done while PWM is running } }
mikroBASIC:
Code:'Program to generate 40kHz output at RC2(CCP1) pin 'Microcontroller: Microchip PIC18452 'Language: BASIC 'Compiler: mikroBASIC PRO for PIC v3.20 'Programmer: Tahmid program PWM40kHz main: TRISC = 0 PORTC = 0 ADCON1 = 7 PWM1_Init(40000) '40kHz PWM1_Set_Duty(128) '50% duty cycle ' Choose Duty cycle as such: ' PWM_Set_Duty(x) ' x = ( (Duty Cycle in %) / 100) * 255 PWM1_Start() 'Start PWM while (1) 'Loop forever ' Whatever else might be needed to be done while PWM is running wend end.
Hope this helps.
Tahmid.