包含LCD1602顯示,串口發(fā)送接收,完美實(shí)現(xiàn)。
文檔內(nèi)容齊全,包含使用說明,相關(guān)驅(qū)動等。
解決了STM32的Proteus串口收發(fā)問題。 注意:每輸入一個(gè)字符后,要按一次“手動發(fā)送”按鈕,才能收到正確字符。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
效果顯示.jpg (233.07 KB, 下載次數(shù): 137)
下載附件
串口收發(fā)
2021-2-10 10:38 上傳
Proteus的串口仿真打開串口調(diào)試助手,如圖11-30所示。進(jìn)入串口調(diào)試助手后,需要設(shè)置相關(guān)參數(shù),串口號為圖11-29所查到的端口號,其他參數(shù)根據(jù)實(shí)際程序來確定。打開串口后,按開發(fā)板上的reset按鍵,顯示區(qū)就能接收到從開發(fā)板發(fā)送過來的“Welcome to HBEU”,每按復(fù)位鍵一次,就會接收一次,如圖11-31所示;在發(fā)送區(qū)輸入字符,如圖11-32所示,點(diǎn)擊“手動發(fā)送”,開發(fā)板上就能收到對應(yīng)的字符,根據(jù)要求,不是以“x”結(jié)束的字符串,當(dāng)超過20個(gè)字符后也接收到字符串。 圖11-29 查看串口號 在Proteus中仿真串口時(shí),先安裝“虛擬串口”驅(qū)動,如圖11-33所示,安裝完之后也可以查看虛擬串口的端口號,查看方法與圖11-29一致。在串口助手中設(shè)置好串口參數(shù)后,按Proteus中運(yùn)行按鈕,也可以在串口助手收到信息,如圖11-34所示,注意串口波特率。 圖11-30 串口助手 圖11-31 STM32發(fā)送數(shù)據(jù) 圖11-32 STM32串口接收數(shù)據(jù) 圖11-33 虛擬串口驅(qū)動文件 圖11-34 Proteus仿真串口接收數(shù)據(jù) 由于Proteus沒有對stm32的時(shí)鐘樹做仿真模型,只固定了一套時(shí)鐘值,可以用GETSYS/HCLK/PCLK函數(shù)看。Proteus不是實(shí)時(shí)仿真,所以串口速率不是硬件速率對應(yīng)的值,可能更高,可能更低,好在Proteus的虛擬終端是可以輸入非標(biāo)準(zhǔn)波特率的,推薦波特率設(shè)為9600bit/s。另外,在Proteus仿真的程序中加了一條語句: RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); 若沒有該系統(tǒng)配置語句,則看不到仿真效果。仿真時(shí)鐘跟實(shí)際時(shí)鐘存在一定的延時(shí)誤差,在上位機(jī)發(fā)送數(shù)據(jù)時(shí),一次發(fā)送一個(gè)字符串時(shí),接收會出現(xiàn)亂碼,因此需要一個(gè)一個(gè)字符發(fā)送,如圖11-35所示,在發(fā)送區(qū)依次輸入“e1x”之后(注意:每輸入一個(gè)字符后,要按一次“手動發(fā)送”按鈕),虛擬終端(Virtual Terminal)會顯示所發(fā)送的所有字符,當(dāng)STM32的串口接收到字符“x”后,表示結(jié)束,在LCD屏上顯示“e1”。 為了接收方便,將最多發(fā)送20個(gè)字符改為最多發(fā)送5個(gè)字符,當(dāng)我們依次輸入“123456”后(注意:每輸入一個(gè)字符后,要按一次“手動發(fā)送”按鈕),LCD顯示收到的字符串“12345”,如圖11-36所示。 圖11-35 STM32串口遇到“x”結(jié)束接收 圖11-36 STM32串口收到6個(gè)字符自動結(jié)束
單片機(jī)main文件源代碼:
- #include "stm32f10x.h"
- #include "stdio.h"
- //#include "led.h"
- #include "delay.h"
- #include "lcd1602.h"
- uint8_t USART_RXBUF[5];
- extern uint8_t RXOVER;
- /* Private function prototypes -----------------------------------------------*/
- void NVIC_Configuration(void);
- //void Delay_Ms(uint32_t nTime);
- void USART_Config(void);
- //void USART_SendString(int8_t *str);
- void USART_SendString(unsigned char *buf);
- int main(void)
- {
- uint8_t i;
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
- //SysTick_Init();
- SysTick_Config(SystemCoreClock/100000);
- GPIO_Configuration();
- LCD1602_Init();
-
- LCD1602_Show_Str(0,0,"Receive:");
- USART_Config();
- //USART_SendString(Tx_Buf);//發(fā)送字符串
- USART_SendString("Welcome to HBEU\r\n");//發(fā)送字符串
- while(1)
- {
- if(RXOVER == 1){
- LCD1602_Show_Str(0,2,USART_RXBUF);
-
- for(i=0;i<5;i++){
- USART_RXBUF[i] =' '; //清空接收區(qū)
- }
- RXOVER = 0;
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);/////////////////////////////////////////////////////////////
- }
-
- /*if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
- {
- USART_SendData(USART1,USART_ReceiveData(USART1));
- delay_ms(1000);
- }*/
-
- }
- }
- /*void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
- //RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
-
- //NVIC_Configuration();
- //配置USART2 TX引腳工作模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //配置USART2 RX引腳工作模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //串口2工作模式配置
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
-
- //USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1, ENABLE);
- }*/
- void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- // 打開串口GPIO的時(shí)鐘
- //DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
-
- // 打開串口外設(shè)的時(shí)鐘
- //DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- NVIC_Configuration();
- // 將USART Tx的GPIO配置為推挽復(fù)用模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- // 將USART Rx的GPIO配置為浮空輸入模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- // 配置串口的工作參數(shù)
- // 配置波特率
- USART_InitStructure.USART_BaudRate = 9600;
- // 配置 針數(shù)據(jù)字長
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- // 配置停止位
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- // 配置校驗(yàn)位
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- // 配置硬件流控制
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- // 配置工作模式,收發(fā)一起
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- // 完成串口的初始化配置
- USART_Init(USART1, &USART_InitStructure);
- // 使能串口
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
- USART_Cmd(USART1, ENABLE);
- }
- /**
- * @說明 USART2字符串發(fā)送函數(shù)
- * @參數(shù) str: 指向字符串的指針
- * @返回值 None
- */
- /*void USART_SendString(int8_t *str)
- {
- uint8_t index = 0;
- do
- {
- USART_SendData(USART1,str[index]);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
- index++;
- }
- while(str[index] != 0); //檢查字符串結(jié)束標(biāo)志
- }*/
- void USART_SendString(unsigned char *buf)
- {
- while (*buf != '\0')
- {
- while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- USART_SendData(USART1, *buf++);
- }
- }
- /**
- * @說明 配置中斷向量控制器
- * @參數(shù) None
- * @返回值 None
- */
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /* Enable the RTC Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
復(fù)制代碼 Proteus仿真工程與代碼51hei下載:
資料.7z
(7.93 MB, 下載次數(shù): 476)
2021-2-10 16:54 上傳
點(diǎn)擊文件名下載附件
STM32仿真 下載積分: 黑幣 -5
|