【平头哥RVB2601创意应用开发】模拟UART之二
[复制链接]
【平头哥RVB2601创意应用开发】模拟UART一 https://bbs.eeworld.com.cn/thread-1199753-1-1.html
继上篇,昨天调到晚上11点,早上6点起来,到现在,终于把接收写完了,调试一下还行。
接收相比发送要用到IO的中断,定时器的数据接,相对复杂多,我在调式中因为对串口启始位的判断认识不足,花了好些心思,这里把成功的函数附下:
//发送一个字符
void send_Char(uint8_t c)
{
aos_msleep(1);
io_uart0_tx_write_Low;//发送起始位 高拉低电
io_uart_para.vm_uart_tx_byte = c;
io_uart_para.vm_uart_tx_bit = 0;
io_uart_para.vm_uart_dir = VM_SEND;
io_uart0_rx_timer_enable();
while(io_uart_para.vm_uart_dir == IDLE);
}
//发送字串符
void send_str(uint8_t buf[],uint8_t len)
{
uint8_t i;
for(i<0; i<len; i++){
send_Char(buf[i]);
}
}
//定时器回调函数
void timer1_handler(void *arg)
{
if(io_uart_para.vm_uart_dir == VM_SEND)//如果是发送
{
if(io_uart_para.vm_uart_tx_bit <=8 ) //如果发送不足8位
{
if((io_uart_para.vm_uart_tx_byte & 0x01) == 0x01 ){
io_uart0_tx_write_High;
}else{
io_uart0_tx_write_Low;
}
io_uart_para.vm_uart_tx_byte>>=1;
}
else{
if(io_uart_para.vm_uart_tx_bit > 8){
io_uart0_tx_write_High;
//io_uart0_rx_timer_disable();
io_uart_para.vm_uart_tx_flag = 0; //设置为发送完毕
io_uart_para.vm_uart_dir = IDLE;
}
}
io_uart_para.vm_uart_tx_bit ++;
}
else if(io_uart_para.vm_uart_rx_irq == VM_REVC)
{
if(io_uart_para.vm_uart_rx_count == 0)//处理起始位
{
if(io_uart0_rx_ReadPin() == 0)//起始位为0时,清零接收缓冲器,准备接收数据位
{
io_uart_para.vm_uart_rx_count ++;
io_uart_para.vm_uart_tx_byte = 0;
//关闭接收IO中断
io_uart0_rx_IO_disable();
}
else //起始位不为 0 时,中止接收
{
io_uart_para.vm_uart_rx_irq = IDLE;
io_uart0_rx_IO_enable();
}
}
else if(io_uart_para.vm_uart_rx_count <= 8)
{
io_uart_para.vm_uart_rx_byte>>=1;
if(io_uart0_rx_ReadPin() == 1)
{
io_uart_para.vm_uart_rx_byte |= 0x80;
}
io_uart_para.vm_uart_rx_count ++;
}
else //停止位处理
{
io_uart0_rx_IO_enable();
io_uart_para.vm_uart_rx_count = 0;
io_uart_para.vm_uart_rx_irq = IDLE;
io_uart_para.vm_uart_rx_buf[io_uart_para.vm_uart_rx_flag] = io_uart_para.vm_uart_rx_byte;
io_uart_para.vm_uart_rx_flag = io_uart_para.vm_uart_rx_flag +1;
}
}
}
//io_uart0rx回调函数
static void io_uart0_rx_interrupt_handler(csi_gpio_pin_t *pin,void *arg)
{
if(csi_gpio_pin_read(&io_uart0_rx)==GPIO_PIN_LOW)
{
io_uart_para.vm_uart_rx_irq = VM_REVC;//接收标志
io_uart0_rx_IO_disable();
//关掉接收中断
}
}
效果如下:
实现效果还可以没有丢包乱码情况。但是这还是理想情况下的收发,不知道用到工程里会不会有什么BUG。欢迎大家指点一下。
附文件:
main.c
(5.42 KB, 下载次数: 0)
|