PDA

View Full Version : PIC16F877A PAUSE command



Nicholas
- 5th December 2006, 20:56
Hi,

Am having trouble with the "PAUSE" command in my code. Am not sure what additional registers need to be set to enable this command. When I remove the ,PAUSE, before the goto Lowvoltagecheck the LED lights up and stays on (whether LowVoltage is 1 or 0). If I keep the pause on the program the LED never goes ON. The LED should flash or stay on for 5 seconds but stays on indefinitely meaning my PAUSES are not working.

What more is needed?

;************************************************* *******
CMCON = %00000111 'Port A all digital
CVRCON = %00000000 'CVref turned off
OPTION_REG = %10000111

Main:

low HoldTxON ; Initialize by clearing all the outputs
low LED
Low MotorControl_1
LOW MotorControl_2

pause 5000
goto Lowvoltagecheck

end
;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:
If lowVoltage = 1 then ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
else
for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
next
endif
return
;************************************************* *******

Thanks,
Nicholas.

Bruce
- 5th December 2006, 21:31
You're using GOTO with RETURN in the sub-routine. Try this version;


;************************************************* *******
CMCON = %00000111 'Port A all digital
CVRCON = %00000000 'CVref turned off
OPTION_REG = %10000111

Main:

low HoldTxON ; Initialize by clearing all the outputs
low LED
Low MotorControl_1
LOW MotorControl_2

pause 5000
gosub Lowvoltagecheck
goto Main

end
;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:
If lowVoltage = 1 then ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
else
for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
next
endif
return
;************************************************* *******

Nicholas
- 6th December 2006, 14:27
Bruce thanks for your prompt reply and advice.
However, that didn't seem to fix the issue. I think I should mention this is a small part of a bigger program but this is the code am using to test this part. Am actually going to be using a PIC16F627A but am using the 16F877A to test the logic of the code. (Am using a LabX1 programmer and its the only 40pin I have). Haven't successfully been able to jumper the board to program an 18pin.

Bruce
- 6th December 2006, 15:39
Hi Nicholas,

Can you post all of your code? It helps to know which pins you have defined,
and how you have things configured.

mister_e
- 6th December 2006, 15:58
i agree to see the whole thing.

I find something strange here...


for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
next

There's something missing... a simple pause after the Low LED.

Nicholas
- 7th December 2006, 17:30
Here's the whole Code, I tried to use NAP in place of PAUSE and the LED stays on for the alloted time but the blink part does not work. I've also attached a variant to the blink part which also failed to blink the LED.

;************************************************* ********

; Define statements.
define NO_CLRWDT 1 ;Don't insert CLRWDTs
define OSC 20 ;every MPU used is 20mhz

'Port operation configurations
CMCON0 = %00000111 'Port A all digital
VRCON = %00000000 'CVref turned off
OPTION_REG = %10000111 'Disable PORTA pullups, set TMR0 scaler to 1:256

; Program Variables
Loop VAR BYTE ;general loop counter for LED flashing

Outp CON 0
Inp CON 1

'Assign Ports to simple name
LowVoltage VAR PortA.0 'Battery low voltage detection port
ReedSwitchState var PORTA.1 'Reed Switch ON and OFF state line
LED VAR PORTA.2 'Turn LED ON and OFF
HoldTxON VAR PORTC.0 'Hold Tx ON when water contacts activated
Tx_Data VAR PORTC.1 'Transmit Data Line
Tx1_Enable VAR PORTC.2 'Transmit 121.5Mhz Line
Tx2_Enable VAR PORTC.3 'Tranmit 156MHz Line
MotorControl_1 VAR PORTC.4 'Activate Release Mechanism
MotorControl_2 VAR PORTC.5 'Activate Latch Mechanism

'Initialize port states
TrisA.0=Inp 'set batt low voltage detection to input
TrisA.1=Inp 'set Reed switch state to input
TrisA.2=Outp 'set led transmission to output
TrisC.0=Outp 'set Hold Tx Line ON to output
TrisC.1=Outp 'set Transmit data line to output
TrisC.2=Outp 'set Transmit @121.5 to output
TrisC.3=Outp 'set Transmit @156 to output
TrisC.4=Outp 'set Motorcontrol_1 to output
TrisC.5=Outp 'set Motorcontrol_2 to output

