memory use problem

gengis

New Member
I've been struggling with this for a few days, reduced the circuit and program to its lowest common denominator and it still won't work. Can some one suggest what to try?

What it is supposed to do. I wanted to find a simple way to input a value into a variable (low number 0-15). To that end I have a pushbutton switch and led. The thing works as expected: I press down on the button, turn on power, and count the number of times the led flashes - then after a delay it flashes the led the same number of times and shuts off.

Things get hinky when I try to get it to commit the last number of flashes to EEPROM. The "remarked" commands for write and read in the program. If I put the read command alone, it flashes continuously (like some high number is in b0) - without ever pushing the button (after a clean program loaded).

The write command without the read causes the circuit to work normally until the led is supposed to flash back the value of b0 - nothing in b0 and no flashes, but if I do press the button it flashes normally while the button is down then comes on and stays on when the button is released and some of the other output pins go high even though there's nothing programmed into them.

Have I got the read and write commands in the correct place to do what I want and am I using them correctly?:confused:

Code:
    pause 100
    'read 201 ,b0        'read the count XXXXXXXXXXX
    pause 100
MAIN:
    IF PIN3 = 1 THEN MAIN2
    LOW 4                 'turns on led
    PAUSE 10
    b0 = b0 + 1            'increments b0 count
    pause 300
    HIGH 4                'turns off led
    PAUSE 300
    IF PIN3 = 0 THEN MAIN    'if the button is still pressed it
                    'continues to flash and increment 
    PAUSE 300
    'write 201, b0    'write count into mem location 201 XXXXXXXXX
    PAUSE 300    
    IF PIN3 = 1 THEN MAIN2    'if the button is released it plays
                    'back the same # of flashes as b0
MAIN2:
    if b0=0 then stops
    for b5 = 1 to b0   'beginning for/next loop, flash N times
    pause 500        'three seconds between flashes
    low 4            'pin 3 high, turn on LED
    pause 500        'turn on for 10 milliseconds    
    high 4            'pin 3 low,turn off LED
    next b5        'back to for/next loop
    pause 200
stops:

    end
 

gengis

New Member
trying to load the circuit schematic

didn't take first time

Button on leg four to ground, of 08M with pull up resistor.

Led on leg three with current limiting resistor tied to B+
 

Attachments

Jeremy Leach

Senior Member
Hi Flooby. You need a pullup resistor from pin 4 to +V, otherwise the pin will float when the switch isn't pressed and you'll probably get wierd results. Might explain the problem.

EDIT: Your text says there IS a pullup, but your diagram doesn't ! ??
 
Last edited:

hippy

Ex-Staff (retired)
Your "WRITE 201,b0" could be overwiting program code on a PICAXE with shared code and data.

Always reserve Eeprom space you want to use for data storage at the start of your program ...

EEPROM 201,(0)
 

westaust55

Moderator
Even pre-allocating a memory at location 201 will cause problems.
I understand from past threads that the program starts from the top of the available EEPROM space , so in smaller PICAXE with shared memory, the data needs to be near the bottom.

Therefore think it would be better to try
EEPROM 1,(0) to pre-allocate and

READ 1 ,b0

WRITE 1 ,b0


EDIT:
looking in the manual 2 at the EEPROM command there is nothing that clearly states the above but the example uses low EEPROM locations to store data.
This be time for another manual clarification
 
Last edited:

lbenson

Senior Member
Your "LOW 4" statements are commented as turning on the LED, and "HIGH 4" as turning off. According to the schematic, shouldn't those be reversed? Doesn't matter for the execution, but referring in the comment to "LOW 4" to pin 3 is confusing to me--it's been recommended that physical pins be referred to as legs--i.e., PIN4 is leg 3.
 

robots42

New Member
How to do it

'I found the program impossible to follow so I wrote a new one which works.

'PicAxe08


'LED on PIN1 to ground
'button on PIN3 to ground

init:
read 0,b1 'read existing count

start:
if pin3=0 then countflash 'if button pressed then count

flashit: 'flash memory count
for b2=0 to b1
low 1
pause 50
high 1
pause 50
next b2

pause 1000 'interflashing wait
goto start


