新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32單片機(jī)學(xué)習(xí)(3) 串口中斷通信

STM32單片機(jī)學(xué)習(xí)(3) 串口中斷通信

作者: 時(shí)間:2016-11-19 來(lái)源:網(wǎng)絡(luò) 收藏
/*
*	* 軟件功能:	 串口實(shí)驗(yàn)(軟件延時(shí)方式)* */#include "stm32f10x.h"#include #include "delay.h"u8 uart1_buf[20];int count=0;  //字符串長(zhǎng)度int flag=0;	  void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void USART1_Configuration(void);void Uart1_PutChar(u8 ch);void Uart1_PutString(u8* buf , u8 len);int USART1_ReceivedStrCmp(const char *str);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無(wú)返回: 無(wú)/int main(void){RCC_Configuration();NVIC_Configuration();GPIO_Configuration();delay_init(72);USART1_Configuration();GPIO_ResetBits(GPIOA,GPIO_Pin_0);//滅while(1){  //GPIO_ResetBits(GPIOA,GPIO_Pin_0);//滅//delay_ms(500);//延時(shí)//Uart1_PutString("Hello",5);//GPIO_SetBits(GPIOA,GPIO_Pin_0);//亮// delay_ms(500);//延時(shí)/* if(flag==1&&count!=0){Uart1_PutString(uart1_buf,count);count=0;flag=0;} 		 */}}/*函數(shù): void RCC_Configuration(void)功能: 復(fù)位和時(shí)鐘控制 配置參數(shù): 無(wú)返回: 無(wú)/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus;                    //定義外部高速晶體啟動(dòng)狀態(tài)枚舉變量RCC_DeInit();                                    //復(fù)位RCC外部設(shè)備寄存器到默認(rèn)值RCC_HSEConfig(RCC_HSE_ON);                       //打開(kāi)外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp();      //等待外部高速時(shí)鐘準(zhǔn)備好if(HSEStartUpStatus == SUCCESS)                  //外部高速時(shí)鐘已經(jīng)準(zhǔn)別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開(kāi)啟FLASH預(yù)讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時(shí)鐘起振之后FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延時(shí)RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)時(shí)鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)鐘==AHB時(shí)鐘RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)鐘==AHB1/2時(shí)鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  //配置PLL時(shí)鐘 == 外部高速晶體時(shí)鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE);                                   //使能PLL時(shí)鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL時(shí)鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //配置系統(tǒng)時(shí)鐘 = PLL時(shí)鐘while(RCC_GetSYSCLKSource() != 0x08)                  //檢查PLL時(shí)鐘是否作為系統(tǒng)時(shí)鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);  //允許 GPIOA、USART1、AFIO時(shí)鐘}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無(wú)返回: 無(wú)/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure;        //定義GPIO初始化結(jié)構(gòu)體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出 	 GPIO_Init(GPIOA, &GPIO_InitStructure); 	   //PA0輸出控制LED燈GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)合推挽輸出 	 GPIO_Init(GPIOA, &GPIO_InitStructure); 	   //PA9串口輸出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入GPIO_Init(GPIOA, &GPIO_InitStructure); 	   //PA10用于串口讀入}void NVIC_Configuration(void)	 //中斷分組和優(yōu)先級(jí)配置	 詳見(jiàn)《STM32的函數(shù)說(shuō)明(中文).pdf》P165{NVIC_InitTypeDef NVIC_InitStructure;//NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  // Set the Vector Table base location at 0x08000000 配置中斷向量偏移表的.默認(rèn)是FLASH的,所以你不設(shè)置也無(wú)關(guān)系. RAM調(diào)試的時(shí)候,必須設(shè)置.NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);	 //0組,先占優(yōu)先級(jí)1位,從優(yōu)先級(jí)3位NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //USART1 全局中斷//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; 	  //中斷響應(yīng)優(yōu)先級(jí)1NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能NVIC_Init(&NVIC_InitStructure); //初始化配置}/*函數(shù)名:USART1_Configuration輸  入:輸  出:功能說(shuō)明:初始化串口硬件設(shè)備,啟用中斷配置步驟:(1)打開(kāi)GPIO和USART1的時(shí)鐘(2)設(shè)置USART1兩個(gè)管腳GPIO模式(3)配置USART1數(shù)據(jù)格式、波特率等參數(shù)(4)使能USART1接收中斷功能(5)最后使能USART1功能*/void USART1_Configuration(void)	  //串口配置   詳見(jiàn)《STM32的函數(shù)說(shuō)明(中文).pdf》P346{USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate=9600;   //波特率為9600USART_InitStructure.USART_WordLength=USART_WordLength_8b;  //數(shù)據(jù)位為8USART_InitStructure.USART_StopBits=USART_StopBits_1; //在幀結(jié)尾傳輸 1 個(gè)停止位USART_InitStructure.USART_Parity=USART_Parity_No; //校驗(yàn)?zāi)J?奇偶失能USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //硬件流控制失能USART_InitStructure.USART_Mode=USART_Mode_Tx | USART_Mode_Rx; //USART_Mode 指定了使能或者失能發(fā)送和接收模式:發(fā)送使能|接收失能USART_Init(USART1, &USART_InitStructure);	  //初始化配置USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //使能或者失能指定的 USART 中斷	,此處為接收中斷USART_Cmd(USART1,ENABLE);	//使能或者失能 USART 外設(shè)USART_ClearFlag(USART1, USART_FLAG_TC);//清除傳輸完成標(biāo)志位,否則可能會(huì)丟失第1個(gè)字節(jié)的數(shù)據(jù).USART_FLAG_TC為發(fā)送完成標(biāo)志位}/*中斷服務(wù)程序*/void USART1_IRQHandler(void){	u8 dat;//u8 uart1_buf[6];if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)	//若接收完成{//GPIO_SetBits(GPIOA,GPIO_Pin_0);//點(diǎn)亮LED	              //delay_ms(500);//延時(shí)//GPIO_ResetBits(GPIOA,GPIO_Pin_0);//滅 dat=USART_ReceiveData(USART1);//Uart1_PutChar(dat);//uart1_buf[count++]=dat;dat = USART_ReceiveData(USART1);if(dat!=0x23)	 //!=#在發(fā)送的信息末尾應(yīng)包含#號(hào){uart1_buf[count++]=dat;if(count>19){Uart1_PutString("超出長(zhǎng)度![最長(zhǎng)20個(gè)字符/10個(gè)漢字]",32);count=0;}}else if(count!=0){//Uart1_PutString("Received Data:",14);//Uart1_PutString(uart1_buf,count);if(USART1_ReceivedStrCmp("我愛(ài)你")) Uart1_PutString("我也愛(ài)你",8);if(USART1_ReceivedStrCmp("Hello")) Uart1_PutString("Hi",2);count=0;}		USART_ClearFlag(USART1,USART_IT_RXNE);} //溢出處理-如果發(fā)生溢出需要先清除ORE,再讀DR寄存器 則可清除不斷入中斷的問(wèn)題if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET){USART_ClearFlag(USART1,USART_FLAG_ORE);        //清除OREUSART_ReceiveData(USART1);                                //讀DR}}//發(fā)送一個(gè)字符void Uart1_PutChar(u8 ch){USART_SendData(USART1, (u8) ch);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待發(fā)送完成}//發(fā)送一個(gè)字符串 Input : buf為發(fā)送數(shù)據(jù)的地址 , len為發(fā)送字符的個(gè)數(shù)void Uart1_PutString(u8* buf , u8 len){   u8 i;for(i=0;i
				
            
                
			
							

評(píng)論


技術(shù)專區(qū)

關(guān)閉