'Goto Main
;************************************************* ******************************
Main:

low HoldTxON ; Initialize by clearing all the outputs
low LED
low Tx_Data
Low Tx1_Enable
low Tx2_Enable
Low MotorControl_1
LOW MotorControl_2

GOSUB Lowvoltagecheck

end
;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:

If lowVoltage = 1 then ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
else
for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
next
endif
return
;************************************************* ********


PS: This is the code I used to try and break up the subroutine further for testing but failed to work also.

;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:
If lowVoltage = 1 then GoodBattery
if lowvoltage = 0 then LowBattery

LowBattery:
for loop = 1 to 10 ; If the battery voltage is low
gosub Blink
next loop

Blink:
high led ; Flash the LED 10 times
pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
return

GoodBattery: ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
return
;************************************************* ********

skimask
- 7th December 2006, 17:34
Here's the whole Code, I tried to use NAP in place of PAUSE and the LED stays on for the alloted time but the blink part does not work. I've also attached a variant to the blink part which also failed to blink the LED.

;************************************************* ********

; Define statements.
define NO_CLRWDT 1 ;Don't insert CLRWDTs
define OSC 20 ;every MPU used is 20mhz

'Port operation configurations
CMCON0 = %00000111 'Port A all digital
VRCON = %00000000 'CVref turned off
OPTION_REG = %10000111 'Disable PORTA pullups, set TMR0 scaler to 1:256

; Program Variables
Loop VAR BYTE ;general loop counter for LED flashing

Outp CON 0
Inp CON 1

'Assign Ports to simple name
LowVoltage VAR PortA.0 'Battery low voltage detection port
ReedSwitchState var PORTA.1 'Reed Switch ON and OFF state line
LED VAR PORTA.2 'Turn LED ON and OFF
HoldTxON VAR PORTC.0 'Hold Tx ON when water contacts activated
Tx_Data VAR PORTC.1 'Transmit Data Line
Tx1_Enable VAR PORTC.2 'Transmit 121.5Mhz Line
Tx2_Enable VAR PORTC.3 'Tranmit 156MHz Line
MotorControl_1 VAR PORTC.4 'Activate Release Mechanism
MotorControl_2 VAR PORTC.5 'Activate Latch Mechanism

'Initialize port states
TrisA.0=Inp 'set batt low voltage detection to input
TrisA.1=Inp 'set Reed switch state to input
TrisA.2=Outp 'set led transmission to output
TrisC.0=Outp 'set Hold Tx Line ON to output
TrisC.1=Outp 'set Transmit data line to output
TrisC.2=Outp 'set Transmit @121.5 to output
TrisC.3=Outp 'set Transmit @156 to output
TrisC.4=Outp 'set Motorcontrol_1 to output
TrisC.5=Outp 'set Motorcontrol_2 to output

'Goto Main
;************************************************* ******************************
Main:

low HoldTxON ; Initialize by clearing all the outputs
low LED
low Tx_Data
Low Tx1_Enable
low Tx2_Enable
Low MotorControl_1
LOW MotorControl_2

GOSUB Lowvoltagecheck

end
;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:

If lowVoltage = 1 then ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
else
for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
next
endif
return
;************************************************* ********


PS: This is the code I used to try and break up the subroutine further for testing but failed to work also.

;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:
If lowVoltage = 1 then GoodBattery
if lowvoltage = 0 then LowBattery

LowBattery:
for loop = 1 to 10 ; If the battery voltage is low
gosub Blink
next loop

Blink:
high led ; Flash the LED 10 times
pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
return

GoodBattery: ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
return
;************************************************* ********


Don't you need another RETURN after the LOWBATTERY blinks?
JDG

Bruce
- 7th December 2006, 19:46
I've inserted comments where I added a few things. Let us know if this
version works.


;************************************************* ********

; Define statements.
' Now you know for sure what these are set for
@ device pic16F877A, hs_osc, wdt_off, lvp_off, protect_off

' Potential problem below unless you're disabling WDT in config fuse
' (like above) you do not want to use this define. PBP needs to insert
' CLRWDT commands throughout your code to make sure the WDT does
' not cause a device reset
define NO_CLRWDT 1 ;Don't insert CLRWDTs

