0.准备
【简单介绍】——该库更确切的名称为raspberry-gpio-python,树莓派官方资料中推荐且容易上手。python GPIO是一个小型的python库,可以帮助用户完成raspberry相关IO口操作。但是python GPIO库还没有支持SPI、I2C或者1-wire等总线接口。除了python GPIO之外,还有众多的python扩展库(例如webiopi),毫无疑问的说python非常适合树莓派,树莓派也非常适合python。
【简单介绍】——wiringPi适合那些具有C语言基础,在接触树莓派之前已经接触过单片机或者嵌入式开发的人群。wiringPi的API函数和arduino非常相似,这也使得它广受欢迎。作者给出了大量的说明和示例代码,这些示例代码也包括UART设备,I2C设备和SPI设备等,毫无疑问地说wiringPi功能非常强大。
【简单介绍】BCM2835 C Library可以理解为使用C语言实现的相关底层驱动,它给我的感觉更像STM32的库函数,BCM2835 C Library的驱动库包括GPIO、SPI和UART等,可以通过学习BCM2835 C Library熟悉BCM2835相关的寄存器操作。如果有机会开发树莓派上的linux驱动,或自主开发python或PHP扩展驱动,可以从BCM2835 C Library找到不少的“灵感”。
1.硬件准备
2.注意事项
【编号方式】
2 wiringPi GPIO实现
//mydht11.c
//
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;#define HIGH_TIME 32int pinNumber =1; //use gpio1 to read data
uint32 databuf;
uint8 readSensorData(void)
{
uint8 crc;
uint8 i;
pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 0); // output a high level
delay(25);
digitalWrite(pinNumber, 1); // output a low level
pinMode(pinNumber, INPUT); // set mode to input
pullUpDnControl(pinNumber,PUD_UP);
delayMicroseconds(27);
if(digitalRead(pinNumber)==0) //SENSOR ANS
{
while(!digitalRead(pinNumber)); //wait to high
for(i=0;i<32;i++)
{
while(digitalRead(pinNumber)); //data clock start
while(!digitalRead(pinNumber)); //data start
delayMicroseconds(HIGH_TIME);
databuf*=2;
if(digitalRead(pinNumber)==1) //1
{
databuf++;
}
}
for(i=0;i<8;i++)
{
while(digitalRead(pinNumber)); //data clock start
while(!digitalRead(pinNumber)); //data start
delayMicroseconds(HIGH_TIME);
crc*=2;
if(digitalRead(pinNumber)==1) //1
{
crc++;
}
}
return 1;
}
else
{
return 0;
}
}
int main (void)
{
printf(“Use GPIO1 to read data!\n”);
if (-1 == wiringPiSetup()) {
printf(“Setup wiringPi failed!”);
return 1;
}
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
printf(“Enter OS——-\n”);
while(1) {
pinMode(pinNumber,OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
delay(3000);
if(readSensorData())
{
printf(“Congratulations ! Sensor data read ok!\n”);
printf(“RH:%d.%d\n”,(databuf>>24)&0xff,(databuf>>16)&0xff);
printf(“TMP:%d.%d\n”,(databuf>>8)&0xff,databuf&0xff);
databuf=0;
}
else
{
printf(“Sorry! Sensor dosent ans!\n”);
databuf=0;
}
}
return 0;
}