Welcome! Log In Create A New Profile

Advanced

Running my progam from Flash

Posted by BruceV 
Running my progam from Flash
September 09, 2013 10:35AM
I have written the code to run my Hot tub, using Rookie and it all functions correctly (finally, programming not my strong point !!!)
I have modified Rookie3.bas and removed the main() function and re-used the label in my code.
The code works until I save it to flash, then when it start the code, it initalises the Max7219 display,runs a test, then hangs.
I have put print statements in to try and work out where it is stopping and it doesn't seem to get past the For/next loop when it's reading adc_get(2). But it works when its not in flash ??
Any ideas or is there a different way to write the code for saving to flash ??

function main()
IrFlag = 0 // clear interrupt flag from Rookie code
print "\n Hottub"
I7219() //COMPLETE
C7219() //COMPLETE
TESTDISP() // Display Test Routine
while io_read(PORTA,4) = 0 // HALT SWITCH
print "\nRead temp"
RT() // READ TEMPS & switches //COMPLETE
------------------------------------------------
//Read Temps & Maint sw & CalcTemp ROUTINE *** COMPLETE ***
function RT()
dim a,b //local values
b = 0 //set b = 0
print "\nForNext"
for a = 1 to 500
b = b + adc_get(2) // get avg
next
print "adc2"
Re: Running my progam from Flash
September 09, 2013 04:56PM
Bruce,
which device do you have, MX1 or MX3?
Jim
Re: Running my progam from Flash
September 09, 2013 05:46PM
MX1, I've done a bit more digging and it appears that if I transfer & save Rookie1,2,3 & then my code and then run it from the command line it works, but if I then reset the MX1, it only gets to the code as above.

If I HALT the program and then type any of the function, it just stops.

Also I thought it might be the adc_get code, but if I comment it out, then it stops after the For/Next loop.
Re: Running my progam from Flash
September 09, 2013 06:31PM
Tried the #option only off command
and it does now over look the Rookie3 main() function, but the code still stops at the same place.
Re: Running my progam from Flash
September 10, 2013 07:44AM
Bruce,
Are you using global variables and if so what version (serial) is the MX1 firmware. If it is before 2.08 (Serial 201310020) then there is a bug when using global variables and then saving to flash. This is detailed here..

If this is the case an upgrade is needed, this is free if you get the IC back to me but in your case I will send you another IC as you have made a contribution to the forum.
Re: Running my progam from Flash
September 10, 2013 09:21AM
I think the following confirms that my chip will have this bug, is there a method of programming so as not to encounter the issue ?
======================================
| ByPic Version 2.08
| Saves 0 |
| MBCHIPMX1 Serial# 201310016
| |
| Documentation www.bypic.co.uk |
| |
| Copyright Jim Spence 2013 |
======================================
Re: Running my progam from Flash
September 10, 2013 09:30AM
Bruce,
no they are not user upgradable but as promised I will send you another one free. Let me know your details using the email address here.
Re: Running my progam from Flash
September 11, 2013 12:17PM
Thanks, recieved new chip and replaced, unfortunately the issue is still there.
This is the code that fails
function RT()
dim a,b //local values
b = 0 //set b = 0
print "1 " // ** for testing **
for a = 1 to 500
print "2 " // ** for testing **
b = b + adc_get(2) // get avg
print "3 " // ** for testing **
next
print "4 " // ** for testing **
......................etc

If I clear the flash, download Rookie & save, then download my code & save. At this point if I run main() it all works, but if I reset then it gets as far as the "print "2 " line and stops. This is the output from BV-COM
main()
Hottub
Sep 2013
1 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3
** for 500 times **
3 2 3 4
RT-C (means function completed)
**and it repeats, but if I reset then **
Text input Buffer 83 allocated Baud 115200

System cold start

Hottub
Sep 2013
1 2

and this is as far as it goes.

How can I investigate this further ?
Re: Running my progam from Flash
September 11, 2013 07:02PM
Bruce,
have you initialised the ADC as instructions on this page?

E.g adc_init(2) somewhere in your start up code?

Jim
Re: Running my progam from Flash
September 11, 2013 07:25PM
Yes, there is a section where I setup all the I/O ports,
// Setup ADC ports RB0,1,2
adc_init(2) //Solar Temp sensor
adc_init(3) //Tub Temp sensor
adc_init(4) //Outside Temp sensor

But does this need to be called as part of a function ?
I know the lines get read and executed upon downloading them, but when they have been saved to flash, do they get read again when you perform a power reset ?
Re: Running my progam from Flash
September 11, 2013 07:43PM
Bruce,
as long as the set up is run before the function.

The fault is not with the function RT() as this code works perfectly well when saved to flash, I have just tried it:

function RT()
dim a,b //local values
    b = 0 //set b = 0
    print "1 " // ** for testing **
    for a = 1 to 500
        print "2 " // ** for testing **
        b = b + adc_get(2) // get avg
        print "3 "
    next
    print "4"
endf


function main()
    adc_init(2)
    RT()
endf

It must be with some other function in your code. You need to find out which function is causing the problem by removing functions from your code. When you have the function that is causing the problem, i.e something like the above dosent work, then post it so it can be fixed.
Re: Running my progam from Flash
September 11, 2013 07:56PM
To clarify, I wrote the code following 'basic' thoughts, so my code looks something like this,
//------------------- setup lots of global variables
dim D1,D2,D3,D4,L,DT2,DT3,DT4 //D-digit, LedStatus
L = 0 // LEDs off **etc
//------------------- Setup all I/O ports
adc_init(2) //Solar Temp sensor **etc
//------------------- define all functions
function RT() **etc
endf
//------------------- run main()
function main()
rt()
endf

Using my code none of the variables or init commands are setup via a function. I know that they get executed when I download the code to the BV500, but do they also get executed when a power reset occurs??
Re: Running my progam from Flash
September 12, 2013 05:56AM
Bruce,
when you define a global variable it then needs to be initialised, you normally do this by having an initialise function so that on reset it can be called. In nearly all programs there needs to be a known starting condition. It is easy to forget that you may have initialised a variable by hand that will not be initialised at start up.

In general it is best to avoid global variables and use parameters instead for passing information to a function but this may not always be possible

As an example
// initialise 
function init()
    a=1
    b=2
    c=3
    adc_init(2)
endf

main ()
    init() // call init first
    …..
endf

Re: Running my progam from Flash
September 12, 2013 07:36PM
In your example, a number of variables have values set, but where do you put the dim statements ???
If I understand correctly, if I put the dim statements in a function, then they will only exist in that function (and any function that it calls) - is that correct ??

I tried moving all of the dim, value and port set statements into the start of the main() function, but then the issue is that some of the functions prior to main() have variables that haven't been specified yet, so the download fails. Is there a way to save the *.bas file directly to flash, or at least to not have it interperted ??
Sorry, you do not have permission to post/reply in this forum.