STM32單片機學習(12) 紅外信號接收解碼(外部中斷)
注:
請用質(zhì)量好點的遙控器實驗
用了劣質(zhì)遙控器浪費了一天時間,數(shù)據(jù)位接收總是不完整,后來用宿舍空調(diào)遙控器就解碼成功了
相關資料
STM32單片機學習(2) 外部中斷http://blog.csdn.net/leytton/article/details/38063335
STM32單片機學習(3) 串口中斷通信 http://blog.csdn.net/leytton/article/details/38393553
STM32單片機學習(7) 串口通信printf重定向http://blog.csdn.net/leytton/article/details/38393967
STM32-外設篇 視頻教程(Cortex-M3)-主講人:劉洋 http://yun.baidu.com/pcloud/album/info?uk=2853967793&album_id=5492137931588632574
效果如圖:格力空調(diào)遙控器紅外信號接收解碼,很多復合按鍵編碼,不同編碼實現(xiàn)不同空調(diào)功能貼代碼:
/** * 軟件功能: 紅外線接收* */#include "stm32f10x.h"#include#include "delay.h"void RCC_Configuration(void);void GPIO_Configuration(void);void USART1_Configuration(void);void Uart1_PutChar(u8 ch);void Uart1_PutString(u8* buf , u8 len);int fputc(int ch, FILE *f);void NVIC_Configuration(void);void EXTI_Configuration(void);u8 HW_ReceiveTime(void);/*函數(shù): int main(void)功能: main主函數(shù)參數(shù): 無返回: 無/int main(void){RCC_Configuration();GPIO_Configuration();delay_init(72);USART1_Configuration();NVIC_Configuration();EXTI_Configuration();printf("startn");GPIO_ResetBits(GPIOA,GPIO_Pin_0);//滅 while(1);}/*函數(shù): void RCC_Configuration(void)功能: 復位和時鐘控制 配置參數(shù): 無返回: 無/void RCC_Configuration(void){ErrorStatus HSEStartUpStatus; //定義外部高速晶體啟動狀態(tài)枚舉變量RCC_DeInit(); //復位RCC外部設備寄存器到默認值RCC_HSEConfig(RCC_HSE_ON); //打開外部高速晶振HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速時鐘準備好if(HSEStartUpStatus == SUCCESS) //外部高速時鐘已經(jīng)準別好{FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //開啟FLASH預讀緩沖功能,加速FLASH的讀取。所有程序中必須的用法.位置:RCC初始化子函數(shù)里面,時鐘起振之后FLASH_SetLatency(FLASH_Latency_2); //flash操作的延時RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)時鐘等于==SYSCLKRCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)鐘==AHB時鐘RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)鐘==AHB1/2時鐘RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //配置PLL時鐘 == 外部高速晶體時鐘 * 9 = 72MHzRCC_PLLCmd(ENABLE); //使能PLL時鐘while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL時鐘就緒{}RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系統(tǒng)時鐘 = PLL時鐘while(RCC_GetSYSCLKSource() != 0x08) //檢查PLL時鐘是否作為系統(tǒng)時鐘{}}RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE); //允許 GPIOA、USART1、AFIO時鐘}/*函數(shù): void GPIO_Configuration(void)功能: GPIO配置參數(shù): 無返回: 無/void GPIO_Configuration(void){GPIO_InitTypeDef GPIO_InitStructure; //定義GPIO初始化結(jié)構(gòu)體GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復合推挽輸出 GPIO_Init(GPIOA, &GPIO_InitStructure); //PA9串口輸出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //紅外接收GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //配置成上拉輸入; GPIO_Init(GPIOA, &GPIO_InitStructure);}/*函數(shù)名:USART1_Configuration輸 入:輸 出:功能說明:初始化串口硬件設備,啟用中斷配置步驟:(1)打開GPIO和USART1的時鐘(2)設置USART1兩個管腳GPIO模式(3)配置USART1數(shù)據(jù)格式、波特率等參數(shù)(4)使能USART1接收中斷功能(5)最后使能USART1功能*/void USART1_Configuration(void) //串口配置 詳見《STM32的函數(shù)說明(中文).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 個停止位USART_InitStructure.USART_Parity=USART_Parity_No; //校驗模式:奇偶失能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_Cmd(USART1,ENABLE); //使能或者失能 USART 外設USART_ClearFlag(USART1, USART_FLAG_TC);//清除傳輸完成標志位,否則可能會丟失第1個字節(jié)的數(shù)據(jù).USART_FLAG_TC為發(fā)送完成標志位}//發(fā)送一個字符void Uart1_PutChar(u8 ch){USART_SendData(USART1, (u8) ch);while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待發(fā)送完成}//發(fā)送一個字符串 Input : buf為發(fā)送數(shù)據(jù)的地址 , len為發(fā)送字符的個數(shù)void Uart1_PutString(u8* buf , u8 len){ u8 i;for(i=0;i =250) return t;//超時溢出}return t;}/*中斷服務程序/void EXTI9_5_IRQHandler() {u8 time=0,startSignal=0,Data=0,Count=0;u32 HW_ReceivedData=0;//printf("1n");if(EXTI_GetITStatus(EXTI_Line8) == SET)//檢查指定的EXTI線路觸發(fā)請求發(fā)生與否,返回一個EXTI_Line新狀態(tài){//printf("5n"); //if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) GPIO_SetBits(GPIOA,GPIO_Pin_0);//點亮LED//else GPIO_ResetBits(GPIOA,GPIO_Pin_0);//滅while(1){if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)==1){time = HW_ReceiveTime();if(time>=250){//printf("VoverTime:%xn",time); startSignal=0;HW_ReceivedData=0;Count=0;break;} else if(time>=200 && time<250){startSignal=1;//收到起始信號HW_ReceivedData=0;Count=0;continue;} else if(time>=60 && time<90) Data=1;//收到數(shù)據(jù) 1else if(time>=10 && time<50) Data=0;//收到數(shù)據(jù) 0//printf("%x ",Data);if(startSignal==1){HW_ReceivedData<<=1;HW_ReceivedData+=Data;Count++;if(Count>=32){//printf("received:%xn",HW_ReceivedData);if(HW_ReceivedData==0x1090000a) printf("紅外編碼:0x%8X,指令:打開空調(diào)n",HW_ReceivedData);else if(HW_ReceivedData==0x90000a) printf("紅外編碼:0x%08X,指令:關閉空調(diào)n",HW_ReceivedData);else printf("紅外編碼:0x%08X,指令:未知n",HW_ReceivedData);HW_ReceivedData=0;Count=0;startSignal=0;break;} } }}EXTI_ClearITPendingBit(EXTI_Line8);//清除線路掛起位EXTI_ClearFlag(EXTI_Line8);//清除中斷掛起位 } }
評論