Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:03 01 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 : sensors or devices with SPI interface?

     Page 1 of 2    
Author Message
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:45am 23 Jul 2021
Copy link to clipboard 
Print this post

I'm looking for examples of SPI sensors or devices for which MMBasic driver code is available. I'm not talking about the supported SPI display modules, but some examples of SPI devices which are controlled with basic code, not MMBasic firmware.

This is to test an SPI port on a PCB.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 06:56am 23 Jul 2021
Copy link to clipboard 
Print this post

Hi lizby,

Maybe this helps. The program sends data via SPI port 1 to a 74HC164 shift register (8 bit), then clocks the data from the 8 outputs of the shift register into a CD4021 shift register (these pins are hard wired between the 2 chips), and reads the 8 data bits back in through SPI port 0.

By changing the SPI clocks you can see where the speed limit of the attached chips is. This runs on picomite (last tested on revision a27)

'SPI test picomite

hc164_clk%=20000000
CD4021_clk%=2000000

'configuration 74HC164 at SPI1
 SetPin 19,spi1sck
 SetPin 20,spi1tx
 SetPin 16,spi1rx
 SPI1 open HC164_clk%,0,8

'configuration CD4021 at SPI2
 SetPin 24,spi0sck
 SetPin 21,spi0rx
 SetPin 25,spi0tx
 SPI open CD4021_clk%,0,8
 'load/shift pin 4021
 Const spi0ld=26
 SetPin spi0ld,dout
 Pin(spi0ld)=0 'ld=0:shift, ld=1:load

'send random data to 74HC164 and read CD4021 response
Print "SPI test on Picomite"
Print "Testing 74HC164 @ ";hc164_clk%; " Hz"
Print "Testing CD4021  @ ";CD4021_clk%; " Hz"
Print
Print "press any key to stop"

good%=0:bad%=0
Timer = 0
Do
 snd%=Int(256*Rnd())
 a%=SPI1(snd%)
 Pulse spi0ld,0.001 'load
 get%=SPI(0)
 If snd%=get% Then
   Inc good%
 Else
   Inc bad%
 EndIf
Loop Until Inkey$<>""

takt = Timer / (good%+bad%)
Print
Print "transfers = ";good%+bad%;" in ";Timer;" ms"
Print "good = ";good%;"   bad = ";bad%
Print "takt = ";takt;" ms"

Edited 2021-07-23 16:58 by Volhout
PicomiteVGA PETSCII ROBOTS
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 12:40pm 23 Jul 2021
Copy link to clipboard 
Print this post

I don't have either of those ICs, but thanks for the response and thanks in particular for the PicoMite code.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
PeterB
Guru

Joined: 05/02/2015
Location: Australia
Posts: 655
Posted: 05:17am 24 Jul 2021
Copy link to clipboard 
Print this post

G'Day Lizby.

I did a search of ALIEXPRESS/SPI.

GY-291 ADXL345 Digital triaxial acceleration of gravity inclination Module IIC / SPI transmission.

Other things came up but that was minutes ago and I can't remember them.

Is that the sort of thing you are after?

Peter
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 10:44am 24 Jul 2021
Copy link to clipboard 
Print this post

  PeterB said  I did a search of ALIEXPRESS/SPI.

Thanks. I know of plenty of SPI devices, and have some. What I don't have is the MMBasic code to use them. Just looking for example code for some readily available SPI device.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3292
Posted: 12:27pm 24 Jul 2021
Copy link to clipboard 
Print this post

The DDS Signal Generator project on my website uses a AD9833 waveform generator IC and a MCP41010 8-bit digital potentiometer, both of which are SPI devices.

You can download the MMBasic program from here: https://geoffg.net/SignalGenerator.html
(scroll to the bottom of the page)

Because this program runs on the Micromite LCD Backpack it needs to avoid conflict with the SPI based LCD display panel.  It does this by opening the SPI interface then closing it again whenever communication with the AD9833 or MCP41010 is required (as against leaving the SPI interface open).  This requirement is documented in the manual.

Geoff
Geoff Graham - http://geoffg.net
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 01:19pm 24 Jul 2021
Copy link to clipboard 
Print this post

Thanks. I've ordered one of the AD9833s. The code may be more useful to me.

I have a BMP280, but the MMBasic code I've found for it is I2C, not SPI.

I also ordered a couple of 4-digit 7-segment displays which look SPI-like. That's probably more like what I want, where the SPI device displays something (but is not firmware-driven).

~
Edited 2021-07-24 23:29 by lizby
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 05:01pm 24 Jul 2021
Copy link to clipboard 
Print this post

