Programme being overwritten error (long)

elanman99

Senior Member
I have almost got my project working but now struggling to get the last few
bugs out. When downloaded into an 08M the most of the programme works
OK but crashes when I use one of my functions.

I am sure the problem is to do with writing to NV memory as I still dont fully
understand how it works. In the simulator memory panel only one address
seems to be used (the one in the top left corner), also when I watch the b0
to b13 values in the main simulation panel they do not update straightaway
when the relevant line is executed but do update several lines later.


In the simulator I get a 'Write Error - Overwriting Programme' message when the line 'Write b6,125' is used.

Below is my code, it would be great if anyone could look through it and offer suggestions.

Ian

Code:
'For Picaxe 08M

'Controller for one axis of a small underwater video camera pan and tilt head, (so two of these 'are required). Servo position adjusted by using momentary up/down buttons that move the servo at 'a slowish speed. The servo stops moving when button released. At switch-on the servo is set to 'mid position. The end positions of the servo can be set by the user so that when installed in 'its housing the servo travel only covers the range that the camera lens can see through the 'housing window.
'Setting end stop positions is done by a 'Set' switch which reads the current servo position and 'saves it as a max (or min) value. Depending on the where the current position is the programme 'decides where to put the new value. Another switch 'Reset' clears all the stored values and 'allows the programme to cater for brand new blank chips. 


'Servo on output 0				(IC pin 7)
'Increment sw on Input 1			(IC pin 6)
'Decrement sw on input 2			(IC pin 5)
'Position SET switch on input 3		(IC pin 4)
'Reset memory switch on input 4   		(IC pin 3)

PAUSE 2000' for simulator purposes

Symbol  CW     = b4 'Variable to decrement output pulse width
Symbol  ACW    = b5 'Variable to increment output pulse width
Symbol  POSN   = b6 'Current servo position
Symbol  CWEOT  = b0 'Clockwise End Of (useful) Travel
Symbol  ACWEOT = b1 'Anticlockwise end of (useful) travel

Input 1             'Confirms i/o 1 into an input
Input 2             'Confirms i/o 2 into an input

Servo 0,125         'Only issued once, 2nd number appears irrelevant

If b0 = 0 then gosub NEWCHIP	'Recognises brand new IC and puts default values in memory 

CW      = 0
ACW     = 0
CWEOT   = 200       'Initial starting value
ACWEOT  =  50       'Initial starting value
POSN    = 140       'Start with servo in mid position
  
'Read b0,CWEOT
'Read b1,ACWEOT

'Programme starts here

Main:

If Pin3 = 1 Then Gosub CHOOSER	'SET switch
If Pin4 = 1 Then Gosub CLEARMEM	'Reset Switch

'Pause 200            'Artificially slows travel speed  

Button 1, 1, 1, 1, CW, 0, NOTPRESSED1		'Clockwise travel button
POSN=POSN +1 Max CWEOT
NOTPRESSED1:

Button 2, 1, 1, 1, ACW, 0, NOTPRESSED2		'Anticlockwise travel button
POSN=POSN -1 Min ACWEOT
NOTPRESSED2:

Pause 2

Servopos 0,POSN   'Output the servo pulse on 0

Goto Main

'Various subroutines

NEWCHIP:
Write b0,200	'Default values just to make servo movable on first initialisation
Write b1,50		'values get overwritten later by 'Set' routines
Write b6,125
Return

SETCWEOT:
CWEOT = POSN
Write b0, POSN    'Puts current servo position into non volatile memory as new fixed constant
Return

SETACWEOT:
ACWEOT = POSN
Write b1, POSN    'Puts current servo position into non volatile memory
Return

SETMID:
POSN = POSN
Write b6,125
Return

CHOOSER:							'Decides where to put values
If POSN <100 then Gosub SETACWEOT			'Set Anticlockwise end of travel
If POSN >175 then Gosub SETCWEOT			'Set Clockwise end of travel
If POSN >=100 and POSN <=175 Then Gosub SETMID	'Set Midpoint of travel (Needed at power up)
Return

CLEARMEM:		'Cancels stored values so newchip routine will run next time powered up
eeprom 0,(0)
write 0,0
Return
 

Chavaquiah

Senior Member
Seems you're still having difficulties understanding what the WRITE and READ commands do...

