PDA

View Full Version : 3 HPWM channels



docwisdom
- 2nd March 2006, 22:52
Since HPWM cant operate in 3 channels and it has to be configured manually, I was wondering if someone could help me incorporate it into my code correctly? Theres quite a bit of stuff going on in that code and I dont want it placed inproperly in my script.


my script:


TRISB = %10000000 'portb 7 input 6-0 output
ADCON1 = %11111111 'all ports digital

define OSC 4 '4mhz oscillator


DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0)


DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 4 ' Set LCD Register Select bit


DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 6 ' Set LCD Enable bit


DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

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

B0 Var Byte 'up button
B1 var byte 'down button
B2 var byte 'select button
B3 var byte 'daylight button
B4 Var byte 'tungsten
B9 var byte 'lock button
B10 var byte 'power button

rval var byte 'red
gval var byte 'green
bval var byte 'blue
lcdrval var byte 'red lcd value
lcdgval var byte 'green lcd value
lcdbval var byte 'blue lcd value

rval = 255 'red at 255
gval = 255 'green at 255
bval = 255 'blue at 255
B0 = 0 'zero button up
B1 = 0 'zero button down
B2 = 0 'zero select button
B3 = 0 'zero daylight button
B4 = 0 'zero tungsten button
B9 = 0 'zero lock button
B10 = 0 'zero power button

while portc.6 = 0 'power button
wend

main:
high portc.4 'power indicator
pause 500 '.5 second delay
LCDOUT $FE, 1, "LEADS ", $FE, $C0, "by MSL" 'tag line
pause 1000 '1 second delay
home:
lcdrval = ~rval 'invert red for display
lcdgval = ~gval 'invert green for display
lcdbval = ~bval 'invert blue for display
lcdout $fe, 1, " R", DEC3 lcdrval, " G", $FE, $C0, DEC3 lcdgval, " B", DEC3 lcdbval
hpwm 1, rval, 5000 'set red dim level
hpwm 2, gval, 5000 'set red dim level
hpwm 3, bval, 5000 'set red dim level

bru:
BUTTON porta.0,1,100,1,B0,0,brd 'detect + button
pause 20
goto brup
brd:
button porta.1,1,100,1,B1,0,ld 'detect - button
pause 20
goto brdown
ld:
button portc.7,1,255,0,B9,0,pd 'detect lock button
pause 20
goto lock
pd:
button portc.6,1,255,0,B10,0,dd 'detect power button
pause 20
goto power
dd:
button portc.5,1,255,0,B3,0,td 'detect daylight button
pause 20
goto daylight
td:
button portc.4,1,255,0,B4,0,sd 'detect tungsten button
pause 20
goto tungsten
sd:
button portb.7,1,255,0,B2,0,bru 'detect select button
pause 20
'if portb.7 = 1 then
goto adjr
'else
'goto bru
'endif


brup: 'brightness up
rval = rval + 1
gval = gval + 1
bval = bval + 1
hpwm 1, rval, 5000 'set red dim level
hpwm 2, gval, 5000 'set green dim level
hpwm 3, bval, 5000 'set blue dim level
pause 100
goto home
brdown: 'brightness down
rval = rval - 1
gval = gval - 1
bval = bval - 1
hpwm 1, rval, 5000 'set red dim level
hpwm 2, gval, 5000 'set green dim level
hpwm 3, bval, 5000 'set blue dim level
pause 100
goto home

adjr: 'adjust red level
lcdrval = ~rval 'invert red for display
lcdout $fe, 1, "Red", $FE, $C0, DEC3 lcdrval 'display red level
ru: 'check up button
BUTTON porta.0,1,100,1,B0,0,rd ' Check button
pause 20
goto rup
rd: 'check up button
button porta.1,1,100,1,B1,0,sd2
pause 20
goto rdown
sd2: 'check for select button
button portb.7,1,255,0,B2,0,ru
pause 20
goto adjg 'goto adjust green
rup: 'red up
rval = rval + 1
hpwm 1, rval, 5000
pause 100
goto adjr
rdown: 'red down
rval = rval - 1
hpwm 1, rval, 5000
pause 100
goto adjr

adjg: 'adjust green
lcdgval = ~gval 'invert green for display
lcdout $fe, 1, "Green", $FE, $C0, DEC3 lcdgval 'display green level
gu: 'check up button
BUTTON porta.0,1,100,1,B0,0,gd ' Check button
pause 20
goto gup
gd: 'check down button
button porta.1,1,100,1,B1,0,sd3
pause 20
goto gdown
sd3: 'check select button
button portb.7,1,255,0,B2,0,gu
pause 20
goto adjb 'goto adjust blue
gup: 'green up
gval = gval + 1
hpwm 2, gval, 5000
pause 100
goto adjg
gdown: 'green down
gval = gval - 1
hpwm 2, gval, 5000
pause 100
goto adjg

adjb: 'adjust blue
lcdbval = ~bval 'invert blue for display
lcdout $fe, 1, "Blue", $FE, $C0, DEC3 lcdbval 'display green level
bu: 'check up button
BUTTON porta.0,1,100,1,B0,0,bd ' Check button
pause 20
goto bup
bd: 'check down button
button porta.1,1,100,1,B1,0,sd4
pause 20
goto bdown
sd4: 'check select button
button portb.7,1,255,0,B2,0,bu
pause 20
goto home 'goto home
bup: 'blue up
bval = bval + 1
hpwm 3, bval, 5000
pause 100
goto adjb
bdown: 'blue down
bval = bval - 1
hpwm 3, bval, 5000
pause 100
goto adjb

