Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

writing totals in Eprom

MrDEB

Well-Known Member
working on a Swordfish program to display TOTALS of 3 separate breakfasts that we serve on the first Saturday of each month.
I am trying to get the totals written in the EEPROM but it reads some really weird totals like 6403?
This code works great but in case the batteries take a dump or? the totals in the eeprom would be nice.
using a 4 x 20 LCD

*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 6/19/2022 *
* Version : 1.0 *
* Notes : breakfast caculator 6/22/22 *
* : *
*****************************************************************************
}
//Program BREAKFAST CACULATOR

Device = 18F43K22
Clock = 16

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"

// lcd
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...
Include "utils.bas"
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
Include "LCD.bas"
Include "convert.bas"
Include "EEPROM.bas"
// hardware
Dim Button_1 As PORTB.0 // $8 breakfast
Dim Button_2 As PORTB.1 // $5 breakfast
Dim Button_3 As PORTB.2 // 2x breakfast
Dim Button_4 As PORTB.3// cash totals etc
Dim Cash_Totals As Word //cash totals
Dim Meals_Totals As Word //number of meals sold
Dim Eight_Dollar As Word ' $8 breakfast
Dim Five_dollar As Word ' $5 breakfast
Dim Two_times As Word ' 2x breakfast
Dim Cash As Word ' cash sales
Dim Press As PORTC.2
Dim led As PORTD.0
Dim Value As Byte
Dim Str As String

Sub Sales() //suggestion add each meal $ totals
led = 1
Cls
Cash = (Eight_Dollar * 8) + (Five_dollar * 5) + (Two_times * 3)
WriteAt(1,1,"CASH TOTALS:=$",DecToStr(Cash))
WriteAt(2,1,"$8 BREAKFAST:=",DecToStr(Eight_Dollar)," $",DecToStr(Eight_Dollar *8))
WriteAt(3,1,"$5 BREAKFAST:=",DecToStr(Five_dollar)," $",DecToStr(Five_dollar *5))
WriteAt(4,1,"$3 2xTIME :=", DecToStr(Two_times)," $",DecToStr(Two_times *3))
DelayMS(10000)
Cls
End Sub


main:
// init hdw
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
TRISA = 0
'TRISB = 0
TRISC = 0
TRISD = 0
TRISE = 0
PORTE=%000
PORTD=%00000000
PORTC=%00000000
PORTA=%00000000
'PORTB=%00000000 //pullups
//main:
// init hdw
TRISD = 0 // LED port - all outputs
TRISB = $FF // SW port - all inputs
WPUB = $FF // PORTB pullup mask - all PORTB pullups
INTCON2.bits(7) = 0 // RBPU bit - turn on pullups
Input (Press)
Eight_Dollar = 0
Five_dollar = 0
Two_times = 0
Cash = 0
While true


If Button_1 = 0 Then
Eight_Dollar = (Eight_Dollar + 1)
WriteAt(2,1,"$8 BREAKFAST : =",DecToStr (Eight_Dollar ))
' EE.Write(0,DecToStr (Eight_Dollar ))
DelayMS(200)


Else If Button_2 = 0 Then
Five_dollar = (Five_dollar + 1)
WriteAt(3,1,"$5 BREAKFAST : =" ,DecToStr(Five_dollar))
DelayMS(200)

Else If Button_3 = 0 Then
Two_times = (Two_times + 1)
WriteAt(4,1,"$3 2x times : =",DecToStr(Two_times))
DelayMS(200)


Else If Button_4 = 0 Then //breakfast total meals sold
Repeat
Cash = (Eight_Dollar * 8) + (Five_dollar * 5) + (Two_times * 3)
WriteAt(1,1,"CASH TOTALS:=$",DecToStr(Cash))
WriteAt(2,1,"$8BREAKFAST:=",DecToStr(Eight_Dollar)," $",DecToStr(Eight_Dollar *8))
WriteAt(3,1,"$5BREAKFAST:=",DecToStr(Five_dollar)," $",DecToStr(Five_dollar *5))
WriteAt(4,1,"$3 2xTIME :=", DecToStr(Two_times)," $",DecToStr(Two_times *3))
led = 1
Until Press = 0
Cls
End If
End If
End If
End If

