PDA

View Full Version : HPWM on PIC18F125K22



brid0030
- 3rd February 2013, 19:14
I've got an existing/working device on which I need to implement a simple temporary pulse series using the hardware PWM. I'm trying to use the HPWM command to no avail. The device is a 28-pin PIC18F25K22, and I need to have the pulse on either PORTB.2 or PORTB.3. According to the datasheet, PWM should be possible on both of these pins, but there is so much multiplexing and enhancing on this device, that I think my problem lies in setting registers correctly for HPWM to work.

Here's the relevant code that does not work.




....
define CCP1_REG PORTB
define CCP1_BIT 2

....

HPWM 1,200,2
PAUSE 5000
CCP1CON = %00000000 'turns off PWM

High PORTB.2 'Test port connection with LED
pause 50
low PORTB.2
'pause 50


The last bit flashes an LED on PORTB.2 as it should. But I see nothing resulting from the HPWM command. Anybody got a clue for the registers on this pic? By the way, I've tried various permutations to do the pulses manually:

'TRISB = %00000000
'CCPTMRS0 = %00000000
'PR1 = %00000111
'CCP2CON = %00001100 'define lsb of duty cycle. make PWM move high PxA and high PxB
'CCPR1L = %00011111 'duty cycle MSB
'T2CON = %00000101 'set prescaler and turn on PWM
'TRISB = %00000000

Can't get this to work either. I just don't get this datasheet I'm afraid. Thanks for any help.

HenrikOlsson
- 3rd February 2013, 21:20
Hi,

The device is a 28-pin PIC18F25K22, and I need to have the pulse on either PORTB.2 or PORTB.3.
As far as I can see there's no CCP module connected to PortB.2 so that one is out.

PortB.3 on the other hand can have the CCP2 module connected to it but by default it's connected to PortC.1. You select which pin (either PortB.3 or PortC.1) the CCP2-module should be connected to with the CCP2MX bit in the CONFIG3H configuration word. The purpose of the DEFINE is to let the compliler know how you've configured the device - at least that's how I understand it.

Then, since it's the CCP2 module you need to use HPWM 2, 200, 10000.

Finally, in your code you're trying to get an output frequency of 2Hz, that's not going to work.

/Henrik.

brid0030
- 13th February 2013, 17:11
Thanks Henrik. Following your suggestions, I got it to work on PORTB.3. The crucial thing I was missing was the CCP2MX bit.