News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

[Solved] Wrong Procedure?

Started by keytapper, May 04, 2024, 12:32 PM

Previous topic - Next topic

keytapper

Hello,

I'm trying some experiment. So I tried to make a procedure like this:
Device = 16F1827
Xtal = 32

Declare Hserial_Baud 9600

Dim RXBUFSIZE As 64
Dim STRSIZE As 32
Dim rxbuffer[RXBUFSIZE] As Byte Heap
Dim readbuf As String * STRSIZE  Heap
Dim position As Byte

rxbuffer = "this is a long story"
readbuf = "this is a story less wordy"

HSerOutLn ["ready"]
position = parseBuffer(0, rxbuffer, "STORY")
HSerOutLn ["Match end in rxbuffer at ", Dec position]
position = parseBuffer(0, readbuf, "STORY")
HSerOutLn ["Match end in readbuf at ", Dec position]

End
Proc parseBuffer(bufidx As Byte, ByRef ramPos, ByRef cmpPhrase),Byte
  Dim chk_chr As Byte = 255         ' initial state avoiding exit immediately
  Dim chk_idx As Byte = bufidx      ' memorize the initial value
  Dim bufchr As Byte                ' local variable to store the read in RAM
  Do
    bufchr = ramPos                 ' read the RAM value, but it's wrong content
    If bufchr = 32 Then             ' if space
      Inc bufidx                    ' ignore it
      Continue                      ' one more character
    End If
    ' only for alpha characters, convert then to upper case
    If bufchr > 64 And bufchr < 91 Then
      bufchr = bufchr & 95
    End If
    chk_chr = CRead cmpPhrase         ' read fron the flash
    If chk_chr = 0 Then Break         ' end of the string, exit the loop
    If bufchr = 0 Then Break
    Inc bufidx                        ' increse for the count of the matches
    Inc ramPos                        ' new pointing position
    If bufidx > STRSIZE Then Break    ' in case is going over the limits
    If bufchr = chk_chr Then          ' stop
      Inc cmpPhrase                   ' if there's a match, look for the next
    Else
      Continue                        ' if not, go futher in tne RAM
    End If
  Loop
  If bufidx <> chk_idx Then           ' anything has been found, it returns a value
    Result = bufidx
  Else
    Clear Result                      ' no one matched, return zero
  End If
EndProc
But I can't see that the RAM address is reading the content where it's pointing to. Well it should point, but the value is another.
If I use a fix array like
'bufchr = ramPos
buchar = rxbuffer[bufidx]
then it works, but I can't switch to another array, or string.
Ignorance comes with a cost

keytapper

#1
OK folks, I found my flaw
Device = 16F886
Xtal = 32

Declare Hserial_Baud 9600
$define CONVERSION                  'just a particular option
Dim RXBUFSIZE As 64
Dim STRSIZE As 32
Dim rxbuffer[RXBUFSIZE] As Byte Heap
Dim readbuf As String * STRSIZE  Heap
Dim position As Byte
rxbuffer = "this is a long story"
readbuf = "Hello, a new story less wordy"

HSerOutLn ["ready"]
position = parseBuffer(0, rxbuffer, "STORY")
HSerOutLn ["Match end in rxbuffer at ", Dec position]
position = parseBuffer(0, readbuf, "STORY")
HSerOutLn ["Match end in readbuf at ", Dec position]

Proc parseBuffer(bufidx As Byte, ByRef ramPos, ByRef cmpPhrase),Byte
  Dim chk_chr As Byte               ' initial state avoiding exit immediately
  Dim chk_idx As Byte = bufidx      ' memorize the initial value
  Inc chk_idx                       ' minimum exit value
  Dim bufchr As Byte                ' local variable to store the read in RAM
  ' memorize the initial sequence start
  Dim oldcmpPhrase As Word = cmpPhrase
  Dim startcmp as Bit               ' signal that the sequence has started
  Inc ramPos,bufidx                 ' set to the desired point in RAM
  Do
    bufchr = ptr8(ramPos++)         ' read the RAM value and increase for next
    Skpnz                           ' is it a zero?
    Break                           ' leave the loop
'===================== just a particular option ============================
$ifdef CONVERSION
    If bufchr = 32 Then             ' if space
      Inc bufidx                    ' ignore it
      Continue                      ' one more character
    End If
    ' only for alpha characters, convert then to upper case
    If bufchr > 95 And bufchr < 127 Then
      bufchr = bufchr & 95
    End If
$endif
'===================== just a particular option ============================
    chk_chr = CRead cmpPhrase         ' read the flash value
    Skpnz
    Break                             ' Break if it's a zero
    Inc bufidx                        ' increse the position in the RAM
    If bufidx > STRSIZE Then Break    ' in case is going over the limits
    If bufchr = chk_chr Then          ' when they are same
      Set startcmp                    ' signalize the start of the matching
      Inc cmpPhrase                   ' look for the next
    Else                              ' otherwise, when different
      If startcmp = 1 Then            ' if already started to compare
        cmpPhrase = oldcmpPhrase      ' reset the pointer and try again
        Clear startcmp                ' new start condition
      End If
      Continue                        ' go futher in tne RAM
    End If
  Loop
  ' anything has been found, it returns a value next to the matching end
  If bufidx > chk_idx Then            ' changed?
    Result = bufidx                   ' found a value after the match
  Else
    Clear Result                      ' no one matched, return zero
  End If
EndProc
Clearly there's a limitation for those old MCU with non linear memory addressing. Which are (I suppose) those with 2/3 digit after the F, such as 12Fxxx and 16Fxx(x). Nonetheless to say that the compiler will notice the ptr8 command :D
I was forgetting the ptr8 command to read a byte on the RAM.
Ignorance comes with a cost