PDA

View Full Version : Serial Mishaps



shawn
- 4th July 2010, 17:37
Hello,
I am having problems receiving serial information.
What I want to do is have my pic18f4585 interrupt on a rx interrupt and load the data into an array. I am having major issues. No matter what I do I only receive 2 bytes worth of info. I am using MCS serial communicator to test and an lcd.

When I try this code and send abcdefgh with MCS serial communicator, I get back ab followed by a bunch of garbage


************************************************** *********************************
Define OSC 20

TXSTA = $24
'TXSTA.0 = Can be address/data bit or a parity bit.
'TXSTA.1 = 0 '0 = TSR full
'TXSTA.2 = 1 '1 = High speed
'TXSTA.3 = Unimplemented: Read as ‘0’
'TXSTA.4 = 0 '0 = Asynchronous mode
'TXSTA.5 = 1 '1 = Transmit enabled.
'TXSTA.6 = 0 '0 = Selects 8-bit transmission
'TXSTA.7 = Don’t care.

RCSTA = $90
'RCSTA.0 = Can be address/data bit or a parity bit.
'RCSTA.1 = 0 '0 = No overrun error
'RCSTA.2 = 0 '0 = No framing error
'RCSTA.3 = 0 '0 = Disables address detection.
'RCSTA.4 = 1 '1 = Enables continuous receive.
'RCSTA.5 = 0 '0 = Disables single receive.
'RCSTA.6 = 0 '0 = Selects 8-bit reception.
'RCSTA.7 = 1 '1 = Serial port enabled.

SPBRG = 10 '64 = 19200 Baud Asynchronous mode, High Speed.

INTCON.7 = 1 '1 = Enables all unmasked interrupts
INTCON.6 = 1 '1 = Enables all unmasked peripheral interrupts.
INTCON2.5 = 0 'INT1 edge select 0 = Falling Edge.
INTCON2.7 = 1 'Disable portb pullups.
INTCON3.3 = 1 'Enable INT1 interrupt.
RCON.7 = 0 '0 = Disable priority levels on interrupts.
PIE1.5 = 1 '1 = Enables the USART receive interrupt.
PIE1.4 = 0 '0 = Disables the USART transmit interrupt.

LCD Var PortC.3 'LCD Output alias.
RCIF var PIR1.5 'RCIF: USART Receive Interrupt Flag bit.
TXIF Var PIR1.4 'TXIF: USART Transmit Interrupt Flag bit.

Index var Byte
SerialData Var Byte[40]

On Interrupt Goto INTERUPTS 'Define interrupt handler

Pause 10 'Allows Stuff to get ready

pause 5000

Main:

Goto Main

Disable
INTERUPTS:

Serout2 LCD,16468,[16,105]
Index = 0

while RCif
SerialData[index]=RCREG ' by reading it and store result in a
Index = Index + 1
wend
Index = 0

While Index < 8
HSerout [SerialData[Index]]
Index = Index + 1
Serout2 LCD,16468,[SerialData[index]]
Wend

resume
'************************************************* ******************************

enable
end
************************************************** *********************************

Where am I going wrong I think I am close. This code almost matches some examples I have found on the forum. Eventualy I am going to worp this program into modbus rtu.
Help me Please.

Jerson
- 4th July 2010, 17:56
When I try this code and send abcdefgh with MCS serial communicator, I get back ab followed by a bunch of garbage
Yes of course. Your interrupts should be as short and sweet as possible (ISR = KISS). You are doing time-intensive functions in the interrupt like writing to the LCD. Since you have your data in a SerialData buffer, you should move the LCDout part to your mainline routine. You should also consider taking the RCIF interrupt and exiting after saving to the array. Waiting in the ISR seems to me like a bad idea.

Another idea worth considering if you want to ruggedize your code is to use DT interrupts or ASM interrupts in place of the ON INTERRUPT of PBP which only branches to the ISR between PBP statements.

shawn
- 4th July 2010, 18:47
Jerson,
Understand and appreciete what your saying, Lcd should not be down there, however I think if the code worked right I just still receive all 8 bytes of data to my array.
If I uderstand the code right this.................
while RCif
SerialData[index]=RCREG ' load receive buffer into array until RCIF = 0
Index = Index + 1
wend
should load incoming data into the array and not leave the while routine until the buffer rx buffer is empty,am i right or wrong.
I have also tryed this.....................
while RCif
HSERIN [SerialData[index]] ' load receive buffer into array until RCIF = 0
Index = Index + 1
wend
I get The same result nomatter what I do.