You are on page 1of 2

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

void main() { unsigned char TRISC = 0 ; PORTC = 0 ;

dc ; // set PORTC as output // clear PORTC

/* * configure CCP module as 4000 Hz PWM output */ PR2 = 0b01111100 ; T2CON = 0b00000101 ; CCP1CON = 0b00001100 ; CCP2CON = 0b00111100 ; for(;;) // forever { /* * PWM resolution is 10 bits * don't use last 2 less significant bits CCPxCON, * so only CCPRxL have to be touched to change duty cycle */ for(dc = 0 ; dc < 128 ; dc++) { CCPR1L = dc ; CCPR2L = 128 - dc ; Delay_ms(10) ; } for(dc = 127 ; dc > 0 ; dc--) { CCPR1L = dc ; CCPR2L = 128 - dc ; Delay_ms(10) ; } }

You might also like