PDA

View Full Version : saving program variables



Srigopal007
- 1st November 2004, 18:07
hello everyone, I was wondering if it is possible to save values of a varialbe for later use, into like a specific location in memory. For example lets say the program is slowly turning an LED that is connected to an I/O port, on and off using pwm. And at random, when a switch is pressed, lets say the LED is 35% ON or 60% ON. and I want to save those random value to memory so that when I press that switch again at a later time.. the LED will be lighted 35% or 60% depending on the value stored in the variable. I am not using HW pwm but software PWM.

I have the leds ramping up and down.. here is what I have so far. just need an idea of how to store the values of the variable for later use.


==============================================
include "modedefs.bas"
Duty VAR Word
Cycle1 VAR BYTE
Switch VAR PortB.1
TRISB = %00000010
Cycle1 = 2

start:
For Duty = 0 to 100
*if(Switch = 1) then
For cycles = 0 to Cycle1
PortB.7 = 0 'LED connected to PIN 7 is off Pauseus 100 - Duty
PortB.7 = 1 'LED in Pin7 is ON
Pauseus Duty
Next cycles

Next Duty

For Duty = 100 to 0 step -1
*if(switch = 1) then

For cycles = 0 to Cycle1 PortB.7 = 1 'LED is ON
Pauseus Duty PortB.7 = 0 ' LEd is turned off Pauseus 100 - Duty
Next cycles

Next Duty

goto Start

====================end of program ==============

here is what I know what to do The Asterick symbol (*) in the program above is where I konw to place the switch statement to check to see if the switch is pressed but I am not sure how to save the value of the variable for later use. I want to make it so taht the values gets presetted, for later use. Any help on this confusion would be greatly appreciated. Thank you in advance


srigopal

mister_e
- 1st November 2004, 18:22
to write to internal EEPROM

WRITE 1,VARIABLE


to read later

READ 1,VARIABLE

where VARIABLE has been previously define as a WORD size var

that's it dude

Dwayne
- 1st November 2004, 18:59
If you have Duty defined as a word, you will have some problems reading and writing to Eprom.

EEprom works in bytes, not words... if you write a word, you will have to do a double read....First read will pick up the MSB, and the second read will pick up the LSB.

If you know that your duty will not go more than 255, then change the Duty to a "Byte" instead of a word. Then a very simple Read/Write to EEPROM is all that is needed.

If your Duty is more, then you will have to read Two bytes, and concatinate <sp> the two bytes together to make up your stored Word.


Dwayne

Srigopal007
- 1st November 2004, 19:07
I have never done this before, can you please help me with some sample code that might help me visualize this concept. sorry .. I learn more from visual more than from abstract .... if you konw what I mean.. sorry for the inconvenience.. a sample code would be greatly appreciated. Thank you

mister_e
- 1st November 2004, 19:08
OOPS i am really sorry about that.... i really don't know where my head was when i wrote it. Thanks Dwayne for correcting me!

here's a little example


SetButton VAR PORTA.0
InitialValue VAR BYTE

Read 0,InitialValue

start:
While NOT SetButton
PORTB=InitialValue
pause 1000
PORTB=0
PAUSE 1000
Wend
InitialValue=InitialValue-1
write 0,Initialvalue
While setbutton
wend
pause 50 ;cheap way to debounce pushbutton
goto start

