News:

;) This forum is the property of Proton software developers

Main Menu

Problem with data arrays with uart!!, help

Started by jam19857, Oct 11, 2022, 04:22 PM

Previous topic - Next topic

jam19857

hello everyone, I have problems entering data in an array when receiving it uart from pic16f18855, it does not recognize the data within the array, the counter is correct, the data is not saved, could you help me?

I adapted the same code with pic18f26k22 and it works fine but I need to use pic16f18855

code and simulation.rar

Stephen Moss

This may not be the issue however it appears that in your main program loop (PRINCIPAL) you jump to the subroutine RECIBIR.
When no data is received you jump from RECIBER to NO_DATO.
You then exit NO_DATO using a GoTo PRINCIPAL returning you to the start of your main program loop, therefore one thing that will happen is that there will be no return from the RECIBIR subroutine when there is no data received. Consequently, the stack will likely overflow at some point as with no return from RECIBIR, every time you Gosub RECIBER its return address is pushed onto the stack, but is then not cleared from the stack as there is no corresponding return executed when there is no data.

Therefore you might want to try altering you code to...
'SUBRUTINA PARA RECIBIR DATOS SERIALMENTE Y CONTAR EL VALOR TOTAL DE BYTES INGRESADOS
RECIBIR:
 GoSub RESET_WDT
    N = N + 1
    'SerIn Rx , 84,250,NO_DATO,[DATO[N]]
    DATO[N] = hrsIn 250,NO_DATO
    GoSub RESET_WDT
    X0[N-1]= DATO[N]
    If X0[N-1] = $03 And N = 9 Then  ' ESTA CONDICION PERMITE RECONOCER EL BRILLO EN EL BYTE 3 EL VALOR $03 DE LO CONTRARIO SE SALE
        GoSub RESET_WDT
        'SerOut Tx, 84,[$6]
        hrsout $6
        Return
    EndIf
    If X0[1] = $25 And X0[3] = $03 Then ' ESTA CONDICION PERMITE RECONOCER LA TRAMA DE 4 BYTES Y NO CONFUNDIRSE CUANDO ES UN VALOR DE BRILLO
        GoSub RESET_WDT
        'SerOut Tx, 84,[$6]
        hrsout $6
        Return
    EndIf
GoTo RECIBIR
Return     'It is always a good idea to place a return at the end of your subroutines even if it is not expected to be called, just to be certain that the subroutine will be retuned from if all else fails. *****

NO_DATO:
''        SerOut Tx, 84,[$5]
   GoSub RESET_WDT
Return  'By changing Goto Principle to Return the program should resume at the instruction following the call to RECIBIR (clearing the return address from the stack) rather then the beginning of your main program loop which would result...
         in any data in DATO being cleared and prevent PROTCOLO from being executed which might not be what you intended *****
Changes indicate by lines ending in *****

Another thing I noticed is that you are clearing the WDT a lot, while there is nothing wrong with that you could be wasting a lot of instruction cycles doing it so often. If you set the WDT prescaller to its maximum setting (323768) page 354 of the data sheet indicates that it will take 2 minutes and 18 second before a reset occurs which is quite a long time for a PIC running at 20MHz.
So if you consider how long it would take to execute your entire code it is likely that you would only need to clear it at the beginning of your main program loop   

JonW

Have you simplified the code with just the setup and a single receive routine to ensure that the device and UART are initialised correctly?  I had a brief look at your code and you should look at using an interrupt driven UART as you are calling lots of different routines before the hardware serial command, depending on the data rate the serial buffer may overflow.