Changing declared variables names on the fly


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default How do I READ & WRITE to the EEPROM 1, [1,2,3......]

    Hello Sayzer,

    Thanks for answering my question. Forgive my ignorance but I'm not familiar with your code example or how to implement it. I am familiar with how to read & write to the eeprom, for a byte variable I use

    EEPROMData0 VAR BYTE
    U VAR EEPROMData0
    READ 0, EEPROMData0

    to write I use:
    EEPROMData0 = U
    Write 0, EEPROMData0


    then for a word variable I use

    EEPROMData1 VAR WORD
    V VAR EEPROMData1
    Read 1, EEPROMData1.byte0
    Read 2, EEPROMData1.byte1

    to write I use:
    Write 1, EEPROMData1.byte0
    Write 2, EEPROMData1.byte1

    In your example code I don't know how I would write to locations 1 to 10 and 101 to 110 of the eeprom address location 1? Could you please explain the concept as I'm not sure how to do that. I can sorta understand your Index loop but I don't see any relationship between the Index variable and the EEPROM 1, [1,2,3......] statement? I've seen the EEPROM 1, [1,2,3......] statements in some of the code examples in the archives but I just don't understand the concept.

    Could you educate me about how this works?

    Thanks
    jessey

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by jessey
    ...locations 1 to 10 and 101 to 110 of the eeprom address location 1?..
    jessey

    1, 10 or 101 or 110 these are all already EEPROM locations. So there is no access to "10 of location 1". 10 is the location.

    For the index example,

    when index=1, take a look at the following code.

    READ Index , OnTime
    READ Index+100, OffTime

    Lest put "1" in index variable and re-write these two lines again.

    READ 1, OnTime
    READ 1 + 100, OffTime

    Now, the third time, this will become as follows.

    READ 1, OnTime
    READ 101, OffTime

    With these two lines, you will read EEPROM location 1 and store the location content in Ontime variable. And, read EEPROM location 101 and store the location content into OffTime variable.

    Now, you can take a look at FOR loop above and think about incrementing index variable. When index variable changes, the eeprom location that we read also changes.

    Any question there?
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default How do I write to the eeprom locations?

    Hi Sayzer,

    Thanks for your explanation about the For-Next loop, I think I can grasp the concept of the Index For-Next loop now but what I don't understand is how do I write the values to the different eeprom locations. I added some push buttons to your code to try and increase and decrease the OnTime & OffTime variables and the Hour variable in an attempt to flash an Led but can't get it to work. Could you please help me to understand how to write to the different eeprom locations?

    Thanks
    jessey


    Code:
    'Pic18f452-20/P
    
    Increase_OnTime_Push_Button VAR PORTD.2    '(pin 21)  2nd  
    Decrease_OnTime_Push_Button var PORTB.0    '(pin 33)  3rd  
    Increase_OffTime_Push_Button var PORTB.1   '(pin 34)  4th                 
    Decrease_OffTime_Push_Button var PORTB.2   '(pin 20)  5th  
    Change_On_Off_Time_Push_Button VAR PORTD.1 '(pin 35)  6th  
    Led VAR PORTA.1                            '(pin 3)
    Buzzer VAR PORTC.2                         '(pin 17)
    
    OnTime  var byte
    OffTime var byte
    Index   var byte
    Hour    var byte
    HOUR = 0
    
    Is_Pressed con 0
    Is_Not_Pressed con 1      
    
    EEPROM 1,[1,2,3,4,5,6,7,8,9,10,101,102,103,104,105,106,107,108,109,110]
    
    GOTO MainLoop
    
    Change_On_Off_Time:
        PWM Buzzer,250,1
        WHILE Change_On_Off_Time_Push_Button = Is_Pressed : PAUSE 100 : WEND
    Loop:
        IF Increase_OnTime_Push_Button = Is_Pressed THEN 
           OnTime = OnTime + 1 : PWM Buzzer,250,1 
           WRITE 1, OnTime
        ENDIF
    
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           OnTime = OnTime - 1 : PWM Buzzer,250,1 
           WRITE 1, OnTime
        ENDIF
                                       
        IF Increase_OffTime_Push_Button = Is_Pressed THEN
           OffTime = OffTime + 1 : PWM Buzzer,250,1 
           WRITE 101, OffTime
        ENDIF
    
        IF Decrease_OffTime_Push_Button = Is_Pressed THEN
           OffTime = OffTime - 1 : PWM Buzzer,250,1 
           WRITE 101, OffTime
        ENDIF
    
        IF Change_On_Off_Time_Push_Button = Is_Pressed THEN
          PWM Buzzer,250,1
          WHILE Change_On_Off_Time_Push_Button = Is_Pressed : PAUSE 100 : WEND
          RETURN
        ENDIF
    
           LCDOUT $fe,1,"OnTime  = ",dec Ontime
        LCDOUT $fe, $c0,"OffTime = ",dec OffTime 
        PAUSE 100
    goto Loop
    
    
    MainLoop:
        IF Change_On_Off_Time_Push_Button = Is_Pressed THEN GOSUB Change_On_Off_Time
    
       'Hour variable gets its value here......
        IF Increase_OnTime_Push_Button = Is_Pressed THEN 
           Hour = Hour + 1 : PWM Buzzer,250,1 
        ENDIF
    
       'Hour variable gets its value here......
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           Hour = Hour - 1 : PWM Buzzer,250,1 
        ENDIF
    
           LCDOUT $fe,1,"Hr=",dec Hour," OnTime=",dec Ontime
        LCDOUT $fe, $c0,"OffTime = ",dec OffTime
    
        PAUSE 100
    
        FOR Index = 1 to 10
            READ Index , OnTime            ' Read stored ON  time.
            READ Index +100, OffTime       ' Read stored OFF time.
            IF Hour = Ontime  THEN Led = 1 ' If yes, turn ON the relay.
            IF Hour = Offtime THEN Led = 0 ' If yes, turn OFF the relay.
        NEXT Index 
    GOTO MainLoop
    END

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Red face

    WRITE will work at run-time
    EEPROM or DATA will work at Programming time


    DATA @101,10 will write 10 at address 101 at programming time
    WRITE 101,10 will write 10 at adress 101 at run time

    i will read-back the whole post here, but it think you already understand the concept... OR i'm too drunk ... no way!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    In addition to the info from Steve, here I have some modifications for you.

    Check and come back for the parts you do not understand.

    Code:
    EEPROM 1,[1,2,3,4,5,6,7,8,9,10,101,102,103,104,105,106,107,108,109,110]
    'Keep this command at the first line.
    'this will run only one time during the programming.
    'not each time you power your PIC.
    'These are your initial default values.
    
    
    Increase_OnTime_Push_Button     VAR PORTD.2 '(pin 21)  2nd  
    Decrease_OnTime_Push_Button     VAR PORTB.0 '(pin 33)  3rd  
    Increase_OffTime_Push_Button    VAR PORTB.1 '(pin 34)  4th                 
    Decrease_OffTime_Push_Button    VAR PORTB.2 '(pin 20)  5th  
    Change_On_Off_Time_Push_Button  VAR PORTD.1 '(pin 35)  6th  
    Led                             VAR PORTA.1 '(pin 3)
    Buzzer                          VAR PORTC.2 '(pin 17)
    IncrFlag VAR BIT
    DecrFlag VAR BIT
    
    OnTime  VAR byte
    OffTime VAR byte
    Index   VAR byte
    Hour    VAR byte
    HOUR = 0
    
    Is_Pressed      CON 0
    Is_Not_Pressed  CON 1
          
    IncrFlag = 0 '0 =  button is NOT pressed. 1= button is pressed.
    DecrFlag = 0 
    
    
    GOTO MainLoop
    
    Change_On_Off_Time:
        WHILE Change_On_Off_Time_Push_Button = Is_Pressed : GOSUB buzz : WEND
    Loop:
    
        IF Increase_OnTime_Push_Button = Is_Pressed THEN 
           WHILE Increase_OnTime_Push_Button = Is_Pressed : GOSUB buzz : WEND
           IncrFlag = 1
           OnTime = OnTime + 1
        ENDIF
    
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           WHILE Decrease_OnTime_Push_Button = Is_Pressed : GOSUB buzz : WEND
           IncrFlag = 1
           OnTime = OnTime - 1 
        ENDIF
                                       
        IF Increase_OffTime_Push_Button = Is_Pressed THEN
           WHILE Increase_OffTime_Push_Button = Is_Pressed : GOSUB buzz : WEND
           DecrFlag = 1
           OffTime = OffTime + 1 
        ENDIF
    
        IF Decrease_OffTime_Push_Button = Is_Pressed THEN
           WHILE decrease_OffTime_Push_Button = Is_Pressed : GOSUB buzz : WEND
           DecrFlag = 1
           OffTime = OffTime - 1 
        ENDIF
    
        IF Change_On_Off_Time_Push_Button = Is_Pressed THEN
          WHILE Change_On_Off_Time_Push_Button = Is_Pressed : GOSUB buzz : WEND
          IF IncrFlag THEN WRITE 1,  OnTime       'If zero, then it means OnTime never changed.
          IF DecrFlag THEN WRITE 101,OffTime    'If zero, then it means OffTime never changed.
          gosub buzz  ''''
          pause 100   'kind a confirmation sound.
          gosub buzz  ''''
          GOTO MainLoop
        ENDIF
    
        LCDOUT $fe,1,   "OnTime  = ",dec Ontime
        LCDOUT $fe, $c0,"OffTime = ",dec OffTime 
        PAUSE 10
    
    GOTO Loop
    
    
    MainLoop:
        IF Change_On_Off_Time_Push_Button = Is_Pressed THEN goto Change_On_Off_Time
    
       'Hour variable gets its value here......
        IF Increase_OnTime_Push_Button = Is_Pressed THEN 
           WHILE Increase_OnTime_Push_Button = Is_Pressed : GOSUB buzz : WEND
           Hour = Hour + 1
        ENDIF
    
       'Hour variable gets its value here......
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           WHILE Decrease_OnTime_Push_Button = Is_Pressed  : GOSUB buzz : WEND
           Hour = Hour - 1 
        ENDIF
    
    
        FOR Index = 1 to 10
            READ Index , OnTime            ' Read stored ON  time.
            READ Index +100, OffTime       ' Read stored OFF time.
            IF Hour = Ontime  THEN Led = 1 ' If yes, turn ON the relay.
            IF Hour = Offtime THEN Led = 0 ' If yes, turn OFF the relay.
        NEXT Index 
    
    
    
        LCDOUT $fe,1,   "Hr=",dec Hour," OnTime=",DEC Ontime
        LCDOUT $fe, $c0,"OffTime = ",DEC OffTime
    
        PAUSE 100
        
    
    GOTO MainLoop     
    
    
    Buzz:
         PWM Buzzer,250,1
         RETURN
    
    END
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Partial sucess

    Hello Sayzer,

    Thanks for your code example, I like the way the IncrFlag and the DecrFlag variables work in that you don't have to set it back to 0 and just use "IF DecrFlag THEN", that's cool. I tried your code and it didn't work as posted, in order to save the OnTime & OffTime I had to place Index.0=OffTime & Index.1=OnTime before the write then it would save ok. Another thing I had to do in order to save the on/off times was to change the FOR-NEXT loop from 1 - 10 to 0 - 1 then it saved?

    You and Steve say that EEPROM or DATA will work at Programming time and you have EEPROM 1,[1,2,3,4,5,6,7,8,9,10,101,102,103,104,105,106,107,1 08,109,110] but when I program the code then OnTime shows 2 and OffTime shows 255. As far as I can figure location 1 is OnTime and when I replace 1 in the above EEPROM statement with 8 then after programming then OnTime dose show 8 but I can't figure out how to get address 101 to show anything but 255 using the EEPROM?

    Am I correct in placing Index.1 and Index.0 before the write and if so how would I write to all the other addresses? I've been playing around with the code and can't seem to get it working properly. I'd like to be able to add additional subroutines to program additional on off times.

    Thanks
    jessey

    Code:
    EEPROM 1,[8,2,10,11,12,6,7,8,9,10,101,102,103,104,105,106,107,108,109,110]
    'Keep this command at the first line.
    'this will run only one time during the programming.
    'not each time you power your PIC.
    'These are your initial default values.
    
    
    Increase_OnTime_Push_Button     VAR PORTD.2 '(pin 21)  2nd  
    Decrease_OnTime_Push_Button     VAR PORTB.0 '(pin 33)  3rd  
    Increase_OffTime_Push_Button    VAR PORTB.1 '(pin 34)  4th                 
    Decrease_OffTime_Push_Button    VAR PORTB.2 '(pin 20)  5th  
    Change_Times_Push_Button  VAR PORTD.1       '(pin 35)  6th  
    Led                             VAR PORTA.1 '(pin 3)
    Buzzer                          VAR PORTC.2 '(pin 17)
    IncrFlag VAR BIT
    DecrFlag VAR BIT
    IncrFlag = 0 '0 =  button is NOT pressed. 1= button is pressed.
    DecrFlag = 0
    
    OnTime  VAR byte
    OffTime VAR byte
    Index   VAR byte
    Hour    VAR byte
    HOUR = 0
    
    Is_Pressed      CON 0
    Is_Not_Pressed  CON 1
          
     
    GOTO MainLoop
    
    Change_Times:
        GOSUB buzz
        WHILE Change_Times_Push_Button = Is_Pressed : PAUSE 100 : WEND
    Loop:
    
        IF Increase_OnTime_Push_Button = Is_Pressed THEN
           GOSUB buzz : IncrFlag = 1 : OnTime = OnTime + 1
        ENDIF
    
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           GOSUB buzz : IncrFlag = 1 : OnTime = OnTime - 1
        ENDIF
                                       
        IF Increase_OffTime_Push_Button = Is_Pressed THEN
           GOSUB buzz : DecrFlag = 1 : OffTime = OffTime + 1 
        ENDIF
    
        IF Decrease_OffTime_Push_Button = Is_Pressed THEN
           GOSUB buzz : DecrFlag = 1 : OffTime = OffTime - 1 
        ENDIF
    
        IF Change_Times_Push_Button = Is_Pressed THEN
          gosub buzz 
          IF IncrFlag THEN  
             Index.1=OnTime : WRITE 1,OnTime
          ENDIF       
          IF DecrFlag THEN 
             Index.0=OffTime : WRITE 101,OffTime
          ENDIF   
          WHILE Change_Times_Push_Button = Is_Pressed : PAUSE 100 :WEND
          GOTO MainLoop
        ENDIF
    
        LCDOUT $fe,1,   "OnTime  = ",dec Ontime
        LCDOUT $fe, $c0,"OffTime = ",dec OffTime 
        PAUSE 100
    
    GOTO Loop
    
    
    MainLoop:
        IF Change_Times_Push_Button = Is_Pressed THEN goto Change_Times
    
        IF Increase_OnTime_Push_Button = Is_Pressed THEN 
           GOSUB buzz : Hour = Hour + 1
        ENDIF
    
        IF Decrease_OnTime_Push_Button = Is_Pressed THEN
           GOSUB buzz : Hour = Hour - 1 
        ENDIF
    
        FOR Index = 0 to 1
            READ Index , OnTime            ' Read stored ON  time.
            READ Index +100, OffTime       ' Read stored OFF time.
            IF Hour = Ontime  THEN Led = 1 ' If yes, turn ON the relay.
            IF Hour = Offtime THEN Led = 0 ' If yes, turn OFF the relay.
        NEXT Index 
    
        LCDOUT $fe,1,   "Hr=",dec Hour," OnTime=",DEC Ontime
        LCDOUT $fe, $c0,"OffTime = ",DEC OffTime
    
        PAUSE 100
        
    
    GOTO MainLoop     
    
    
    Buzz:
         PWM Buzzer,250,1
         RETURN
    
    END
    Last edited by jessey; - 13th December 2006 at 05:44.

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Jessey,

    I think you are thinking about FOR loop in a different way.
    Index variable is not an array variable.

    So you need to go back to using FOR Index=1 TO 10.


    Also, Index.1=OnTime and Index.0=OffTime do nothing in the code for you.
    You are, I think, confusing with Arrays here.

    Can you be more specific about your idea?
    May be I got it not good enough.

    --------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. changing osc on the fly
    By viewgsm in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 6th February 2010, 16:13
  2. Mode changing on the fly
    By Angus Anderson in forum GSM
    Replies: 1
    Last Post: - 28th November 2006, 09:58
  3. edit names of variables
    By mischl in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th January 2005, 22:00

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts