当前位置:网站首页>51 single chip microcomputer learning record (VI) IIC, ADC part
51 single chip microcomputer learning record (VI) IIC, ADC part
2022-06-10 01:40:00 【Taochengyi 2.0】
List of articles
51 SCM is almost like this , The relevant code can be seen in my gitee, It can reduce the pain of making your own wheels :51 SCM learning records
1、iic part
About IIC Principle , It helps to understand , Yes. iic What is not clear can be seen :
stm32 Configuration summary -iic Use
Summary of the communication protocol frequently asked in the interview
Here, the development board is also connected with one AT24C02 To be used as IIC experiment , This chip is used to do iic Learning is still very classic 
This was written before stm32 Related articles :stm32 Configuration summary -iic Use , You can check it if you need it .
here 51 MCU has no hardware iic, We need to write our own software iic, Here we directly refer to the routine of Puzhong technology :
#include "iic.h"
void iic_start(void)
{
IIC_SDA = 1;
delay_10us(1);
IIC_SCL = 1;
delay_10us(1);
IIC_SDA = 0; // When SCL For high voltage ,SDA From high to low
delay_10us(1);
IIC_SCL = 0; // Hold on I2C Bus , Ready to send or receive data
delay_10us(1);
}
void iic_stop(void)
{
IIC_SDA = 0;
delay_10us(1);
IIC_SCL = 1;
delay_10us(1);
IIC_SDA = 1; // When SCL For high voltage ,SDA From low to high
delay_10us(1);
}
void iic_ack(void)
{
IIC_SCL = 0;
IIC_SDA = 0; // SDA Low level
delay_10us(1);
IIC_SCL = 1;
delay_10us(1);
IIC_SCL = 0;
}
void iic_nack(void)
{
IIC_SCL = 0;
IIC_SDA = 1; // SDA High level
delay_10us(1);
IIC_SCL = 1;
delay_10us(1);
IIC_SCL = 0;
}
u8 iic_wait_ack(void)
{
u8 time_temp = 0;
IIC_SCL = 1;
delay_10us(1);
while (IIC_SDA) // wait for SDA Low level
{
time_temp++;
if (time_temp > 100) // If the timeout occurs, it will be forced to end IIC signal communication
{
iic_stop();
return 1;
}
}
IIC_SCL = 0;
return 0;
}
void iic_write_byte(u8 dat)
{
u8 i = 0;
IIC_SCL = 0;
for (i = 0; i < 8; i++) // loop 8 One byte at a time , Pass high and then low
{
if ((dat & 0x80) > 0)
IIC_SDA = 1;
else
IIC_SDA = 0;
dat <<= 1;
delay_10us(1);
IIC_SCL = 1;
delay_10us(1);
IIC_SCL = 0;
delay_10us(1);
}
}
u8 iic_read_byte(u8 ack)
{
u8 i = 0, receive = 0;
for (i = 0; i < 8; i++) // loop 8 Read one byte at a time , Read high and then pass low
{
IIC_SCL = 0;
delay_10us(1);
IIC_SCL = 1;
receive <<= 1;
if (IIC_SDA)
receive++;
delay_10us(1);
}
if (!ack)
iic_nack();
else
iic_ack();
return receive;
}
Reading and writing at24c02 Part of it is also :
#include "at24c02.h"
void at24c02_write_one_byte(u8 addr,u8 dat)
{
iic_start();
iic_write_byte(0XA0); // Send write command
iic_wait_ack();
iic_write_byte(addr); // Send write address
iic_wait_ack();
iic_write_byte(dat); // Send byte
iic_wait_ack();
iic_stop(); // Create a stop condition
delay_ms(10);
}
u8 at24c02_read_one_byte(u8 addr)
{
u8 temp=0;
iic_start();
iic_write_byte(0XA0); // Send write command
iic_wait_ack();
iic_write_byte(addr); // Send write address
iic_wait_ack();
iic_start();
iic_write_byte(0XA1); // Enter receive mode
iic_wait_ack();
temp=iic_read_byte(0); // Read byte
iic_stop(); // Create a stop condition
return temp; // Return the read data
}
In this way, you can read and write in the main function .
2、ADC part
here 51 There seems to be no SCM ADC On chip peripherals , So we need something else AD Module to realize the corresponding functions , It's used here XPT2046 This chip is used to realize , Let's take a look at how this chip realizes its corresponding functions :
It can be seen that this chip was originally used as a touch screen chip , It's a 12 Bit AD, Monitoring range 0 To 6V. The following is a description of the relevant pins :
The operating mode is described as follows :
Selection of single ended mode and differential mode :(ADC Just direct single ended mode )
Use IIC And so on , The sequence diagram is as follows :
The explanation is as follows :
The command description of the control word is as follows :
The final code is as follows :
#include "adc.h"
#include "intrins.h"
void xpt2046_wirte_data(u8 dat)
{
u8 i;
CLK = 0;
_nop_();
for (i = 0; i < 8; i++) // loop 8 Time , Transmit one bit at a time , One byte in total
{
DIN = dat >> 7; // Pass high and then low
dat <<= 1; // Move low to high
CLK = 0; // CLK From low to high produces a rising edge , To write data
_nop_();
CLK = 1;
_nop_();
}
}
u16 xpt2046_read_data(void)
{
u8 i;
u16 dat = 0;
CLK = 0;
_nop_();
for (i = 0; i < 12; i++) // loop 12 Time , Read one bit at a time , Greater than one byte , So the return value type is u16
{
dat <<= 1;
CLK = 1;
_nop_();
CLK = 0; // CLK A falling edge is produced from high to low , To read the data
_nop_();
dat |= DOUT; // Read the high bit first , Reread low order .
}
return dat;
}
u16 xpt2046_read_adc_value(u8 cmd)
{
u8 i;
u16 adc_value = 0;
CLK = 0; // Turn down the clock first
CS = 0; // Can make XPT2046
xpt2046_wirte_data(cmd); // Send command word
for (i = 6; i > 0; i--)
; // Delay waiting for conversion results
CLK = 1;
_nop_();
CLK = 0; // Send a clock , eliminate BUSY
_nop_();
adc_value = xpt2046_read_data();
CS = 1; // close XPT2046
return adc_value;
}
3、DAC part
here DAC It's through PWM Combined with the circuit , I've already introduced ADC Related content , I won't repeat it here ,ADC The working principle of is as follows 
The circuits used are as follows , Here we want to realize ADC The function of , As long as the output corresponds to PWM that will do .
边栏推荐
- Short video live broadcast source code, customize the circular playback of pictures or videos
- Luogu p1220 turn off street light problem solving section DP
- Practice of Flink CDC + Hudi massive data entering the lake in SF
- [image classification case] (10) three classifications of animal images of vision transformer, with complete pytoch code attached
- Beyond Compare 3密钥序列号分享及密钥被撤销的解决办法
- 【webrtc】PCF和PC的构建、会话与媒体协商 流程
- What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?
- The JS mouse changes the font color and returns to normal
- 【LeetCode】114. 二叉树展开为链表
- 985毕业,35岁创业失败,36岁回炉40岁被裁,中年夫妻无业咋办?
猜你喜欢

