News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Is there Proteus model for the PIC18F27K42

Started by JohnB, Dec 17, 2022, 10:37 AM

Previous topic - Next topic

JohnB

I am having a problem debugging my message processing code on my hardware.  I set up the circuit in Proteus but on my version of Proteus there isn't a model for the 27K42.  The closest I can get to it is the 26K22 but the Buffered USART library I am using is for the 27/47K42.  Does anyone know if a model exists for the 27K42?
JohnB

Pepe

#1
In version 8.15 it does not appear in the library, only 18F27K40 appears

JohnB

Thanks.  I'll have to revert back to the hardware.  This is the code I am struggling with.

This is the fixed format of the incoming message:

$AA $AA $AA $05 $0E $01 $00 $FF $FF $FF


This is the interrupt handler
' Check the USART2 RX interrupt
'
    If USART2_RX_Flag() = True Then                   ' Was it a USART1 byte receive that triggered the interrupt?
      uSART2_bRXByte = U2RXB                          ' Yes. So place the byte received into USART2_bRXByte
      Inc USART2_bIndexIn                             ' Move up the buffer
      If USART2_bIndexIn >= _cUSART2_BufferSize Then  ' End of buffer reached?
        USART2_bIndexIn = 0                           ' Yes. So reset USART2_bIndexIn
      EndIf
      USART2_wFSR1 = AddressOf(USART2_bRingBuffer)    ' Point FSR1L\H to USART1_bRingBuffer
      USART2_wFSR1 = USART2_wFSR1 + USART2_bIndexIn   ' Add the buffer position to FSR1L\H
      INDF1 = USART2_bRXByte                          ' Place the received byte into the buffer
      Global_tByteInBuffer2 = True                    ' Indicate that there is a byte in the buffer
    EndIf

and this is the procedure  to processes the incoming bytes:

  Proc Process_HMI_Input()

    While USART2_bIndexIn <> USART2_bIndexOut
   'Read the header
      USART2_GetByte()
      RxByte=Wreg
      if MsgCtr<3 then
        ' we are looking for a header of $AA $AA $AA
        U1Txb=RxByte
        If RxByte = $AA Then    ' maybe in message preable
           Inc HdrCtr            ' so count the number of concurrent header bytes received
        Else
          HdrCtr = 0            ' not a header, maybe a display return message,
          MsgCtr = 0            ' so reset counter and continue looking
        endIf
      Else
  ' It never reaches this part****************************************
  '  Read the message part - All messages have same length
       U1Txb=RxByte
        If MsgCtr = 0 Then   ' Must be start of a new message
          MsgType = RxByte   ' first byte after Header is message type
        Else
          Select MsgType     ' MsgType 0 is a null Msg
            Case Set_Filter           ' Filter message
              Select MsgCtr
                Case 1       ' Mode
                 TmpB1 = RxByte
                Case 2       ' Duration ON
                  TmpB2 = RxByte
                Case 3       ' Heating Enabled
                  TmpB3 = RxByte
                Case 4,5     ' Trailer bytes
                Case 6       ' apply the message on receipt of last trailer byte
                If RxByte = $FF Then
                  FilterMode = TmpB1
                  FilterOnTime = TmpB2
                  HeatingEnabled = TmpB3
                  InfoMessage = "Filter Control Updated"
                EndIf
                Break
              EndSelect
     etc...

    Wend
    RxByte= 0
    RxErrByte=0
    HdrCtr = 0              ' reset counters and
    MsgCtr = 0              ' start looking for new message
  EndProc

Any ideas would be appreciated
JohnB

tumbleweed

      if MsgCtr<3 then
        <snip>
      Else
  ' It never reaches this part****************************************
In the code you've shown MsgCtr is never incremented, so 'if MsgCtr<3' would always be true

trastikata

Quote from: JohnB on Dec 17, 2022, 12:19 PMAny ideas would be appreciated

Hello JohnB,

it seems to me your code fails to handle missing/wrong bytes or assumes the In-buffer starts receiving data in a known state, which might not always be the case. Otherwise said if your code starts receiving data from the middle of the string, it will not be able to adjust itself and detect a valid header.

Sorry if I am mistaken, I only glanced at the code and didn't look too deep into it.

In similar situations, I am writing the incoming data while in the ISR in a circular buffer (FIFO) and examining for a header at the beginning of this FIFO buffer, if the header is found then a valid string has been received and I use lock bit to lock, or just notify for the reception of the string and further processing.

Here's an example, hope this gives you some ideas.

Device = 18F25J50
Declare Xtal = 16

Symbol RC2IF = PIR3.5 

Dim InBuffer[10] As Byte                                    'FIFO buffer
Dim pBufferLock As Bit                                      'FIFO lock bit when a valid header is detected
Dim bTemp As Byte                                           'temp variable
Dim bIndex As Byte                                          'temp variable

Isr:
    Context Save
   
        If RC2IF = 1 Then                                   'USART2 byte received     
            bTemp = RCREG2                                  'Read the byte from RCREG2                           
            If pBufferLock = 0 Then                         'Is the InBuffer free to manipulate? - Yes
                For bIndex = 1 UpTo 9
                    InBuffer[bIndex - 1] = InBuffer[bIndex] 'Shift the values to the left
                Next
                InBuffer[9] = bTemp                         'Write latest value in the most right FIFO position
            EndIf
           
            If InBuffer[0] = $AA Then                       'Looking for a header byte 0
                If InBuffer[1] = $AA Then                   'Looking for a header byte 1
                    If InBuffer[2] = $AA Then               'Looking for a header byte 2
                        pBufferLock = 1                     'Header found, lock the FIFO buffer
                    EndIf
                EndIf
            EndIf
           
            RC2IF = 0                                       'Clear the interrupt flag
        EndIf
       
    Context Restore

Main:
    While 1 = 1                                             'Create a loop
        If pBufferLock = 1 Then                             'Have we found a valid header? -Yes
                                                            'Do something with the buffer
            pBufferLock = 0                                 'Unlock the FIFO buffer
        EndIf
    Wend

towlerg

It is most unfortunate that Proteus (well Labcenter actually) have apparently given up on mc, they seem to be focused on Arduino. Hard to decern the logic behind this as Arduino is all about free stuff. The 18F27K42 is a nice chip, pity it's not supported.

JohnB

Apologies guys I had tried to pare down the code and omitted too much and managed to exchange MsgCtr with HdrCtr.  I'll post when I have a cleaner bit of code.
JohnB