這是某STM32教材串口寄存器編程的例子,要在教材配套的實驗版上驗證功能,我把程序修改了一下能用proteus里虛擬端口仿真。如果用VSPD驅動,還能用SSCOM等串口調試工具查看串口通信內容。
串口仿真
串口仿真1.png (48.77 KB, 下載次數: 23)
下載附件
2022-5-16 13:46 上傳
串口調試仿真
串口調試仿真.png (133.6 KB, 下載次數: 31)
下載附件
2022-5-16 13:58 上傳
單片機源程序如下:
- #include <stm32f10x.h>
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- int main(void)
- {
- u8 t;
- u8 len;
- u16 times=0;
- Stm32_Clock_Init(9); //系統時鐘設置
- delay_init(72); //延時初始化
- // uart_init(72,9600);//串口初始化為115200
- uart_init(72,9600);
- LED_Init(); //初始化與LED連接的硬件接口
- while(1)
- {
- if(USART_RX_STA&0x80)
- {
- if(USART_RX_BUF[0]=='d'){
-
- LED1=!LED1;
- }
-
- len=USART_RX_STA&0x3f;//得到此次接收到的數據長度
- for(t=0;t<len;t++)
- {
- USART1->DR=USART_RX_BUF[t];
- while((USART1->SR&0X40)==0);//等待發送結束
- }
- printf("\n\n");//插入換行
- USART_RX_STA=0;
- }
- else
- {
- times++;
- if(times%2000==0)
- {
- printf("\r\nSTM32核心板串口通信\r\n");
- printf("嵌入式技術與應用開發項目教程(STM32版)\r\n\r\n");
- }
- if(times%200==0)
- printf("請輸入數據,以回車鍵結束\n");
- if(times%30==0)
- LED0=!LED0;//閃爍LED,提示系統正在運行.
- delay_ms(10);
- }
- }
- }
復制代碼
Keil5代碼與Proteus8.13仿真下載(其他版本無法打開的,只能用8.13的Proteus):
STM32串口通信.7z
(328.57 KB, 下載次數: 170)
2022-5-16 18:18 上傳
點擊文件名下載附件
|