I have a simple program shown below which works fine for about 27 loops, then i notice one of the variables, X is reset to zero for an unknown reason. After that, it will run for about another 27 loops then the CPU will become unstable.

If I input character such that the 100ms timeout isn't being triggered, it will keep looping properly. The issue seems to be related to the timeout. I think I have the internal oscillator configured correctly as the baud rate is operating properly.

Any input or suggestions would be appreciated. There is no ICD model for this chip that I can find so I can't see what is internally going on.

Thanks
George

'************************************************* ***********************************
'* CTETI *
'* inc file located at C:\Program Files (x86)\Microchip\MPLABX\v3.61\mpasmx *
'* by George Jachode 01/01/2018 *
'* *
'************************************************* ***********************************






'Configuration
DEFINE FOSC INTIO67
OSCCON = %01110010 ' Set internal 8Mhz OSC
'OSCCON2 = %00001100


Define OSC 16


DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
'DEFINE HSER_SPBRG 25 ' 2400 Bauds
define HSER_BAUD 9600
DEFINE HSER_CLROERR 1


DEFINE HSER2_RCSTA 90h
DEFINE HSER2_TXSTA 24h
'DEFINE HSER2_SPBRG 207 ' 2400 Bauds
define HSER2_BAUD 9600
DEFINE HSER2_CLROERR 1






ANSELA = %00000000
ANSELB = %00000000
ANSELC = %00000000 ' Config A2d


ADCON2 = %10101101
ADCON1 = %00001000
ADCON0 = %00011000




TRISA = %11111111 ' Setup TRIS
TRISB = %10110000
TRISC = %10111111


PORTA = %00100000 ' Setup ports
PORTB = %00000000
PORTC = %00000000




'I/O Configuration


'LED Outputs, PORTB is used for development, Port A for production PCB
LED1 var PortB.0
LED2 var PortB.1
LED3 var PortB.2
LED4 var PortB.3


'DIP SW inputs
DipA var PortA.0
DipB var PortA.1
DipC var PortA.2
DipD var PortA.3


'Variables
'Flag variable declerations
Elo var byte 'Indicates if the elo is booted
OEC var byte 'Indicates if the 9800 is booted
Touch var byte 'Indicates a touch has been received


'Numeric value declerations
ETXlo var byte 'Elo touch raw data X axis low byte
ETXhi var byte 'Elo touch raw data X axis high byte
ETYlo var byte 'Elo touch raw data Y axis low byte
ETYhi var byte 'Elo touch raw data Y axis high byte
ETX var word 'The X coordinate raw data, 0 to 4096 value
ETY var word 'The Y coordinate raw data, 0 to 4096 value
IRX var byte 'The X coordinate scaled value, 0 to 85 value
IRY var byte 'The Y coordiante scaled value, 0 to 85 value
XOffset1 var word 'The amound of raw data to be ignored on the left
Xoffset2 var word 'The amound of raw data to be ignored on the right
Yoffset1 var word 'The amound of raw data to be ignored on the top
Yoffset2 var word 'The amound of raw data to be ignored on the bottom
ScaleX var word 'X axis scaler
ScaleY var word 'Y axis scaler
Flag var byte 'general varable used in any loops
X var byte 'general varaible used in any loops
i var byte 'general variable used in any loops
char var byte[20] 'general varaible used as hserin character
char2 var byte[20] 'general varaible used as hserin2 character


'Variables in EEPROM at time of programming
_Xoffset1 data word $0000
_Xoffset2 data word $0000
_Yoffset1 data word $0000
_Yoffset2 data word $0000


Elo = $0
OEC = $0
Touch = $0
ETXlo = $0
Etxhi = $0
Etylo = $0
etyhi = $0
etx = $0
ety = $0
irx = $0
iry = $0
read _Xoffset1,xoffset1
read _Xoffset2, xoffset2
Read _Yoffset1, yoffset1
read _Yoffset2, yoffset2
scalex = $0
scaley = $0
flag = $0
X = 0
i = 0
arraywrite char, ["_","_","_","_","_","_","_","_","_","_","_"]
arraywrite char2,["_","_","_","_","_","_","_","_","_","_","_"]












goto Main
'-------------------------------------------------------------------------------
' Main Loop
'-------------------------------------------------------------------------------


main:
'This is just for development testing to know when the loop is starting over
High LED1
'hserout["LED Change", 13, 10]
pause 100
high LED2
'hserout["LED Change", 13, 10]
pause 100
High LED3
'hserout["LED Change", 13, 10]
pause 100
High LED4
'hserout["LED Change", 13, 10]
pause 100
Low LED1
'hserout["LED Change", 13, 10]
pause 100
low LED2
'hserout["LED Change", 13, 10]
pause 100
low LED3
'hserout["LED Change", 13, 10]
pause 100
low LED4
hserout["LED Change Routine Complete", 13, 10]
pause 100


'Program will consist of several sections
'Initialize Elo
'hserout ["Elo = ", Hex Elo, 13, 10]
hserout ["x = ", dec x, 13, 10] 'here i am just testing the x variable which is what I see reset to 0 after about 27 loops
x = x + 1
If elo = 0 then 'Elo has not been initialized yet
'Query Elo looking for a response
HSerout [$55,$6D,$0,$0,$0,$0,$0,$0,$0,$0]
pause 50
gosub getChar 'getChar will get an array of 10 characters 'I know the actual problem is in the getChar sub
'Check for correct response.
if char[0] = "U" AND char[1] = "M" then Elo = 1 'The elo has initialized
'Send Elo configuration to respond only to "Untouch"
if Elo = 1 then
hserout ["U", "M", 0, "D", 0, 0, 0, 0, 0, 0]
'Need to verify that Elo has received the command by checking response
'Important that response is two messages long, first is reesponse and the second is a status
gosub getChar 'getChar will get an array of 10 characters
'Verify correct response which should be UM0D000000
'if correct set Elo to $02
if char[0] = "U" and char[1] = "M" then Elo = 2'No need to verify entire message
endif
endif
'Initialize 9800
' Send 0 until receive 13 (CR) 3x
'
'Touch Response
' When touch is received from Elo, scale it and send to 9800
'9800 response
' Check for query from 9800 and respond accordingly
'Service
' Use 9800 port (port 2) for terminal
' Send raw touch data, all offset data and scale data to terminal
' Provide for input and storage of both offset and scale data







goto main





'getChar will receive 10 characters and place into an array 0-9
'if there is nothing to receive, it will ensure the array is blank and return
'each message must begin with an ASCII "U" as character 0
'so this will keep looking for an ASCII "U" for x time then return
getChar


'Receive a character, if no character within 100ms, then goto main
hserin 100, main, [flag]
pause 100
hserout ["received a character", 13]
' if char[0] = "U" then
' HSerout ["received a 'U'", 13]
'Receive the remainder of the message
' hserin [STR char[1]\9]
' hserout ["received 9 more characters",13]
' hserout [str char\10]
'array char[] should now hold all 10 bytes of the message from Elo
' endif







return

end