![]() The test circuit below is constructed to test the PWM module of PIC18f2455. LED of the 2x16 LCD is controlled by a NPN transistor BC547. PWM output is feed through CCP1 for the brightness control. The example below uses the code from ADC and LCD guides. The first requirement is to set the output port for the CCP1 pin. Setting the tris register to 0 for the bit put the port to output mode. TRISCbits.TRISC2 = 0; PWM Period = [(PR2) + 1] • 4 • TOSC • (TMR2 Prescale Value) PWM Duty Cycle = (CCPRXL:CCPXCON<5:4>) • TOSC • (TMR2 Prescale Value) For the example we set the module configuration as below. T2CON = 0x04; PR2 = 0xFF; CCP1CON = 0x3F; CCPR1L = 0x00; With this we can obtain a pwm period and maximum duty cycle of PWM Period = (255 + 1) * 4 * (1 / (8 * (10^6))) = 0.000128 Max PWM Duty Cycle period = (1024 * (1/(8*10^6))) =0.000128 ADCValue = ReadADC(); CCP1CONbits.DC1B0 = ADCValue; CCP1CONbits.DC1B1 = ADCValue >> 1; CCPR1L = ADCValue >> 2; |
Guides >