News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

HRSIn

Started by Teo, Dec 27, 2025, 06:56 PM

Previous topic - Next topic

Teo

Hello everyone,
I have a problem using HRSIn Wait ("XYZ") , Temp_rec
I am using PIC12F1840
The simulator gives me the error shown below.
Thank you in advance!
Teo

Teo

It's as if the RAM memory address l is incremented with each rewrite.
 Very strange. I think I'm making an error somewhere...
Thank you in advance!!!
Teo

top204

I've just created a code template to receive texts using the 'Wait' modifier, then a byte, on a PIC12F1840, and everything is working as it should.

The code listing is below:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A PIC12F1840 code listing template.
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    Device = 12F1840                                                ' Tell the compiler what device to compile for
    Declare Xtal = 32                                               ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Auto_Heap_Strings = On                                  ' Tell the compiler to create Strings above standard variables, so assembler code is more efficient
    Declare Auto_Variable_Bank_Cross = On                           ' Tell the compiler to create any multi-byte variables in the same RAM bank. For more efficiency
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRSOut1_Pin  = PORTA.0
    Declare HRSIn1_Pin   = PORTA.1
'
' Create any global variables here
'
    Dim ByteIn As Byte

'------------------------------------------------------------------------------------------
' The main program starts here
' Transmit text to a serial terminal, to make sure the device is operating at the correct frequency.
'
Main:
    Setup()                                                         ' Setup the program and any peripherals
    HRsout1Ln "Start"

    Do                                                              ' Create a loop
        HRSIn Wait("XYZ"), ByteIn                                   ' Wait for the text "XYZ", then receive a byte
        HRsoutLn ByteIn                                             ' Transmit the byte received
    Loop                                                            ' Do it forever

'------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_32MHz()                                                     ' Setup the internal oscillator to operate at 32MHz
'
' Extra setups here, when required...
'
EndProc

'-------------------------------------------------------
' Setup the internal oscillator to operate at 32MHz on a PIC12F1840 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_32MHz()
    OSCCON = %11110010                                              ' Enable the 4xPLL so the device operates at 32MHz with its internal oscillator
    OSCTUNE = $00
    BORCON = $00
EndProc

'------------------------------------------------------------------------------------------
' Set the config fuses for internal oscillator on a PIC12F1840 device.
'
    Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_ON, CP_OFF, CPD_OFF, BOREN_OFF
    Config2 WRT_OFF, PLLEN_ON, LVP_OFF

And below, is a screenshot of the above code lisitng operating in the proteus simulator, and receiving the letters "HELLO". The "XYZ" letters and the received character can also be seen on the serial terminal because it has character echo enabled on it for testing.

RX_Screenshot.jpg

Teo

Hi Les, 
Thank you for the response! For me, ByteIn is a string of 10 ASCII characters. The first three are XYZ and then there are 7 ASCII characters that represent a number. I tried your example and the variable where I load what I receive is empty.
I don't understand why? 
Thanks in advance! 
Teo

Teo

If I write "Declare Hserial_Clear = On" it receives the first string and then starts giving the error I showed above.
Thank you,
Teo

Teo

If I define the variable as a dword, for example, it works. With the variable defined as a string, it doesn't work.
Thank you,
Teo

top204

#6
Do not always trust the proteus simulator's messages, as it sometimes gets things wrong. Especially on some of the devices that have a different type of mechanism for indirect operations.

I have created two code listings for transmit and receive of Strings on a PIC12F1840 device, operating at 32MHz using the internal oscillator. Below is the receiver code listing:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A PIC12F1840 code listing for USART1 reception of a String.
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    Device = 12F1840                                                ' Tell the compiler what device to compile for
    Declare Xtal = 32                                               ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Auto_Heap_Arrays = On                                   ' Tell the compiler to create arrays above standard variables, so assembler code is more efficient
    Declare Auto_Heap_Strings = On                                  ' Tell the compiler to create Strings above standard variables, so assembler code is more efficient
    Declare Auto_Variable_Bank_Cross = On                           ' Tell the compiler to create any multi-byte variables in the same RAM bank. For more efficiency
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRSOut1_Pin  = PORTA.0
    Declare HRSIn1_Pin   = PORTA.1
'
' Create any global variables here
'
    Dim MyString As String * 32

'------------------------------------------------------------------------------------------
' The main program starts here
' Receive serial data into a String variable, and re-transmit what was received
'
Main:
    Setup()                                                         ' Setup the program and any peripherals

    Do                                                              ' Create a loop
RX_Again:
        HRSIn {2000, TimedOut}, Wait("XYZ"), MyString               ' Wait for the text "XYZ", then receive a String
        'If ReceiveString(MyString, SizeOf(MyString)) = 0 Then      ' Call a procedure to Wait for the text "XYZ", then receive a String
        '    Goto TimedOut                                          ' Jump to TimedOut if the procedure returned a 0
        'EndIf
        HRSOutLn "Received: ", MyString                             ' Transmit the text received
    Loop                                                            ' Do it forever
