"); //-->
The Raspberry Pi has a Broadcom BCM 2835 chip allowing it to interface with SPI devices on its GPIO pins. There are two chip select pins meaning that the Pi can control two devices simultaneously.
| 19 | MOSI – master output slave input |
| 21 | MISO – master input slave output |
| 23 | SCLK – clock |
| 24 | CE0 – chip enable 0 |
| 26 | CE1 – chip enable 1 |
Step 1: Enable SPI on the Raspberry Pi
In your Pi’s terminal, run
sudo raspi-config
Go to Advanced Options > SPI
Choose “Yes” for both questions then select Finish to exit raspi-config
Either reboot your Pi or run this command to load the kernel module
sudo modprobe spi-bcm2708
Step 2: Install spidev
Spidev is a python module that allows us to interface with the Pi’s SPI bus.Watch movie online The Transporter Refueled (2015)
sudo apt-get update sudo apt-get upgrade sudo apt-get install python-dev python3-dev cd ~git clone https://github.com/doceme/py-spidev.gitcd py-spidev make sudo make install
Step 3: Python script
Finally, we can write and run a python script to control the SPI device.
Create a file called spi-test.py in your favorite editor
#!/usr/bin/pythonimport spidevimport time spi = spidev.SpiDev()spi.open(0, 0)spi.max_speed_hz = 7629# Split an integer input into a two byte array to send via SPIdef write_pot(input): msb = input >> 8 lsb = input & 0xFF spi.xfer([msb, lsb])# Repeatedly switch a MCP4151 digital pot off then onwhile True: write_pot(0x1FF) time.sleep(0.5) write_pot(0x00) time.sleep(0.5)
Make the file executable and run it
chmod +x spi-test.py sudo ./spi-test.py
Notes on spidev
Unless the spi.max_speed_hz field is a value accepted by the driver, the script will fail when you run it. The field can be set to these values on the raspberry pi:
| 125.0 MHz | 125000000 |
| 62.5 MHz | 62500000 |
| 31.2 MHz | 31200000 |
| 15.6 MHz | 15600000 |
| 7.8 MHz | 7800000 |
| 3.9 MHz | 3900000 |
| 1953 kHz | 1953000 |
| 976 kHz | 976000 |
| 488 kHz | 488000 |
| 244 kHz | 244000 |
| 122 kHz | 122000 |
| 61 kHz | 61000 |
| 30.5 kHz | 30500 |
| 15.2 kHz | 15200 |
| 7629 Hz | 7629 |
Two SPI devices can be controlled in python by creating two SpiDev objects, one for each device.
spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 976000 spi2 = spidev.SpiDev() spi2.open(0, 1) spi2.max_speed_hz = 976000
https://www.takaitra.com/spi-device-raspberry-pi/
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
DSP,数字信号处理,TI第三方,DSP教学系统,DSP解决方案,DSP培训
ISL55210子评估板的介绍
Pickering推出行业极具定制性的高压舌簧继电器600系列
6TOPS算力驱动30亿参数LLM,米尔RK3576部署端侧多模态多轮对话
微机原理与网络应用
印度半导体野心勃勃:联手东京电子能否改变芯片竞争格局?
DSP教学系统,DSP解决方案,DSP培训,DSP图像处理,电机控制,通信信号处理,测量仪器开发
无线收发芯片的比较与选择
正弦波发生器电路(555)
三星计划在 Galaxy S26 机型中使用 Exynos 2600,减少对高通的依赖
嵌入式电子的复杂世界:独立生态系统的分层迷宫
无线收发数传MODEM 模块PTR2000 的原理与应用
LM741CN构成的RC正弦波振荡器
Intersil Zilker Labs电源导航软件
网络上经典的DOS小命令
ZL9101M数字电源模块简介
英伟达表示,尽管詹森在财报电话会议中暗示了相反的情况,但它的 H100/H200 GPU 并没有售罄
谷歌正拉拢小型云服务提供商托管 TPU,目标直指英伟达
美国政府吊销了台积电向其中国大陆晶圆厂运送设备的授权
飓风数字系统(北京)有限公司,DSP发展动态
TMS320F2812,TMS320VC5402, TMS320VC5502,OMAP,TMS320DM642,TMS320C6416,OMAP
2025 年上半年中国芯片产业迅猛发展 — 38 家 A 股上市公司据报道实现增长
μA741构成简单的正弦波发生器
石英晶体正弦振荡器
无线传输血壓計
Intersil ZL9101 数字电源模块 - 热设计
电源模块设计
单结晶体管正弦波振荡器
站名:www.cyclone.com.cn,DSP,DSP培训,DSP图像处理,电机
iDEAL 半导体宣布推出基于 SuperQ™的 200V MOSFET 系列,具有行业领先的成本×性能