1、只实现了,使用普通 IO 口模拟串口的发送,没有实现接收
2、由于是模拟发送的是 TTL 电平,所以在使用串口助手验证发送数据的准确性时,应该使用 USB 转 TTL ,而不能使用 USB 转 232(验证时,本人犯了这样的错误)
方法一:调节占空比,实现定时器延时
/对应波特率的1个电平持续时间
//(1/2400) = 416us
#define IO_USART_SENDDELAY_TIME 416
void uart_tx_bit(uint8_t bit) {
if (bit == 1) {
GPIO_SetBits(GPIOD, GPIO_Pin_12);
} else {
GPIO_ResetBits(GPIOD, GPIO_Pin_12);
}
}
void uart_tx_delayus(uint32_t nTime) {
uint16_t tmp;
//获得 TIM4 计数器的值
tmp = TIM_GetCounter(TIM4);
if (tmp + nTime <= 65535)
while ((TIM_GetCounter(TIM4) - tmp) < nTime);
else {
TIM_SetCounter(TIM4, 0);
//设置 TIM4 计数器寄存器值为0
while (TIM_GetCounter(TIM4) < nTime);
}
}
void uart_tx_byte(uint8_t data) {
uint8_t i, tmp;
// 开始位
uart_tx_bit(0); //将TXD的引脚的电平置低
uart_tx_delayus(IO_USART_SENDDELAY_TIME);
for (i = 0; i < 8; i++) {
tmp = (data >> i) & 0x01;
if (tmp == 0) {
uart_tx_bit(0);
uart_tx_delayus(IO_USART_SENDDELAY_TIME); //0
} else {
uart_tx_