Because the chip serial port is not enough, I have to use IO port to simulate the serial port. I downloaded a program to simulate the serial port on the Internet, which can be run. However, it is found that the output of the serial port will be garbled after several characters. The main part of the code is as follows:
#define OI_TXD PAout(12) #define OI_RXD PAin(11) #define BuadRate_9600 100 u8 len = 0; //Receive count u8 USART_buf[11]; //Receive buffer u8 recvStat = COM_STOP_BIT; u8 recvData = 0; void IO_TXD(u8 Data) { u8 i = 0; OI_TXD = 0; delay_us(BuadRate_9600); for(i = 0; i < 8; i++) { if(Data&0x01) OI_TXD = 1; else OI_TXD = 0; delay_us(BuadRate_9600); Data = Data>>1; } OI_TXD = 1; delay_us(BuadRate_9600); } void USART_Send(u8 *buf, u8 len) { u8 t; for(t = 0; t < len; t++) { IO_TXD(buf[t]); } } void IOConfig(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE); //Enable PB,PC port clock //SoftWare Serial TXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO port speed is 50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_12); //SoftWare Serial RXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource11); EXTI_InitStruct.EXTI_Line = EXTI_Line11; EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling; //Falling edge trigger interrupt EXTI_InitStruct.EXTI_LineCmd=ENABLE; EXTI_Init(&EXTI_InitStruct); NVIC_InitStructure.NVIC_IRQChannel= EXTI15_10_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); }
In general, there is output scrambling, which may be caused by insufficient delay in the output part. Therefore, check the use of delay in the output part, and find that there is no delay in USART_Send. Add a 1ms delay in USART_Send, and the error is removed. The changed code is as follows:int main(void) { delay_init(); IOConfig(); while(1) { USART_Send("100",3); delay_ms(100); } }
Although the delay is simple, it can really play a great role sometimes.void USART_Send(u8 *buf, u8 len) { u8 t; for(t = 0; t < len; t++) { IO_TXD(buf[t]); delay_ms(1); } }