Hoo虎符研究院 | 币圈后浪-类宝可梦的GameFi:EvoVerses

MySQL multi table query

MySQL 多表查询

Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)

LeetCode 700:二叉搜索树中的搜索

iNFTnews | 元宇宙进行时:那些跑步入场的互联网大厂在如何谋篇布局?

What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?

How can I right-click win11 to open all options directly?

IDEA 版 Postman问世,亲测好用

Locust: a powerful tool for microservice performance testing
随机推荐
[email protected] 项目实训
【神经网络】(22) ConvMixer 代码复现,网络解析,附TensorFlow完整代码
Leetcode 530: minimum absolute difference of binary search tree
短视频直播源码,自定义图片或视频的循环播放
[neural network] (22) convmixer code reproduction, network analysis, complete tensorflow code attached
Using GPU accelerated training model in keras; Install CUDA; cudnn; cudnn_ cnn_ infer64_ 8.dll is not in path; device_ lib. list_ local_ Devices has no GPU; hang up
【LeetCode】437.路径总和III
【FPGA】day16-FIFO实现uart协议
Curriculum Learning and Graph Neural Networks (or Graph Structure Learning)
【webrtc】PCF和PC的构建、会话与媒体协商 流程
A small problem in installing composer
From these papers in 2022, we can see the trend of recommended system sequence modeling
[從零開始學習FPGA編程-16]:快速入門篇 - 操作步驟2-4- Verilog HDL語言描述語言基本語法(軟件程序員和硬件工程師都能看懂)
Trichview and scalericview set default Chinese
IDEA 版 Postman问世,亲测好用
Rhcsa day 7
加密机与数据库加密产品的区别?
985毕业,35岁创业失败,36岁回炉40岁被裁,中年夫妻无业咋办?
d内存中映射指令并执行
Software engineering final review