Quote Originally Posted by sayzer View Post
Hi there,

There are couple of things you did not specify in your code.

1. You set your OSC to be 8Mhz (internal OSC) but you are declaring it to PBP as 20Mhz. Are you using internal OSC or external OSC?

2. Since you did not enable internal weak pull ups on PORTB pins, I assume you are using external pull up resistors on your input buttons. If you are not using any, then input pins may behave like crazy.

3. This chip has analog port on PORTA. You have a speaker on PORTA.0 but you did not disable analog module.

4. "If Power_LED = 1 ..." this won't work. You can not read the state of an output pin. I suggest that you set a flag bit and read that flag instead.

===============================
1. OSCON is recycled code, I commented it out. However I am programming it to use HS in the config bit so it shouldn’t really matter... (?)

2. Weak pull ups are not enabled but the hardware uses cookie cutter schematic example from PBP manual under the "Button" command. Pin is held low Via 10K resistor until pushed, then goes high.

3. I haven’t even bothered with the port a stuff yet. Its basically just sending a DTFMOUT command when a button is pressed.

4. "IF POWER LED = 1 Then..", It actually DOES work. In fact, there is absolutely no issues with that line of code at all. I tested it by simply not turning on the power LED. When Power LED is off it does not execute the if/then statement; only when it is on. Either way, I’ve tried using a simple 1 bit flag and it didn’t resolve anything.


Here are the lines of code in question with my notes as to what’s happening:

Start:
If Power = 1 Then Power_Pressed <----- Only branch to power pressed if button pushed
'If Power_LED = 1 then ' Execute only if in ON state
' If Normal = 1 Then Normal_Pressed ' Normal button pressed?
' If Pursuit = 1 Then Pursuit_Pressed ' Pursuit button Pressed?
' If Auto = 1 then auto_pressed ' Auto button pressed
'EndIF

Goto Start ' Loop forever

.
.
.

Power_Pressed: <---- Should only be here when power pressed. This part seems fine until...
If Power_LED = 1 Then <--- Check to see if we are on of off by looking at power LED
Low Power_LED ' Turn off Power LED
Low Auto_LED ' Turn off Auto LED
Low Normal_LED ' Turn off Normal LED
low Pursuit_LED ' Turn off Pursuit LED
pause 1000 ' 1 second denounce delay

================================================== ===============
Right here is the problem. At this point what will happen is the unit goes into (simulated) power off mode and waits for 1 second. That at least confirms the code is working, and the swtich should have had ample time to no longer be in a pushed state. And the above block does indeed do what it is supposed to when it is supposed to.

So at this point we should be jumping over everything in the ELSE statement and going directly back to start. Instead, it executes everything in the else statement.
================================================== ==================

ELSE
High Power_LED ' Turn on Power_LED
High Normal_LED ' Turn on Normal_LED
Pause 1000 ' 1 Second Debounce delay
endif
Goto Start



What I have done to try and resolve this is: use a flag instead of directly reading the pin: there was no change. I’ve added test code to confirm that the unit is executing the code in the "else" section as opposed to just restarting the program. I have even tried putting in Repeat/until loops to check for the button being 0 again and then being 1. I’m starting to think the think is just possessed. Everything else works exactly the way it is supposed to. I’ve tried the same code testing different switches... It all boils down to this one if/then/else statement that SHOULD work.


Just for complete fun, I decided to go back and re-add a flag instead of reading the port directly. It had no effect. I even went as far as trying a brand new chip (16F818 instead of 819) and it still does the same exact thing. here is the NEW code: And I’m just totally stumped.




DEFINE OSC 20 ' 20 Mhz Oscillation
'OSCCON = $70 ' Oscilator Config reg.

Clear ' Clear all memory
TRISA = %00000000 ' Set port a to all out
TRISB = %00001111 ' Set Port b to i/o


Speaker Var PortA.0 ' Tone pin out
Power Var PortB.0 ' Input for power switch
Auto VAR PortB.1 ' Input for Auto Switch
Normal VAR PortB.2 ' Input for Normal Switch
Pursuit VAR Portb.3 ' Input for Pursuit Switch
Power_LED VAR Portb.7 ' Output for Power Lamp
Auto_LED Var Portb.6 ' Output for Auto Lamp
Normal_LED VAR Portb.5 ' Output for Normal Lamp
Pursuit_LED VAR Portb.4 ' Output for Pursuit Lamp
Power_Flag Var bit ' Flag to indicate power on/off


Initialize:
High Power_LED ' Power Default = ON
low Auto_Led ' Auto Defualt = OFF
High Normal_LED ' Normal Default = ON
low Pursuit_LED ' Pursuit Default = ON
Power_Flag = 1 ' Unit is on, set power flag

Start:
If Power = 1 Then Power_Pressed
If Power_Flag = 1 then ' Execute only if in ON state
If Normal = 1 Then Normal_Pressed ' Normal button pressed?
If Pursuit = 1 Then Pursuit_Pressed ' Pursuite button Pressed?
If Auto = 1 then auto_pressed ' Auto button pressed
EndIF
Goto Start ' Loop forever

'**************************SUBROUTINE AREA********************************

Auto_Pressed: ' Auto button code:
Low Pursuit_LED ' Turn off Pursuit lamp
Low Normal_LED ' Turn off Normal Lamp
High Auto_LED ' Turn on Auto Lamp
'DTMFOUT Speaker, [1] ' Send tone sound out
Goto start ' Go back to main routine

Normal_Pressed: ' Normal button code:
Low Pursuit_LED ' Turn off Pursuit lamp
Low Auto_LED ' Turn off Auto Lamp
High Normal_LED ' Turn on Normal Lamp
'DTMFOUT Speaker, [1] ' Send tone sound out
Goto Start ' Go back to main routine

Pursuit_Pressed: ' Pursuit button code:
Low Normal_LED ' Turn off Normal Lamp
Low Auto_LED ' Turn off Auto Lamp
High Pursuit_LED ' Turn on Pursuit Lamp
'DTMFOUT Speaker, [1] ' Send tone sound out
Goto Start ' Go back to main routine


Power_Pressed:
If Power_Flag = 1 Then
Low Power_LED ' Turn off Power LED
Low Auto_LED ' Turn off Auto LED
Low Normal_LED ' Turn off Normal LED
low Pursuit_LED ' Turn off Pursuit LED
pause 1000 ' debounce delay
Power_Flag = 0 ' Falgged for off state
ELSE
High Power_LED ' Turn on Power_LED
High Normal_LED ' Turn on Normal_LED
Pause 1000 ' Debounce delay
Power_Flag = 1 ' Flagged for on state
endif
Goto Start