News:

Let's find out together what makes a PIC Tick!

Main Menu

Possible Anomaly?

Started by joesaliba, Jun 26, 2022, 12:14 PM

Previous topic - Next topic

joesaliba

I do not know if this is an anomaly or not: -

Dim SAV as bit
Dim Capacity as word = 2000
Dim Alarm_1 as word  = 1000
Dim Alarm_2 as word  = 500

EWrite 1, [Capacity]            ' Write value of Capacity to Eeprom location 1
EWrite 3, [Alarm_1]             ' Write value of Alarm_1 to Eeprom location 3
EWrite 5, [Alarm_2]             ' Write value of Alarm_2 to Eeprom location 5

Main:

Sav = 1
if capacity = eread 1 then          ' If Capacity has not been changed then
    if alarm_1 = eread 3 then       ' if Alarm_1 has not been changed then
        if alarm_2 = eread 5 then   ' if Alarm_2 has not been changed then
            Sav = 0                 ' there is no need  to save, therefore clear Sav
        endif                       ' End If...Then instruction
    endif                           ' End If...Then instruction
endif                               ' End If...Then instruction

Values in EEPROM are correct. Only the comparison is wrong, Sav should change to 0.

Replacing comparison to variable instead of eread n solves the issue.

Again not sure if it is the correct syntax, but this works: -

If ERead 0 > 0 Then GoSub Default
Regards

Joe

trastikata

Maybe I am missing something, but you are not writing in the EEPROM address 0.

By default the data in the EEPROM, not previously written is 0xFF, thus your statement

If ERead 0 > 0 Then GoSub Default

will be always true if no value was written at this position.

shantanu@india

Joe,
The correct syntax of ERead is MyVar = Eread 1
I always use this method to pass the EEPROM value to a variable.
Regards
Shantanu

top204

#3
It is not an anomaly.

Because of its generic nature, and because it is one of the original functions of the compiler many years ago, the ERead function will read a byte from EEPROM by default, so it cannot be compared with a 16-bit variable within a comparison, if the variable holds more than a value of 255.

For that type of use, either create a temparary variable so that the ERead function knows how many bytes to read based upon the assignment variable size, of better, create a procedure to always read 16-bits from EEPROM:

    Dim MyWord As Word
 
    MyWord = EEPROM_Read16(100)
    If MyWord = EEPROM_Read16(200) Then Stop
   
'----------------------------------------------------------
' Read 16-bits from on-board EEPROM
' Input     : pOffset holds the offset within the EEPROM (0 to 1023)
' Output    : Returns the 16-bit value read from EEPROM
' Notes     : Uses the parameter variable as the return variable to save RAM
'   
Proc EEPROM_Read16(pOffset As Word), pOffset
    Result = ERead pOffset                  ' Read 16-bits from EEPROM into the 16-bit variable            
EndProc

I may add extra functions to the compiler as I have done with flash reading, such as Eread8, ERead16, ERead24, ERead32. Because the ERead command is too generic and it is not always known what size variable is on the other side of a comparison.


joesaliba

Hi Les,

Thanks for the information. Now I understand how it works. That is why: -

If ERead 0 > 0 Then GoSub Default
works becuase address 0 stores a byte.

No big deal. As in my first post, I read the eeprom to a word variable and that worked.

Regards

Joe