gpio analog serial communication

1. Transmission protocol of serial port

        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 bit, the data bit is 5~8 bits, the parity bit is 0 or 1 bit, and the stop bit is 1, 1.5 or 2 bits. However, the most commonly used format is 1 start bit, 8 data bits, no parity, and 1 stop bit, abbreviated as 8/N/1.

        The timing diagram of the 8/N/1 format is as follows:

        A logic 1 is specified on the data line when idle.

        When starting to transmit data, send the start bit first, which is specified as logic 0, and the receiving end will detect this falling edge, so that it can start sampling the received data later.

        After the start bit is the data bit, it is stipulated that the lowest bit is sent first, that is, LSB First. Because the UART has no clock signal, the baud rate is used to determine the length of each bit. However, to ensure the accuracy of the 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. The sampled value is taken as a result of this bit.

        Parity bits are generally not used.

        The stop bit generally uses 1 bit, which is defined 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 the chip with crystal oscillator frequency 11.0592M as the clock as an example, realize the serial port communication with the baud rate of 9600BPS (that is, it takes 1/9600 seconds to transmit one bit), timing time = timer count value * machine cycle = timer count value * 12 /Crystal oscillator frequency, now the timing time is 1/9600 seconds, timer count value=timing time*crystal oscillator frequency/12=110592/1152=96

        Supplement: The machine cycle indicates the time required to execute a cpu instruction. The clock cycle is 1/crystal oscillator frequency. 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       //设传输标志位
#define RXD P1_0         //通过P1_0 IO脚来模拟RXD功能引脚
#define TXD P1_1         //通过P1_0 IO脚来模拟TXD功能引脚

/* 计数器及中断初始化 */
void S2INI(void)
{
    TMOD |= 0x02;       //计数器0,方式2
    TH0 = 0xA0;         //预值为256-96=140,十六进制A0
    TL0 = TH0;
    TR0 = 0;            //在发送或接收才开始使用
    TF0 = 0;
    ET0 = 1;            //允许定时器0中断
    EA = 1;             //中断允许总开关
}
/* 从串口读一个字符 */
uchar RByte()
{
    uchar Output = 0;
    uchar i = 8;
    TR0 = 1;               //启动Timer0
    TL0 = TH0;
    WaitTF0();             //等过起始位
    
    //发送8位数据位
    while(i--)
    {
        Output >>= 1;
        if(RXD) Output |= 0x80;    //先收低位
        WaitTF0();                 //位间延时
    }
    while(!TM0_FLAG)
    {
        if(RXD) break;              //收到结束位
    }
    TR0 = 0;                        //停止
    Timer0
    return Output;
}

/* 往串口写一个字节 */
void WByte(uchar input)
{
    uchar i = 8;
    TXD = (bit)0;                    //发送启始位
    WaitTF0();
    
    //发送8位数据位
    while(i--)
    {
        TXD = (bit)(input & 0x01);  //先传低位
        WaitTF0();
        input = input >> 1;
    }
    
    TXD = (bit)1;                    //发送结束位
    WaitTF0();
}

/* 中断1处理程序 */
void IntTimer0() interrupt 1
{
    TM0_FLAG = 1;   //设置标志位。
}
/* 查询传输标志位 */
void WaitTF0( void )
{
    while(!TM0_FLAG);
    TM0_FLAG = 0;        //清标志位
}

Guess you like

Origin blog.csdn.net/qq_41076734/article/details/125979074