'
' Jump here if the serial timed out
'
TimedOut:
    HRSOutLn "Timed Out"
    GoTo RX_Again

'------------------------------------------------------------------------------------------
' Receive serial data into a String variable
' Input     : pStrAddr holds the address of the String to receive into
'           : pMaxAmount holds the maximum amount of bytes to receive
' Output    : The String passed to the procedure will contain the bytes received
'           : Returns 0 if the receive timed out, else returns 1
' Notes     : Ends reception when a value less than or equal to 13 is received
'
Proc ReceiveString(ByRef pStrAddr As Word, pMaxAmount As Word), Bit
    Dim bCharin As Byte

    Result = 1                                                      ' Return a correct result
    HRSIn {2000, TimedOut}, Wait("XYZ")                             ' Wait for the reception of "XYZ"
    Repeat                                                          ' Create a loop
        HRSIn {2000, TimedOut}, bCharin                             ' Receive the byte data
        If bCharin <= 13 Then Break                                 ' Exit the loop if it is 13 or less
        Ptr8(pStrAddr++) = bCharin                                  ' Add the byte received to the String passed
        Dec pMaxAmount                                              ' \
    Until pMaxAmount = 0                                            ' / Loop until the maximum amount of bytes are received
    ExitProc                                                        ' Exit the procedure
TimedOut:
    Result = 0                                                      ' Return a timed out result
EndProc

'------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_32MHz()                                                     ' Setup the internal oscillator to operate at 32MHz
    HRSOut1 0                                                       ' Needed to initialise the USART
EndProc

'-------------------------------------------------------
' Setup the internal oscillator to operate at 32MHz on a PIC12F1840 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_32MHz()
    OSCCON = %11110010                                              ' Enable the 4xPLL so the device operates at 32MHz with its internal oscillator
    OSCTUNE = $00
    BORCON = $00
EndProc

'------------------------------------------------------------------------------------------
' Set the config fuses for internal oscillator on a PIC12F1840 device.
'
    Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_ON, CP_OFF, CPD_OFF, BOREN_OFF
    Config2 WRT_OFF, PLLEN_ON, LVP_OFF

Notice that the receiving of the string's text can be either the HRSin command, or the ReceiveString procedure. My preference is the procedure, because the user has more control of its mechanism.

Below is the code listing for a transmitter, so that the receiver can be tested in the simulator, or in the real world:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A PIC12F1840 code listing for USART1 transmission of a String.
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    Device = 12F1840                                                ' Tell the compiler what device to compile for
    Declare Xtal = 32                                               ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Auto_Heap_Arrays = On                                   ' Tell the compiler to create arrays above standard variables, so assembler code is more efficient
    Declare Auto_Heap_Strings = On                                  ' Tell the compiler to create Strings above standard variables, so assembler code is more efficient
    Declare Auto_Variable_Bank_Cross = On                           ' Tell the compiler to create any multi-byte variables in the same RAM bank. For more efficiency
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                                     ' Set the Baud rate for USART1
    Declare HRSOut1_Pin  = PORTA.0
    Declare HRSIn1_Pin   = PORTA.1
'
' Create any global variables here
'
    Dim MyString As String * 7 = "1234567"
   
'------------------------------------------------------------------------------------------
' The main program starts here
' Transmit text serially, to test the receiver
'
Main:
    Setup()                                                         ' Setup the program and any peripherals
   
    Do                                                              ' Create a loop
        HRSOut "XYZ", MyString, 0                                   ' Transmit the String
        DelayMS 1000                                                ' Wait between each transmission
    Loop                                                            ' Do it forever

'------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_32MHz()                                                     ' Setup the internal oscillator to operate at 32MHz
EndProc

'-------------------------------------------------------
' Setup the internal oscillator to operate at 32MHz on a PIC12F1840 device
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_32MHz()
    OSCCON = %11110010                                              ' Enable the 4xPLL so the device operates at 32MHz with its internal oscillator
    OSCTUNE = $00
    BORCON = $00
EndProc

'------------------------------------------------------------------------------------------
' Set the config fuses for internal oscillator on a PIC12F1840 device.
'
    Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_ON, CP_OFF, CPD_OFF, BOREN_OFF
    Config2 WRT_OFF, PLLEN_ON, LVP_OFF

The Positron8 code listings and the proteus project files are attached below, and a screenshot of the devices operating in the proteus simulator is shown below:

TX_RX_Screenshot.jpg

Teo

Hi Les, 
A thousand thanks!!!!! 
With friendship,
Teo