![]() |
Forum Index : Microcontroller and PC projects : MM basic (on raspbery pico)
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
EXAL81 Newbie ![]() Joined: 05/08/2025 Location: United KingdomPosts: 6 |
I am new here so please pardon any goofs ! I have a project that requires me to convert a 32 bit unsigned inter value to 4 hex bytes. So far I have tried this: DIM FRVAl = 909377536 ' a typical value for my project DIM fbytes$ (4) DIM FBVAL DIM I = 4 DO WHILE i =>1 NEXT line Reasoning that FRVAL has the value in binary, If try to use FBVAL = FRVAL AND &Hff to mask off all but the LS BYTE, I get an error . But if I use FRVAL AND 255 ( 255 like &HFF is all ones) it works ! NEXT lines Fbytes (i) = HEX$(FBVAL,2) ' convert byte to hex FRVAL = FRVAL >>8' shift to right for next byte i = i-1 LOOP I end up with the four correct bytes (36 34 00 00) but in string format, but i need them to be &H format. I can find no way to do this as most MM basic functions return strings. Please HELP |
||||
Martin H.![]() Guru ![]() Joined: 04/06/2022 Location: GermanyPosts: 1260 |
Fbytes$ (i) = "&H"+HEX$(FBVAL,2) ' convert byte to hex youll get four Strings (&H36 &H34 &H00 &H00) apart from that, your listing doesn't work.... 2 times NEXT but not a single FOR Edited 2025-08-09 00:54 by Martin H. 'no comment |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5138 |
It can be simpler, but this is straight forward. FRVAL=909377536 value_in_hex$ = hex$(FRVAL,8) 'change to hex, 8 chars long (leading digits 0) MSB$="&H"+left$(value_in_hex$,2) 'MSB is &Hxx UMB$="&H"+mid$(value_in_hex$,3,2) 'UMB is &Hxx LMB$="&H"+mid$(value_in_hex$,5,2) 'LMB is &Hxx LSB$="&H"+right$(value_in_hex$,2) 'LSB is &Hxx print value_in_hex$ print MSB$,UMB$,LMB$,LSB$ Volhout Edited 2025-08-09 01:01 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 8000 |
... and using LINE as a variable when it's a reserved word. :( Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5138 |
But when you are only interested in numbers, not in the formatting, you can also calculate them. FRVAL = 909377536 MSB = FRVAL and &HFF000000 UMB = FRVAL and &HFF0000 LMB = FRVAL and &HFF00 LSB = FRVAL and &HFF And for compacter code you can also do this in a loop. Volhout Edited 2025-08-09 01:05 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4061 |
If you just want 4 8-bit values as numbers rather than strings: FRVAL = 909377536 MSB = (FRVAL and &HFF000000) >> 24 UMB = (FRVAL and &HFF0000) >> 16 LMB = (FRVAL and &HFF00) >> 8 LSB = FRVAL and &HFF Or you can shift and then mask. A "hex" number is the same as a non-hex number because a number is a number. A "hex" literal in a BAS program needs the &H so the interpreter understands what you want and allows A-F as well as 0-9. John Edited 2025-08-09 01:24 by JohnS |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4061 |
In case you have a string containing such as &Hff e.g. x$ = "&hff" you can use VAL() to get the number: i = val(x$) John |
||||
EXAL81 Newbie ![]() Joined: 05/08/2025 Location: United KingdomPosts: 6 |
Thanks for the speedy replies. Sorry for the confusion, but the NEXT and LINES in my post were just comments used to try and explain my problem, not actually code that I use in my program. I did try to use "Fbytes$(i) = &H + HEX(FBVAL,2)", but this just returns a string, and if I try to send this to the device that I am trying to control via the SPI interface, using the following expression; ReadSP = SPI (Fbytes$(i)). Not surprisingly I get "ERROR NUMBER EXPECTED". Maybe I will have to write my own conversion routine ! |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10350 |
BIN2STR is designed specifically for this sort of application |
||||
mozzie Senior Member ![]() Joined: 15/06/2020 Location: AustraliaPosts: 171 |
G'day EXAL81, If I read this correctly, you are chasing the 4 bytes as binary (hex) values from the Integer FRVAL? Not sure why FRVAL AND &HFF is not working for you, it works here on multiple platforms. This is basically your code, tested and works: Option default integer Dim FRVAL = 909377536 Dim Fbytes(3) Print Hex$(frval) For i = 0 To 3 Fbytes(i)=FRVAL And &hff FRVAL = FRVAL >> 8 Next For i = 3 To 0 Step -1 ' to print in correct order Print Hex$(Fbytes(i),2); Next End This is a quicker way, you can directly access bytes in an integer using PEEK(VAR var,offset) remember offset is 0 for lowest byte. It also doesn't alter the original value. Dim FRVAL = 909377536 Dim Fbytes(3) For i = 0 to 3 Fbytes(i)=peek(var FRVAL,i) next For i = 3 To 0 Step -1 ' to print in correct order Print Hex$(Fbytes(i),2); Next End I'm sure there are better ways ![]() Regards, Lyle. Edited 2025-08-09 02:18 by mozzie |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 8000 |
Numbers are numbers but you can view them in different ways. They are merely values. a%=10 'a% is an integer print hex$(a%,2), oct$(a%,3), bin$(a%,8) 0A 012 00001010 They are all decimal 10 but have been commanded to display as strings. The value is identical in all cases. You can use: a = &hA print a 10 because the system will convert values (in this case a is floating point) to decimal by default unless you convert them to string format. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4061 |
I wish you'd said you want to use SPI. Something like BIN2STR is likely what you want. Perhaps you could say more about what you're trying to do. John |
||||
AlbertR Newbie ![]() Joined: 29/05/2025 Location: GermanyPosts: 18 |
I think the shortest version will be with MEMORY UNPACK Dim integer FRVA = 909377536 Dim integer Fbytes(3) Memory unpack Peek(varaddr FRVA),Fbytes(),4,8 because of the Error message "ERROR NUMBER EXPECTED" The function SPI only accepts integer or floating point The '$' at the end of your array 'Fbytes$(i)' explicit shows it is a string. Albert |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 8000 |
These values are correct, they are the 2-character slices of "36340000". SPI always sends and receives in bytes, no matter what the data is. MMBasic can send those bytes as numeric, an array or as a string. You have to know how many bytes are going to be sent and received beforehand. You could send using SPI WRITE 8, "36340000" and it would try (note that you have to include the number of characters in the string), but the other end may not be able to read it! You have to be very specific about what the device is expecting, including whether it is expecting LSB or MSB first as this varies. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
EXAL81 Newbie ![]() Joined: 05/08/2025 Location: United KingdomPosts: 6 |
OOPS I made some mistakes in my original post! Notably, the missing line "FBVAL = FreqVAL AND &HFF" which now works as expected, I don't know why. The command code byte in Fbytes(0) tells the device that I am controlling what the following bytes are for (which have to be in the &HNN form). SO here is the more complete (experimemtal code) Thanks for the many helpful suggestions. OPTION explicit option default integer DIM Freqval = 909377536 DIM Fbval Dim Fbytes$(4) Dim i = 4 DIM Fromspi DO while i => 1 Fbval = Freqval AND &HFF 'mask off all but LSB Fbytes$(i) = "&H" + HEX$(Fbval,2) 'Convert byte to HEX Freqval = Freqval >>8 'right shift for next byte i = i-1 loop Fbytes$(i) = "&H86" ' insert command code byte Do while i <= 4 print Fbytes$(i) i = i+1 loop SETPIN GP18, GP19, GP16, SPI SPI open 500000,0,8 'spi = 500KHZ clk, mode0, 8 bits Fromspi = SPI(FBYTES$(1))'send BYTE 1 (= &H36) spi close END |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4061 |
I am nearly sure that Fbytes$(i) = "&H" + HEX$(Fbval,2) 'Convert byte to HEX is not what you want. You will get 4 chars: & then H then 2 more hex ones. Perhaps you can explain what the device is that you're trying to use SPI with and what it expects to be sent. John |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 8000 |
MMBasic doesn't have HEX, BINARY or OCTAL variables. All numeric variables are decimal stored as signed 64-bit numbers. &H, &B and &O, together with their reverse string functions HEX$(), BIN$() and OCT$() are only ways of viewing the data. The data isn't transferred using them. You can't actually send the characters &H36 as a number over SPI. & is not a number H is not a number 36 is a number You can send the 4-character string over SPI but it will send it as four ASCII characters: 38 72 51 54. This might well be what's expected. If you want to send &H36 you will have to send its value VAL(&H36), which is 54 (the decimal value). Once again, you have to be absolutely specific in every way about what the receiving device is expecting. e.g. MODBUS data can be binary or ASCII - but it's the same data and would be sent over the same sort of interface. Both ends have to know what the other will do. You can't assume anything that isn't on the data sheet. :) . Edited 2025-08-09 23:53 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
AlbertR Newbie ![]() Joined: 29/05/2025 Location: GermanyPosts: 18 |
Hello EXAL81, it looks like we spin round. It would be the best you tell us more about the device you want to control. Fbytes$ is an array of strings! "option default integer" is not activ if variable ends with '$' You insert a command code byte to Fbytes$(i) = "&H86" ' is a string no number What did you do with this? Should it be send too? Why such a long extraxtion from "DIM Freqval = 909377536" when you use only the LSB? The function SPI() sends only the specified 8 bit you defined when OPEN and get only 8bit back! Perhaps you are looking for the spi-bulk send /receive. Albert |
||||
EXAL81 Newbie ![]() Joined: 05/08/2025 Location: United KingdomPosts: 6 |
YES I think you are correct, we are going in circles. In my My first post I was asking if anyone new of a method of converting a string to a number in HEX. My only reason for using HEX is that the data sheet for the device that I am using gives all of the command codes and parameters in Hex (they actually give them in 0Xnn form as used in many other languages). The commands can vary from just a command code to a command code followed by 9 parameter bytes. IF I just wanted to use the same RF Frequency, I could just tend everything to the device in the &Hnn form, but I made things more difficult for myself by including the facility to chose one of 10 Rf channels. This means that when a new channel is selected, I have to calculate the frequency setting value for the new channel and send it to the device in HEX As this does not appear to be possible in MMBASIC I will send all of the other codes etc, in HEX, and send 4 number bytes for the frequency value. This possible because sending the byte &H36 to the device result in binary 00110110 being trasmitted by SPI interface and as the number corresponding to &H36 is 54 (which gives the same binary code), I can just send 54. This will greatly simplify things. I Was able to use an oscilloscope to observe t that the above mention binary value was sent by the SPI. If anyone is interested the device is an RF solutions LAMDA62 which uses A Semtech SX1262. The data sheet is at WWW.Semtech.com/products/wireless-RF/lora-connect/SX1262. You should see a big "DATA SHEET" button on the right hand side. But be aware that it is 100 pages long. Thanks for all your patient efforts |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1437 |
What is it about STR2BIN that doesn't work here? |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |