Example of how to generate PWM in mikroC using the CCP module

//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
     }
}

In mikroC, you set the duty cycle by using the function PWM1_Change_duty(x). The value you put within the parentheses (x) has to be between 0 and 255. 0 means 0 duty cycle, 255 means 100% duty cycle, so 128 means 50%.

You get PWM output at RC2 (pin 17).

Comments

Popular posts from this blog

Low-Side MOSFET Drive Circuits and Techniques - 7 Practical Circuits

Using the SG3525 PWM Controller - Explanation and Example: Circuit Diagram / Schematic of Push-Pull Converter

Simple MW Transmitter Circuit Diagram