Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:26 17 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM basic (on raspbery pico)

     Page 2 of 2    
Author Message
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4061
Posted: 01:12pm 11 Aug 2025
Copy link to clipboard 
Print this post

  EXAL81 said  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).

I took a quick look.

Yes it documents them that way but it looks like they're not wanted as printable chars but as the binary equivalent, e.g. for 0x36 (or &h36) you wouldn't send a 3 then a 6 but instead send a single byte with the appropriate bit pattern.

So you're not really wanting to have a string and then convert to a number.

Instead you can use a number as a literal in your program.

Conceptually, send_spi &h36 (but not send_spi "&h36").  MMBasic will then convert the &h36 for you.  You could of course use 54 but if you want the program to look more like the data sheet then yes use the notation &h36.

You may not even need the likes of BIN2STR.

You can split (into individual byes) a 32-bit value (if you need to) using the shift operator (>>) as previously posted.

John
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 18
Posted: 04:34pm 11 Aug 2025
Copy link to clipboard 
Print this post

i found some sample code in the net.



Ok it is in 'C' but I think you can convert it to BASIC.

Part of /scr/LoRa.c
[CODE
// Set RF frequency
void SetRfFrequency(void) {
   pllFrequency = frequencyToPLL(freq_hz);
   NSS = 0;
   Spi_Write_Byte(0x86);
   Spi_Write_Byte(0x08);
   Spi_Write_Byte(0x60);
   Spi_Write_Byte((pllFrequency >> 24) & 0xFF);
   Spi_Write_Byte((pllFrequency >> 16) & 0xFF);
   Spi_Write_Byte((pllFrequency >> 8) & 0xFF);
   Spi_Write_Byte(pllFrequency & 0xFF);
   NSS = 1;
}
[/CODE  

exchange only '0x' to '&H' and an add a varible for the function res=SPI(&H86)
Albert
 
AlbertR
Newbie

Joined: 29/05/2025
Location: Germany
Posts: 18
Posted: 04:37pm 11 Aug 2025
Copy link to clipboard 
Print this post

sorry with the link, was my first time here

URL=https://github.com/DiveshRDH/8051-LoRa-LLCC68
Edited 2025-08-12 02:38 by AlbertR
 
EXAL81
Newbie

Joined: 05/08/2025
Location: United Kingdom
Posts: 6
Posted: 03:20pm 12 Aug 2025
Copy link to clipboard 
Print this post

Yes the values have to be in 8 bit binary not ASCII chars.  If I send the hex byte &H36, it is correctly interpreted by MM.Basic and sent as an 8 bit binary pattern.
This OK if you already now what the HEX value is, but in my case I don't.
There are many bytes to be sent to the device, most of which are basically set up items (which will not need changing during device operation) I can choose the values to get the set up I require, so already know the values to use, without have to covert them all to decimal and, they will be easy to check against the device data sheet.
But because I may want to alter the RF frequency during operation, I will have to calculate a new frequency setting value and and use decimal notation to get over the difficulty of not being able to convert my new value to hex. This does have the advantage of not needing to do any conversion of my new value to a different system.
Instead of sending say; &H36 I can send 54 and the receiving device will see the same binary pattern.  
I have written a new bit of code that works just fine. Partly based on a suggestion from Mozzie to use the PEEK function.

OPTION explicit
option default integer
DIM Frval = 909377536
Dim Fbytes(4)
DIM Fromspi
DIM iPK
DIM iBF = 4
FOR iPK = 0 to 3
FBYtes(iBF) = PEEK(VAR Frval,iPK) 'Offset  0 is for lowest byte
iBF = iBF -1
NEXT
Fbytes(0) = &H86 'add command code byte
For iBF = 0 to 4
Print Fbytes(iBF) 'see if it works
NEXT
'YEP
END
Oh, The "fromspi" is a left over from trying the spi system, fogot to delete it
Thank you all for your time and help.
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1437
Posted: 04:53pm 12 Aug 2025
Copy link to clipboard 
Print this post

More efficient:

OPTION explicit
option default integer
DIM Frval = 909377536
Dim Fbytes(4)

Fbytes(0)=&h86:print Fbytes(0)
Fbytes(1)=(Frval>>24 and &hff):print Fbytes(1)
Fbytes(2)=(Frval>>16 and &hff):print Fbytes(2)
Fbytes(3)=(Frval>>8 and &hff):print Fbytes(3)
Fbytes(4)=(Frval and &hff):print Fbytes(4)
 
EXAL81
Newbie

Joined: 05/08/2025
Location: United Kingdom
Posts: 6
Posted: 09:32am 13 Aug 2025
Copy link to clipboard 
Print this post

Thanks PhenixRising,
Yes you are correct, so I will use a slightly modified version of yours (removed multiple print statements). I only used the print to check that it all works!
When loaded, this version used 32 bytes less than my original code.

OPTION explicit
option default integer
DIM Frval = 909377536
Dim Fbytes(4)
Dim i
Fbytes(0)=&h86
Fbytes(1)=(Frval>>24 and &hff)
Fbytes(2)=(Frval>>16 and &hff)
Fbytes(3)=(Frval>>8 and &hff)
Fbytes(4)=(Frval and &hff)
For i = 0 to 4
print Fbytes(i)
NEXT
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025