PDA

View Full Version : HSERIN restarts PIC18F2525?



breesy
- 29th June 2006, 10:22
Hey, im trying to (at first) get some simple RS-232 communications between a computer the pic using the following code:

DEFINE OSC 20

' Setup Hardware for UART
DEFINE HSER_BAUD 9600
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

RCIF var PIR1.5
address var byte
sentinal var byte
buffer var byte[30]

init:
Pause 10 'Safe Start Up Delay

ADCON1 = 15 ' All pins are digital
CMCON = 7 ' Turn off comparators
Poke $81,$FF ' Turn off PORT B pull ups

TRISA = %00000000
TRISB = %00000000
TRISC = %10000000

address = %00000100
sentinal = %11111111

HSEROUT ["Activated"]

High PORTB.7

ON Interrupt GOTO inthandle
INTCON = %11000000
PIE1.5 = 1

mainloop:
@ Nop
Goto mainloop


DISABLE

inthandle:
If RCIF Then
HSERIN 100, intreturn, [wait(address),str buffer\30\sentinal]
HSEROUT [str buffer\30]

Low PORTB.7
EndIf
If RCIF Then inthandle

intreturn:
Resume mainloop

ENABLE




So what happens? Upon startup the pic reports 'Activated' and the LED on PORTB.7 lights up... When data is sent to the PIC the LED flickers off and on and sends 'Activated' to the computer, usually once but occasionally several times.

When the 'Low PORTB.7' command is moved before the HSERIN command, incoming data shuts off the LED, but the PIC still restarts. The LED does not reignite after restart.

Why does this happen?

Darrel Taylor
- 30th June 2006, 01:07
Hi breesy,

It's probably the Resume mainloop causing a Stack Overflow.

Change it to just Resume

For more info, see this thread
http://www.picbasic.co.uk/forum/showthread.php?p=20286&highlight=resume
and post 17, same thread.
<br>

breesy
- 30th June 2006, 03:32
No luck, still restarts after every HSERIN..

Strange thing is with only a few small modifications the code works on 16F chips.

Also, if i send data with the wrong address byte, ie something over than %00000100, it does not restart and does not disable the led. Jumps to intreturn after timeout?

Darrel Taylor
- 30th June 2006, 05:50
Hmmmm, very odd.

I've run it here with an 18F452 and everything seems fine.
Just changed the CMCON and the LED Port. (and the resume)

http://www.darreltaylor.com/files/HserinReset.GIF

Barring a wiring error, or maybe something with the 2525 (don't have one), ya got me.
<br>

Bruce
- 30th June 2006, 17:02
Don't use POKE. Just use the register name followed by the value to write to
it like you have with ADCON1 = 15.

Poke $81,$FF is writing $FF to RAM address $81 in bank 0 on this part.

INTCON2.7 controls portb pull-ups, but you don't really need to turn them off
with all of portb set to outputs. And the default value of INTCON2 at POR has
them disabled.

Don't use RETURN mainloop. As Darrel already pointed out, this is causing
stack overflow. Using a specific return address here keeps the compiler from
generating RETFIE, popping the "real" return address from the stack, and
resetting global interrupts on return.

Another potential problem is using a timeout with label allowing exit from your
interrupt handler without clearing RCIF interrupt flag bit first.

Also with address = %00000100 and wait(address) your terminal program ( or
whatever you're sending serial data with ) needs to send this as non ASCII. If
you make address = "4", then it should work when receiving the ASCII value 4.

With the optional terminator stuck on the end, it can terminate reception of
the full 30 byte string too. Then HSEROUT [str buffer\30] will send the whole
30 byte string which may or may not be full of ASCII characters causing
garbage to be output in non ASCII array element positions.

Try this;


DEFINE OSC 20

' Setup Hardware for UART
DEFINE HSER_BAUD 9600
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1

RCIF var PIR1.5
Junk VAR BYTE
address var byte
sentinal var byte
buffer var byte[30]

init:
Pause 10 'Safe Start Up Delay

ADCON1 = 15 ' All pins are digital
CMCON = 7 ' Turn off comparators
' Poke $81,$FF ' Turn off PORT B pull ups
INTCON2.7=1 ' Turn off PORT B pull ups

TRISA = %00000000
TRISB = %00000000
TRISC = %10000000
PORTB = $ff

address = "4" ' or %00000100 +"0"
sentinal = $FF '%11111111

HSEROUT ["Activated"]

HIGH 7

ON Interrupt GOTO inthandle
INTCON = %11000000
PIE1.5 = 1

mainloop:
@ Nop
Goto mainloop


DISABLE

inthandle:
TOGGLE 1
If RCIF Then
HSERIN 100, intreturn, [wait(address),str buffer\30\sentinal]
HSEROUT [str buffer\30,13,10]
Low 7
EndIf
If RCIF Then inthandle

intreturn:
WHILE RCIF
Junk = RCREG ' trash leftovers to clear RCIF before return
WEND

Resume
ENABLE

END
Send 4=12345678901234567890123456789 with MCS terminal program to it.

Then try inserting $FF in the string somewhere to see what happens when it
finds the terminator. 4=123456789012345678$FF9012345678

Ron Marcus
- 30th June 2006, 21:22
I had issues like this with the LVP being enabled(The equation is...me = moron). It caused havoc, and was one of those stupid errors that create problems that are seemingly unrelated and difficult to debug.If you don't need it, disable it.

Ron

breesy
- 1st July 2006, 06:34
Hmm I tried the changes that bruce recommended and made sure lvp was off, but still not working... I get 'Activated' after everything i send to it (and the LED doesn't turn off)

mister_e
- 1st July 2006, 09:50
Bad psu filtering
poor contact
missing or loose MCLR pull-up
bad crystal
wrong crystal value

my guesses as now.

EDIT: i just tried it here with a 2525... it doesn't do the problem you have.