Bascom and AVR, EEPROM


The AT90S2313 has 128 bytes of EEPROM storage available. Larger controllers have more EEPROM storage. EEPROM storage is typically used for constants that seldom change. You may have an application where you want to store calibration data so that the program can use this data in calculations. EEPROM should NOT be used as RAM, because of the limited number of write cycles the EEPROM can endure. (Read the datasheet)
Reading from and writing to EEPROM is done with the Readeeprom and Writeeeprom statements. But before these statement can be used, each constant to be stored in EEPROM has to be dimensioned in a special way. This can be done in two ways. The first way is using $eeprom:
...
Dim ADCcal as Byte
...
$eeprom
ADCcal1:
Data &H34
$data
...
Readeeprom, ADCcal, ADCcal1
...
The $eeprom must be placed in the program before any reference is made to the constants used.
$eeprom switches Bascom from interpreting a Data block to an Eeprom block. The $data key switches back. I prefer to place the $eeprom before the Data line in a program.

An example to write and read data to and from EEPROM:
eeprom0.bas
$regfile = "2313def.dat"
$crystal = 4000000

Config Pind.6 = Output
Config Pind.2 = Input
Config Int0 = Falling

Dim Wtime As Byte
Dim Addval As Integer
Const Debouncetime = 75
Dim Buttoncounter As Integer

$eeprom
Buttoncounterdefault:
Data 17%
$data

Readeeprom Buttoncounter , Buttoncounterdefault
Cls
Lcd "Def.Ctr.: " ; Buttoncounter
Addval = 1

On Int0 Button

Wtime = 255

Enable Interrupts
Enable Int0

Do
  Set Portd.6
  Waitms Wtime
  Reset Portd.6
  Waitms Wtime
  Buttoncounter = Buttoncounter + Addval
  Cls
  Lcd "Btn.Ctr: " ; Buttoncounter
Loop

Button:
  Writeeeprom Buttoncounter , Buttoncounterdefault
  Gifr = 64
  Lowerline
  Lcd "to eeprom"
  Wait 3
  Addval = 0
Return

End
The program starts by reading the Buttoncounterdefault value from EEPROM
It then starts to count up from this value
Once the button is pressed, in the interrupt routine the last value of Buttoncounter is written to EEPROM
After a controller reset, this value is again read as default and the count up starts from that value

The second method to use EEPROM is through the Dim statement:
...
Dim ADCcal as Byte
Dim ADCcal1 As Eram Byte
...
Here, ADCcal is dimensioned as a byte variable in ERAM (EEPROM). It is not a real variable, all we can only do is:
ADCcal = ADCcal1
assign the ADCcal1 constant to the variable ADCcal
ADCcal1 = ADCcal
write the value of the variable ADCcal to EEPROM at the predefined storage place of ADCcal1.

What you cannot do is:
ADCcal1 = ADCcal1 +1
The previous example would now go as:
eeprom0a.bas
$regfile = "2313def.dat"
$crystal = 4000000

Config Pind.6 = Output
Config Pind.2 = Input
Config Int0 = Falling

Dim Wtime As Byte
Dim Addval As Integer
Const Debouncetime = 75
Dim Buttoncounter As Integer
Dim Bc As Eram Integer

Buttoncounter = Bc
Cls
Lcd "Def.Ctr.: " ; Buttoncounter
Addval = 1

On Int0 Button

Wtime = 255

Enable Interrupts
Enable Int0

Do
  Set Portd.6
  Waitms Wtime
  Reset Portd.6
  Waitms Wtime
  Buttoncounter = Buttoncounter + Addval
  Cls
  Lcd "Btn.Ctr: " ; Buttoncounter
Loop

Button:
  Bc = Buttoncounter
  Gifr = 64
  Lowerline
  Lcd "to eeprom"
  Wait 3
  Addval = 0
Return

End

If data is stored in EEPROM with the $eeprom directive, Bascom will generate a seperate 'eep' file. So, compiling the eeprom0.bas program will result in a 'bin' file as well as a 'eep' file. You first program the chip as usual, this sends the 'bin-file to the controllers flash memory. Then you start TWinavr manually, click on WRITE, select 'EEPROM files' from the 'files of type' list, open the 'eeprom0.eep' file TWinavr sends that file to the controllers EEPROM.

Both methods of assigning EEPROM space can be used, I prefer the first method, because the fact that when default constants are used, it is more apparent that a separate 'eep' file has to be sent to EEPROM.

TOC