当前位置:网站首页>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}边栏推荐
- 安全又省钱,“15岁”老小区用上管道燃气
- 消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
- Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- sql is not null 优化(oracle语句索引优化)
- Gradle系列——Gradle测试,Gradle生命周期,settings.gradle说明,Gradle任务(基于Groovy文档4.0.4)day2-3
- PAT1166 Summit(25)
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- 2022-07-29 网工进阶(二十二)BGP-其他特性(路由过滤、团体属性、认证、AS欺骗、对等体组、子路由器、路由最大接收数量)
- 为什么最大值加一等于最小值
猜你喜欢
随机推荐
易优压双驱挖掘机压路机器类网站源码 v1.5.8
硬链接、软连接浅析
搭建LNMT架构
【2022蓝帽杯】file_session && 浅入opcode
SQL函数 %SQLUPPER
全链路灰度在数据库上我们是怎么做的?
How to Integrate Your Service Registry with Istio?
fh511小风扇主控芯片 便携式小风扇专用8脚IC 三档小风扇升压芯片sop8
postgresql之page分配管理(一)
多线程案例——阻塞式队列
Istio Pilot代码深度解析
Detailed explanation of table join
Towhee 每周模型
Windows 安装PostgreSQL
测试发文
PAT 1167 Cartesian Tree(30)
如何使用OpenCV测量图像中物体之间的距离
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
SQL函数 %SQLSTRING
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能








