Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:59 05 Dec 2026 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 : MicroMag3

Author Message
geodav
Newbie

Joined: 09/05/2014
Location: Australia
Posts: 15
Posted: 02:44am 10 Nov 2026
Copy link to clipboard 
Print this post

Hi brains trust,  
trying to interface a micromag3 magnetic sensor in MMBasic

This is an Altronics Z6300 purchase, aro Jim Rowes article in Silicon Chip Mag (June 2024 p64), which was encoded in Arduino language which I have not studied as
I  tend to a-{void} it,
However the module requires a reset  on pin 6 to clear previous data,  and an SPI interface at 9600 baud, with a 40 mS delay following its data request,
as Jim mentioned
followed by DOUT request for, respectively x,y,z magnetic fields as
byte Cmdx = B01100001 , and for Y ....01 and Z ....011.
followed by 16 bit data in...
Question is:  what code in MMBasic is required ?
Its ok I know the Pins
Pin x, high 1 for 10 uS ?
Open "Com1:9600, 9 bit" as #1   ?
and so on.
Not quite sure, and struggling here

regards
Geodav ( a newbie)
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1147
Posted: 05:35am 10 Nov 2026
Copy link to clipboard 
Print this post

Hi GeoDav,

Welcome to TheBackShed forum.  To assist you, can you say what MicroMite you plan to use to talk to the MicroMag3 and also what version of MMBasic you have installed (there are some minor differences in the SPI usage between versions and models of MicroMite/PicoMite)? Also, does your MicroMite have a display and if so, what type?

Doug

The following is a translation by Gemini - it should be close to what you need - you will need to adapt it to your particular platform and MMBasic version.


' ==============================================================================
' MicroMag3 Magnetic Sensor Control Program
' Translated to MMBasic for Micromite / Maximite / PicoMite
' Based on Silicon Chip June 2024 (Jim Rowe) and September 2024 updates
' ==============================================================================

Option Explicit

' Define Pin Numbers (Adjust these to match your hardware layout)
Const pinSSNOT  = 1    ' Slave Select / Chip Select (Output)
Const pinRESET  = 2    ' Reset Line (Output)
Const pinDRDY   = 3    ' Data Ready Line (Input)
' Note: Hardware SPI pins (SDI, SDO, SCK) are automatically managed by SPI OPEN.

' Global tracking variables
Dim x_val, y_val, z_val
Dim heading_deg
Dim PI : PI = 4 * Atn(1)

' Initialise I/O Pins
SetPin pinSSNOT, DOUT : Pin(pinSSNOT) = 1 ' Set High (Idle)
SetPin pinRESET, DOUT : Pin(pinRESET) = 0
SetPin pinDRDY, DIN

' Initialise Hardware SPI channel
' Configures SPI Mode 0, Master, 8-bit data, and a safe clock speed (e.g., 100kHz)
SPI Open 100000, 0, 8

Print "MicroMag3 MMBasic Controller Initialised."
Print "-------------------------------------------"

' Main Executive Loop
Do
 x_val = ReadAxis(1) ' Read X-Axis (Command offset 1)
 y_val = ReadAxis(2) ' Read Y-Axis (Command offset 2)
 z_val = ReadAxis(3) ' Read Z-Axis (Command offset 3)
 
 heading_deg = GetHeading(x_val, y_val)
 
 Print "X: "; x_val; " | Y: "; y_val; " | Z: "; z_val; " | Heading: "; heading_deg; " deg"
 
 Pause 2000 ' Update rate (2 seconds)
Loop

