串口初始化見:
STM32F407初學之串口:查詢方式收發字符串教程與源碼
http://www.zg4o1577.cn/bbs/dpj-135607-1.html
(出處: 單片機論壇)
串口中斷步驟
1.串口初始化(略)
2.配置NVIC(設置優先級別,讓NVIC相應該中斷)
NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority((7-3),2,2));//core_cmd.h->1629
NVIC_EnableIRQ(USART1_IRQn); //core_cmd.h->1547
3.模塊級中斷使能(使能串口中斷)
USART1->CR1 |=0x01<<5;
4.編寫中斷服務函數。//函數名在啟動文件.S100行附近
- #include "includes.h"
- int main(void)
- {
- u8 data=10;
-
- //NVIC優先級分組
- NVIC_SetPriorityGrouping(7-2) ;
- LED_Init( );//LED初始化
- Key_Init( );
- Uart1_Init(84,115200);
-
-
- while(1)
- {
- if(revice_ok)
- {
- revice_ok=0;//清除標志
- printf("%s\r\n",rev_buf);//回發數據給PC端
- if( strcmp((const char *)rev_buf,(const char *)"OPEN") ==0)
- {
- LED1(0);//LED2(0);LED3(0);LED4(0);
- }
- if( strcmp((const char *)rev_buf,(const char *)"CLOSE")==0 )
- {
- LED1(1);//LED2(1);LED3(1);LED4(1);
- }
- }
-
-
- }
-
- }
復制代碼
全部資料51hei下載地址:
串口中斷接收字符串-添加特殊字符作結尾.rar
(1.22 MB, 下載次數: 63)
2018-9-22 21:58 上傳
點擊文件名下載附件
|