PDA

View Full Version : What would cause this anomaly?



peterdeco1
- 5th August 2005, 10:52
Hello Everyone. I just had an interesting experience I just can't explain. My PIC is a 16F818. PORTA is held high through a 47K resistor network. 3 switches on PORTA turn on 3 LED's on PORTB. When all 3 switches are closed 2 additional PORTB's go high to turn on 2 MOSFETS. Here is the anomaly: ONLY when PORTA.5 is the LAST to close, the LED on PORTB.3 does NOT light. However all the other PORTB's do go high the way they are supposed to, including the MOSFET's, proving it is indeed jumping to "REWARD". Only PORTB.3 does NOT! Now, I did fix it by changing "REWARD" as shown below. The only difference is instead of 5 lines turning on 5 PORTB's, 1 line (PORTB = 255) turns them all on. What would possibly cause this? Thank you.

START:
NAP 0
PORTB = 0 'OFF ALL LEDS & TRANSISTORS
IF PORTA.5 = 1 AND PORTA.4 = 1 AND PORTA.3 = 1 Then START 'WAIT HERE UNTIL A SWITCH IS CLOSED

RUN:
IF PORTA.5 = 0 Then High PORTB.3 'SWITCH CLOSED - LIGHT UP LED
IF PORTA.5 = 1 Then Low PORTB.3 'SWITCH OPEN - TURN OFF LED
IF PORTA.4 = 0 Then High PORTB.2 'SWITCH CLOSED - LIGHT UP LED
IF PORTA.4 = 1 Then Low PORTB.2 'SWITCH OPEN - TURN OFF LED
IF PORTA.3 = 0 Then High PORTB.5 'SWITCH CLOSED - LIGHT UP LED
IF PORTA.3 = 1 Then Low PORTB.5 'SWITCH OPEN - TURN OFF LED
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD 'ALL SWITCHES CLOSED
IF PORTA.5 = 1 AND PORTA.4 = 1 AND PORTA.3 = 1 Then START 'ALL SWITCHES OPEN
GoTo RUN 'STAY HERE UNTIL ALL ON OR OFF

************OLD ONE DOESN'T WORK***************
REWARD:
HIGH PORTB.3 'MAKE SURE ALL 3 LED'S ON PORTB.3, 2 & 5 ARE ON (PORTB.3 DOES NOT!!)
HIGH PORTB.2
HIGH PORTB.5
HIGH PORTB.4 TURN ON 'MOSFETS ON PORTB.4 & 7
HIGH PORTB.7
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD 'ALL SWITCHES CLOSED STAY HERE
Pause 100
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD 'ALL SWITCHES CLOSED STAY HERE
Low PORTB.7 '1 OR MORE SWITCHES OPEN OFF MOSFETS
Low PORTB.4
GoTo RUN

***************NEW ONE WORKS**********************
REWARD:
PORTB = 255 '*********HERE IS THE FIX************
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD
Pause 100
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD
Low PORTB.7
Low PORTB.4
GoTo RUN

rhino
- 5th August 2005, 13:49
Not sure why you check your inputs twice like this...
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD Pause 100
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARDWhen you IF statement is true it will jump back up to the REWARD label and set your outputs again. I would think that..
REWARD:
HIGH PORTB.3 'MAKE SURE ALL 3 LED'S ON PORTB.3, 2 & 5 ARE ON (PORTB.3 DOES NOT!!)
HIGH PORTB.2
HIGH PORTB.5
HIGH PORTB.4 TURN ON 'MOSFETS ON PORTB.4 & 7
HIGH PORTB.7
IF PORTA.5 = 0 AND PORTA.4 = 0 AND PORTA.3 = 0 Then REWARD 'ALL SWITCHES CLOSED STAY HERE
Pause 100
'removed 2nd if statement
Low PORTB.7 '1 OR MORE SWITCHES OPEN OFF MOSFETS
Low PORTB.4
GoTo RUN would work.

Acetronics2
- 5th August 2005, 14:03
Hi, Rhino

Has a little smell of debouncing ... no ???

Alain

peterdeco1
- 5th August 2005, 14:06
The switches are actually magnetic reed switches activated by...yes a magnet. However the magnets are embedded in wooden pegs. The pegs slip into a slot to close the switches. However the fit between the pegs and slots are a little sloppy (loose). By pausing and repeating the command keeps the reward going in case one of the switches jitters.

rhino
- 5th August 2005, 14:15
Hi, Rhino

Has a little smell of debouncing ... no ???

Alain
I agree... if this is truely the probem, peterdeco1, you might want to search through the forums for different techniques on debouncing your switches. There should be a wealth of information on it here somewhere.

Acetronics2
- 5th August 2005, 14:31
For the lack of PortB.3 ... I'm not sure, but I read somewhere here about a bug or twisting in the pins and more generally modifyers ...

you could verify that by naming your inputs and outputs pins as

LedB3 var PortB.3

etc

and using no more modyfiers ...

Alain

Bruce
- 5th August 2005, 17:39
See if this helps.

Add TRISB = %00000000 to the initialization section of your code to set RB pins up as outputs.

Change each HIGH or LOW PORTB.? to PORTB.? = 1 or 0.

Does it work as expected?

NavMicroSystems
- 5th August 2005, 17:57
I'm not sure if you have already done in a part of your code that has not been posted,

PortA 4:0 are analog inputs by default, set them to digital!

peterdeco1
- 6th August 2005, 10:36
I'm sorry I didn't post the complete code. I tried to shorten my post. At the top of my program I have:
OSCCON = $60 'set int osc to 4mhz
adcon1 = 7 ' set inputs to digital
@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON
TRISA = %11111111
TRISB = %00000000

Again - the anomaly is ONLY when PORTA.5 is the LAST to close, the LED on PORTB.3 does NOT light. But the program DOES jump to "REWARD" and the MOSFETS will turn on, but PORTB.3 stays low. If you close the switch on PORTA.5 first or second, PORTB.3 goes high the way it's supposed to. Thank you.