The displays using a TM1637 are marketted as I2C but really are not. In my wakeup clock, I bitbang them.
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 06:33pm 24 Jul 2021
Copy link to clipboard 
Print this post

  Quote  This is to test an SPI port on a PCB.


Connect SPI-TX to SPI-RX (out/MOSI or whatever you want to call them), Connect SPI-CLK to a count input.

Type ?SPI(123),PIN(countpin)

If you get 123, 8 back then its working
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:48pm 24 Jul 2021
Copy link to clipboard 
Print this post

AS3935.zip
The AS3935 is a rather useless lightning detector but it does use SPI
VK7JH
MMedit
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 10:53pm 24 Jul 2021
Copy link to clipboard 
Print this post

  matherp said  Type ?SPI(123),PIN(countpin)

If you get 123, 8 back then its working

I get 123,1. What have I done wrong?
> option reset
option count 9,10,11,12
> setpin 19,spi1sck ' SPI1 SCK
> setpin 20,spi1tx  ' SPI1 MOSI
> setpin 16,spi1rx  ' SPI1 MISO
> spi1 open 1000000,0
> setpin 9,cin
> ?SPI1(123),PIN(9)
123     1



PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:23am 25 Jul 2021
Copy link to clipboard 
Print this post

  TassyJim said  The AS3935 is a rather useless lightning detector but it does use SPI

Thanks for the code sample. I'd prefer something with a visible output, so a TM1638-type 7-segment display would probably be perfect.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 07:28am 25 Jul 2021
Copy link to clipboard 
Print this post

Hi Lizby,

It is most unfortunate that you request help with SPI and prefer something (TM1638) that is a crippled form of SPI. It has MOSI and MISO combined on one pin. Similar the TM1637 (wich has a crippled form of I2C).

I don't have one, so I can't be of any help. But if you need a solution for the TM1638 be aware this is not education generic SPI.

Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 08:01am 25 Jul 2021
Copy link to clipboard 
Print this post

  Quote  I get 123,1. What have I done wrong?


Should work - try the clock at 10000 and put a pause 1000 before reading the count pin
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:12pm 25 Jul 2021
Copy link to clipboard 
Print this post

  matherp said  Should work - try the clock at 10000 and put a pause 1000 before reading the count pin


Thanks. After:

option count 9,10,11,12

setpin 19,spi1sck ' SPI1 SCK
setpin 20,spi1tx  ' SPI1 MOSI
setpin 16,spi1rx  ' SPI1 MISO
spi1 open 10000,0
setpin 9,cin
?SPI1(123);" ";: pause 1000: ?PIN(9)


Gives "123 8".
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:16pm 25 Jul 2021
Copy link to clipboard 
Print this post

  Volhout said  It is most unfortunate that you request help with SPI and prefer something (TM1638) that is a crippled form of SPI. It has MOSI and MISO combined on one pin. Similar the TM1637 (wich has a crippled form of I2C).

I don't have one, so I can't be of any help. But if you need a solution for the TM1638 be aware this is not education generic SPI.


I didn't know. It appeared that it would give me a visible result with SPI output. So still looking for an SPI device with MMBasic driver code (not firmware) which would ideally give a visible result not just on the console.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 02:22pm 25 Jul 2021
Copy link to clipboard 
Print this post

  Quote  Gives "123 8".


That test meets your original brief by proving SPI is working properly
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 02:41pm 25 Jul 2021
Copy link to clipboard 
Print this post

  matherp said  That test meets your original brief by proving SPI is working properly

I'm happy with that test, thank you. It's a very concise exercise in showing functionality (and the first time I've actually explicitly used either SPI or COUNT).

I'm sorry if I did not fully enough explain originally that the SPI device I'm ideally looking for would be able to illustrate (without the console) that it was working.

I looked through older posts to see if there were examples where displays, for instance, had been gotten to work in MMBasic code before the drivers were incorporated into the firmware, but didn't find them. They may be there, but my searches were inadequate.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 03:48pm 25 Jul 2021
Copy link to clipboard 
Print this post

I know that I am the poster boy for electronics ignorance but wouldn't a MAX7219 driven 8x8 LED matrix fit the requirement for a cheap SPI device that you can "see" working?

Best wishes,

Tom
Edited 2021-07-26 01:49 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3378
Posted: 04:03pm 25 Jul 2021
Copy link to clipboard 
Print this post

  thwill said  I know that I am the poster boy for electronics ignorance but wouldn't a MAX7219 driven 8x8 LED matrix fit the requirement for a cheap SPI device that you can "see" working?


That is in fact what I ordered yesterday from Aliexpress:  MAX7219 8*8 dot matrix module
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025