' ==============================================================================
' FUNCTION: ReadAxis
' Pulses the MicroMag3, sends the axis command, and extracts 16-bit signed data
' ==============================================================================
Function ReadAxis(axisNum)
 Local cmdByte, highByte, lowByte, rawWord
 
 ' Formulate the command byte based on the desired axis:
 ' Core measurement control configuration: division ratio /2048 (Bits: 01100)
 ' End bits map: X=01 (1), Y=10 (2), Z=11 (3)
 If axisNum = 1 Then
   cmdByte = &B00110001 ' X Axis
 ElseIf axisNum = 2 Then
   cmdByte = &B00110010 ' Y Axis
 Else
   cmdByte = &B00110011 ' Z Axis
 EndIf
 
 ' 1. Cycle Reset to begin measurement sequence
 Pin(pinRESET) = 1 : Pause 2 : Pin(pinRESET) = 0 : Pause 2
 
 ' 2. Select the sensor chip
 Pin(pinSSNOT) = 0 : Pause 2
 
 ' 3. Transmit the command byte via SPI
 SPI Write 1, cmdByte
 
 ' 4. Wait for the Data Ready line to clear/assert or fall back on a safe timeout
 ' Alternatively, if bypassing IO6 as per Silicon Chip, use a hard delay (e.g., Pause 15)
 Local timeout = 0
 While Pin(pinDRDY) = 0 And timeout < 500
   timeout = timeout + 1
   Pause 1
 Wend
 
 ' 5. Retrieve 16 bits of measurement data (2 Bytes)
 highByte = SPI(0)
 lowByte = SPI(0)
 
 ' Deselect the sensor
 Pin(pinSSNOT) = 1
 
 ' Combine bytes into a raw 16-bit integer
 rawWord = (highByte * 256) + lowByte
 
 ' Handle 16-bit two's complement sign conversion
 If rawWord >= 32768 Then
   ReadAxis = rawWord - 65536
 Else
   ReadAxis = rawWord
 EndIf
End Function

' ==============================================================================
' FUNCTION: GetHeading
' Implements a manual 4-quadrant arctan safely using the standard MMBasic ATN()
' ==============================================================================
Function GetHeading(x, y)
 Local angle
 
 ' Trap zero denominators cleanly to avoid division-by-zero math errors
 If x = 0 Then
   If y < 0 Then angle = PI / 2
   If y >= 0 Then angle = 3 * PI / 2
 Else
   ' Basic single-variable arctan
   angle = Atn(y / x)
   
   ' Quadrant correction logic matching the module's magnetic framework
   If x < 0 Then
     angle = PI - angle
   ElseIf x > 0 And y < 0 Then
     angle = -angle
   ElseIf x > 0 And y > 0 Then
     angle = (2 * PI) - angle
   EndIf
 EndIf
 
 ' Convert Radians to Degrees and normalise
 Local deg = angle * (180 / PI)
 If deg < 0 Then deg = deg + 360
 
 GetHeading = Int(degMod(deg))
End Function

' Helper to tightly clamp degrees inside a 0-359 compass bounds
Function degMod(d)
 Dim outD = d
 Do While outD >= 360 : outD = outD - 360 : Loop
 Do While outD < 0    : outD = outD + 360 : Loop
 degMod = outD
End Function
Use code with caution.

Essential Conversion Notes

1. SPI Implementation: Rather than bit-banging the individual pins, the translation implements MMBasic's native hardware SPI OPEN and SPI Write / SPI() stream functions. This reflects the design principles highlighted in the original Silicon Chip article.
2. Quadrant Math (ATN vs ATAN2): Because standard MMBasic does not natively expose a two-argument ATAN2 function, the GetHeading routine features safety boundaries for X = 0 along with explicit block filters to accurately isolate the four vector quadrants.
3. Data Handling: The code transforms raw High/Low incoming bytes back into standard signed variables using 16-bit two's complement logic explicitly parsed for standard BASIC limitations.
To tailor this exactly to your hardware setup, could you let me know:
• Which specific hardware platform are you using to run MMBasic (e.g., a custom Micromite, a Maximite, or a PicoMite)?
• Do you need to adjust the default SPI channel assignments or pin numbers inside the code to match how your module is wired?
Try without personalization
•
Silicon Chip
September 2024
September 9, 2024 — There is no ATAN2 function on the Micromite, so one has to use the ATN function and then work out which quadrant the heading lies ...
•
Geoff's Projects
MMBasic
MMBasic is a Microsoft BASIC compatible implementation running on hardware like Maximite (PIC32) and DOS, featuring floats, string...
•
MMBasic
BASIC Interpreter Overview - MMBasic
MMBasic is a full featured interpreter for the BASIC language. It's focus is on ease of use and rapid software development. This p...
Show all

Edited 2026-11-10 16:12 by panky
... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1147
Posted: 12:07am 11 Nov 2026
Copy link to clipboard 
Print this post

  geodav said  Hi brains trust,  
trying to interface a micromag3 magnetic sensor in MMBasic

This is an Altronics Z6300 purchase, aro Jim Rowes article in Silicon Chip Mag (June 2024 p64), which was encoded in Arduino language which I have not studied as
I  tend to a-{void} it,
However the module requires a reset  on pin 6 to clear previous data,  and an SPI interface at 9600 baud, with a 40 mS delay following its data request,
as Jim mentioned
followed by DOUT request for, respectively x,y,z magnetic fields as
byte Cmdx = B01100001 , and for Y ....01 and Z ....011.
followed by 16 bit data in...
Question is:  what code in MMBasic is required ?
Its ok I know the Pins
Pin x, high 1 for 10 uS ?
Open "Com1:9600, 9 bit" as #1   ?
and so on.
Not quite sure, and struggling here

regards
Geodav ( a newbie)


Further to my response above, can you advise what MicroMite platform you are using to talk to the MicroMag?

I have tested an updated version of the converted software (see below) and it runs on a Silicon Chip Packpack V2 (PIC32MX170 based) running MMBasic V5.05.05. As I don't have a MicroMag, I can't say if the SPI section is sending and receiving correctly.

If you want assistance in loading or testing the software any further, let me know.


'========================================================================
' MicroMag3 Magnetic Sensor Control Program
' Translated to MMBasic for Micromite / Maximite / PicoMite
' Based on Silicon Chip June 2024 (Jim Rowe) and September 2024 updates
'========================================================================

Option Explicit

' Define Pin Numbers (Adjust these to match your hardware layout)'
' - currently set for Sil Chip Backpack MicroMite running MMBasic V5.05.05
Const pinSSNOT  = 15    ' Slave Select / Chip Select (Output)
Const pinRESET  = 16    ' Reset Line (Output)
Const pinDRDY   = 17    ' Data Ready Line (Input)
' Note: Hardware SPI pins (SDI, SDO, SCK) are automatically managed by SPI OPEN.

' Global tracking variables
Dim x_val, y_val, z_val
Dim heading_deg

Dim outD

' Initialise I/O Pins
SetPin pinSSNOT, DOUT : Pin(pinSSNOT) = 1 ' Set High (Idle)
SetPin pinRESET, DOUT : Pin(pinRESET) = 0
SetPin pinDRDY, DIN

' Initialise Hardware SPI channel
' Configures SPI Mode 0, Master, 8-bit data,
'  and a safe clock speed (e.g., 100kHz)
SPI Open 500000, 0, 8

Print "MicroMag3 MMBasic Controller Initialised."
Print "-------------------------------------------"

' Main Executive Loop
Do
 x_val = ReadAxis(1) ' Read X-Axis (Command offset 1)
 y_val = ReadAxis(2) ' Read Y-Axis (Command offset 2)
 z_val = ReadAxis(3) ' Read Z-Axis (Command offset 3)
 
 heading_deg = GetHeading(x_val, y_val)
 
 Print "X: ";x_val;" | Y: ";y_val;" | Z: ";z_val;
 Print " | Heading: ";heading_deg;" deg"
 
 Pause 2000 ' Update rate (2 seconds)
Loop

' ==============================================================================
' FUNCTION: ReadAxis
' Pulses the MicroMag3, sends the axis command, and extracts 16-bit signed data
' ==============================================================================
Function ReadAxis(axisNum)
 Local cmdByte, highByte, lowByte, rawWord
 
 ' Formulate the command byte based on the desired axis:
 ' Core measurement control configuration:
 '   division ratio /2048 (Bits: 01100)
 ' End bits map: X=01 (1), Y=10 (2), Z=11 (3)
 If axisNum = 1 Then
   cmdByte = &B00110001 ' X Axis
 ElseIf axisNum = 2 Then
   cmdByte = &B00110010 ' Y Axis
 Else
   cmdByte = &B00110011 ' Z Axis
 EndIf
 
 ' 1. Cycle Reset to begin measurement sequence
 Pin(pinRESET) = 1 : Pause 2 : Pin(pinRESET) = 0 : Pause 2
 
 ' 2. Select the sensor chip
 Pin(pinSSNOT) = 0 : Pause 2
 
 ' 3. Transmit the command byte via SPI
 SPI Write 1, cmdByte
 
 ' 4. Wait for the Data Ready line to clear/assert or fall back
 '    on a safe timeout
 ' Alternatively, if bypassing IO6 as per Silicon Chip, use a
 '  hard delay (e.g., Pause 15)
 Local timeout = 0
 While Pin(pinDRDY) = 0 And timeout < 500
   timeout = timeout + 1
   Pause 1
 Wend
 
 ' 5. Retrieve 16 bits of measurement data (2 Bytes)
 highByte = SPI(0)
 lowByte = SPI(0)
 
 ' Deselect the sensor
 Pin(pinSSNOT) = 1
 
 ' Combine bytes into a raw 16-bit integer
 rawWord = (highByte * 256) + lowByte
 
 ' Handle 16-bit two's complement sign conversion
 If rawWord >= 32768 Then
   ReadAxis = rawWord - 65536
 Else
   ReadAxis = rawWord
 EndIf
End Function

' ==============================================================================
' FUNCTION: GetHeading
' Implements a manual 4-quadrant arctan safely using the standard MMBasic ATN()
' ==============================================================================
Function GetHeading(x, y)
 Local angle
 
 ' Trap zero denominators cleanly to avoid division-by-zero math errors
 If x = 0 Then
   If y < 0 Then angle = PI / 2
   If y >= 0 Then angle = 3 * PI / 2
 Else
   ' Basic single-variable arctan
   angle = Atn(y / x)
   
   ' Quadrant correction logic matching the module's magnetic framework
   If x < 0 Then
     angle = PI - angle
   ElseIf x > 0 And y < 0 Then
     angle = -angle
   ElseIf x > 0 And y > 0 Then
     angle = (2 * PI) - angle
   EndIf
 EndIf
 
 ' Convert Radians to Degrees and normalise
 Local deg = angle * (180 / PI)
 If deg < 0 Then deg = deg + 360
 
 GetHeading = Int(degMod(deg))
End Function

' Helper to tightly clamp degrees inside a 0-359 compass bounds
Function degMod(d)
 outD = d
 Do While outD >= 360 : outD = outD - 360 : Loop
 Do While outD < 0    : outD = outD + 360 : Loop
 degMod = outD
End Function
' Use code with caution.

' Essential Conversion Notes

' 1. SPI Implementation: Rather than bit-banging the individual pins, the
' translation implements MMBasic's native hardware SPI OPEN and
' SPI Write / SPI() stream functions. This reflects the design principles
' highlighted in the original Silicon Chip article.
' 2. Quadrant Math (ATN vs ATAN2):
' Because standard MMBasic does not natively expose a two-argument
' ATAN2 function, the GetHeading routine features safety boundaries
' for X = 0 along with explicit block filters to accurately isolate
' the four vector quadrants.
' 3. Data Handling:
' The code transforms raw High/Low incoming bytes back into standard
' signed variables using 16-bit two's complement logic explicitly
' parsed for standard BASIC limitations.


... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
geodav
Newbie

Joined: 09/05/2014
Location: Australia
Posts: 15
Posted: 12:16am 11 Nov 2026
Copy link to clipboard 
Print this post

Wow... thank you so much Panky,  this is more than I expected from our Backshed specialists as the request is quite simplistic on my behalf as a newbie.
Initially I shall use the PIC32M250 on MM basic ver 4.5,
 but shall continue with a  Picomite2 V6 once she's working. Shall adjust the CPU frequency accordingly, and have observed the COM pinouts etc
However having seen this translation of yours, it gives one a very good idea of how to interrogate the MicroMag for data out at once per second, and how to go from here.

Dont have a screen attached to this one, as it is to be used as a driveway sentry, for vehicles which would register at over 1000 uT magnetic field easily and then transmit the data at 433 MHz to  my receiver where Ime working.

However you  have thought through everything, and can only thanks you for this, sir

Much obliged Doug
George Davidson
 
geodav
Newbie

Joined: 09/05/2014
Location: Australia
Posts: 15
Posted: 11:35pm 15 Nov 2026
Copy link to clipboard 
Print this post

Hi Brains trust and Panky in particular

The micromag3 works well now on MMBver5.05 using a P170f256B
Did change the code to place the PINRESET before the SS to clear the data before interrogation...

and used Magtotal = sqr( x^2 + y^2 + z^2) as to obtain the total magnetic field

Noted that the x,y, and z are not exactly  orthogonal, spose it depends on their exact orientation when the Micromag was manufactured, but can work around this  

Question therefore: Can the SPI be shared with an IL9341 display (without the touch )

However many many thanks team,

Finest regards
George


CODE as FOLLOWS:-
' MicroMag3 Magnetic Sensor Control Program 15/11/2026 DAVIDSON
Print "Ver ";MM.Ver   'Option Explicit
magtot = 0
Const pinSSNOT  = 2    ' Slave Select / Chip Select (Output)
Const pinRESET  = 7    ' Reset Line (Output) led
Const pinDRDY   = 5    ' Data Ready Line (Input)
'' pin3 SPI out, pin 14: spi in, pin25, SPI clock (SDI, SDO, SCK
Dim x_val, y_val, z_val
SetPin pinSSNOT, DOUT : Pin(pinSSNOT) = 1 ' Set High (Idle)
SetPin pinRESET, DOUT : Pin(pinRESET) = 0
SetPin pindrdy,din
SPI Open 100000, 0, 8 ' 100 khz
Print "MicroMag3 MMBasic Controller Initialised."
Do 'main loop
x_val = ReadAxis(1) ' Read X-Axis (Command offset 1)
y_val = ReadAxis(2) ' Read Y-Axis (Command offset 2)
z_val = ReadAxis(3) ' Read Z-Axis (Command offset 3)
magtot = Sqr(x_val * x_val + y_val * y_val +z_val *z_val)' orthogonality
Print "X: ", x_val, " | Y: "; y_val, " | Z: "; z_val, " | Total: ";magtot-460
Pause 100.0 ' Update rate (0.1 seconds)
Loop
Function ReadAxis(axisNum)
 Pin(pinSSNOT) = 0 ' Set low.............(.High= Idle)
 Local cmdByte, highByte, lowByte, rawWord
 Pin(pinRESET) = 1:Pause 40: Pin(pinRESET) = 0
 ' Core measurement control configuration: division ratio /2048 (Bits: 01100)
 If axisNum = 1 Then
   cmdByte = &B00110001 ' X Axis
  ElseIf axisNum = 2 Then
   cmdByte = &B00110010 ' Y Axis
  Else
   cmdByte = &B00110011 ' Z Axis
EndIf
   Pause 40
Pin(pinSSNOT) = 0: Pause 20 ' 1. Cycle Reset to begin sequence
Pin(pinRESET) = 1 : Pause 5 : Pin(pinRESET) = 0 : Pause 5
Pin(pinSSNOT) = 0: Pause 2  ' 2. Select the sensor chip
SPI Write 1, cmdByte '' 3. Transmit the command byte via SPI
 Pause 40 'important'' 4. Wait for the Data Ready line
 highByte = SPI(0)'' 5. Retrieve 16 bits of measurement data (2 Bytes)
lowByte = SPI(9)' why same/? 0 or 9?
Pin(pinSSNOT) = 1 ' ' Deselect the sensor
rawWord = (highByte * 256) + lowByte'' Combine bytes as raw 16-bit integer
If rawWord >= 32768 Then' ' Handle 16-bit two's complement sign conversion
   ReadAxis = rawWord - 65536
  Else
   ReadAxis = rawWord
EndIf
End Function
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 3479
Posted: 12:15am 16 Nov 2026
Copy link to clipboard 
Print this post

From the V5.05.05 manual, with a little extra clarification added to the demo code comments.
  Appendix D – SPI Communications said  Using the SPI with the Colour LCD and Touch Features
The standard 28 and 44-pin Micromites have only one SPI channel so this must be shared between the display
and touch controllers (if these features are used) and the BASIC program. The Micromite Plus has two SPI
channels so this issue does not occur with this version.
To share the SPI port with a LCD display on the standard Micromite it must be opened in BASIC and closed
again without any intervening commands that might cause MMBasic to send/receive data to the display or
touch controllers. This includes commands such as CLS, LINE, etc and the TOUCH() function.
The following provides an example. The SPI port is opened and closed within the one function with no
intervening graphic or touch commands:
PIN(26) = 1 ' preset the chip select pin for your device high before it is setup
SETPIN 26, DOUT ' pin 26 will be used as the enable signal for your device
CIRCLE 100, 100, 50 ' draw a circle (uses the SPI port)
nbr% = ReadSPI%() ' function to send commands and read SPI data to/from your device (see below)
x% = TOUCH(X) ' get the X touch location (uses the SPI port)
nbr2% = ReadSPI%() ' get the SPI data for a second time
y% = TOUCH(Y) ' get the Y touch location (uses the SPI port)
END

' function to get the SPI data
FUNCTION ReadSPI%()
 SPI OPEN 4000000,0,8 ' 4MHz speed, mode 0, 8bits
 PIN(26) = 0 ' chip select low
 SPI write 3,3,0,0 ' send a command
 SPI read 1, ReadSPI% ' and get the response
 PIN(26) = 1 ' chip select high
 SPI CLOSE
END FUNCTION


Edit.
If your LCD screen has a touch chip but it is not enabled in the OPTIONs it may be best to ensure it is not connected to the SPI bus. If that isn't practical add a 4k7 pullup resistor to its T_CS pin (to 3.3V) to ensure it doesn't block access to the SPI bus.
Edited 2026-11-16 12:00 by phil99
 
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 2026