PDA

View Full Version : Must reset PIC to get correct operation?



Christopher4187
- 28th May 2006, 13:12
Below is a code that I am using to monitor inputs to a 16F870. As discussed in an earlier post, I am controlling and monitoring these inputs through a wireless link. When I activate them locally (by applying voltage right to the pin), it works well and I have no problems. When I control it with the wireless transmitter, I find that the PIC "locks" up and I have to reset it to work correctly. I found, through a lot of time experimenting, that using a nap or sleep command seems to correct the problem. I can't figure out why it's happening and I am not sure if it is hardware or software related. I don't think it's the relays because the transmitter and receiver are on seperate boards. I am suspect of the wireless transmission and I wonder if it is possible that the data or power of the transmitter is locking up the PIC. Anyhow, please take a look at my code but without the nap command at the end, it won't work correctly.

Thanks,

Chris



@ DEVICE PIC16F870, HS_OSC, WDT_On, PWRT_ON, BOD_on, LVP_OFF , DEBUG_OFF, PROTECT_OFF

DEFINE OSC 4
DEFINE HSER_BAUD 2400
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_CLROERR 1
ADCON1=7
b1 var BYTE 'first of 4 vars to store verified good data
b2 var BYTE
b3 var BYTE
b4 var BYTE
C1 var byte
C2 var byte
C3 var byte
C4 var byte
start var byte 'first incoming byte - should be zero
sum var WORD 'incoming checksum from the sending PIC
datasum var BYTE 'used to add values of 4 incoming data bytes
errorflag var bit 'flag to indicate bad data (1 = bad or untested data)

TRISA=%11111111
TRISB=%11111111
TRISC=%10111111

HIGH portb.3
PAUSE 100
MAINLOOP:


if PORTA.5=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTA.2=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTA.1=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTA.0=0 THEN
B4=0
ELSE
B4=1
ENDIF
C1=(B1*1)+(B2*2)+(B3*4)+(B4*8)


if PORTB.1=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTB.5=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTB.4=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTB.2=0 THEN
B4=0
ELSE
B4=1
ENDIF
C2=(B1*1)+(B2*2)+(B3*4)+(B4*8)

if PORTC.0=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTC.1=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTC.2=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTC.3=0 THEN
B4=0
ELSE
B4=1
ENDIF
C3=(B1*1)+(B2*2)+(B3*4)+(B4*8)

if PORTC.7=0 THEN
B1=0
ELSE
B1=1
ENDIF
if PORTC.5=0 THEN
B2=0
ELSE
B2=1
ENDIF
if PORTC.4=0 THEN
B3=0
ELSE
B3=1
ENDIF
if PORTB.0=0 THEN
B4=0
ELSE
B4=1
ENDIF
C4=(B1*1)+(B2*2)+(B3*4)+(B4*8)
GOSUB SENDATA


GOTO MAINLOOP

SENDATA:
sum = C1+C2+C3+C4
HSEROUT [30,C1,C2,C3,C4,sum.byte0]
pause 40
nap 0

RETURN

Archilochus
- 28th May 2006, 17:46
Hi Chris,
I've no idea if this could cause the problem you describe... but you have the COFIG set to "HS", but seem to be using a 4MHz osc? (which I think would use the "XT" setting).




@ DEVICE PIC16F870, HS_OSC, WDT_On, PWRT_ON, BOD_on, LVP_OFF , DEBUG_OFF, PROTECT_OFF

DEFINE OSC 4


Arch

Acetronics2
- 28th May 2006, 18:03
Hi, Arch

No Problem to use a Faster OSC config ... that only draws a little more current to the supply.
Sometimes permits to use "lazy" Xtals ...

Alain

mister_e
- 29th May 2006, 14:16
How about if you disable the watchdog timer?

I know PBP add some CLRWDT instruction here and there but, i guess it worth a try. Unless you use a WDT interrupt, i guess you simply don't need it.

Just a suggestion


if PORTA.5=0 THEN
B1=0
ELSE
B1=1
ENDIF


you'll save codespace and increase your program speed if you use


B1=PORTA.5

Maybe the HS mode can do something... maybe for some crystal brand and not on some other.

Christopher4187
- 29th May 2006, 15:36
Steve,

Thanks for the help with the code (b1=portx.x), that will certainly save some space. I tried removing the WDT but I still had the same results. Now I need it because the NAP command is dependent upon the WDT....if I read the manual correctly. I'll try messing around with the clearwdt and wdt commands to see what happens.

Thanks

mramos
- 2nd June 2006, 01:59
Christopher4187

You have a reset resistor on the chip for sure, what value? Also, with the RF, you do have the chip decoupled well?

I designed a PIC circuit many years ago, and it worked great. It moved a servo to hide a plate on the back of a show motorcycle. And sometimes it would go nuts when the owner rode it. Turned out, he stored his Nextel celephone in the compartment the circuit was in, and the RF was resetting the PIC, and dropping the plate back down (init setting). I added some caps, and all fixed.

Just a thought as you are wondering if HW/SW. Maybe drop a scope on the reset, and on the POWER pin and watch it..