当前位置:网站首页>gpio analog serial communication
gpio analog serial communication
2022-08-01 13:33:00 【Four Seasons Sail】
1. Serial transmission protocol
UART works in asynchronous mode and does not require a clock signal. Its general format is: start bit + data bit + parity bit + stop bit.Among them, the start bit is 1, the data bit is 5~8, the parity bit is 0 or 1, and the stop bit is 1, 1.5 or 2.But the most commonly used format is 1 start bit, 8 data bits, no parity, 1 stop bit, abbreviated as 8/N/1.
The timing diagram of the 8/N/1 format is as follows:
When idle, the data line is specified as logic 1.
When starting to transmit data, the start bit is sent first, which is specified as logic 0. The receiving end will detect this falling edge so that it can start to sample the received data later.
The start bit is followed by the data bit, which stipulates that the lowest bit is sent first, that is, LSB First.Because the UART does not have a clock signal, the baud rate is used to determine the length of each bit. However, in order to ensure the accuracy of detection, the actual sampling frequency will be higher than the baud rate. Generally, each bit will be sampled several times, and the middle one will be taken.Sample value as the result of this bit.
The parity bit is generally not used.
The stop bit generally uses 1 bit, which is specified as logic 1. In addition to indicating the end of the transmission, the stop bit can also play a role in clock synchronization.
It should be noted that the logic 0 here is not necessarily 0V, which is related to the level standard used.For TTL level, logic 0 is 0V, logic 1 is high level (usually 3.3V or 5V); for RS-232 level, logic 0 is 3V~15V, logic 1 is -3~-15V.
2. gpio analog serial communication
Take a chip with a crystal frequency of 11.0592M as an example to implement serial communication with a baud rate of 9600BPS (that is, it takes 1/9600 seconds to transmit one bit), timing time = timer count value * machine cycle = timer countValue*12/crystal frequency, now the timing time is 1/9600 second, timer count value=timing time*crystal frequency/12=110592/1152=96
Supplementary: The machine cycle represents the time required to execute a cpu instruction, the clock cycle is 1/crystal frequency, and a machine cycle is generally composed of multiple clock cycles. For example, a machine cycle of a 51 single-chip microcomputer consists of 12 clock cycles.
#define TM0_FLAG 0 //Set the transmission flag#define RXD P1_0 //Use the P1_0 IO pin to simulate the RXD function pin#define TXD P1_1 //Use P1_0 IO pin to simulate TXD function pin/* Counter and interrupt initialization */void S2INI(void){TMOD |= 0x02; //counter 0, mode 2TH0 = 0xA0; //The preset value is 256-96=140, hexadecimal A0TL0 = TH0;TR0 = 0; //Start using when sending or receivingTF0 = 0;ET0 = 1; //Enable timer 0 interruptEA = 1; //interrupt enable master switch}/* Read a character from the serial port */uchar RByte(){uchar Output = 0;uchar i = 8;TR0 = 1; //Start Timer0TL0 = TH0;WaitTF0(); //Wait for the start bit//Send 8 data bitswhile(i--){Output >>= 1;if(RXD) Output |= 0x80; //Close the low firstWaitTF0(); //Delay between bits}while(!TM0_FLAG){if(RXD) break; //Receive end bit}TR0 = 0; //stopTimer0return Output;}/* Write a byte to the serial port */void WByte(uchar input){uchar i = 8;TXD = (bit)0; //transmit start bitWaitTF0();//Send 8 data bitswhile(i--){TXD = (bit)(input & 0x01); //Pass the low bit firstWaitTF0();input = input >> 1;}TXD = (bit)1; //Transmission end bitWaitTF0();}/* Interrupt 1 handler */void IntTimer0() interrupt 1{TM0_FLAG = 1; //Set the flag bit.}/* Query the transfer flag */void WaitTF0( void ){while(!TM0_FLAG);TM0_FLAG = 0; //clear the flag bit}
边栏推荐
- Towhee 每周模型
- iframe标签属性说明 详解[通俗易懂]
- 全链路灰度在数据库上我们是怎么做的?
- 求方阵的无穷范数「建议收藏」
- SQL function SQUARE
- 计算器:中缀表达式转后缀表达式
- 软件测试之发现和解决bug
- leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
- Do wildcard SSL certificates not support multiple domains?
- 高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
猜你喜欢
库函数的模拟实现(strlen)(strcpy)(strcat)(strcmp)(strstr)(memcpy)(memmove)(C语言)(VS)
Batch replace tables in Word with pictures and save
全球都热炸了,谷歌服务器已经崩掉了
shell 中的 分发系统 expect脚本 (传递参数、自动同步文件、指定host和要传输的文件、(构建文件分发系统)(命令批量执行))
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
数据挖掘-04
Data Mining-04
MCU开发是什么?国内MCU产业现状如何
使用open3d可视化3d人脸
脚本语言Lua的基础知识总结
随机推荐
脚本语言Lua的基础知识总结
How does the SAP ABAP OData service support the Create operation trial version
批量替换Word中的表格为图片并保存
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
数据挖掘-04
代理商替代义隆153 Aip4210
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
2022-07-29 网工进阶(二十二)BGP-其他特性(路由过滤、团体属性、认证、AS欺骗、对等体组、子路由器、路由最大接收数量)
预防和制止家庭暴力 人身安全保护令司法解释今起施行
计算器:中缀表达式转后缀表达式
一文带你彻底厘清 Kubernetes 中的证书工作机制
PAT1166 Summit(25)
魔众文档管理系统 v5.0.0
力扣160题,相交链表
D - I Hate Non-integer Number(背包dp)
34、树莓派进行人体姿态检测并进行语音播报
硬链接、软连接浅析
[Cloud Enjoying Freshness] Community Weekly Vol.73- DTSE Tech Talk: 1 hour in-depth interpretation of SaaS application system design
How to integrate 3rd party service center registration into Istio?