PDA

View Full Version : PWM problem CCP2 port



NL2TTL
- 18th September 2006, 16:56
I'm Using a PIC16F737 and want to use the three pwm ports on it:

But i get onlu CCP1 and 3 Working, does anybody have a idea?

I have the following code:

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2006 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 9/18/2006 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ DEVICE HS_OSC

Include "modedefs.bas"

DEFINE OSC 20 ' Gebruikte kristal is 20MHz

DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
DEFINE CCP2_REG PORTB 'Hpwm 2 pin port
DEFINE CCP2_BIT 3 'Hpwm 2 pin bit
DEFINE CCP3_REG PORTB 'Hpwm 3 pin port
DEFINE CCP3_BIT 5 'Hpwm 3 pin bit

Duty1 VAR WORD ' Channel #1
Duty2 VAR WORD ' #2
Duty3 VAR WORD ' #3

Rval CON 256
Gval CON 128
Bval CON 64


'Rval var word
'Gval var word
'Bval var word

' Set CCPx pins to outputs
TRISC.2=0 ' CCP1 output
TRISB.3=0 ' CCP2 output (could also be assigned to RB3)
TRISB.5=0 ' CCP3 output
trisa=%00000111

' Set CCP modules to PWM mode
CCP1CON = %00001100 ' Mode select = PWM
CCP2CON = %00001100 ' Mode select = PWM
CCP3CON = %00001100 ' Mode select = PWM

' Set period up for 1.22kHz PWM freq
PR2 = $7F

' Set TMR2 up for 1:16 prescale & turn it on
T2CON = %00000110 ' TMR2 ON 1:16 prescale

mainloop:


Duty1 = Bval *2
CCP1CON.4 = Duty1.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = Duty1.1 ' a 10-bit word
CCPR1L = Duty1 >> 2

Duty2 = Rval *2
CCP2CON.4 = Duty2.0
CCP2CON.5 = Duty2.1
CCPR2L = Duty2 >> 2

Duty3 = Gval *2
CCP3CON.4 = Duty3.0
CCP3CON.5 = Duty3.1
CCPR3L = Duty3 >> 2

Goto mainloop
End

peterdeco1
- 18th September 2006, 18:00
Hi. I'm using a 16F73 with 2 HPWM's and have had no problems. I didn't use any DEFINEs. I just set the 2 PWM ports as outputs and use:

HPWM 1,LEFT,15000 'SEND 15KHZ WITH DUTY CYCLE VALUE IN VARIABLE LEFT
HPWM 2,RIGHT,15000 'SEND 15KHZ WITH DUTY CYCLE IN VARIABLE RIGHT
DO THIS AND THAT
LET CCP1CON = 0 'TURN OFF LEFT PWM
LET CCP2CON = 0 'TURN OFF RIGHT PWM
Low PORTC.1 'CCP1 OUTPUT LOW
Low PORTC.2 'CCP2 OUTPUT LOW

Darrel Taylor
- 18th September 2006, 18:11
NL2TTL,

Seems everyone is forgetting the analog ports these days. :eek:

ADCON1 = $0F

Added: It will probably be better to have subroutines that set the dutycycle of the CCP modules. Then call them only when needed.
Continually setting the registers in the main loop may cause some problems, and isn't needed since the hardware will continue putting out the PWM while the main program is doing other things.

Also, by using these statements Duty3 = Gval *2, it effectively reduces the PWM resolution to 9-bit since bit0 will always be 0.

Added2: The RB3/CCP2 pin is the alternate CCP2 output. You'll need to change the CCPMX configuration fuse for it to work there.
<br>

NL2TTL
- 18th September 2006, 19:22
@Darrel Taylor

Thank you for you replay, about adcon and the Rb3 i understand, but what exactly do you mean about:

Also, by using these statements Duty3 = Gval *2, it effectively reduces the PWM resolution to 9-bit since bit0 will always be 0

How can i fix that?

Darrel Taylor
- 18th September 2006, 19:44
In the binary world, multiplying by 2 is the same as shift left 1 (<< 1).

This will always leave a 0 it the bit0 location. So if you had a dutycycle of say 257 (100000001), * 2 would be 514 (1000000010).

The easiest way to fix it is to just use bigger numbers. 10-bit numbers range from 0 to 1023. <br>

NL2TTL
- 18th September 2006, 19:56
I've added:

ADCON1 = $0F

But still ccp2 doesn;t work (also on portc.2 it gives problems)

The pin is always high.

NL2TTL
- 18th September 2006, 20:05
And correct me if i'm wrong but when i don't make a loop in the program i get no response on any pwm port

So i need the Goto mainloop

Darrel Taylor
- 18th September 2006, 20:10
Did you also change the CCPMX configuration fuse for RB3/CCP2 alternate?

portc.2 is CCP1, are you saying that that one stopped working ?

And yes, you need to have a mainloop. Otherwise it goes to the END statement and enters Sleep mode. But changing the CCP registers doesn't need to be in the mainloop.
<br>

NL2TTL
- 19th September 2006, 08:13
it's working thank you very much!

NL2TTL
- 19th September 2006, 17:44
Oke when change the value to 1023 what to do with the >> 2 value?

Darrel Taylor
- 19th September 2006, 23:34
It'll stay the same as you had it before. As will the
CCP?CON.4 = Duty?.0
CCP?CON.5 = Duty?.1


Unless I'm missing the point of the question. :confused: