![]() ![]() Setup_adc_ports(AN0_TO_AN1) // Configure AN0 & AN1 as analog inputs Setup_adc(ADC_CLOCK_DIV_8) // Set ADC conversion time to 8Tosc Setup_oscillator(OSC_8MHZ) // Set internal oscillator to 8MHz PIC18F4550 microcontroller internal oscillator is 8MHz and MCLR pin function is disabled. PWM1 output (RC2) and PWM2 output (RC1) are connected to an oscilloscope channels A and B. These pots are used to set PWM1 and PWM2 duty cycles respectively. In the above circuit there are 2 potentiometers POT1 and POT2. This is a simple example shows how to use ECCP (CCP1) and CCP2. PIC18F4550 standard PWM example with CCS C: Setup_ccp2(CCP_PWM) // Configure CCP2 as a PWM ![]() Setup_ccp1(CCP_PWM) // Configure CCP1 as a PWM In CCS C compiler the following lines configures the CCP modules as PWM modules: The CCP2 module produces a PWM signal on pin RC1. In standard mode ECCP works as simple PWM module and a PWM signal is produced on pin RC2. If you want to stop the PWM signal use the following two commands for PWM1 and PWM2 respectively: Here maximum duty cycle resolution is a little bit less than 9-bit and duty cycle range is from 0 to 511.įor values from 500 and 511 PWMs outputs will not be cleared and duty cycle value shouldn’t exceed 511. If we want to get 1KHz PWM frequency with 8MHz mcu frequency then Timer2 configuration is: If an 8-bit value is used, the duty cycle of the PWM signal is determined as follows: ![]() If value is greater than 1004 PWM1 output or PWM2 output will not be cleared.įor this example PWM duty cycle range is between. The 10 bit value is then used to determine the duty cycle of the PWM signal as follows:įor the previous example where PR2 = 250: Where ‘value’ may be an 8 or 16 bit constant or variable. PIC18F4550 has a maximum PWM duty cycle of 10-bit and for CCS C compiler the following two lines are used to write PWM1 and PWM2 duty cycles respectively: Previous example gives the following Timer2 configuration command: Where: mode is TMR2 Prescale Value, period is PR2 and postscaler is not used in the determination of the PWM frequency (keep it 1). The CCS Timer2 configuration has the following form: The PWM period can be calculated using the following formula:Īnd the PWM frequency is defined as 1/.įor example for PR2 = 250, microcontroller frequency = 8MHz and Prescale = 16 we get a PWM frequency of 500Hz. The PWM period is specified by writing to the PR2 register. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |