This simple code fragment crashes and restarts MCU!


+ Reply to Thread
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    Also it will crash randomly even when GPS is connected. Here's how output looks:

    Time: 15:41:00
    Time: 15:41:01
    Time: 15:41:02
    Time: 15:41:03
    Time: 15:41:04
    Time: 15:41:05
    Time: 15:41:06
    Time: 15:41:07
    Start ok <------- it crashed and restarted here
    Time: 15:41:10
    Time: 15:41:11
    Time: 15:41:12
    Time: 15:41:13
    Time: 15:41:14

    So I think this is issue with arrays?

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


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    Could be, easy to verify
    Code:
        ' Wait for data from GPS
        serin2 GPS_RX,85,[gps_char] 'If I remove this line or disconnect the GPS, MCU will crash!
        ' Store data in buffer
        gps_data[i] = gps_char
        i = i + 1
    
       If i >= 80  'or whatever size you set the array to
         serout2 portc.5,84, ["Next char will overrun buffer"]
       ENDIF

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    not sure but first use of i and i+1 might overrun.

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    I am not currentl with PBP, so, please refer the manual where necessary.

    It is most likely you are overrunning the specified receive buffer gps_data of 80 bytes as you are not doing any bounds checking to keep the data within that 80 bytes region. That is trashing the variables declared beyond the 80 bytes boundary.

    Ideally, you should receive characters until a terminating character is received like a CR or a $00 character (whichever method is used by the module) or the buffer limit is reached (i = 80 as per your code)

    edit:looks like I posted together with Henrik and amgen.

  5. #5
    Join Date
    Feb 2013
    Posts
    1,124


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    Yes you're correct, i is overrun.
    This is AI generated code, by the way

    I added this:

    Code:
        if i>=80 then
        serout2 portc.5,84, ["Oops!", 10, 13]
        i=0
        endif
    And now output looks like this with GPS connected:
    Oops!
    Oops!
    Oops!
    Time: 16:36:37
    Time: 16:36:38
    Time: 16:36:39
    Time: 16:36:40
    Oops!
    Time: 16:36:42
    Oops!

    And only "Oops!" with GPS disconnected, but there are no more resets...

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default Re: This simple code fragment crashes and restarts MCU!

    If you're gettings false data when GPS is disconnected, perhaps you have left the RX pin floating. Maybe you could see if adding a pull-up resistor eliminates that problem for you.

Similar Threads

  1. MicroCode studio crashes while typing in code.
    By CuriousOne in forum General
    Replies: 19
    Last Post: - 27th August 2022, 13:47
  2. Is it possible to feed PBP variable data into ASM code fragment?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th July 2017, 10:36
  3. Replies: 15
    Last Post: - 28th March 2014, 02:43
  4. ABS brake Pic code with 16f917 mcu
    By Foodie in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th November 2009, 20:58
  5. HSERIN restarts PIC18F2525?
    By breesy in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2006, 09:50

Members who have read this thread : 10

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