PDA

View Full Version : Programming freezing



alec
- 24th January 2010, 12:29
Hello all
I have what should be a simple program to control a car heater motor - two pushbuttons to increment or decrement a counter and the counter becomes the duty cycle for a PWM output. For some reason, whenever I depress one of the buttons the program freezes - the PWM output continues but it stops cycling. When I hit the reset button on my EasyPic 5 board, the PWM ceases and gos to five volts until I cycle power on and off. I thought that it was getting stuck in one of the increment or decrement loops so I added a pilot for that - it never comes on. I'm using version 2.5, and compiling through MicroCode Studio. Any thoughts or comments would be appreciated - it's driving me batty...

'************************************************* ***************
'* Name : auto_heater_controller.pbp *
'* Author : Alec Noble *
'* Notice : Copyright (c) 2010 Alec Noble *
'* : All Rights Reserved *
'* Date : 1/18/2010 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ device pic16F628a
@ device INTRC_OSC_NOCLKOUT
CMCON =7 'port a digital i/o
VRCON =0 'a/d voltage refernce disabled
TRISB = %00000000 'port b all outputs
TRISA = %11000000 'a.7, a.6 inputs, all else output

'********************** assing I/O ****************************

up var porta.6 'up or increment button input
down var porta.7 'down or drcrement button input
heat var portb.3 'heater PWM output

LED var word '10 bits - 1 for each led segment


'********************** create variables **********************

duty var byte 'variable for the dutycycle
counter var byte 'variable to increment/decrement
led_chk var byte
pwr var bit 'on/off bit
preset con 183 'minimum pwm duty value for low
stp con 18 'duty cycle steps

'*********************** initialize variables *****************

pwr = 1
duty = 0
counter = 0
LED = 0
porta.3 = 0
porta.4 = 0

'*********************** program begins here ******************
pause 1000
main:

'********** button_chk ***********
if up = 0 then increment
if down = 0 then decrement

'********** duty_set **************
duty = preset + counter
if duty >=255 then duty = 255
if pwr = 0 then duty = 0
hpwm 1, duty, 20000

'********** display ***************
led = 0
' pause 1000
lookdown2 duty, [93, 111, 129, 147, 165, 183, 201, 219, 237, 255], led_chk
lookup led_chk, [%0000000001, %0000000011, %0000000111, %0000001111, %0000011111, _
%0000111111, %0001111111, %0011111111, %0111111111, %1111111111], led
if led.0 = 1 then portb.0 = 1
if led.1 = 1 then portb.1 = 1
if led.2 = 1 then portb.2 = 1
if led.3 = 1 then portb.4 = 1
if led.4 = 1 then portb.5 = 1
if led.5 = 1 then portb.6 = 1
if led.6 = 1 then portb.7 = 1
if led.7 = 1 then porta.0 = 1
if led.8 = 1 then porta.1 = 1
if led.9 = 1 then porta.2 = 1

pause 100
toggle porta.3 'heartbeat
goto main
end
increment:
high porta.4
' pause 20 'debounce
' if up = 1 then main
pause 500
if up = 1 then 'check for button release
counter = counter + stp 'if so, increment
low porta.4
goto main
endif

pause 2500 'check if button still pushed
if up = 0 then
pwr = 1 'if turn heater on
counter = counter + stp
low porta.4
goto main
endif

decrement:
pause 20 'debounce
if down = 1 then main
pause 500
if down = 1 then 'check for button release
counter = counter - stp 'if so, decrement
goto main
endif
pause 2500
if down = 0 then 'check if button still pushed
pwr = 0 'if so, turn heated off
endif
goto main

Archangel
- 25th January 2010, 07:49
Hi Alec,
"I feel your pain" I drive a 62 Ford, try this:


'************************************************* ***************
'* Name : auto_heater_controller.pbp *
'* Author : Alec Noble *
'* Notice : Copyright (c) 2010 Alec Noble *
'* : All Rights Reserved *
'* Date : 1/18/2010 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
;@ device pic16F628a
;@ device INTRC_OSC_NOCLKOUT
@ __config _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _BOREN_OFF & _PWRTE_ON & _WDT_ON & _DATA_CP_OFF & _CP_OFF & _LVP_OFF
DEFINE OSC 4
CMCON = 7 'port a digital i/o
VRCON = 0 'a/d voltage refernce disabled
PortB = 0 ' initialize all portB logic low
TRISB = %00000000 'port b all outputs
PortA = 0 ' Initialize all outputs as low
TRISA = %11000000 'a.7, a.6 inputs, all else output
CCP1CON = %00001100 ' Set CCP1 to PWM
'********************** aliasing I/O ****************************

up var porta.6 'up or increment button input
down var porta.7 'down or drcrement button input
heat var portb.3 'heater PWM output

LED var byte[10] '10 bits - 1 for each led segment
LED0 var PortB.0
LED1 var PortB.1
LED2 var PortB.2
LED3 var PortB.4
LED4 var PortB.5
LED5 var PortB.6
LED6 var PortB.7
LED7 var PortA.0
LED8 var PortA.1
LED9 var PortA.2

'********************** create variables **********************

duty var byte 'variable for the dutycycle
counter var word 'variable to increment/decrement
led_chk var byte
pwr var bit 'on/off bit
preset con 183 'minimum pwm duty value for low
stp con 2 'duty cycle steps

'*********************** initialize variables *****************

pwr = 1
duty = 15
counter = 50
LED = 0
'porta.3 = 0
'porta.4 = 0

'*********************** program begins here ******************
'pause 1000
gosub adjust
main:
if up = 1 then gosub increment
if down = 1 then gosub decrement

'********** display ***************
led = 0
' pause 1000
lookdown2 duty, [93, 111, 129, 147, 165, 183, 201, 219, 237, 255], led_chk
lookup led_chk, [%0000000001, %0000000011, %0000000111, %0000001111, %0000011111, _
%0000111111, %0001111111, %0011111111, %0111111111, %1111111111], led

'Remmed out replaced above
' if led.0 = 1 then portb.0 = 1
' if led.1 = 1 then portb.1 = 1
' if led.2 = 1 then portb.2 = 1
' if led.3 = 1 then portb.4 = 1
' if led.4 = 1 then portb.5 = 1
' if led.5 = 1 then portb.6 = 1
' if led.6 = 1 then portb.7 = 1
' if led.7 = 1 then porta.0 = 1
' if led.8 = 1 then porta.1 = 1
' if led.9 = 1 then porta.2 = 1
pause 100
toggle porta.3 'heartbeat
goto main
end
increment:
portb.1 = 1
pause 5
counter = counter + stp 'if so, increment
if counter > 254 then counter = 254
portb.1 = 0
pause 5
goto adjust

decrement:

portb.2 = 1
pause 5
counter = counter - stp
if counter < 2 then counter = 2
Portb.2 = 0
pause 5



goto adjust
adjust:
duty = counter
'********** duty_set ************** duty = preset + counter
' if duty >=255 then duty = 255
'if pwr = 0 then duty = 0
hpwm 1, duty, 2000
'********** button_chk ***********
return


I used the lower b ports as indicators, you can change them back, as I am using a Microchip demo board. I scoped the output and it does what you want, i didn't fuddle much with your display except to remove some of my favorite pasta :D too many if thens for me.

alec
- 25th January 2010, 16:54
Thanks Joe - I'll try it when get home tonight

"LED var byte[10] '10 bits - 1 for each led segment
LED0 var PortB.0
LED1 var PortB.1
LED2 var PortB.2
LED3 var PortB.4
LED4 var PortB.5
LED5 var PortB.6
LED6 var PortB.7
LED7 var PortA.0
LED8 var PortA.1
LED9 var PortA.2"

I tried this - I must have misread the manual, I thought the bit number had to be in brackets.

"remove some of my favorite pasta too many if thens for me. " I'm still learning how to write clean code

Thanks again - I'll post again tonight.
Alec

Archangel
- 25th January 2010, 17:52
" I'm still learning how to write clean code

Alec
<b>MEE TOOOO!</b> Darrel has busted me out over " PASTA" numerous times.

Archangel
- 26th January 2010, 02:08
I tried this - I must have misread the manual, I thought the bit number had to be in brackets.


Alec
That's because you are right! Should be LED.0 = portb.0 . . . check Darrel's virtual port . bas and look for a post in PBP about it today. Still the PWM routines work, still struggling with the display, funny it works if using portB. Going to have to sleep more I guess.

alec
- 26th January 2010, 12:53
Hi Joe
Thanks for the links - I followed some of the threads and Melanie's explanation that Darrel refers to is easiest for my simple brain to wrap around...
But, I have to resist the temptation to work on it more now - I'm supposed to be working my day job now. Should have more time tonight.

alec
- 27th January 2010, 01:05
Well, I got it working the essentially the way I wanted it to.
Holding the up button for a few seconds turns the power on, holding the down button turns the power off.
Thanks for the help and links for guidance.
BTW - how do I insert code neatly in a window?

'************************************************* ***************
'* Name : auto_heater_controller_rev1.pbp *
'* Author : Alec Noble *
'* Notice : Copyright (c) 2010 Alec Noble *
'* : All Rights Reserved *
'* Date : 1/25/2010 *
'* Version : 2.0 *
'* Notes : Modified with input from Joe S on PicBasicPro forum
'* : *
'************************************************* ***************
@ device pic16F628a
@ device INTRC_OSC_NOCLKOUT

DEFINE OSC 4
CMCON = 7 'port a digital i/o
VRCON = 0 'a/d voltage refernce disabled
PortB = 0 'initialize all portB logic low
TRISB = %00000000 'port b all outputs
PortA = 0 'Initialize all outputs as low
TRISA = %11000000 'a.7, a.6 inputs, all else output

'********************** aliasing I/O ****************************

up var porta.6 'up or increment button input
down var porta.7 'down or drcrement button input
heat var portb.3 'heater PWM output
tx var porta.4 'serial output pin for debuggin LCD

'********************** create variables **********************
LED var word '10 bits - 1 for each led segment
duty var word 'variable for the dutycycle
counter var word 'variable to increment/decrement
led_chk var byte 'variable for lookdown/lookup
pwr var bit 'on/off bit
counter1 var word 'counter for on/off check


preset con 93 'minimum pwm duty value for low
stp con 18 'duty cycle steps

'*********************** initialize variables *****************

pwr = 0
duty = 0
counter = 0
counter1= 0
LED = 0
led_chk = 0


'*********************** initialize serial LCD for debugging ****
pause 1000
serout2 tx, 396, [22, 17, 12]
pause 250

'*********************** program begins here ******************

main:
if up = 1 then increment
if down = 1 then decrement

'************************* display ***************

lookdown duty, [0,93, 111, 129, 147, 165, 183, 201, 219, 237, 255], led_chk
lookup2 led_chk, [%0000000000,%0000000001, %0000000011, %0000000111, %0000001111, %0000011111, _
%0000111111, %0001111111, %0011111111, %0111111111, %1111111111], LED

portb.0 = led.0
portb.1 = led.1
portb.2 = led.2
portb.4 = led.3
portb.5 = LED.4
portb.6 = LED.5
portb.7 = LED.6
porta.0 = LED.7
porta.1 = LED.8
porta.2 = LED.9

pause 100
toggle porta.3 'heartbeat

LCD_output: 'LCD output for debugging
serout2 tx, 396, [128, dec3 duty, " ", dec1 led_chk, " ", dec LED]
pause 5
goto main
end

increment:
pause 20 'debounce input
if up = 0 then main
while up = 1 'wait for button to be released
pause 5
counter1 = counter1 + 1
if counter1 > 250 then jump1 'if held long enough turn on
wend
counter = counter + 1 'increment PWM counter
if counter > 9 then counter = 9 'set maximum PWM counter
jump1:
pwr = 1 'turn on
counter1 = 0 'resete pwr counter to 0
goto adjust

decrement:
pause 20
if down = 0 then main 'debounce
while down = 1 'wait for button to be released
pause 5
counter1 = counter1 + 1
if counter1 > 250 then jump2 'if held long enough turn off
wend
counter = counter - 1 'decrement PWM counter
if counter < 2 then counter = 1 'set mimimum PWM counter
counter1 = 0 'reset on/off counter
goto adjust
jump2:
pwr = 0 'turn off
counter1 = 0 'reset on/off counter
goto adjust

'************************* PWM set ***********************************
adjust:
duty = preset + (counter * 18)
if duty >=254 then duty = 254 'set maximum PWM
if pwr = 0 then duty = 0 'if power off set PWM to 0
hpwm 1, duty, 20000
goto main 'do it again

ScaleRobotics
- 27th January 2010, 01:36
BTW - how do I insert code neatly in a window?


[ code ]
code goes here
[ /code ]

(without the spaces after and before the brackets)

Art
- 27th January 2010, 01:58
lookup2 led_chk, [%0000000000,%0000000001, %0000000011, %0000000111, %0000001111, %0000011111, _
%0000111111, %0001111111, %0011111111, %0111111111, %1111111111], LED

could be :



LED = $FF : LED = LED >> (9 - led_chk)