hserin 100, main [flag] causing 18f24k22 to become unstabloe


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2017
    Posts
    17

    Default hserin 100, main [flag] causing 18f24k22 to become unstabloe

    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

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517

    Default Re: hserin 100, main [flag] causing 18f24k22 to become unstabloe

    For the sake of testing, can you try moving the code in the getchar subroutine to the actual place where the GOSUB getchar is? In other words, don't use a GOSUB just place the actual code "in line" instead.

    It's just a hunch but when you GOSUB getchar, where the HSERIN times out and jumps back to main you have an "unresolved" GOSUB/RETURN "pair". The number 27 happens to match the number of nested GOSUBs you can have on an 18F series, I think that's more than a coincidence.

    /Henrik.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796

    Default Re: hserin 100, main [flag] causing 18f24k22 to become unstabloe

    Timeout from a subroutine getchar to main is wrong.

    You should do it like this:

    Code:
    getChar
    
    
    'Receive a character, if no character within 100ms, then goto main
    hserin 100, exit_getchar, [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
    
    exit_getchar:
    
    return
    Of course this will not start over from main, but if you need this, then set a flag in the subroutine that Hserin timed out and then jump to main (resetting the flag first).

    Ioannis
    Last edited by Ioannis; - 5th January 2018 at 09:11.

  4. #4
    Join Date
    Sep 2009
    Posts
    737

    Default Re: hserin 100, main [flag] causing 18f24k22 to become unstabloe

    You are right.
    He uses GOSUB and when times out, RETURN isn't executed. And it will jump with GOTO from HSERIN function to main.
    Other workaround is

    Code:
    getChar:
    'Receive a character, if no character within 100ms, then goto main >> IF you use goto main stack overflow, because RETURN isn't executed. 
    hserin 100, GetCharError, [flag]
    pause 100
    hserout ["received a character", 13]
    
    GetCharError:
    return

  5. #5
    Join Date
    Sep 2017
    Posts
    17

    Default Re: hserin 100, main [flag] causing 18f24k22 to become unstabloe

    Thank you and I changed the gosub to simply goto which works. I would ask, where is the maximum number of nested gosub routines documented?

    Again, thanks

    George

  6. #6
    Join Date
    Sep 2017
    Posts
    17

    Default Re: hserin 100, main [flag] causing 18f24k22 to become unstabloe

    Not sure why this posted a second time, but thank you and yes the nested gosub is the issue. I went back and it is documented in the manual. I changed the gosub to goto and it works as intended.
    Thanks everyone
    George

Similar Threads

  1. Can someone show me how to set ccp3 to RC6 on pic 18f24k22
    By jimseng in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 23rd March 2012, 21:57
  2. Can someone help me get hpwm working on 18f24k22
    By jimseng in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st March 2012, 14:31
  3. Argument out of range - how to find what is causing it?
    By BrianT in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd November 2011, 03:00
  4. Fan causing noise in audio circuit
    By The Master in forum Off Topic
    Replies: 11
    Last Post: - 28th August 2011, 12:47
  5. MCLR causing trouble . .
    By shahidali55 in forum General
    Replies: 10
    Last Post: - 21st December 2007, 13:20

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts