![]() |
Forum Index : Microcontroller and PC projects : Using VAL with PWM
Author | Message | ||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Hi I posted a question about Lookup tables earlier this is not a Bump but a code question I am sort of getting somewhere with generating tones using the table, I have converted this { 1,15, 9}, { 1,15,10}, { 1,15,11}, { 1,15,12}, { 1,15,13}, { 1,15,14}, { 1,15,15}, { 2, 8, 8} to this DIM A(8) AS STRING DATA "1,15, 9"," 1,15,10","1,15,11","1,15,12","1,15,13","1,15,14","1,15,15"," 2, 8, 8" FOR B = 0 TO 7 READ (A)B NEXT B What I need to do is use the 3 numbers in each element ie: "1,15,9" to generate sequential tones, then add a fixed delay before moving onto the next element, this is what I have tried DO FOR B= 0 TO 7 PWM 1, 2500, VAL(A(B))*5 PAUSE 64 NEXT B My initial problem is that only reads the first character in the sequence not all three And maybe a bigger problem is that I cannot DIM the complete array which is 256 elements Thanks Jed |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10189 |
Your first problem is the READ statement - it should be READ A(B) Then you need to parse the strings to extract the three numbers. Have a look at the INSTR function. Use it to find the commas and then use LEFT$, MID$ and RIGHT$ to get the individual numbers for the VAL function. I could give you the code but it is better if you try yourself and ask for more help if needed ![]() |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Thanks matherp My sentiments exactly I don't want it done for me that achieves 0 |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10189 |
Actually looking at your data again if you used leading spaces in the strings (as in the C code) to make all the strings the same length you wouldn't need INSTR and could just use LEFT$, MID$ and RIGHT$ because you would know where the commas are. |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Also look at LENGTH when dimensioning an array. Now each string will reserved 256 bytes using too much memory A length of 8 would be sufficient. Microblocks. Build with logic. |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Ok maybe a bit untidy but it works do for B = 1 to 120 vcode1$ = LEFT$(mid$(A(B), 1), 2) vcode2$ = left$(mid$(A(B), 4), 2) vcode3$ = right$(mid$(A(B), 1), 2) pwm 1, 50000, val(vcode1$)*6 pause 64 pwm 1, 50000, val(vcode2$)*6 pause 64 pwm 1, 50000, val(vcode3$)*6 pause 64 next B loop Now to sort the arrays out Ill will have to do that TZAdvantage |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Tried Dimensioning the array with DIM A(256) LENGTH 8 Error: Not Enough Memory |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10189 |
Try: DIM STRING A(256) LENGTH 8 works for me uses 3K memory as expected |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
That worked :) |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Actually I was trying DIM A(256) AS STRING LENGTH 8 That didd'nt work |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4033 |
If you took the quotes out of the DATAs you could simply use READ and drop all the strings. (In effect it's like using INPUT but the value comes from DATA.) John |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
That would work good too. Storing it would use more space as each value is then at least 4 bytes in memory. Combining the 3 values into one would save some. [code] READ A READ B READ C Values(count) = A + B * 16 + C * 256 [/code] But going through that exercise is only worth it when memory gets scarce or speed of execution is not fast enough. Maybe use DIM STRING A(256,3) LENGTH 1. That will use only about 1200 bytes of memory and might be easier to work with as it has the same structure. Each value can then be stored with CHAR() and retrieved with ASC(). In the newest version arrays can be initialized directly. That would save even more as no more data statements are necessary. Another solution would be to treat the DATA statements as the storage and use READ statements in the loop, and then use RESTORE to reset reading from the top. This would be the most memory efficient as it will not be stored in RAM (is stored along the program in flash) with some sacrifice in speed and usability. Microblocks. Build with logic. |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
(In effect it's like using INPUT but the value comes from DATA.) John Thanks JohnS The way DominoEX16 (earlier post) works means that the array must stay intact as each character consists of 3 nibbles ie: the character < ASCII 60 is "0,8, 10" |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Combining the 3 values into one would save some. [code] READ A READ B READ C Values(count) = A + B * 16 + C * 256 [/code] But going through that exercise is only worth it when memory gets scarce or speed of execution is not fast enough. Maybe use DIM STRING A(256,3) LENGTH 1. That will use only about 1200 bytes of memory and might be easier to work with as it has the same structure. Each value can then be stored with CHAR() and retrieved with ASC(). In the newest version arrays can be initialized directly. That would save even more as no more data statements are necessary. Another solution would be to treat the DATA statements as the storage and use READ statements in the loop, and then use RESTORE to reset reading from the top. This would be the most memory efficient as it will not be stored in RAM (is stored along the program in flash) with some sacrifice in speed and usability. |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Combining the 3 values into one would save some. [code] READ A READ B READ C Values(count) = A + B * 16 + C * 256 [/code] But going through that exercise is only worth it when memory gets scarce or speed of execution is not fast enough. Maybe use DIM STRING A(256,3) LENGTH 1. That will use only about 1200 bytes of memory and might be easier to work with as it has the same structure. Each value can then be stored with CHAR() and retrieved with ASC(). In the newest version arrays can be initialized directly. That would save even more as no more data statements are necessary. Another solution would be to treat the DATA statements as the storage and use READ statements in the loop, and then use RESTORE to reset reading from the top. This would be the most memory efficient as it will not be stored in RAM (is stored along the program in flash) with some sacrifice in speed and usability. Should DIM STRING A(512,3) LENGTH 1 work as the arrays need to be indexed together I am getting an error on this line FOR B = 0 TO 511 READ A(B) the error occurs here "error number of dimensions" NEXT B |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6266 |
If you create a multidimensional array, you need to specify all dimensions each time. OPTION BASE 0 DIM STRING A(512,3) LENGTH 1 FOR B = 0 TO 511 READ A(B,0) READ A(B,1) READ A(B,2) NEXT B AS you are using a string array, the data statements need to be single length strings, which could be difficult, or you need to convert the data from decimal to strings with chr$() something like: OPTION BASE 0 DIM STRING A(512,3) LENGTH 1 FOR B = 0 TO 511 READ X, Y, Z A(B,0) = CHR$(X) A(B,1) = CHR$(Y) A(B,2) = CHR$(Z) NEXT B Jim VK7JH MMedit |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
Ok I am a little stuck at this point So far I have sorted out sending the tones using PWM, and I have extracted the ASCII values from my string that I need to transmit I think the table DIM is going to work The next step is to match my ASCII value to the varicode table and send the three tones with a 64ms delay, here is a reference http://www.qsl.net/zl1bpu/MFSK/DominoEX%20Varicode%20Table.p df Is it possible for instance to count the segments according to the ASCII value ie: E = 69 so count 68 then 256 so that the 2 tables are read then another 64ms delay and so on Jed |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
I have made some progress:) I can successfully read the tables in the correct sequence The problem I am having now is that when I read the nibbles 3,8,0 like this syma = val(LEFT$(mid$(A(txtchr), 1), 2)) symb = val(left$(mid$(A(txtchr), 4), 2)) symc = val(right$(mid$(A(txtchr), 1), 2)) It retains the trailing 0 likewise for 0,10,8 It retains the leading 0 Is there a way I can read the table and remove any single 0's |
||||
edu001 Regular Member ![]() Joined: 14/07/2014 Location: United KingdomPosts: 82 |
I can successfully read the tables in the correct sequence The problem I am having now is that when I read the nibbles 3,8,0 like this syma = val(LEFT$(mid$(A(txtchr), 1), 2)) symb = val(left$(mid$(A(txtchr), 4), 2)) symc = val(right$(mid$(A(txtchr), 1), 2)) It retains the trailing 0 likewise for 0,10,8 It retains the leading 0 Is there a way I can read the table and remove any single 0's Just reading up on DominoEX16 if the 0 is the first of the 3 nibbles it has to stay |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |