geodav Newbie
 Joined: 09/05/2014 Location: AustraliaPosts: 13 |
| 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: AustraliaPosts: 1142 |
| 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! |