Replacing the variable w3 withmed POKE and PEEK

Jacobsen

New Member
As I'm using the variable "w3" in other places and have no further variables available, is it somehow possible to release a variable with a poke and peek commando?
Hopefully that isn't a too stupid question!!

Addresses :
PICAXE-28X 16F873A = 112 : $50-$7F / $C0-$FF

Symbol b20 = $50
Poke b20, b1
Peek b20, b1

I have tried with the commands:
Poke b20, ADCvolt
And then fetching the value again with:
Peek b20, ADCvolt

I'm would like to use the commands in the Basic-code below.

<code><pre><font size=2>
Symbol ADCvolt = W3 ;B6,B7

ReadFlowVolt:

READADC 3,ADCvolt

If ADCvolt&gt; ADCmax then SetHiVolt
If ADCvolt&lt; ADCmin then SetLoVolt
Goto ReadFlowVolt_Ialt
SetHiVolt:
ADCmax = ADCvolt
Goto ReadFlowVolt_Ialt
SetLoVolt:
ADCmin = ADCvolt
ReadFlowVolt_Ialt:
Return
</font></pre></code>
regards
Monie
 

Technical

Technical Support
Staff member
Peek and poke only use bytes. Therefore to save your adc value w3 (which is a word variable - so two bytes, also known as b6 and b7) you have to peek/poke both b6 and b7. This is the same as peek/poking one word (w3)


Symbol b20 = $50
Symbol b21 = $51

...here w3 is some value

Poke b20, b6
Poke b21, b7

...

Peek b20, b6
Peek b21, b7

...now w3 is restored

Edited by - Technical on 2/28/2006 2:15:50 PM
 

Jacobsen

New Member
To Technical

Do you menens I'm can do as Basic-code below.

<code><pre><font size=2>
SYMBOL get = b11
SYMBOL byte = b12
SYMBOL rsbit = b13
SYMBOL b20 = $50


PowerOnReset: GOSUB InitialiseLcd

DisplayLine1:

READADC10 0,b6
Poke b20,b6

byte = $80; Start Line 1
GOSUB SendCmdByte
EEPROM 6,(&quot;ADC 0: &quot;) ; 6..14
FOR get = 6 TO 14
READ get,byte
GOSUB SendDataByte
NEXT


Peek b20,b6
LET b6 = b6 * 50 / 32 * 10 / 32

SERTXD(&quot;Volt: &quot;,#b6,CR,LF)
</font></pre></code>

Best regards
Monie
 

MartinM57

Moderator
Be careful Monie!

You've changed the line I taught you last time about converting ADC10 readings to volts to:

LET b6 = b6 * 50 / 32 * 10 / 32

THIS WILL NOT WORK!!!!!

It only works when the reading is held in a word variable, not a byte variable. Please spend a little time on convincing yourself why this is so.....

 

Jacobsen

New Member
And have used all my variables.
I am very unsure of how Poke og Peek are used in practice and how these parameters are to be inserted in the basic codes below.
I need to e.g. give the variable B2 a value from READADC10, Solar in..
As I have to get the binary value..
How is this done? When the variable &quot;w1&quot; will stil be available for values from before (READADC10,Solar in).
I would like you to put in some remarks and explanations to what the codes do, if possible.
I really want to learn hoe to master this command ;-)

<code><pre><font size=2 face='Courier'>
SYMBOL Amp = w1 ; B3 : B2
SYMBOL b20 = $50
SYMBOL b21 = $51

Poke b20,b2
Poke b21,b3
..........
Peek b20, b6
Peek b21, b7

READADC10 0,Solar ; w0
B2 = Solar
</font></pre></code>

And in the next READADC to be able to insert the variable &quot;b1&quot; and use this in a calculation as shown below.
<code><pre><font size=2 face='Courier'>
READADC10 1,Amp ; w1

Byte = $C0; Start Line 2
GOSUB SendCmdByte
EEPROM 26,(&quot;Power: &quot;); 26..34
FOR get = 26 TO 34
READ get,byte
GOSUB SendDataByte
NEXT

