树莓派用wiringPi控制SPI口的方法有什么?

树莓派用wiringPi控制SPI口的方法有什么?,第1张

方案——使用GIT工具:

如果在你的平台上还没有安装GIT工具,可以输入以下命令:

sudo apt-get install git-core

如果在这个过程中出现错简戚误,尝试更新软件,例如输入以下指令:

sudo apt-get update

sudo apt-get upgrade

紧接着可以通过GIT获得wiringPi的源代码

git clone git://git.drogon.net/wiringPi

若需要更新wiringPi。

cd wiringPi

git pull origin

进入wiringPi目录并安装wiringPi

cd wiringPi

./build

build脚本会帮助你编译和安装wiringPi

WiringPi简介:

WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3。wiringPi使用C或者C++开发并且可以被其他语言包转,例如python、ruby或者PHP等。WiringPi中的函数类似于Arduino的wiring系统,这使得熟悉arduino的用户使用wringPi更为方便。

树莓派具有26个普通输入和输出引脚。在这26个引脚中具有8个普通输入和输出管脚,这8个引脚既可以作为输入管脚也可以作为输出管脚。除此之外,树莓派还有一个2线形式的I2C、一个4线形判亩式的SPI和一个UART接口。

树莓派上的I2C和SPI接口也可以作为普通端拦冲陵口使用。如果串口控制台被关闭便可以使用树莓派上的UART功能。如果不使用I2C,SPI和UART等复用接口,那么树莓派总共具有8+2+5+2 =17个普通IO。wiringPi包括一套gpio控制命令,使用gpio命令可以控制树莓派GPIO管脚。

用户可以利用gpio命令通过shell脚本控制或查询GPIO管脚。wiringPi是可以扩展的,可以利用wiringPi的内部模块扩展模拟量输入芯片,可以使用MCP23x17/MCP23x08(I2C 或者SPI)扩展GPIO接口。

另外可通过树莓派上的串口和Atmega(例如arduino等)扩展更多的GPIO功能。另外,用户可以自己编写扩展模块并把自定义的扩展模块集成到wiringPi中。WiringPi支持模拟量的读取和设置功能,不过在树莓派上并没有模拟量设备。但是使用WiringPi中的软件模块却可以轻松地应用AD或DA芯片。

树莓派2上面启用spi的方法和树莓派1不太一样。网上关于1代的设置方法很多,好不容易找到一个适合2代用的设置方法,相比起来简单了不少。最简单的方法当然是raspi-config!

The Raspberry Pi has a Serial Peripheral Interface (SPI) bus which can be enabled on Pins 19,21,23,24 &26. It is a synchronous serial data link standard and is used for short distance single master communication between devices. As far as the Pi is concerned this is usually relevant to certain sensors and add-on boards.

There is more technical information about SPI on the SPI Wikipedia page if you are interested.

The default Raspbian image disables SPI by default so before you can use it the interface must be enabled. This can be done uses either of two methods. I’ll describe both methods but the first one is probably easier and quicker.

Start by running the following command :

This will launch the raspi-config utility. Select option 8 “Advanced Options”.

Select the “SPI” option.

Set the option to “Yes”.

Select “OK”.

Select “Finish”.

Reboot for the changes to take effect :

SPI is now enabled.

This process assumes you are using the latest Raspbian build from the official downloads page. You can update your current image using :

To enable hardware SPI on the Pi we need to make a modification to a system file :

Add the following line at the bottom :

Use CTRL-X, then Y, then RETURN to save the file and exit. Reboot using the following :

Checking If SPI Is Enabled (Optional)To check if the SPI module is loaded by the system run the following command :

You should see “spi_bcm2708″ listed in the output. You can use the following command to filter the list and make it easier to spot the spi entry :

SPI is now enabled.

Install Python SPI Wrapper (Optional)In order to read data from the SPI bus in Python we can install a library called ‘py-spidev’. To install it we first need to install ‘python-dev’ :

Then to finish we can download ‘py-spidev’ and compile it ready for use :

You should now be ready to either communicate with add-on boards using their own libraries (e.g. the PiFace) or other SPI devices (e.g. the MCP3008 ADC).

#include <stdio.h>

#include <wiringPi.h>

#include <wiringPiSPI.h>

void main(void)

{

int spi1

unsigned char date[2],rec[2]

int spiChannel = 0

int clock = 1000000

wiringPiSetup()

spi1 = wiringPiSPISetup(spiChannel,clock)

if(spi1!=-1)

{

printf("Starting SPI Test\n")

printf("Write:\n")

date[0]=0x80//寄存器地址

date[1]=0x55//要写的数据

wiringPiSPIDataRW(0,date,2)

printf("date[0] : %d ",date[0])

printf("date[1] : %d \n",date[1])

delay(500)

printf("Read:\n")

rec[0]=0x00//寄存器地址

rec[1]=0x00

wiringPiSPIDataRW(0,rec,2)

printf("rec[0] : %d ",rec[0])

printf("rec[1] : %d \n",rec[1])//返回的数据是这个

 }

}

我试过可行的,编译gcc -Wall -o testSPI testSPI.c -lwiringPi

sudo ./testSPI


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12480305.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存