当前位置:网站首页>Learning notes of raspberry pie 4B - IO communication (I2C)
Learning notes of raspberry pie 4B - IO communication (I2C)
2022-07-05 03:27:00 【Xiao Xiang is a der】
List of articles
GPIO signal communication
That was mentioned before GPIO Input detection and output control , Now record about raspberry pie GPIO Communication function , In embedded I2C、SPI、UART Is the most commonly used communication protocol , Through these protocols, we can communicate with many master-slave devices , Use today I2C Of 0.96 " OLED Screen as an example , Use the raspberry pie that comes with it I2C Interface to drive OLED The screen shows what we want to show .
Here is a little knowledge about the agreement :
full duplex : You can receive data or send data You can do it at the same time ----------- Two data lines
Half duplex : You can receive data or send data It cannot be done at the same time ------- A data cable
Simplex : You can only receive data or send data ---------------------------- A data cable
Serial : Data bit by bit transmission --------- One line transmits data
For example, we will mention I2C、UART.
parallel : Data is transmitted all at once --------- Multiple lines transmit data
For example, the camera in smart car competition adopts parallel communication .( The above pictures are from This article )
field bus : It can be transmitted over a long distance can (10km) 485(1km) // Differential mode signal
Commonly used in industrial control .
Board level bus : Communication between chips ---- A little distance IIC SPI // Common mode signal
General communication mode between components .
Synchronous communication : Both communication parties use the same clock source ( clock frequency )
asynchronous communication : Both communication parties use their own clock source
The specific classification of communication is shown here post .
I2C brief introduction
I2C It is a two-way two-wire bus launched by Lipper ,SCL Clock line and SDA cable , For data transmission , According to the knowledge points mentioned above I2C yes Serial half duplex board level synchronous wired Transmission bus .
One bus mounts multiple IIC Interface devices ----- Parallel connection at IIC On the bus .
About a group I2C How many buses can be connected at most I2C Devices and I2C For your reference This blog post
of I2C The transmission flow of , I've seen it before and thought that bloggers made an analogy between the whole process and playing football , I think it's very image , here link Share with you
of I2C Please refer to the above blog for detailed knowledge of , The author will not make an analysis here , Let's go to the topic : Using raspberry pie I2C.
Raspberry pie 4B+0.96OLED(I2C agreement )
Query interface
Open terminal command , Input gpio readall, enter , In the returned IO You can see in the table SDA1、SCL1;SDA0,SCL0 Two groups I2C Interface , We use SCL1 And SDA1 This group carries out .
Hardware connection
The wiring is as follows :
Raspberry pie | OLED |
---|---|
5V | VCC |
GND | GND |
3 foot (SDA1) | SDA |
5 foot (SCL1) | SCL |
With raspberry pie I2C Interface
For those who have an interface or are not used to the command line, it is recommended to skip the following section , Refer to the interface .
There is no interface or you want to use the command line
Use putty perhaps Xshell Log in to raspberry pie , At the terminal command input sudo raspi-config, enter
Enter the following interface .
Use the keyboard to select up and down 3.Interface Option, enter , Use the keyboard to select up and down I5 I2C enter ;
Keep going back ;
Continue to enter ;
Use the right mouse button to select finish, enter , Return to the command window ;
Input sudo reboot, Restart raspberry pie .
There is an interface
open Raspberry Pi Configuration
Open it as shown below I2C.
Then select logout ;Reboot Wait for restart .
wiringPiI2c library
Library profile
Same as before IO The input and output are the same , You also need to install the corresponding library to realize the function . of wiringPiI2c Library functions and STM32 I2C The common functions of are compared as follows :
wiringPiI2c | STM32 |
---|---|
wiringPiI2CSetup (const int devId) ; | peripherals nit(void); Initialize peripherals |
wiringPiI2CReadReg8 (int fd, int reg) | iic_read_byte(void); Read one byte of data |
wiringPiI2CWriteReg8 (int fd, int reg, int data) | iic_send_byte(u8 data)// Send a byte of data |
For a detailed description of this library, refer to this link .
You can find wiringPiI2CSetup (const int devId) There's a parameter in , Combine the above kicking analogy and I2C Theoretical knowledge of , We know that this parameter is the address of the slave , In raspberry pie, you can pass i2detect Command to query the address of the peripheral , To use this command, you need to install i2c-tool.
install i2c-tool
Open the terminal command input :sudo apt-get install i2c-tools enter , Wait to return to the command line again .( You need to make sure your raspberry pie has a net )
Inquire about I2C The address of the peripheral
Enter at the command line : sudo i2cdetect -l enter , The following display appears , The description uses I2C1 Interface .
Then input : sudo i2cdetect -y 1, enter , The following display will appear , there 3C Namely OLED The address of ( Now raspberry pie I2C Do not connect other devices ), General 0.96 " OLED The address of the screen is 0x30.
Programming effect
Through the above comparison , We know the whole I2C The function of is just replaced by the function interface of raspberry pie , We will usually use I2C The effect can be achieved by replacing the corresponding function in the screen driver , The specific replacement process can be referred to This article .
open Geany, Enter the following code ( Because the mold is too long, you need to go to this link to download the link :https://pan.baidu.com/s/13SFd-MNfgJ1wD3R3np0T4w
Extraction code :yjyx
Yes C standing VIP You can also download it directly , I set it up. 0 Download points , The name of the resource is raspberry pie 4BIICOLED The screen ):
// An highlighted block
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<assert.h>
#include<termios.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<sys/types.h>
#include<errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <wiringPiI2C.h>
#include <unistd.h>
typedef unsigned char uint8_t;
typedef uint8_t u8;
#define OLED_CMD 0x00
#define OLED_DAT 0x01
int fd;
unsigned char yi[16]={
"Angle of beam:"};
unsigned char er[16]={
"ming"};
unsigned char san[16]={
"Distance:"};
unsigned char si[16]={
"okok"};
const unsigned char zi[];
const unsigned char picture1[];
const unsigned char picture2[];
const unsigned char picture3[];
const unsigned char picture4[];
const unsigned char picture5[];
const unsigned char picture6[];
const unsigned char picture7[];
void WriteCmd(unsigned char I2C_Command)
{
wiringPiI2CWriteReg8(fd,0x00, I2C_Command);
}
// Drawing point function
void OLED_SetPos(unsigned char x, unsigned char y)
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
//BMP Bitmap display
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y%8==0)
y = 0;
else
y += 1;
for(y=y0;y<y1;y++)
{
OLED_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
wiringPiI2CWriteReg8(fd,0x40,BMP[j++]);
}
}
}
void init(void)//OLED initialization
{
wiringPiSetup();
fd=wiringPiI2CSetup(0x3c);//i2c?
wiringPiI2CWriteReg8(fd,0x00,0xa1);//t?í??90xa0
wiringPiI2CWriteReg8(fd,0x00,0xc8);//L?úí??90xc0
wiringPiI2CWriteReg8(fd,0x00,0x8d);//A?5w?
wiringPiI2CWriteReg8(fd,0x00,0x14);
wiringPiI2CWriteReg8(fd,0x00,0xa6);//óí?>:90xa7
wiringPiI2CWriteReg8(fd,0x00,0xaf);// >:
}
void qingping(void)// Clear the screen
{
char zt1,zt2;
for(zt1=0;zt1<8;zt1++)
{
wiringPiI2CWriteReg8(fd,0x00,0xb0+zt1);
for(zt2=0;zt2<128;zt2++) wiringPiI2CWriteReg8(fd,0x40,0x00);
}
}
void ascii(float Angle,float distance)// Character display function
{
sprintf(er,"%f",Angle); // float 0 char
sprintf(si,"%f",distance); // double 0 char
int zt;
char zt3,zt4;
for(zt3=0;zt3<4;zt3++)
{
wiringPiI2CWriteReg8(fd,0x00,0xb0+(zt3*2));
for(zt4=0;zt4<16;zt4++)
{
for(zt=0;zt<8;zt++)
{
if(zt3==0) wiringPiI2CWriteReg8(fd,0x40,zi[yi[zt4]*16+zt]);
else if(zt3==1) wiringPiI2CWriteReg8(fd,0x40,zi[er[zt4]*16+zt]);
else if(zt3==2) wiringPiI2CWriteReg8(fd,0x40,zi[san[zt4]*16+zt]);
else if(zt3==3) wiringPiI2CWriteReg8(fd,0x40,zi[si[zt4]*16+zt]);
}
}
wiringPiI2CWriteReg8(fd,0x00,0xb0+(zt3*2)+1);
for(zt4=0;zt4<16;zt4++)
{
for(zt=0;zt<8;zt++)
{
if(zt3==0) wiringPiI2CWriteReg8(fd,0x40,zi[yi[zt4]*16+zt+8]);
else if(zt3==1) wiringPiI2CWriteReg8(fd,0x40,zi[er[zt4]*16+zt+8]);
else if(zt3==2) wiringPiI2CWriteReg8(fd,0x40,zi[san[zt4]*16+zt+8]);
else if(zt3==3) wiringPiI2CWriteReg8(fd,0x40,zi[si[zt4]*16+zt+8]);
}
}
}
}
int main()
{
float Angle = 2.98754546;
float distance = 5.754644545;
init();
delay(10);
qingping();// Clear the screen
delay(10);
ascii(Angle,distance);// Character display
delay(10);
while(1)
{
//qingping();
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture1);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture2);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture3);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture4);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture5);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture6);
delay(1);
OLED_DrawBMP(0,0,128,8,(unsigned char *)picture7);
delay(1);
}
}
Then name and save , I think the previous code has been placed on the desktop too messy , So I created a new folder , Put the code in the folder , You can do it according to your own needs .
Because I changed the directory, the compilation time also changed
Input is :cd Desktop/my_program/c( Switch to the directory where I saved the files ) enter
Then input gcc -o display display.c -lwiringPi Compile and generate the execution file enter
Last sudo ./display enter After running, you can see the following effects .
It may be due to I2C Not fast enough , It makes you look uncomfortable , I'm going to change it later SPI Try your screen again , For the mold taking method, please refer to this article post Just go , You can also chat privately about the author's document tutorial .
summary
I want to put Bing Dwen Dwen , But the official doesn't allow it to be related to Bing Dwen Dwen , Those who want to see the effect can go B I'm looking at it from the website .
边栏推荐
- Pat class a 1162 postfix expression
- Design of KTV intelligent dimming system based on MCU
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Yyds dry goods inventory embedded matrix
- ICSI213/IECE213 Data Structures
- Apache Web page security optimization
- [105] Baidu brain map - Online mind mapping tool
- 看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
- Sqoop安装
- [daily problem insight] Li Kou - the 280th weekly match (I really didn't know it could be so simple to solve other people's problems)
猜你喜欢
This + closure + scope interview question
Talk about the SQL server version of DTM sub transaction barrier function
Huawei MPLS experiment
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
TCP security of network security foundation
Master Fur
[groovy] string (string type variable definition | character type variable definition)
El select, El option drop-down selection box
Sqoop command
Qrcode: generate QR code from text
随机推荐
平台入驻与独立部署优缺点对比
Single box check box
Acwing第 58 场周赛【完结】
MySQL winter vacation self-study 2022 11 (9)
Basic authorization command for Curl
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
Port, domain name, protocol.
Six stone programming: advantages of automated testing
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
1.五层网络模型
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
[Chongqing Guangdong education] 2777t green space planning reference questions of National Open University in autumn 2018
C file in keil cannot be compiled
[groovy] string (string splicing | multi line string)
The database and recharge are gone
2. Common request methods
[groovy] string (string type variable definition | character type variable definition)
College Students' innovation project management system
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Sqoop installation