LET Amp = b2 * Amp 'kW ; The variable inserted for calculation
Let Amp = Amp / 30
Let Amp = Amp / 3
Let Amp = Amp * 10

SERTXD(&quot;Amp: &quot;,#Amp,CR,LF)
</font></pre></code>

I really hope that you can help me with this, I have tried very hard myself.

Best regards
Monie
 

Jacobsen

New Member
<b> To MartinM57 </b>

I've now made the &quot;GOSUB to&quot; r outine work. It works like this: When an event occur, it's written in Display linje 1, but only where the output from the variables take place, it's replaced with a text and remains like this untill the condition ends. As shown below
<code><pre><font size=2 face='Courier'>
NoSun:
<b> byte = $0C; Start Line 1 </b>
GOSUB SendCmdByte
EEPROM 15,(&quot; No Sun&quot;) ;66..85
FOR get = 15 TO 25
READ get,byte
GOSUB SendDataByte
NEXT
Goto DisplayLine2
</font></pre></code>

The first line, marked bold, this code: &quot;byte = $0C&quot; replaces the text to the far right of line 1
I've found the codes on Hippy website: <A href='http:// www.hippy.freeserve.co.uk/picaxelc.htm ' Target=_Blank>External Web Link</a>

Please see below.

LCD Character Addressing
<code><pre><font size=2 face='Courier'>
20 x 4 - 20 Characters x 4 lines
Line 1 : $80 .. $93 ..............&gt;&gt; Here I read it as a possibility to start a text another place in the line!
Line 2 : $C0 .. $D3
Line 3 : $A0 .. $B3
Line 4 : $E0 .. $F3
</font></pre></code>
But it works great now ;-)

As to the Poke and Peek command, I've also made this work, but it took really long for me to understand the functions. E.g. I didn't know if it would have an effect on the variable &quot;W0&quot; when Poke and Peek reaches &quot;W0&quot; later in the program, and has to be used again. That is something with the Open and close function?
<code><pre><font size=2 face='Courier'>
Symbol b20 = $50

Poke 20,b0
&#8230;&#8230;&#8230;&#8230;&#8230;

B0 = Solar --&gt;&gt; Here I'm using the variable in the program.
Peek 20,b0
</font></pre></code>

Basic-Code:
<code><pre><font size=2 face='Courier'>
Symbol b20 = $50
Symbol b21 = $51

DisplayLine1:
READADC10 0,Solar
Poke 20,b0
Poke 21,b1
Byte = $80; Start Line 1
GOSUB SendCmdByte
EEPROM 6,(&quot;Solar :&quot;) ; 6..14
FOR get = 6 TO 14
READ get,byte
GOSUB SendDataByte
NEXT

IF Solar &lt; 205 THEN NoSun ; 205 = 1 Volt

LET Solar = Solar * 50 / 32 * 10 / 32

DisplayLine2:
READADC10 1,Amp
Byte = $C0; Start Line 2
GOSUB SendCmdByte
EEPROM 26,(&quot;Power: &quot;); 23..31
FOR get = 26 TO 34
READ get,byte
GOSUB SendDataByte
NEXT

B0 = Solar
Peek 20,b0
Peek 21,b1
LET Amp = b0 * Amp 'kW
Let Amp = Amp / 30
Let Amp = Amp / 3
Let Amp = Amp * 10

