Let me know if that’s what you need or not.
#MPLAB XC8 PWM SIMULATOR#
I don’t have a PIC18F14K50 to try this program with, but I’ve run it in the MPLAB simulator and it seems to work. I estimated the pulse widths for 0 and 90 degrees based on the typical properties of servos I’ve used, but you may need to adjust the values for CCP1RL (I used 8 and 12). That’s the pin you should connect your relay to. The pulse width is either 1ms or 1.5ms (approx) depending on whether RC4 (pin 6) is high or low. The PWM period is 20ms, which is correct for most of the servos I use. The PWM signal to control the servo is outputted on CCP1 (pin 5). PR2 = 156 // PWM period = (PR2+1) * prescaler * Tcy = 19.968msĬCPR1L = 8 // pulse width = CCPR1L * prescaler * Tcy = 1.024ms TRISC = 0b11011111 // Make CCP1 an output Set clock frequency to 500kHz, therefore Tcy = 8us #pragma config FOSC=IRC,MCLRE=OFF,WDTEN=0,LVP=OFF,BOREN=OFF
#MPLAB XC8 PWM CODE#
I’m afraid I’m still a bit confused! Do you mean that you want a servo to go back and forth between 0 and 90 degrees as the relay switches on and off? If so, perhaps the following code would do it…? Here, the logic analyzer cursors are measuring the pulse width of the waveform:įinally, here’s the waveform when the duty cycle is set to 10% (i.e. Here, the logic analyzer cursors are measuring the period of the waveform: The signal was connected to channel 3 of the logic analyzer (pin 6 of the PICkit 2): I used the PICkit2 Logic Analyzer to take the following snapshot of the waveform when the duty cycle was 50% (when CCPR1L = 125). Measuring the waveform period and pulse width Here’s how it looked when I compiled the program on my laptop: I save the C code as “main.c” and the build script in the same folder as “build.bat”. The build script I use (which contains just one command) is shown below. I don’t use MPLAB I just write my C code in a text editor, compile it with XC8 in a command window, then use the PICkit 2 application to transfer the hex file to the PIC. PR2 = 249 // PWM period = (PR2+1) * prescaler * Tcy = 1msĬCPR1L = 25 // pulse width = CCPR1L * prescaler * Tcy = 100us T2CON = 0b00000100 // Enable TMR2 with prescaler = 1 Set up PWM (see section 15.4 of the PIC18F4620 datasheet)ĬCP1CON = 0b00001100 // Enable PWM on CCP1
#pragma config OSC=INTIO67,MCLRE=OFF,WDT=OFF,LVP=OFF,BOREN=OFF
#MPLAB XC8 PWM FULL#
This is the full C code for the XC8 compiler. However, the value of CCPR1L changes every half a seconds, causing the duty cycle of the waveform to cycle through the values 50%, 10%, 0% repeatedly. In this example, the period is constant at 1ms, giving a frequency of 1kHz. The values of PR2 and CCPR1L can be changed at any time, which will change the period or pulse width of the waveform. Initially, CCPR1L = 125, the Timer 2 prescaler = 1 and Tcy = 4us. PWM pulse width = CCPR1L * Timer 2 presacler value * Tcy The formula for calculating PWM pulse width is: The pulse width of the PWM waveform is determined by the value written to a special function register called CCPR1L (short for Capture/Compare/PWM channel 1 Register, low byte). In this example, PR2 = 249, the Timer 2 prescaler = 1 and Tcy = 4us. PWM period = (PR2+1) * Timer 2 prescaler value * Tcy
The formula for calculating PWM period is: The period of the PWM waveform is determined by the value written to a special function register called PR2 (short for Period Register for Timer 2). This is an important figure because Tcy is really the fundamental unit of time when measuring time in PIC programs. The PIC18F4620 performs one machine code instruction every four clock cycles, so the instruction cycle in this example is Tcy = 4us. The oscillator period is therefore Tosc = 1us. In this example, the clock oscillator frequency of the 18F4620 is left at the default value of Fosc = 1MHz. Because I’m making this simplification, the calculations I present below are a bit simpler than those described in the PIC18F4620 datasheet. Unless you really need the extra resolution, I recommend ignoring the 2 least significant bits and treating both the PWM period and pulse width as 8-bit values. However, to keep things simple, I’m disregarding the 2 least significant bits, which reside in a different register to the 8 most significant bits. NB The PIC18F4620 provides 10-bit resolution in setting the PWM pulse width. The program is written in C for Microchip’s XC8 C compiler. This simple example program generates a 1kHz PWM (pulse width modulation) signal on pin 17 (RC2 / CCP1). Applications include motor speed control, servo control, and varying the brightness of an LED. The PIC18F4620 includes a hardware pulse width modulation feature which is really useful for generating periodic pulse sequences with different frequencies and duty cycles.