View Full Version : Changing declared variables names on the fly
jessey
- 27th November 2006, 10:56
Hello,
This is probably a dumb question but is there a way using PBP to have a variable with-in a declared variable name? Its a little hard to explain exactly what I mean but it would be sort of like this:
You want to print a variable name to your Lcd so you print:
LCDOUT $FE,1,"On_Timer_Hour",DEC M," "
the On_Timer_Hour would be your declared variable and the variable with-in it would be the M variable. Setting the M variable before the print to the Lcd (or in your code) would change the variable name displayed to the Lcd. So the variable On_Timer_Hour could be On_Timer_Hour1, On_Timer_Hour2, On_Timer_Hour3 ect. ect.. It would be great if I could implement that concept in my code. Wishful thinking?
Why I'm asking is: I'm writing a timer program where the user can programme 10 different On and Off times that repeats every 24 hours using Darrels ELASPED.PBP. I have in excess of 100 variables that all have a numerical value at the end of the variable name. If it is possible to use a variable with-in a declared variable name then it would save a lot of code space.
In each of the 10 timer sections (I'd like to have more if I can gain more code space) I have intermediate dummy variables that all have to be set then re-set to implement my code. It would be great if I could just change one M variable that would change them all. There's a lot I don't understand about programming, like array variables with a variable index or other concepts that could make writing code more efficient. What is the concept of a variable index? Would that have anything to with what I'm trying to accomplish here?
Is it possible or just wishful thinking.
Thanks
jessey
sayzer
- 27th November 2006, 12:41
Hi Jessey,
It seems you really need to use array.
Since you need 10 different ON times and 10 different OFF times, 20 variables would give you a lot of headache if not using array.
But, if I got it right, then a simple code as below could be the idea for you, may be.
EEPROM 1,[1,2,3,4,5,6,7,8,9,10,101,102,103,104,105,106,107,1 08,109,110]
'Say these are your stored alarm time(s).
'1 to 10 : ON time
'101 to 110 : OFF time
OnTime var byte
OffTime var byte
Index var byte
Hour var byte
Alarm var PORTB.0 'A relay is connected here to wake you up!
'Hour variable gets its value from RTC routine somewhere in the code....
Start:
FOR Index = 1 to 10
READ Index , OnTime 'Read stored ON time.
READ Index +100, OffTime 'Read stored OFF time.
IF Hour = Ontime THEN ALARM = 1 'Check if there is a match with the current time. If yes, turn ON the relay.
IF Hour = Offtime THEN Alarm = 0 'Check if there is a match with the current time. If yes, turn OFF the relay.
NEXT Index
GOTO Start
Hour variable is just an example. Assuming you get your hour variable somewhere in the code. It could be the minute, the second, the day, year etc... or all of them together.
Say you stored the "Alarm ON" time in the eeprom location 1. Then its corresponding "Alarm OFF" time should be stored in a sequenced number, for example +100, 101.
This way you can have an array to make more and more variables to match. Use a simple code as above first to start understanding the idea.
jessey
- 10th December 2006, 17:26
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
sayzer
- 10th December 2006, 20:54
...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?
jessey
- 11th December 2006, 21:44
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
'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,1 08,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
mister_e
- 11th December 2006, 21:52
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 :D ... no way!
sayzer
- 12th December 2006, 18:19
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.
EEPROM 1,[1,2,3,4,5,6,7,8,9,10,101,102,103,104,105,106,107,1 08,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
jessey
- 13th December 2006, 03:40
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
EEPROM 1,[8,2,10,11,12,6,7,8,9,10,101,102,103,104,105,106,10 7,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
sayzer
- 13th December 2006, 08:03
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.
--------------------------------
jessey
- 15th December 2006, 13:11
Hi Sayzer,
Well I'm finally getting somewhere, I was way off track in my thinking but I think I'm getting closer now. This code works but I still have a few questions for you.
In this code below that I wrote, I had to change the "READ Index +100, OffTime" to "READ Index +10, OffTime" when using the "EEPROM 1,[1,3,5....]" statement to get the OffTimes to display to the Lcd which makes sense because I only have 10 OnTimes and 10 OffTimes but when I don't use the "EEPROM 1,[1,3,5....]" statement and just set and save the different times to the eeprom then I have to use "READ Index +100, OffTime" in order to make it work otherwise my OffTimes will show 255 and won't change? Why is that?
Another question, what does the EEPROM 1, in the "EEPROM 1,[1,3,5....]" statement make reference to. Can I have multiple eerpom statements like "EEPROM 1,[1,3,5....]", "EEPROM 2,[1,3,5....]", "EEPROM 3,[1,3,5....]" ect. ect, for different sets of variables?
'18f452 pic
Decrease_Hour_Push_Button VAR PORTB.7
Increase_Hour_Push_Button VAR PORTD.2
OnTime var byte
OffTime var byte
Index var byte
Hour var byte
Hour = 0
EEPROM 1,[1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20]
'--------------------------------------------------------'
EEPROMData1 var BYTE
Timer_1_OnTime VAR EEPROMData1 'alias for eepromdata 1
READ 1, EEPROMData1
' To write to the EEPROM
' =====================
'Timer_1_OnTime = 1
'EEPROMData1 = Timer_1_OnTime
'Write 1, EEPROMData1
'--------------------------------------------------------'
EEPROMData2 var BYTE
Timer_2_OnTime VAR EEPROMData2 'alias for eepromdata 2
READ 2, EEPROMData2
' To write to the EPROM
' =====================
'Timer_2_OnTime = 3
'EEPROMData2 = Timer_2_OnTime
'Write 2, EEPROMData2
'--------------------------------------------------------'
EEPROMData3 var BYTE
Timer_3_OnTime VAR EEPROMData3 'alias for eepromdata 3
READ 3, EEPROMData3
' To write to the EPROM
' =====================
'Timer_3_OnTime = 5
'EEPROMData3 = Timer_3_OnTime
'Write 3, EEPROMData3
'--------------------------------------------------------'
EEPROMData4 var BYTE
Timer_4_OnTime VAR EEPROMData4 'alias for eepromdata 4
READ 4, EEPROMData4
' To write to the EPROM
' =====================
'Timer_4_OnTime = 7
'EEPROMData4 = Timer_4_OnTime
'Write 4, EEPROMData4
'--------------------------------------------------------'
EEPROMData5 var BYTE
Timer_5_OnTime VAR EEPROMData5 'alias for eepromdata 5
READ 5, EEPROMData5
' To write to the EPROM
' =====================
'Timer_5_OnTime = 9
'EEPROMData5 = Timer_5_OnTime
'Write 5, EEPROMData5
'--------------------------------------------------------'
EEPROMData6 var BYTE
Timer_6_OnTime VAR EEPROMData6 'alias for eepromdata 6
READ 6, EEPROMData6
' To write to the EPROM
' =====================
'Timer_6_OnTime = 11
'EEPROMData6 = Timer_6_OnTime
'Write 6, EEPROMData6
'--------------------------------------------------------'
EEPROMData7 var BYTE
Timer_7_OnTime VAR EEPROMData7 'alias for eepromdata 7
READ 7, EEPROMData7
' To write to the EPROM
' =====================
'Timer_7_OnTime = 13
'EEPROMData7 = Timer_7_OnTime
'Write 7, EEPROMData7
'--------------------------------------------------------'
EEPROMData8 var BYTE
Timer_8_OnTime VAR EEPROMData8 'alias for eepromdata 8
READ 8, EEPROMData8
' To write to the EPROM
' =====================
'Timer_8_OnTime = 15
'EEPROMData8 = Timer_8_OnTime
'Write 8, EEPROMData8
'---------------------------------------------------------'
EEPROMData9 var BYTE
Timer_9_OnTime VAR EEPROMData9 'alias for eepromdata 9
READ 9, EEPROMData9
' To write to the EPROM
' =====================
'Timer_9_OnTime = 17
'EEPROMData9 = Timer_9_OnTime
'Write 9, EEPROMData9
'---------------------------------------------------------'
EEPROMData10 var BYTE
Timer_10_OnTime VAR EEPROMData10 'alias for eepromdata 10
READ 10, EEPROMData10
' To write to the EPROM
' =====================
'Timer_10_OnTime = 19
'EEPROMData10 = Timer_10_OnTime
'Write 10, EEPROMData10
'-----------------------------------------------------------'
'-----------------------------------------------------------'
EEPROMData101 var BYTE
Timer_101_OffTime VAR EEPROMData101'alias for eepromdata 101
READ 101, EEPROMData101
' To write to the EPROM
' =====================
'Timer_101_OffTime = 2
'EEPROMData101 = Timer_101_OffTime
'Write 101, EEPROMData101
'-----------------------------------------------------------'
EEPROMData102 var BYTE
Timer_102_OffTime VAR EEPROMData102'alias for eepromdata 102
READ 102, EEPROMData102
' To write to the EPROM
' =====================
'Timer_102_OffTime = 4
'EEPROMData102 = Timer_102_OffTime
'Write 102, EEPROMData102
'-----------------------------------------------------------'
EEPROMData103 var BYTE
Timer_103_OffTime VAR EEPROMData103'alias for eepromdata 103
READ 103, EEPROMData103
' To write to the EPROM
' =====================
'Timer_103_OffTime = 6
'EEPROMData103 = Timer_103_OffTime
'Write 103, EEPROMData103
'-----------------------------------------------------------'
EEPROMData104 var BYTE
Timer_104_OffTime VAR EEPROMData104'alias for eepromdata 104
READ 104, EEPROMData104
' To write to the EPROM
' =====================
'Timer_104_OffTime = 8
'EEPROMData104 = Timer_104_OffTime
'Write 104, EEPROMData104
'-----------------------------------------------------------'
EEPROMData105 var BYTE
Timer_105_OffTime VAR EEPROMData105'alias for eepromdata 105
READ 105, EEPROMData105
' To write to the EPROM
' =====================
'Timer_105_OffTime = 10
'EEPROMData105 = Timer_105_OffTime
'Write 105, EEPROMData105
'-----------------------------------------------------------'
EEPROMData106 var BYTE
Timer_106_OffTime VAR EEPROMData106'alias for eepromdata 106
READ 106, EEPROMData106
' To write to the EPROM
' =====================
'Timer_106_OffTime = 12
'EEPROMData106 = Timer_106_OffTime
'Write 106, EEPROMData106
'-----------------------------------------------------------'
EEPROMData107 var BYTE
Timer_107_OffTime VAR EEPROMData107'alias for eepromdata 107
READ 107, EEPROMData107
' To write to the EPROM
' =====================
'Timer_107_OffTime =14
'EEPROMData107 = Timer_107_OffTime
'Write 107, EEPROMData107
'-----------------------------------------------------------'
EEPROMData108 var BYTE
Timer_108_OffTime VAR EEPROMData108'alias for eepromdata 108
READ 108, EEPROMData108
' To write to the EPROM
' =====================
'Timer_108_OffTime = 16
'EEPROMData108 = Timer_108_OffTime
'Write 108, EEPROMData108
'-----------------------------------------------------------'
EEPROMData109 var BYTE
Timer_109_OffTime VAR EEPROMData109'alias for eepromdata 109
READ 109, EEPROMData109
' To write to the EPROM
' =====================
'Timer_109_OffTime = 18
'EEPROMData109 = Timer_109_OffTime
'Write 109, EEPROMData109
'-----------------------------------------------------------'
EEPROMData110 var BYTE
Timer_110_OffTime VAR EEPROMData110'alias for eepromdata 110
READ 110, EEPROMData110
' To write to the EPROM
' =====================
'Timer_110_OffTime = 20
'EEPROMData110 = Timer_110_OffTime
'Write 110, EEPROMData110
'----------------------------------------------------------'
Is_Pressed CON 0
GOTO mainloop
Sound_Buzzer:
PWM Buzzer,250,1
RETURN
MainLoop:
FOR Index = 1 to 10
IF Increase_Hour_Push_Button = Is_Pressed THEN
GOSUB Sound_Buzzer : Hour = Hour + 1
ENDIF
IF Decrease_Hour_Push_Button = Is_Pressed THEN
GOSUB Sound_Buzzer : Hour = Hour - 1
ENDIF
LCDOUT $fe,1, "Hr=",dec Hour," OnTime=",DEC Ontime
LCDOUT $fe, $c0,"OffTime = ",DEC OffTime
READ Index , OnTime 'Read stored ON time.
READ Index +10, OffTime 'Read stored OFF time.
IF Hour = Ontime THEN Led = 1 'turn ON the Led.
IF Hour = Offtime THEN Led = 0 'turn OFF the Led.
PAUSE 500
NEXT Index
GOTO mainloop
END
Here is one of the segments of code that I'd like to change in an attachment here if you want to see what I'm trying to accomplish.This code uses 1404 bytes in my program and I thought I could use your example code to save some code space for this part and if it works out then I can save a lot in other parts of my program as well.
Thanks
jessey
Dave
- 15th December 2006, 15:05
jessey, This should save you a few bytes......
_On con 1
COUNTER VAR BYTE
Timer_Is var byte[13]
Clock_Mode var byte
On_Am_Pm_Mode VAR BYTE[13]
Hours VAR BYTE
On_Timer_Hour VAR BYTE[13]
Minutes VAR BYTE
On_Timer_Min VAR BYTE[13]
Seconds VAR BYTE
On_Timer_Sec VAR BYTE[13]
Check_The_Timers_Relay: 'this code uses 1404 bytes
COUNTER = 1
WHILE COUNTER < 13 'LOOP 12 TIMES
IF Timer_Is(COUNTER) = _On then
IF Clock_Mode = On_Am_Pm_Mode(COUNTER) THEN ' this checks the am/pm mode for on Timer
IF Hours = On_Timer_Hour(COUNTER) THEN
IF Minutes = On_Timer_Min(COUNTER) then
IF Seconds = On_Timer_Sec(COUNTER) THEN
GOSUB Turn_On_The_Relay
ENDIF
ENDIF
ENDIF
endif
IF Clock_Mode = Off_Am_Pm_Mode(COUNTER) THEN
IF Hours = Off_Timer_Hour(COUNTER) THEN
IF Minutes = Off_Timer_Min(COUNTER) then
IF Seconds = Off_Timer_Sec(COUNTER) THEN
GOSUB Turn_Off_The_Relay
ENDIF
ENDIF
ENDIF
endif
ENDIF
COUNTER = COUNTER + 1
WEND
RETURN
Dave Purola,
N8NTA
sayzer
- 15th December 2006, 16:26
.....
Another question, what does the EEPROM 1, in the "EEPROM 1,[1,3,5....]" statement make reference to. Can I have multiple eerpom statements like "EEPROM 1,[1,3,5....]", "EEPROM 2,[1,3,5....]", "EEPROM 3,[1,3,5....]" ect. ect, for different sets of variables?.....
Jessey,
I did not take look at your code now but for your EEPROM question, here is the key.
If you look at the help file or the manual etc..., you will understand the followings.
Example:
EEPROM 1,[1,3,5,7,9,11,$C0,"A",%10100101]
1 is the start address for EEPROM location.
This command will do the following.
Store value 1 on Location 1
Store value 3 on Location 2
Store value 5 on Location 3.
Store value 7 on Location 4.
Store value 9 on Location 5.
Store value 11 on Location 6.
Store value $C0 on Location 7.
Store value "A" on Location 8.
Store value %10100101 on Location 9.
Therefore, once you start from a location on epprom to store some values, then you can not cross that address range by another eeprom command.
Example:
EEPROM 5,["A","B","C","D"]
This will store "A" , "B" , "C" , and "D" on epprom locations 5,6,7,and 8 respectively. This way, you are having locations from 5 to 8 reserved by this epprom command.
Then if you have another EEPROM command like EEPROM 3,[90,100,25,70] then this epprom command will try to write on locations 3,4,5, and 6.
Now, you get two EEPROM commands trying to write on same locations at 5 and 6.
Thus, you get a collision in EEPROM. You will need to change the location in one of these eeprom commands.
As long as you have no collision, you are free to use the given space by your epprom with multiple epprom commands.
Is it clear now?
------------
jessey
- 15th December 2006, 16:52
Hi Dave,
I'm not familiar with "var byte[13]" or how it works but I'd really like to understand it as I would Sayzer's code example, I have a real thirst to learn PBP and this forum is a great opportunity for me.
The attachment shows all of the variables for one timer and all the necessary subroutines that are shared by all of the 12 timers except for some sub's that are used to select prints to show to the Lcd and a repeat On/Off timer. The Set_Timer_1: subroutine is repeated 11 more times and they all share the same sub's in the attachment, that'll give you a good idea of what I'm working on. If you have the time to explain the process using "var byte[13]", maybe something simple that'll flash an Led or just some theory then I'd love to see it.
Thanks
jessey
sayzer
- 15th December 2006, 17:22
Jessey,
Check these and see how array works.
http://www.picbasic.co.uk/forum/showthread.php?t=544
http://www.picbasic.co.uk/forum/showthread.php?t=3753
Then have your questions either in those posts or here.
jessey
- 15th December 2006, 21:40
Hi Sayzer,
Yes I found Melanie's thread a while back and I even wrote a short simple program to try and understand it better. I got the program to turn on an led whenever the bits equaled 1 and to shut it off when 0 and with the press of a button the sequence changes but I couldn't figure an application for it in any of my programs. I'd still be interested in hearing Dave's concept as it seems he can see an application that's beyond my comprehension and I'm eager to learn that.
Thanks for your explanation of the eepron and it does make sense to me for that part now. I did read the manual but couldn't make any real sense of it. This is just a hobby for me, I haven't had any courses or schooling on programming or electronics so I take it slowly. Now that I'm semi retired I've started to spend more time with electronics and programming and I find it very rewarding.
Thanks for your help and the links
jessey
Select_Push_Button VAR PORTB.7 '(pin 40) 1st
Hours_Push_Button VAR PORTD.2 '(pin 21) 2nd
Minutes_Push_Button var PORTB.0 '(pin 33) 3rd
Seconds_Push_Button var PORTB.1 '(pin 34) 4th
Toggle_Push_Button var PORTB.2 '(pin 35) 5th
A VAR BYTE
A = 0
MyWord var word
MyWord = %1010101010101010
MyByte Var word
MyVar var word
MyBit var BIT
Reference_Word VAR WORD
Reference_Word1 VAR WORD
Reference_Word2 VAR WORD
Reference_Word3 VAR WORD
Reference_Word = 1010
Reference_Word1 = 1010
Reference_Word2 = 1010
Reference_Word3 = 1010
Is_Pressed CON 0
GOTO mainloop
Sound_Buzzer:
PWM Buzzer,250,1
RETURN
PAUSE_100:
PAUSE 100
RETURN
mainloop:
Show_Print:
For MyVar=0 to 15
MyBit=MyWord.0(MyVar)
LCDOut $FE,1,"Bit ",DEC MyVar,"=",DEC MyBit
LCDOut $fe,$c0,$fe,$0e,dec Reference_Word,DEC Reference_Word1, _
DEC Reference_Word2,DEC Reference_Word3
if a = 1 then Return_To_Push_Button_1
if a = 2 then Return_To_Push_Button_2
if a = 3 then Return_To_Push_Button_3
if a = 4 then Return_To_Push_Button_4
if a = 5 then Return_To_Push_Button_5
IF MyBit = 1 then
HIGH LED
PAUSE 1000
ELSE
LOW LED
PAUSE 1000
ENDIF
IF Select_Push_Button = Is_Pressed then ' #1
GOSUB Sound_Buzzer : A = 1
MyWord = %1100110011001100
Reference_Word = 1100
Reference_Word1 = 1100
Reference_Word2 = 1100
Reference_Word3 = 1100
MyVar = 0
IF A = 1 THEN Show_Print
Return_To_Puch_Button_1:
A = 0
WHILE Select_Push_Button = Is_Pressed : GOSUB PAUSE_100 : WEND
IF MyVar = 0 THEN MAINLOOP
ENDIF
IF Hours_Push_Button = Is_Pressed then ' #2
GOSUB Sound_Buzzer : A = 2
MyWord = %1110111011101110
Reference_Word = 1110
Reference_Word1 = 1110
Reference_Word2 = 1110
Reference_Word3 = 1110
MyVar = 0
IF A = 2 THEN Show_Print
Return_To_Puch_Button_2:
A = 0
WHILE Hours_Push_Button = Is_Pressed : GOSUB PAUSE_100 : WEND
IF MyVar = 0 THEN MAINLOOP
ENDIF
IF Minutes_Push_Button = Is_Pressed then ' #3
GOSUB Sound_Buzzer : A = 3
MyWord = %1101101111001001
Reference_Word = 1101
Reference_Word1 = 1011
Reference_Word2 = 1100
Reference_Word3 = 1001
MyVar = 0
IF A = 3 THEN Show_Print
Return_To_Puch_Button_3:
A = 0
WHILE Minutes_Push_Button = Is_Pressed : GOSUB PAUSE_100 : WEND
IF MyVar = 0 THEN MAINLOOP
ENDIF
IF Seconds_Push_Button = Is_Pressed then ' #4
GOSUB Sound_Buzzer : A = 4
MyWord = %1010100110101001
Reference_Word = 1010
Reference_Word1 = 1001
Reference_Word2 = 1010
Reference_Word3 = 1001
MyVar = 0
IF A = 4 THEN Show_Print
Return_To_Puch_Button_4:
A = 0
WHILE Seconds_Push_Button = Is_Pressed : GOSUB PAUSE_100 : WEND
IF MyVar = 0 THEN MAINLOOP
ENDIF
IF Toggle_Push_Button = Is_Pressed then ' #5
GOSUB Sound_Buzzer : A = 5
MyWord = %1111101110101101
Reference_Word = 1111
Reference_Word1 = 1011
Reference_Word2 = 1010
Reference_Word3 = 1101
MyVar = 0
IF A = 5 THEN Show_Print
Return_To_Puch_Button_5:
A = 0
WHILE Toggle_Push_Button = Is_Pressed : GOSUB PAUSE_100 : WEND
IF MyVar = 0 THEN MAINLOOP
ENDIF
Next MyVar
GOTO mainloop
end
sayzer
- 16th December 2006, 06:34
Jessey,
"There are many ways to skin a cat"; for some "a dog", "a snake", etc...
Someone writes a code in his/her logic and it works with no problem.
Someone else writes a different code in a different logic and it also works and does the same job with no problem.
A third one writes a code in much shorter and faster way, it does the same job and works with no problem, too.
Now, since you are starting to programming, I suggest you skip the arrays at first and write long lines instead .
If you start your coding with arrays at the beginning, I see more complexity and confusion in front of you.
So when it comes to your code and project, there are of course several ways to do the same job in relatively less lines with arrays and some advanced coding techniques.
But why now?
-----------------------
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.