News:

;) This forum is the property of Proton software developers

Main Menu

Simple application example of hardware USART

Started by broderic, Jan 01, 2022, 06:20 PM

Previous topic - Next topic

broderic

Hello.
The following code is a simple one, to learn the use of HR USART macro.
Writing on hyperterminal for instance 08=7F, should light up all but one led on portD.

As its is written seems to work, but my intention was to simply use

FSR=SerData
INDF=SerData[1]

but it seems not working, unless I use a variable as intermediate ("Value").

Looking at the assembly, it seems missing an additional Movwf INDF:

F1_000028 equ $ ; in [PIC16F917_USART_PROJECT8_CANC.BAS] FSR=SerData[0]
    movf SerData#0,W
    movwf FSR
F1_000029 equ $ ; in [PIC16F917_USART_PROJECT8_CANC.BAS] INDF=SerData[1]
    movf SerData#1,W

Also the use of i index in array gives me wrong INDF assignement :

FSR=SerData
INDF=SerData


I don't understand.
And I don't know if using indirect addressing, like I did, is the more adequate way to do this job.

Thank you for any suggestion/clarification/improvement.

Regards

' From Project8 of PICDEM MC: Positron version
' Wait for a REG ADDR = VALUE <CR> (assign a register a value)
' or wait for a REG ADDR <CR>(read and display the value in a register).
' Simulation possible with Proteus PICDEM MC demo (JP1-off, JP15 a JP22 on).
  Device = 16F917
  Xtal=8
Declare Hserial_Baud = 9600 ' Set Baud rate to 9600 for HRsin and HRsout
  Dim SerData[5] As Byte   ' Storage for serial character
  Dim i As Byte=0
  Dim Value As Byte
  TRISD=0
  PORTD=0
  LCDSE0=0x00                ' disable pin LCD function
  LCDSE1=0x00                ' disable pin LCD function
  LCDSE2=0x00                ' disable pin LCD function
 
  Do
  HRSIn Hex SerData[i]        'wait for a typed byte: if non-numeric char received, go down
  FSR=SerData[i]
  If RCREG=0x0D Then          'if <CR> typed
    If i=0 Then              'after the REG ADDR
      Value=INDF
      HRSOut Hex Value,13    'show the the REG ADDR contents
    ElseIf i=1 Then          'if <CR> typed after REG ADDR = Value
      'Value=SerData[i]      'when index i is used, locate it before FSR assignement, because
                              'otherwise, when SerData[i] is evaluated, FSR is changed
                              'to 0x35 and not 0x08 as it should be.
      FSR=SerData
      Value=SerData[1]      'to put directly the array element SerData[1] in INDF doesn't work.   
      INDF=Value            'put value in REG ADDR 
      Clear i
    EndIf
  Else
    Inc i   
  EndIf
  Loop

top204

Using the INDF SFR as an assignment from an array has confused the compiler's code generator, because the array code usually uses the INDF and FSR SFRs for the array's RAM access itself. However, using a constant as the array's index does not use the indirect SFRs, so the compiler made the, incorrect, assumption that it had, and the value was aready sitting in the RAM address pointed too by FSR/INDF.

The method you are using is very much not recommended because FSR and INDF will be altered by the array if a variable is used as its index, so the original RAM address held in FSR will not be correct, so INDF will be pointing to the array's element itself. Also, with the, now obsolete, standard 14-bit core devices, the FSR SFR can only hold the address of RAM below 256, so a bit within the STATUS register has to be set to access RAM above 255, then cleared when finished.

In your code, you can use: INDF=SerData#1, which will load the RAM address held in INDF with the array's element variable. But with your method, you will get problems, and not just with the compiler not quite knowing what is happening, but with the methodology itself.

I'll alter the compiler's code generator to handle the INDF as an assignment from an array, but it is something I may add a warning with.

broderic

Many thanks for your kind explanation,Les!
In my previous message I also asked if there's a difference in using HRsin and HSerin (or when it is recommended to use one and when to use the other, if it is the case).
Thanks again.

Regards and wish a successful and healthy new year time to you and all the members of this nice forum!