Wend






[/CODE]
[/ICODE]
 

tumbleweed

Active Member
How about something like this...

It keeps a total of each of the meals along with the total number of meals sold in EEPROM.
When it powers up it checks to see if things match, so there's a little sanity check there.

Code:
Program BREAKFAST_CALCULATOR

Device = 18F43K22
Clock = 16

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"

// lcd
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...
Include "LCD.bas"
Include "utils.bas"
Include "convert.bas"
Include "EEPROM.bas"

// hardware
Dim Button_1 As PORTB.0     // $8 breakfast
Dim Button_2 As PORTB.1     // $5 breakfast
Dim Button_3 As PORTB.2     // 2x breakfast
Dim Button_4 As PORTB.3     // cash totals etc
Dim Press    As PORTC.2
Dim led      As PORTD.0

Dim Cash_Totals As Word     //cash totals
Dim Meals_Totals As Word    //number of meals sold
Dim Eight_Dollar As Word    // $8 breakfast
Dim Five_dollar As Word     // $5 breakfast
Dim Two_times As Word       // 2x breakfast
Dim Cash As Word            // cash sales

// eeprom data storage and initialization
// automatically creates 'EE_xxx' constants with locations
eeprom(@EE_EIGHT_DOLLAR) = (0 as word)
eeprom(@EE_FIVE_DOLLAR) = (0 as word)
eeprom(@EE_TWO_TIMES) = (0 as word)
eeprom(@EE_MEALS_TOTALS) = (0 as word)
    
Sub Sales() //suggestion add each meal $ totals
    led = 1
    Cls
    Cash = (Eight_Dollar * 8) + (Five_dollar * 5) + (Two_times * 3)
    WriteAt(1,1,"CASH TOTALS:=$",DecToStr(Cash))
    WriteAt(2,1,"$8 BREAKFAST:=",DecToStr(Eight_Dollar)," $",DecToStr(Eight_Dollar *8))
    WriteAt(3,1,"$5 BREAKFAST:=",DecToStr(Five_dollar)," $",DecToStr(Five_dollar *5))
    WriteAt(4,1,"$3 2xTIME :=", DecToStr(Two_times)," $",DecToStr(Two_times *3))
    DelayMS(10000)
    Cls
End Sub

// calc total number of meals
Function TotalMeals() as word
    result = Eight_Dollar + Five_dollar + Two_times
end function

main:
// init hdw
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
TRISA = 0
TRISC = 0
TRISE = 0
PORTE = %000
PORTD = %00000000
PORTC = %00000000
PORTA = %00000000
'PORTB=%00000000 //pullups

TRISD = 0       // LED port - all outputs
TRISB = $FF     // SW port - all inputs
WPUB = $FF      // PORTB pullup mask - all PORTB pullups
INTCON2.bits(7) = 0 // RBPU bit - turn on pullups

Input (Press)   // PORTC.2 is an input

cls

// recall eeprom data
EE.Read(EE_EIGHT_DOLLAR, Eight_Dollar, Five_dollar, Two_times, Meals_Totals)
// add number of meals and see if it matches eeprom total
// if not, eeprom is invalid so clear vars
if (TotalMeals() <> Meals_Totals) then
    WriteAt(1,1,"MEAL EEPROM INVALID")
    Eight_Dollar = 0
    Five_dollar = 0
    Two_times = 0
endif

