Positron Compiler Documentation

Lread

Source: Positron8 Compiler User Manual, PDF page 408

Syntax

Variable = Lread Label

Overview

Read a value from an Ldata table and place into Variable

Parameters

Variable is a user defined variable. Label is a label name preceding the Ldata statement, or expression containing the Label name.

Example

  Device = 18F26K40
                                  ' Select the device to compile for
  Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
  Declare Hserial_Baud = 9600
                                  ' Set the Baud rate for HRsout
  Dim Char as Byte
  Dim MyLoop as Byte
  Cls
  For MyLoop = 0 to 9
                                     ' Create a loop of 10
     Char = Lread Label + MyLoop
                                     ' Read memory location Label + MyLoop
     HRsout Char
                                     ' Display the value read
  Next
  Stop
Label: Ldata "HELLO WORLD"
                                     ' Create a string of text in code memory

The program above reads and displays 10 values from the address located by the Label accompanying the Ldata command. Resulting in "Hello Worl" being displayed.

Ldata is not simply used for character storage, it may also hold 8, 16, 24, 32 bit, or floating point values. The example below illustrates this: -

  Device = 16F628
                                  ' Select the device to compile for
  Declare Xtal = 8 ' Tell the compiler the device will be operating at 8MHz
  Dim MyByte as Byte
  Dim MyWord as Word
  Dim MyDword as Dword
  Dim MyFloat as Float
  MyByte = Lread Bit8_Val
                                  ' Read the 8-bit value
  HRsoutLn Dec Var1
  MyWord = Lread Bit16_Val
                                  ' Read the 16-bit value
  HRsoutLn Dec Wrd1
  MyDword = Lread Bit32_Val
                                  ' Read the 32-bit value
  HRsoutLn Dec Dwd1
  MyFloat = Lread MyFloat_Val
                                  ' Read the floating point value
  HRsoutLn Dec Flt1
  Stop
Bit8_Val: Ldata 123
Bit16_Val: Ldata 1234
Bit32_Val: Ldata 123456
MyFloat_Val: Ldata 123.456

Floating point examples.

Enhanced 14-bit core example

' Enhanced 14-bit read floating point data from a table and display the results
  Device = 16F1829
                                       ' Select the device to compile for
  Declare Xtal = 8 ' Tell the compiler the device will be operating at 8MHz
  Dim MyFloat as Float
                                       ' Create a Floating Point variable
  Dim Fcount as Byte
  Cls
                                       ' Clear the LCD
  Fcount = 0
                                       ' Clear the table counter
  Repeat
                                       ' Create a loop
     MyFloat = Lread FlTable + Fcount ' Read the data from the Ldata table
     Print At 1, 1, Dec3 MyFloat
                                       ' Display the data read
     Fcount = Fcount + 4
                                       ' Point to next value, by adding 4 to counter
     DelayMs 1000
                                       ' Slow things down
  Until MyFloat = 0.005
                                       ' Stop when 0.005 is read
  Stop
FlTable:
  Ldata as Float 3.14, 65535.123, 1234.5678, -1243.456, -3.14, 998999.12,_
                    0.005

18F device example

' Read floating point data from a table and display the results
  Device = 18F452
                                     ' Select the device to compile for
  Declare Xtal = 8 ' Tell the compiler the device will be operating at 8MHz
  Dim MyFloat as Float
                                       ' Create a Floating Point variable
  Dim Fcount as Byte
  Cls
                                       ' Clear the LCD
  Fcount = 0
                                       ' Clear the table counter
  Repeat
                                       ' Create a loop
     MyFloat = Lread FlTable + Fcount ' Read the data from the Ldata table
     Print At 1, 1, Dec3 MyFloat
                                       ' Display the data read
     Fcount = Fcount + 2
                                       ' Point to next value, by adding 2 to counter
     DelayMs 1000
                                       ' Slow things down
  Until MyFloat = 0.005
                                       ' Stop when 0.005 is read
  Stop
FlTable:
  Ldata as Float 3.14, 65535.123, 1234.5678, -1243.456, -3.14, 998999.12,_
                    0.005

Notes

Ldata tables should be placed at the end of the BASIC program. If an Ldata table is placed at the beginning of the program, then a GoTo command must jump over the tables, to the main body of code.

  GoTo OverDataTable
  Ldata 1,2,3,4,5,6
OverDataTable:
  { rest of code here}

With 14-bit core devices, an 8-bit value (0 - 255) in an Ldata statement will occupy a single code space, however, 16-bit data (0 - 65535) will occupy two spaces, 32-bit and floating point values will occupy 4 spaces. This must be taken into account when using the Lread command. See 14-bit floating point example above.

With 18F devices, an 8, and 16-bit value in an Ldata statement will occupy a single code space, however, 32-bit and floating point values will occupy 2 spaces. This must be taken into account when using the Lread command. See previous 16-bit floating point example.

See also : Cdata, Cread, Cread8, Cread16, Cread24, Cread32, Ldata.