define OSC 20 ;every MPU used is 20mhz

'Port operation configurations
CMCON0 = %00000111 'Port A all digital
VRCON = %00000000 'CVref turned off

' Added this to disable A/D. Now you can use ports A/E for digital I/O
ADCON1 = 7

OPTION_REG = %10000111 'Disable PORTA pullups, set TMR0 scaler to 1:256

; Program Variables
Loop VAR BYTE ;general loop counter for LED flashing

Outp CON 0
Inp CON 1

'Assign Ports to simple name
LowVoltage VAR PortA.0 'Battery low voltage detection port
ReedSwitchState var PORTA.1 'Reed Switch ON and OFF state line
LED VAR PORTA.2 'Turn LED ON and OFF
HoldTxON VAR PORTC.0 'Hold Tx ON when water contacts activated
Tx_Data VAR PORTC.1 'Transmit Data Line
Tx1_Enable VAR PORTC.2 'Transmit 121.5Mhz Line
Tx2_Enable VAR PORTC.3 'Tranmit 156MHz Line
MotorControl_1 VAR PORTC.4 'Activate Release Mechanism
MotorControl_2 VAR PORTC.5 'Activate Latch Mechanism

'Initialize port states
TrisA.0=Inp 'set batt low voltage detection to input
TrisA.1=Inp 'set Reed switch state to input
TrisA.2=Outp 'set led transmission to output
TrisC.0=Outp 'set Hold Tx Line ON to output
TrisC.1=Outp 'set Transmit data line to output
TrisC.2=Outp 'set Transmit @121.5 to output
TrisC.3=Outp 'set Transmit @156 to output
TrisC.4=Outp 'set Motorcontrol_1 to output
TrisC.5=Outp 'set Motorcontrol_2 to output

'Goto Main
;************************************************* ******************************
Main:

low HoldTxON ; Initialize by clearing all the outputs
low LED
low Tx_Data
Low Tx1_Enable
low Tx2_Enable
Low MotorControl_1
LOW MotorControl_2

GOSUB Lowvoltagecheck ' <-- problem area here. When program flow returns
' from Lowvoltagecheck, it lands on END and the PIC
' enters a continuous sleep loop
GOTO Main ' <-- this fixes the problem

end
;************************************************* ******************************
; Subroutine to check for battery voltage at the beginning and end of
; transmission flashing to signify low voltage or high voltage

LowVoltageCheck:
If lowVoltage = 1 then ; If the battery voltage is OK
high led ; turn on the LED for 5 seconds
pause 5000
LOw LEd
else
for loop = 1 to 10 ; If the battery voltage is low
high led ; Flash the LED 10 times
Pause 500 ; at a rate discernable from the
low led ; transmit LED flash rate
PAUSE 500 ' <-- added this pause so you can see the LED blink
next
endif
return
If this "doesn't" work, make sure you have a pull-up on /MCLR to Vcc, at least
a 0.1uF cap between PIC Vcc/GND pins, and a clean/stable power supply.

Nicholas
- 11th December 2006, 14:18
I finally got it to work through two changes. First I had to remove the CLRWDT define term and I was able to get the LED to blink accordingly. I also had to add the Analog Select Command (ANSEL) to set some of my pins to make the whole program work. Although am not quite clear how that affected my program, the ANSEL changes seemed to make it work.
Thanks for all your assistance.
Nicholas.

skimask
- 12th December 2006, 01:40
I finally got it to work through two changes. First I had to remove the CLRWDT define term and I was able to get the LED to blink accordingly. I also had to add the Analog Select Command (ANSEL) to set some of my pins to make the whole program work. Although am not quite clear how that affected my program, the ANSEL changes seemed to make it work.
Thanks for all your assistance.
Nicholas.



The CLRWDT has tripped me up on a few projects myself, and this was with the WDT disabled, not running, and otherwise not a factor... Somehow it seemed to have something to do with PBP's timing underneath somewhere, don't know where, don't really care. All I know is, if I'm having an under-fixable problem, either adding or removing the CLRWDT define is one of my personal 'troubleshooting' steps.
JDG