Srigopal007
- 1st November 2004, 19:34
.. I just found that READ and WRITE operations are only useful if you have internal EEPROM, my device 16F77 does not have internal EEPROM... what to do now? :(

mister_e
- 1st November 2004, 19:51
In your case you must use an external eeprom such 24c00,24c01,24c03 or those 93c06,93c46. Easier to use 24c series and less connection to be used. Let me know which EEPROM you've in stock if you have. At this time we will do the code for you.

regards

Srigopal007
- 2nd November 2004, 00:19
Wait .. why cant i use the 8k program memory space that is internal to the microcontroller. why do i need to use the eeprom....internal or external ? this is prolly an easy question to answer.. that is not very obvious to me right now.

oh by the way I am using the 16F77 which has no internal eeprom. the reason why I am asking this question is because the 16F77 does not have any internal eeprom.

if my only choice is to use the external eeprom. I will use the 256 byte (93C06) how do I read and write to this device. any help with an accompanying code would be greatly appreciated. thanksss lots in advance.


srigopal

mister_e
- 2nd November 2004, 05:11
Hi srigopal,
you can write and read 93c06 like these lines:




;This code will write to the first 128 memory allocation
;of an FM93C06 EEPROM 16x16-bit array
;===========================================

CS VAR PORTA.0 ;CS pin of 93c06 (usualy pin1)
CLK VAR PORTA.1 ;CLK (pin2)
DI VAR PORTA.2 ;DI (pin3)
DO VAR PORTA.3 ;DO(pin4)
Addr VAR BYTE
Value1 VAR BYTE
Value2 VAR BYTE
A VAR BYTE

CS=1 ;disable 93c06

start:

;let's write to eeprom
;====================
for a=0 to 128 step 2
Addr=a
Value1=a+100
Value2=Value1+1
GOSUB EEPROMWrite
next

;now read it and send to LCD
;==========================

for a=0 to 128 step 2
Addr=a
GOSUB EEPROMRead
LCDOUT $FE,1,"addr:",dec a,$fe,$c0,"V1:",dec Value1," V2:",dec value2
pause 500
next
goto start


EEPROMWrite:
CS = 0 ;enable eeprom
Shiftout DI, CLK, MSBFIRST, [%10011,0\4] ;write enable command+dummy clocks
pause 10
CS = 1 ; Disable to execute command

CS = 0
Shiftout DI, CLK, MSBFIRST, [%101\3, addr\6, Value1,Value2] ;Send Adress+data
pause 10
CS = 1
Return


EEPROMRead:
CS = 0
Shiftout DI, CLK, MSBFIRST, [%110\3, addr\6] ;read command+address
Shiftin DO, CLK, MSBPOST, [Value1,Value2] ;get data
CS = 1
Return

mister_e
- 2nd November 2004, 08:21
But depending on what you want to experiment. Instead of use external EEPROM we can also turn PIC in SLEEP mode. in this case we do not need external EEPROM, BUT, all the data will be lost if battery or else power source is removed.

regards

Dwayne
- 2nd November 2004, 16:54
Hello Srigo,

s>>oh by the way I am using the 16F77 which has no internal eeprom. the reason why I am asking this question is because the 16F77 does not have any internal eeprom. <<

Is there any way you can use a different chip? if you are going to go through the bother of purchasing external EEPROM, why not attempt to get a chip that is similar to the 16F77 that has EEProm?? Less circuitry, easier on pocket book, and the whole works.

Dwayne

Dwayne
- 2nd November 2004, 17:11
Hello Srigo,

S>>Wait .. why cant i use the 8k program memory space that is internal to the microcontroller. why do i need to use the eeprom....internal or external ? this is prolly an easy question to answer.. that is not very obvious to me right now.<<

I think you are referring to using the remaining 8k on your chip? is this correct?

Yes, you can use the remain 8k on your chip, but it is not "changable" on the run, like EEPROM. You can put a "table" out at the end of your 8K, but that table is "fixed" data.

Mel had a excellent example of using "End of your ROM" for extra data a few months ago. But that data is "fixed".

Dwayne

Srigopal007
- 2nd November 2004, 19:55
Hi dwayne,

Yes I am talking about the 8K program memory that is in the 16F77.....what do you mean by fixed data?... do you mean that the data is not going to change if I try to write to it? if that is the case.. then how does the PWM work... I'm sure that the software changes the value of the PWM variable continuously to change the duty cycle ... can you please elaborate on that a little more for me. it is very hard for me to follow along with that "no changeable" concept.


srigopal

Dwayne
- 2nd November 2004, 20:58
Hello Srig,

Srig>>Yes I am talking about the 8K program memory that is in the 16F77.....what do you mean by fixed data?... do you mean that the data is not going to change if I try to write to it?<<

No, you cannot write to it....You can only write to it during your programming of your chip. Then you are out of luck.
A good example to use this upper part of your memory, is for table lookup. For example...

If you are receiving data via a serial port, and the data are the numbers 1-25, you can write a table and put it out at teh very end of your memory, and use this to covert your 1-25 into meaningful data.

Table:
1,a
2,7
3,d
4,r
5,m
..
..
..
25,#FF 'end of table


You cannot chang this data while the chip is running. You can only do a "Look-Up" and/or "Cross-reference" by reading only.

Srig>> if that is the case.. then how does the PWM work...<<

The PWM is a "Hardware" situation. It is built into the chip by the MANUFACTURER!, not the Programmer (which is you). And the Hardware PWM will continuously put out a train of pulses while the chip does other things. Once the HPWM is turn on, it will continuously (without intervention) output its pulses.

the HPWM is "Hardware" and the Table on the end of your memory is *Like* hardware. The table cannot be changed, only during programming of the chip(notice is is the PROGRAMMER who is changing this table, not the manufacturer). thus the table is a constant and is unchangeable.

The HPWM is changeable (via a variable by the manufacturer) that the chip will recognize and react by changing the Pulses of a HPWM.

Lets say we want to build a Secret Code between you and I.....for a "A" we want a different letter..Lets build the Table, so that we always know the the table is good.

Table1:
A,J 'assign every letter A to the letter J
B,O ' assign ever letter B to the letter O
C,I
D,P
..
..
..
Z,U 'assign every letter Z to the letter "U"

I will do this in Psuedo code.

1st. I will tell the compiler to put that tabe at the end of the 8k.(hey that table will never change will it? We dont' want it to change do we?, If we change it in any way, I will not be able to decode your letter to me). So the table is "fixed", and unchangeable. I "can" put it at the end of my 8k, because I will never have to change it.

Message[100] var byte
Decoded[100] var byte

for s=1 to 100
Message[s]=Letter from Srig
Next s

'Message now contains your message you sent from me.

Now decode it!

For s = 1 to 100
Decoded[s]=TableLookUp(Message[s])
Next s

'If Message[1]="C" then Decoded[1]="I"
'etc etc.

Now lets output our decoded message to the LCD

LCDout, Decoded 'output decoded message to lcd!



Now what this means, if you decided to change the letters to mean a new set of letters, *I* must reprogram the chip with a new table. I cannot "change" the table any other way.

***********************************************
Now, lets use EEPROM...
With using EEPROM, you can "SEND" me the table BEFORE sending me the message.

I then put the Table into EEPROM, and decode your message using the table *YOU* sent me via the serial port. I don't have to reprogram my chip...I just want you message print out on the screen.

If you decide to change the table, that is ok... the table will be sent, and my chip will automatically load the NEW table into EEProm, and decode your message... I sit back and watch your message come through.

mister_e
- 2nd November 2004, 21:08
Any body here already use dangerous WRITECODE... i really don't know if it can do the job. I read a few limitation but... a pure guess it can do the job. Never do anything with this.

i'll try and post it later.

regards

Dwayne
- 2nd November 2004, 21:43
Hello Mister_e!

Mister_e>>Any body here already use dangerous WRITECODE... i really don't know if it can do the job. I read a few limitation but... a pure guess it can do the job. Never do anything with this.<<

Thanks Mister_e! thanks for setting me straight on this PIC issue...I owe you one...

I still refer to Mel's previous post of a few months ago...

I finally looked it up...

Srig, Again... Take a look at Mel's excellent example of Tabling outside the program space into oblivian...

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=137&highlight=writecode

Dwayne

mister_e
- 2nd November 2004, 22:25
He he that's how i was expected... but be sure of all memory space your using. thanks Dwayne & Melanie!!!

regards