While true
    If Button_1 = 0 Then
        Eight_Dollar = (Eight_Dollar + 1)
        WriteAt(2,1,"$8 BREAKFAST : =",DecToStr (Eight_Dollar ))
        EE.Write(EE_EIGHT_DOLLAR, Eight_Dollar)
        Meals_Totals = TotalMeals()
        EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        while (Button_1 = 0)    // wait for button release
            DelayMS(200)
        end while
    ElseIf Button_2 = 0 Then
        Five_dollar = (Five_dollar + 1)
        WriteAt(3,1,"$5 BREAKFAST : =" ,DecToStr(Five_dollar))
        EE.Write(EE_FIVE_DOLLAR, Five_dollar)
        Meals_Totals = TotalMeals()
        EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        while (Button_2 = 0)    // wait for button release
            DelayMS(200)
        end while
    ElseIf Button_3 = 0 Then
        Two_times = (Two_times + 1)
        WriteAt(4,1,"$3 2x times : =",DecToStr(Two_times))
        EE.Write(EE_TWO_TIMES, Two_times)
        Meals_Totals = TotalMeals()
        EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        while (Button_3 = 0)    // wait for button release
            DelayMS(200)
        end while
    ElseIf Button_4 = 0 Then //breakfast total meals sold
        Repeat
            Cash = (Eight_Dollar * 8) + (Five_dollar * 5) + (Two_times * 3)
            WriteAt(1,1,"CASH TOTALS:=$",DecToStr(Cash))
            WriteAt(2,1,"$8BREAKFAST:=",DecToStr(Eight_Dollar)," $",DecToStr(Eight_Dollar *8))
            WriteAt(3,1,"$5BREAKFAST:=",DecToStr(Five_dollar)," $",DecToStr(Five_dollar *5))
            WriteAt(4,1,"$3 2xTIME :=", DecToStr(Two_times)," $",DecToStr(Two_times *3))
            led = 1
        Until Press = 0
        Cls
    EndIf
Wend

end program
 

For The Popcorn

Active Member
Wow. Throw some crap out that doesn't even attempt to do what you want, and let minions do all the rest.
 

MrDEB

Well-Known Member
Here is edited code but it dosen't write what the eeprom has. see notes
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 6/28/2022                                                      *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Program BREAKFAST_CALCULATOR

Device = 18F43K22
Clock = 16

// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"

// lcd
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// import LCD library...
Include "LCD.bas"
Include "utils.bas"
Include "convert.bas"
Include "EEPROM.bas"

// hardware
Dim Button_1 As PORTB.0     // $8 breakfast
Dim Button_2 As PORTB.1     // $5 breakfast
Dim Button_3 As PORTB.2     // 2x breakfast
Dim Button_4 As PORTB.3     // cash totals etc
Dim Press    As PORTC.2
Dim led      As PORTD.0

Dim Cash_Totals As Word     //cash totals
Dim Meals_Totals As Word    //number of meals sold
Dim Eight_Dollar As Word    // $8 breakfast
Dim Five_dollar As Word     // $5 breakfast
Dim Two_times As Word       // 2x breakfast
Dim Cash As Word            // cash sales
Dim TotalMeals As Word
//dim Meals_Totals as word



// eeprom data storage and initialization
// automatically creates 'EE_xxx' constants with locations
EEPROM(@EE_EIGHT_DOLLAR) = (0 As Word)
EEPROM(@EE_FIVE_DOLLAR) = (0 As Word)
EEPROM(@EE_TWO_TIMES) = (0 As Word)
EEPROM(@EE_MEALS_TOTALS) = (0 As Word)
    


Sub Total_ee()
     // recall eeprom data
EE.Read(EE_EIGHT_DOLLAR, Eight_Dollar)//, Five_dollar, Two_times)//, Meals_Totals)
writeat(1,1,EE_EIGHT_DOLLAR, eight_dollar)
delayms(1000)
// add number of meals and see if it matches eeprom total
// if not, eeprom is invalid so clear vars
//If (TotalMeals() <> Meals_Totals) Then
  //  WriteAt(1,1,"MEAL EEPROM INVALID")
   // Eight_Dollar = 0
  //  Five_dollar = 0
 //   Two_times = 0
'EndIf
End Sub

// calc total number of meals
//Function TotalMeals() As Word
  //  result = Eight_Dollar + Five_dollar + Two_times
//End Function

main:
// init hdw
// LED port - all outputs 2 LEDs per port in parallel 330 ohm resistor
TRISA = 0
TRISC = 0
TRISE = 0
PORTE = %000
PORTD = %00000000
PORTC = %00000000
PORTA = %00000000
'PORTB=%00000000 //pullups

TRISD = 0       // LED port - all outputs
TRISB = $FF     // SW port - all inputs
WPUB = $FF      // PORTB pullup mask - all PORTB pullups
INTCON2.bits(7) = 0 // RBPU bit - turn on pullups

Input (Press)   // PORTC.2 is an input
Eight_Dollar = 0
Five_dollar = 0
Two_times = 0
Cash = 0
Cls
{
============I PUT THIS IN A SUB--------------
// recall eeprom data
EE.Read(EE_EIGHT_DOLLAR, Eight_Dollar, Five_dollar, Two_times, Meals_Totals)
// add number of meals and see if it matches eeprom total
// if not, eeprom is invalid so clear vars
if (TotalMeals() <> Meals_Totals) then
    WriteAt(1,1,"MEAL EEPROM INVALID")
    Eight_Dollar = 0
    Five_dollar = 0
    Two_times = 0
endif
 }
While true
    If Button_1 = 0 Then
        Eight_Dollar = (Eight_Dollar + 1)
        WriteAt(2,1,"$8 BREAKFAST : =",DecToStr (Eight_Dollar ))
        EE.Write(EE_EIGHT_DOLLAR, Eight_Dollar)
       // Meals_Totals = TotalMeals()                 TOTAL MEALS and MEAL TOTALS  are already processed in the REPEAT/UNTIL LOOP
       // EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        While (Button_1 = 0)    // wait for button release
            DelayMS(200)
        End While
    ElseIf Button_2 = 0 Then
        Five_dollar = (Five_dollar + 1)
        WriteAt(3,1,"$5 BREAKFAST : =" ,DecToStr(Five_dollar))
        EE.Write(EE_FIVE_DOLLAR, Five_dollar)
       // Meals_Totals = TotalMeals()
       // EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        While (Button_2 = 0)    // wait for button release
            DelayMS(200)
        End While
    ElseIf Button_3 = 0 Then
        Two_times = (Two_times + 1)
        WriteAt(4,1,"$3 2x times : =",DecToStr(Two_times))
        EE.Write(EE_TWO_TIMES, Two_times)
       // Meals_Totals = TotalMeals()
      //  EE.Write(EE_MEALS_TOTALS, Meals_Totals)
        While (Button_3 = 0)    // wait for button release
            DelayMS(200)
        End While
    ElseIf Button_4 = 0 Then //breakfast total meals sold
        Repeat
            Cash = (Eight_Dollar * 8) + (Five_dollar * 5) + (Two_times * 3)
            WriteAt(1,1,"CASH TOTALS:=$",DecToStr(Cash))
            WriteAt(2,1,"$8BREAKFAST:=",DecToStr(Eight_Dollar)," $",DecToStr(Eight_Dollar *8))
            WriteAt(3,1,"$5BREAKFAST:=",DecToStr(Five_dollar)," $",DecToStr(Five_dollar *5))
            WriteAt(4,1,"$3 2xTIME :=", DecToStr(Two_times)," $",DecToStr(Two_times *3))
            led = 1
        Until Press = 0
        Cls
        Total_ee()      //WRITE EEPROM SUB  ALL i GET IS TWO COLUMS OF FOUR LINES
        DelayMS(3000)
        led=0
    EndIf
Wend

End Program
 

MrDEB

Well-Known Member
I ran the code again and it progresses past the RECALL EEPROM ( I didn't wait long enough, I panicked) routine but the eeprom needs to be cleared with another button input. Luckly I have two additional buttons already connected.
If I leave it "as is" the totals add the totals from the previous breakfast plus the present breakfast which is ok, but not the current totals for each breakfast. I know it sounds nit picking but am using this as an exercise in eeprom READING/WRITING as well.
 

tumbleweed

Active Member
Well, it does what you asked.

To clear the eeprom and variables, use this and add a call in your new button handling code
Code:
sub clear_eeprom()
    Eight_Dollar = 0
    Five_dollar = 0
    Two_times = 0
    Meals_Totals = 0
    EE.Write(EE_EIGHT_DOLLAR, Eight_Dollar, Five_dollar, Two_times, Meals_Totals)
end sub
 

For The Popcorn

Active Member
....I know it sounds nit picking but am using this as an exercise in eeprom READING/WRITING as well....
I'm sure tumbleweed doesn't need any exercise in EEPROM use.
 

New Articles From Microcontroller Tips

Top