#申请原创#
【AT-START-L021 测评】串口通信测试 1. 计时器功能实现:串口打印计时器 每隔一秒串口输出数字依次加 1,从 0 开始,依次输出 0,1,2,...,n 代码主函数代码如下 - int main(void)
- {
- system_clock_config();
- at32_board_init();
- uart_print_init(115200);
- /* output a message on hyperterminal using printf function */
- printf("usart printf example: retarget the c library printf function to the usart\r\n");
- while(1)
- {
- printf("usart printf counter: %u\r\n",time_cnt++);
- delay_sec(1);
- }
- }
效果
2. 串口应答测试示例程序 modbus_character_match ,将USART的发送引脚 A9 与接收引脚 A10 用杜邦线连接,打开串口监视器,发送字符串,可以看到串口会回应相同的字符。 代码主函数代码
- int main(void)
- {
- system_clock_config();
- at32_board_init();
- usart_configuration();
- print_info();
- usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
-
- while(1)
- {
- if(rx_complete_flag == 1)
- {
- usart_interrupt_enable(USART1, USART_RDBF_INT, FALSE);
- rx_complete_flag = 0;
- at32_led_on(LED2);
- print_echo();
- at32_led_off(LED2);
- usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
- }
- }
- }
测试前需要将 发送 和 接收 端口连接,发送字符串,接收端接收到相同字符串。
效果
3. 半双工通信
半双工通信(half-duplex) 半双工通信允许信号在两个方向上传输,但某一时刻只允许信号在一个信道上单向传输。 典型应用:对讲机 由于对讲机传送及接收使用相同的频率,不允许同时进行。因此一方讲完后,需加上结束语OVER,另一方才开始讲话。
代码
主函数代码
- int main(void)
- {
- system_clock_config();
- at32_board_init();
- usart_configuration();
- /* usart2 transmit and usart1 receive */
- data_count = USART2_TX_BUFFER_SIZE;
- while(data_count)
- {
- while(usart_flag_get(USART2, USART_TDBE_FLAG) == RESET);
- usart_data_transmit(USART2, usart2_tx_buffer[USART2_TX_BUFFER_SIZE-data_count]);
- while(usart_flag_get(USART1, USART_RDBF_FLAG) == RESET);
- usart1_rx_buffer[USART2_TX_BUFFER_SIZE-data_count] = usart_data_receive(USART1);
- data_count--;
- }
- /* usart1 transmit and usart2 receive */
- data_count = USART1_TX_BUFFER_SIZE;
- while(data_count)
- {
- while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
- usart_data_transmit(USART1, usart1_tx_buffer[USART1_TX_BUFFER_SIZE-data_count]);
- while(usart_flag_get(USART2, USART_RDBF_FLAG) == RESET);
- usart2_rx_buffer[USART1_TX_BUFFER_SIZE-data_count] = usart_data_receive(USART2);
- data_count--;
- }
- while(1)
- {
- /* compare data buffer */
- if(buffer_compare(usart2_tx_buffer, usart1_rx_buffer, USART2_TX_BUFFER_SIZE) && \
- buffer_compare(usart1_tx_buffer, usart2_rx_buffer, USART1_TX_BUFFER_SIZE))
- {
- at32_led_toggle(LED2);
- at32_led_toggle(LED3);
- at32_led_toggle(LED4);
- delay_ms(200);
- }
- }
- }
连接 USART2 的发送端 PA2 和 USART1 的接收端 PB6 ,当接收到相同内容时,板载的 3 个 LED 闪烁。
效果
|