当前位置:网站首页>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}边栏推荐
猜你喜欢
随机推荐
SQL functions STR
The obstacles to put Istio into production and how we solve them
Detailed explanation of table join
【StoneDB Class】入门第二课:StoneDB 整体架构解析
【2022蓝帽杯】file_session && 浅入opcode
NFV迈向云原生时代:Network Service Mesh项目介绍
人像分割技术解析与应用
207.数组序号转换
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
四足机器人软件架构现状分析
postgresql之page分配管理(一)
意大利普拉托华社将游行示威 盼解决安全问题
Multithreading Case - Timer
28uA待机8米距离低压保护单片机探头太阳能灯人体PIR定制方案
【码蹄集新手村600题】判断一个数字是否为完全平方数
批量替换Word中的表格为图片并保存
PAT1166 Summit(25)
响应式2022英文企业官网源码,感觉挺有创意的
NebulaGraph v3.2.0 Performance Report
SQL function SQUARE