lock: 'lock the keys
high portc.3 'lock indicator
pause 1000
while portc.7 = 0 'wait for unlock
wend
low portc.3 'lock indicator
goto home

power: 'power down display and LEDs
low portc.4 'take down power indicator
hpwm 1, 0, 5000 'take down red channel
hpwm 2, 0, 5000 'take down green channel
hpwm 3, 0, 5000 'take down blue channel
lcdout $fe, 1 'clear display
pause 1000
while portc.6 = 0 'wait for power on
wend
high portc.4 'put up power indicator
goto home

daylight: 'set color levels to approx 5600k
rval = 255
gval = 255
bval = 252
goto home


tungsten: 'set color levesl to approx 3200k
rval = 255
gval = 241
bval = 224
goto home

end

docwisdom
- 6th March 2006, 18:29
can anyone help with the 3rd hpwm channel. I need it to match the others, but am not sure how to incorporate it or set it for 5khz operation.

Srigopal007
- 6th March 2006, 18:43
Depending on what device you are using, and what version of the pbp compiler you are using, you can support 3 pwm channels. Look at the manual for more info. If all else fails, then you can always configure the appropriate CCP registers to manually get the pwm to function. By coding this manually, you can achieve more versatility by this than using the simple HPWM command that pbp offers you.


Srig

docwisdom
- 7th March 2006, 06:28
I am using a 16F737 with PBP 2.46

Bruce
- 7th March 2006, 15:13
There's an example in this thread for controlling all three channels on this
PIC. http://www.picbasic.co.uk/forum/showthread.php?t=1770&highlight=hpwm

Srigopal007
- 7th March 2006, 17:09
I suggest you do it the way BRUCE suggested. set them manually, by setting the registers yourself. here is an example for the 5khz case for the pic16f737 that I made, just for you: you may need to tweak it a bit to adjust the frequency by changing the PR2 value. (Dont forget that the value for the duty variable has to be 4x the PR2 value.) So if you change your PR2 Value... make sure you change the value of the duty varaible accordingly.

Enjoy,

Srig

-----------------------------------------------------------------
Duty1 var Byte 'Variable for controlling the PWM in CCP1
Duty2 Var Byte 'Variable for controlling the pwm in CCP2
Duty3 Var Byte 'Variable for controlling the PWM in CCP3


CCP1CON = %00001100 'Using CCP1 as PWM Mode Register 9-1 in Datasheet
CCP2CON = %00001100 'Using CCP2 as PWM Mode Register 9-1 in Datasheet
CCP3CON = %00001100 'Using CCP3 as PWM Mode Register 9-1 in Datasheet
T2CON = %00000101

TRISC.2 = 0 'Setting the CCP1 PIN as OUTPUT
TRISC.1 = 0 'Setting the CCP2 PIN as OUTPUT
TRISB.5 = 0 'Setting the CCP3 PIN as OUTPUT

Duty1 = 0
Duty2 = 0
Duty3 = 0


' Fosc = Clock Frequency (4MHz)
' PS = Timer2 Prescale Value (T2CON<1:0>)
' Freq = PWM output frequency
' Duty% = Duty cycle (20% = 0.2)

' formulas:
' PR2=(Fosc/(4*PS*Freq))-1
' CCPR1L:CCP1CON<5:4>=(PR2+1)*4*Duty%

PR2 = 50 with prescalar of 4 in Timer2

For i = 0 to 200
Duty1 = Duty1 + 1
CCP1CON.4 = Duty1.0
CCP1CON.5 = Duty1.1
CCPR1L = Duty1 >> 2
Pause 17
Next i

For i = 0 to 200
Duty2 = Duty2 + 1
CCP2CON.4 = Duty2.0
CCP2CON.5 = Duty2.1
CCPR2L = Duty2 >> 2
Pause 17
Next i


for i = 0 to 200
Duty3 = Duty3 + 1
CCP3CON.4 = Duty3.0
CCP3CON.5 = Duty3.1
CCPR3L = Duty3 >> 2
Pause 17
Next i
---------------------------------------------

docwisdom
- 7th March 2006, 18:07
thanks for the post.

I am understanding some of this, but since I am still at a novice level, some parts and variables aren't clicking yet. In my user interface, the user is able to use the up and down buttons to change the color level from 0 to 255. Mathmatically, how would I incorporate that into this configuration?

Thank you for all your help so far. I dont mean for you to do all the work for me. I am sifting through manuals and datasheets trying to wrap my mind around some of this.

Also, in my haste to post, I mixed up my PIC #, its actually a 767.

Srigopal007
- 7th March 2006, 18:34
The code which I gave you, should work for any of the PIC16F737/767/777 product of family.

I would love to help you out, but I cannot do all the work for you, or else you will not learn. Why dont you go ahead and try things out first, and then come back if you have any more errors/ problems. I think the code which I have posted in my previous post should get you started.

docwisdom
- 30th March 2006, 02:34
Thanks for all the help so far, and sorry for the long gap between posts.


This code works, and it works beautifully. Now all I need is to make a little more user friendly. As it stands right now, value 0 is 100%, and 204 is 0%. Somehow I need to make a calculation so my user sees 0 to 100% not 204 to 0. I also need to make limit blocks at 0 and 100.
I am working on the stops right now, but the code I have tried to divide and invert the numbers just isnt working right.


thanks
-brian

docwisdom
- 4th April 2006, 02:43
can anyone lead me in the right track?