SERTXD(&quot;Solar: &quot;,#Solar,&quot; Amp: &quot;,#Amp,&quot; B0: &quot;,#b0,&quot; B1: &quot;,#b1,CR,LF)

NoSun:
Byte = $0C; Start Line 1
GOSUB SendCmdByte
EEPROM 15,(&quot; No Sun&quot;) ;66..85
FOR get = 15 TO 25
READ get,byte
GOSUB SendDataByte
NEXT
Goto DisplayLine2
</font></pre></code>

Do you have a comment or some routines that will enable me to improve my Basic-codes I would be happy to hear from you.

Best regards
Monie
 

hippy

Technical Support
Staff member
- Byte = $0C
- GOSUB SendCmdByte

Will not move the cursor to the start of line 1 for a HD44780 LCD. It turns the display on and turns the cursor off but should not have any other effect.

If the code works as it is, you can probably remove both lines without affecting the operation of the code.
 

MartinM57

Moderator
OK Monie.

I am sorry to say this really is a complete mess, and strangely I admire you for persevering this far without putting your PICAXES in your Too Hard Box (tm) and doing something else instead.

Here are a few pointers for you. PLEASE PLEASE take some time to think about each one rather than just blindly changing your code and then coming back with more questions. I haven't answered everything completely, or even addresed some of the problems you have in this code - you will have to do some thinking yourself!

- Put your Programming Editor away, and your PICAXES and just read your code, pretending that you are the PICAXE and work out what the code is telling you to do.

- Get a piece of paper and write down &quot;b0&quot; to &quot;b13&quot;, and the memory locations $50 to $7F, in a big list. Read your code, line by line, following all the THENs and GOSUBs, write down next to each variable or memory location the value it will hold as the PICAXE processes the code. Then see if those values are the ones you want. I'm sure you will find that this is not the case. So, change your code and repeat the paper based exercise until it does.

- Then follow all the branches of the code and see how the code &quot;flows&quot;. For example, imagine starting at READADC10 0,Solar, with Solar being read as, say, 200. Follow the code - you will find that you have an infinite loop that never reads Solar ever again. I suspect that is not what you want. Try it with imagining Solar is read as 210 - another infinite loop occurs after some different initial processing.

- ONLY when your happy with your use of the variables and memory locations, and happy with the flow of your code, THEN get your Programming Editor and PICAXES out, write your code and test it on a real PICAXE.

- Be very careful with byte and word variables - you REALLY REALLY need to understand them perfectly and make sure you are using the right type of variable at the right time. Take your line &quot;B0 = Solar&quot;. What are you trying to do here? Set a byte variable equal to a word variable? Why?

- Understand PICAXE integer mathematics. Your sequence:
LET Amp = b0 * Amp 'kW
Let Amp = Amp / 30
Let Amp = Amp / 3
Let Amp = Amp * 10
is very strange. Use your piece of paper again, start at some different values of b0 (whatever that represents) and different values of Amp. Look at what the final values of Amp - are they really what you want? I doubt it.

- Understand integer and hex representation of numbers. Read the manual about PEEK and POKE and see why your commands like &quot;Poke 20,b0&quot; are totally wrong for what you are trying to do.

I don't mean to be negative Monie, but these are all simple things that you MUST understand before you move on to more complex real applications. Experiment as much as you like, but when you get a problem or want to make your code do something a little different, go back to first principles and think carefully and clearly about how to go about it.



By the way, here's some general things:
- Use meaningful names for your symbols. Solar is OK, but sometimes it holds the 10-bit ADC value and sometimes it holds the actual voltage in units of hundredths of volts. It's very confusing
- Use labels that also indicate what the code is doing, Your DisplayLineX: codes do not display on line X of the LCD - so change them to more useful names
- Your memory map for a 4 line LCD is not the same as mine. My string line positions are $80, $C0, $94 and $D4 - write yourself a simple program that just displays different characters at different positions on the LCD to check


Good luck Monie - I wish you great success in all your future projects....

Regards, Martin

Edited by - MartinM57 on 05/03/2006 21:17:06
 

Jacobsen

New Member
To hippy

When I remove: &quot;byte = $0C; Start Line 1&quot; as you suggested all four lines of the display are filled with random signs.
So that doesn't work, but if I'm doing something else wrong in the basic-codes I can't figure out.

Best regards
Monie
 

hippy

Technical Support
Staff member
I'm not sure that I understand what your code is meant to do.

It looks okay in principle, but after printing &quot;No Sun&quot; the code jumps to DisplayLine2, then falls through into the NoSun routine again and goes round in circles.

This is possibly what is causing the random display, although it could be that this is just a code extract.
 
Top