当前位置:网站首页>Raspberry pie 4B learning notes - IO communication (1-wire)
Raspberry pie 4B learning notes - IO communication (1-wire)
2022-07-02 01:25:00 【Xiao Xiang is a der】
List of articles
Introduction to single bus protocol
1-wire Single bus is Maxim Wholly owned subsidiary Dallas A proprietary technology of . With most current standard serial data communication methods , Such as SPI/I2C/MICROWIRE Different , It uses a single signal line , Transmission clock , It also transmits data, and the data transmission is bidirectional . It has the advantage of saving I/O Port line resources 、 Simple structure 、 The cost is low 、 It is convenient for bus expansion and maintenance .1
The data transmission rate of single bus is generally 16.3Kbit/s, Up to 142 Kbit/s, Usually, we use 100Kbit/s Transmit data at the following rate
1-Wire The line port is the port of open drain structure or three state gate , Therefore, it is generally necessary to add a pull resistance Rp, Generally selected 5K~10KΩ.
For the detailed introduction of single bus, please refer to This article .
Participate in the blue bridge cup or use it through MCU DS18B20 My classmates must be familiar with this agreement , The step used in the process of obtaining temperature is to initialize , Then write the order ; And then initialize , Read the command to get the temperature . Communication steps of the whole single bus : initialization 1-wire device 、 distinguish 1-wire Devices and exchange data .
Because they are master-slave structures , Only when the master calls the slave , Only the slave can answer , So the host accesses 1-wire Devices must strictly follow the single bus command sequence , namely 1. initialization 、2.ROM、3. command Function command .
Used today DHT11 He just uses the single bus data format , Specific command sequence and DS18B20 These strictly follow initialization 、ROM、 The components of the command trilogy are different .DHT11 Only the communication steps of single bus : initialization 1-wire device 、 distinguish 1-wire Devices and exchange data .
Raspberry pie 4B+DHT11(1-Wire agreement )
DHT11 brief introduction
Interface definition

data format
DATA foot For microprocessors and DHT11 Communication and synchronization between , Use single bus data format , Primary communication time 4ms about , The data is divided into decimal part and integer part , The specific format is explained below :
8bit Humidity integer data +8bit Humidity decimal data
+8bi Temperature integer data +8bit Temperature decimal data
+8bit The checksum 
When the data transmission is correct, the check sum data is equal to “ 8bit Humidity integer data +8bit Humidity decimal data
+8bi Temperature integer data +8bit Temperature decimal data ” The end of the result 8 position .
Communication process
1. Bus initialization ;( Pull down the bus for no less than 18ms, Then release , Bus returns to high level )
2.DHT11 The reply ;(DHT11 The answer pulls the bus down )
3. Output data frame ( A complete data transfer is 40bit, High first out .)
Data frame format :
Communication sequence diagram :
Hardware connection
| Raspberry pie | DHT11 |
|---|---|
| 5V | vcc |
| 7 Pin No ( Onboard code ) | DATA |
| GND | GND |

Start raspberry pie 1-Wire Interface
This step is the same as before SPI、I2C Same operation , No more maps , For the sake of insurance , It is suggested to open the mouth and restart the raspberry pie sudo reboot.
Programming to realize
because DHT11 The reading of is relatively simple , Is to control according to the sequence IO Input and output of the port .
Code from This article .
The author simply added notes according to the sequence diagram
open Geany, Enter the following code :
// An highlighted block
//
//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 32
int pinNumber =7; //use gpio1 to read data(wPi code )
uint32 databuf;
// Read the value of the sensor
uint8 readSensorData(void)
{
uint8 crc;
uint8 i;
pinMode(pinNumber,OUTPUT); // Set the pin to output mode ( Used to control bus level )
digitalWrite(pinNumber, 0); // Pull down the bus ( Start signal )
delay(25);//( The starting signal shall not be less than 18ms Low level of , Here the author uses 25ms)
digitalWrite(pinNumber, 1); // Host releases bus ( Bus returns to high level )
pinMode(pinNumber, INPUT); // Set the pin to input mode , It is convenient to detect the response signal of the slave
pullUpDnControl(pinNumber,PUD_UP);// Set as pull-up input .
delayMicroseconds(27);
if(digitalRead(pinNumber)==0) // Check whether the bus is pulled down by the slave , If you pull it down, it means that the slave answers .
{
while(!digitalRead(pinNumber)); // Flag waiting for data transmission from the slave ( Pull high )
for(i=0;i<32;i++)// Read 32 Is the temperature and humidity data
{
while(digitalRead(pinNumber)); //data clock start
while(!digitalRead(pinNumber)); //data start
delayMicroseconds(HIGH_TIME);
databuf*=2;// Carry is equivalent to a left shift in a bit operation .
if(digitalRead(pinNumber)==1) //1
{
databuf++;
}
}
for(i=0;i<8;i++)// Collect 8-bit parity data
{
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;
}
The effect is as follows :
Using single bus and DS18B20 Communication obtains temperature
Because the big guys have already made DS18B20 Read file , So I'm using DS18B20 When obtaining temperature , Just call the file interface to get the temperature information , There is no need to think STM32 Like other monolithic devices, it initializes the sequence diagram bit by bit , Writing data / Instructions , Read the data .c Language implementation detailed steps reference This article ;Python Reference resources This article .
summary
The use of single bus is recorded here , If there is something wrong, you are welcome to criticize and correct .
Wire Basic principle of single bus
边栏推荐
- 【疾病检测】基于BP神经网络实现肺癌检测系统含GUI界面
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- Edge computing accelerates live video scenes: clearer, smoother, and more real-time
- 微信小程序中使用tabBar
- 学习笔记3--高精度地图关键技术(上)
- What is commercial endowment insurance? Is commercial endowment insurance safe?
- Minimize the error
- Using tabbar in wechat applet
- "C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
- Design and implementation of radio energy transmission system
猜你喜欢

How to compress video size while adding watermark with one click?

Learn about servlets

Learn C language from scratch day 025 (maze)

Review notes of compilation principles

About asp Net core uses a small detail of datetime date type parameter

6-2漏洞利用-ftp不可避免的问题

Edge extraction edges based on Halcon learning_ image. Hdev routine

How does schedulerx help users solve the problem of distributed task scheduling?

Docker安装Oracle_11g

How to reflect and solve the problem of bird flight? Why are planes afraid of birds?
随机推荐
Global and Chinese markets of beverage seasoning systems 2022-2028: Research Report on technology, participants, trends, market size and share
About asp Net core uses a small detail of datetime date type parameter
[rust web rokcet Series 1] Hello, world and get, post, put, delete
Learn about servlets
Learning note 24 - multi sensor post fusion technology
Datawhale 社区黑板报(第1期)
II Basic structure of radio energy transmission system
It's already 30. Can you learn programming from scratch?
技术大佬准备就绪,话题C位由你决定
Deb file installation
2022年6月国产数据库大事记
Learning note 3 -- Key Technologies of high-precision map (Part 1)
Global and Chinese market of picture archiving and communication system (PACS) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
The concept and application of Cartland number
学习笔记24--多传感器后融合技术
error: . repo/manifests/: contains uncommitted changes
Architecture evolution from MVC to DDD
cookie、session、tooken
Daily work and study notes