When you have WRITE b6, 125, what you're doing is trying to write the value 125 to the memory position pointed to by b6. For instance, if b6 holds the value 222, you will be writing 125 to memory position 222. Not a good thing to do on a 08M. :eek:

Using If b0 = 0 is not a proper way to detect if you have a brand new chip. Registers are probably initialized to zero at program start, so your program will always think it's running on a new chip.

You also have POSN = POSN. This is the same as writing b6 = b6. Well, that you can do, but... it's useless.

Perhaps you're confusing variables/registers (b0, b1, b2, ...) with memory positions. Think of registers (b0, b1, etc.) as variables that can hold values and that you can use in calculations or commands (e.g. b0 = b1 + 2). Think of memory locations as extra places where you can store values but that you can not use directly in calculations. These memory locations are identified by a number (see in the manual which numbers are valid, do NOT use any other on the 08M), whereas registers are identified with the letter "B" plus a number.

To access that extra memory, you use the commands WRITE and READ. You can not simply write Location = Value.

If you code Write b0, POSN, you won't be writing to location zero, you will be writing (or trying to write) into whatever position is pointed by b0. If b0 holds 33, "WRITE b0, POSN" is the same as writing the value currently held by POSN (or b6) into position 33.
 

BeanieBots

Moderator
Also
eeprom 0,(0) 'stores the value zero in location zero during download.
write 0,0 'writes the value zero to location zero during runtime.

The 08M shares it's EEPROM space with the program, so the larger your program the less EEPROM space you have. Writing beyond the 'boundary' will attempt to save your data in the same place as your program. Hence the error message.
 

elanman99

Senior Member
Seems you're still having difficulties understanding what the WRITE and READ commands do...

When you have WRITE b6, 125, what you're doing is trying to write the value 125 to the memory position pointed to by b6. For instance, if b6 holds the value 222, you will be writing 125 to memory position 222. Not a good thing to do on a 08M. :eek:

Using If b0 = 0 is not a proper way to detect if you have a brand new chip. Registers are probably initialized to zero at program start, so your program will always think it's running on a new chip.

You also have POSN = POSN. This is the same as writing b6 = b6. Well, that you can do, but... it's useless.

Perhaps you're confusing variables/registers (b0, b1, b2, ...) with memory positions. Think of registers (b0, b1, etc.) as variables that can hold values and that you can use in calculations or commands (e.g. b0 = b1 + 2). Think of memory locations as extra places where you can store values but that you can not use directly in calculations. These memory locations are identified by a number (see in the manual which numbers are valid, do NOT use any other on the 08M), whereas registers are identified with the letter "B" plus a number.

To access that extra memory, you use the commands WRITE and READ. You can not simply write Location = Value.

If you code Write b0, POSN, you won't be writing to location zero, you will be writing (or trying to write) into whatever position is pointed by b0. If b0 holds 33, "WRITE b0, POSN" is the same as writing the value currently held by POSN (or b6) into position 33.
You are right, I was confusing variable with registers. what you have just explained though has increased my knowledge significantly so I will go back and review my mistakes.

I can see that my method of detecting a new chip is rubbish, I will change the code to work regardless of chip history. The POSN=POSN is just a silly mistake.

Ian
 

westaust55

Moderator
Program and EEPROM sharing in 08M chips

With your program loaded into the Programming Editor,
click on the Syntax Check button on the toolbar.
As well as a syntax check, this will show you the estimated size of your program in bytes.


In the 08M, you have 256 bytes (0 to 255) of shared EEPROM/program space and the program is stored in the top part of this memory.
So if the syntax check indicates 160 bytes used then 256 – 160 = 96 bytes remaining for EEPROM data storage.
Those 96 available EEPROM data locations for READ and WRITE commands are then numbered 0 to 95. You may find an extra one or two bytes could be used by the program so to be safe try and stop a couple of bytes short of the theoretical available data space.
 

elanman99

Senior Member
Thanks to all the people who have replied to my posting. Following the help that was so given I now have a much fuller understanding of the memory, and how the picaxe system works in general.

I have modified and simplified my code so that I eliminated the need for blank chip detection as I have a reset button that puts in default values. The picaxe now remembers the variables when the power is off so it all works!

I do still have one small problem but I am hoping research of the forum will solve it (the servo twitches about once a second) ISTR its something to do with the picaxe going into low power mode?

Once again, I thank you all.


Ian
 
Top