writing an auto ranging program
Elkin
Posts: 58
I am trying to write an auto ranging program that can tell a little conductivity detector to switch its parameters (frequency, excitation voltage, and sampling range) using the protocol already in place.
Here is a little example of what I have in mind. The issue that I have is that I need to tell it when to change the values on channel 0 while also sending the 3 proceeding strings only one time. The way it is currently written, strings will be sent continuously and it won't work like that. I was thinking that there could perhaps be some type of repeat loop added here or maybe I have to use a different conditional command than IF (maybe using UNTIL). Any ideas?
Here is a little example of what I have in mind. The issue that I have is that I need to tell it when to change the values on channel 0 while also sending the 3 proceeding strings only one time. The way it is currently written, strings will be sent continuously and it won't work like that. I was thinking that there could perhaps be some type of repeat loop added here or maybe I have to use a different conditional command than IF (maybe using UNTIL). Any ideas?
CON _clkmode = XTAL1 + PLL16X ' Crystal and PLL settings _xinfreq = 5_000_000 ' 5 MHz crystal rx = 30 'tx on Isopod rx on prop (green) tx = 1 'rx on Isopod tx on prop (white) #1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS ' PST formmatting control DAT crlf byte CR, LF, 0 OBJ term: "fullduplexserial" PUB Detect | time term.start(Rx, Tx, 00, 115_200) DIRA[rx]~ DIRA[tx]~~ time := cnt term.str(string(CR, CR,)) waitcnt(time += 400_000_000) term.str(string("set range 0.02 0.2 490", CR)) waitcnt(time += 400_000_000) term.str(string("set k 160", CR)) waitcnt(time += 400_000_000) term.str(string("sample ascii 50", CR)) 'sample ascii at 50 Hz IF ((ch0 > 0.01) AND (ch0 =< 0.1)) term.str(string("!",CR)) 'stop current sampling to reset range term.str(string("set range 0.2 0.2 990", CR)) 'set parameters to new range values term.str(string("sample ascii 50",CR)) 'sample ascii at 50 Hz IF ((ch0 > 0.1) AND (ch0 =< 1)) term.str(string("!",CR)) term.str(string("set range 2 0.02 990", CR)) term.str(string("sample ascii 50",CR)) IF ((ch0 > 1) AND (ch0 =< 10)) term.str(string("!",CR)) term.str(string("set range 20 0.2 9990", CR)) term.str(string("sample ascii 50",CR)) IF ((ch0 =< 0.01)) term.str(string("!",CR)) term.str(string("set range 0.02 0.2 490", CR)) term.str(string("sample ascii 50",CR))
Comments
I enjoy reading the forums, but I don't have a lot of real programming time. I looked at your code , did not try running it, it will only operate once because there is no LOOP instruction.
I added a REPEAT command that should put your machine into an infinite loop.
I planned to show your code with the added REPEAT command, but I cannot get the code to display correctly.
repeat
time := cnt
indention are important.
Can you descibe in more detail what you want your code to do. It would help if you walked through it step by step and stated what you want it to do.
The flash memory program that I want to use for this is not written yet but I am most likely going to use something like this:
I am still working on writing the mux bit program which will be (EDAQ.detect)
You want to send a command to change the range but only once.
To do this, your program needs to remember what the previous range was. This is a very common need in computer/uC programming.
Setup some constants to define ranges.
I don't know if you've seen constants set up like this before or not. The above is just an easier way of writing:
It's important that each constant have a different value.
Setup two variables to keep track of the range. Since these varaiables are only going to have the values of 0 through 4, I'll use byte variables.
Early in the program you would set "previousRange" to a value outside of the possible values of ranges to make sure the first range read is detected as a new range.
As usual with programming, there are more than one way of doing this.
Is this what you're trying to do?
This seems like it is exactly what I am trying to do. I have used constants like this before as well so this really isn't anything new, I just couldn't visualize it before so thanks for that! Now I just have to get it to save on to the memory card!
here below: I know this is not going to work directly but I know it is something like this.
Another alternative is to have a separate cog updating the ch0 value.
I don't understand your second section of code in post #9. Does "read_adc(@ch0)" return the number of times you want the loop repeated? Even if this is the case, I don't think it's a good idea to use a method call as a parameter of repeat since (as Phil pointed out) Spin calls the method each time it loops. There are times calling methods as a repeat parameter would be okay, but I think they all have a while or until added to the repeat statement to indicate when to exit the loop based on the return value of the method call.
Here is the EDAQ.spin code that I have updated. I have changed the code from post #9 to fit into this code better and tried to make it easier to understand. It would probably be just as easy to put the code that reads ch0 directly into the main loop that includes the if/ if else tests but here it is with the main loop calling the read_ch0 code. I am not really sure I understand the difference between having the repeat loop call a method with the if/ if else tests from the read_ch0 code. if it does make a difference as you mentioned then that really wouldn't be that difficult to change.
Thanks for the help!