countflash:
b1 =0 'initialise count
nextcount:
if pin3=1 then writecount 'finger off button so goto save count
low 1
pause 200
high 1
pause 200
b1 =b1+1
goto nextcount

writecount: 'save count
write 0,b1

goto start


'David
 

gengis

New Member
Sorry a bunch of errors got in there. LED is going to B+ and low on the 'axe is ON. There is a pull up on the push button switch. And one remark says a 3 second pause when it is 1/2 second etc..


From the manual:

With the PICAXE-08, 08M, 14M and 18 the data memory is shared with program memory. Therefore only unused bytes may be used within a program.

To establish the length of the program use ‘Check Syntax’ from the PICAXE menu. This will report the length of program. Available data addresses can then be used as follows:

PICAXE-08M, 14M, 20M 0 to (255 - number of used bytes)

I took that to mean that if the program used the first 60 bytes, any number higher than 60 and lower than 256 should work.

I've already used that command, in that way on another project, and it did work that way.

BUT I just plugged in "read 0 ,b0" and write 0, b0 - and it works! More or less - at least the other pins are staying quiet - I intended that if the button is down it should reset the b0 count and that isn't in there yet - so it will keep adding to the b0 variable - but that is an obvious mistake and should be easy to fix.

Now enlighten me about usable addresses? I assumed, based on what I was reading, that the basic program loads beginning at byte 0 and occupies space sequentially up to byte N and everything above is free space, for storing numbers or variables?

So if I'm (now) telling it that I want to store a variable in byte 0 does it make allowances and load the program above that number? And does that mean that any location can be used or only at the very beginning?

Does this also mean my much longer program, that is working with location 200 to store a variable, shouldn't be working? That's confusing to me.

Thanks guys!
 

robots42

New Member
repost of working program with (code)(/code), sorry

My program resets the count if the button is pressed.
If not it remembers the count even between power offs because of the WRITE.

The program code is stored from the top of memory down, (comes of copying Parallax's BasicStamp1) so if top of memory is 255 then the code is stored from 255 down leaving 0 up free for DATA, Got to make sure they don't overlap!!!

Code:
'PicAxe08


'LED on PIN1 to ground
'button on PIN3 to ground
    
init:
	read 0,b1			'read existing count	
	
start:
	if pin3=0 then countflash	'if button pressed then count

flashit:				'flash memory count	
	for b2=0 to b1
	  low 1
	  pause 50
	  high 1
	  pause 50
	 next b2 
	
	pause 1000  		'interflashing wait
	goto start


countflash:
	b1 =0			'initialise count		
nextcount:	
	if pin3=1 then writecount 	'finger off button so goto save count 
	low 1
	pause 200
	high 1
	pause 200
	b1 =b1+1
	goto nextcount
	
writecount:			'save count
	write 0,b1

	goto start
David
 

hippy

Ex-Staff (retired)
Available data addresses can then be used as follows:

PICAXE-08M, 14M, 20M 0 to (255 - number of used bytes)

I took that to mean that if the program used the first 60 bytes, any number higher than 60 and lower than 256 should work.
That's a misunderstanding. Let's assume your program size is 56 bytes ( makes the maths easier ) ...

There are 256 program bytes, less the 56 bytes your program uses, leaving 200 bytes which can be used for data. These will be numbered 0 to 199 ( 1st to 200th ), once you get above 199 you are trampling on the program code.

Note the bit in the manual as you quoted above; "address can be ... zero to 255 minus number of bytes used".
 
Last edited:

vttom

Senior Member
Thanks!

Oh wow. This post made me go back and read the documentation on the write command. I hadn't quite realized write went into EEPROM. I though it was an alias for poke!

This post just helped me solve a problem with one of my projects where I need to save a 2-byte word value that will persist after losing power!
 

westaust55

Moderator
PICAXE memory types

There are four distinct pairs of commands for accessing various type/sections of memory:
1. READ and WRITE and EEPROM for EEPROM
2. PEEK and POKE for SFR memory and registers
3. PUT and GET for scratchpad
4. TABLE and READTABLE for Table memory (located in program space and for X1 and X2 parts only)
 
Top