欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
stm32單片機萬能遙控器,親測可用控制空調(diào)
[打印本頁]
作者:
211209
時間:
2020-5-9 18:40
標題:
stm32單片機萬能遙控器,親測可用控制空調(diào)
紅外接收頭收到紅外碼后馬上發(fā)送一個一樣的紅外碼出去,需要做萬能遙控的自行保存紅外碼再發(fā)出去即可
單片機源程序如下:
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "Infrared.h"
int main(void)
{
u8 t;
u8 len;
u16 times=0;
delay_init(); //延時函數(shù)初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設置中斷優(yōu)先級分組2
uart_init(9600); //串口初始化為9600
TIM2_PWM_Init(1895,0);
Infrared_GPIO_Configuration();
Infrared_EXTI_Configuration();
while(1)
{
}
}
復制代碼
/***************************************************************************************
* FileName : Infrared.c
* CopyRight :
* ModuleName :
*
* CPU :
* RTOS :
*
* Create Data : 2015/04/21
* Author/Corportation : Ray
*
* Abstract Description :
*
*--------------------------------Revision History--------------------------------------
* No version Data Revised By Item Description
* 1 v1.0 2015/4/21 Ray Create this file
* 2 v2.0 2015/4/23 Ray Sucessfully control the air conditioner
* 3 v2.1 2015/4/24 Ray Packaging the Infrared Module
*
***************************************************************************************/
/**************************************************************
* Debug switch Section
**************************************************************/
/**************************************************************
* Include File Section
**************************************************************/
#include "Infrared.h"
/**************************************************************
* Macro Define Section
**************************************************************/
//debug調(diào)試宏定義,根據(jù)表達式a的真假執(zhí)行has_bug或no_bug
#define BUG_DETECT_PRINT(a,has_bug,no_bug) { if(a) \
printf("%s",has_bug); \
else \
printf("%s",no_bug);}
/**************************************************************
* Struct Define Section
**************************************************************/
/**************************************************************
* Prototype Declare Section
**************************************************************/
/**************************************************************
* Global Variable Declare Section
**************************************************************/
u8 Flag_LearnState = 0;
u16 PulseTab[MAX_PULSE_LEN];
/**************************************************************
* File Static Variable Define Section
**************************************************************/
/**************************************************************
* Function Define Section
**************************************************************/
#ifdef INFRARED_RECEIVE
/**
* @name void Infrared_GPIO_Configuration()
* @description 紅外接收端GPIO口設置
* @param PA.01 作外部中斷接收口
* @return
* @notice
*/
void Infrared_GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitType;
RCC_APB2PeriphClockCmd(INFRARED_RCC_GPIOx,ENABLE);
GPIO_InitType.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
GPIO_InitType.GPIO_Pin = INFRARED_GPIO_Pinx;
GPIO_InitType.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(INFRARED_GPIOx,&GPIO_InitType);
GPIO_EXTILineConfig(INFRARED_EXTI_GPIOx,INFRARED_EXTI_Line);
}
/**
* @name void Infrared_EXTI_Configuration()
* @description 紅外線接受端外部中斷初始化設置,設置為 線路1,使用PA.1作為外部中斷的輸入端
* @param
* @return
* @notice 若改變外部中斷的輸入端,此函數(shù)內(nèi)的線路設置也需改變s
*/
void Infrared_EXTI_Configuration()
{
EXTI_InitTypeDef EXTI_InitType;
NVIC_InitTypeDef NVIC_InitType;
EXTI_InitType.EXTI_Line = EXTI_Line1;
EXTI_InitType.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitType.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitType.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitType);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //NVIC 中斷設置
NVIC_InitType.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitType.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitType.NVIC_IRQChannelSubPriority = 0;
NVIC_InitType.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitType);
}
/**
* @name void EXTI1_IRQHandler()
* @description 外部中斷1中斷處理程序,用于采集紅外波形
* @param
* @return 全局變量Flag_LearnState可以用于返回是否有學習到波形
* @notice
*/
void EXTI1_IRQHandler()
{
u16 pulseWidth = 0;
u16 i = 0;
Flag_LearnState = 0;
//中斷指示
while(1)
{
if(IR_RDATA) //有高脈沖出現(xiàn),代表空閑信號
{
pulseWidth = 0;
while(IR_RDATA)
{
pulseWidth++;
delay_us(19);
if(pulseWidth >= 2000) // >40ms 則結束記錄
break;
}
if(pulseWidth<=15 || pulseWidth>=2000) // >40ms || <300us 則結束記錄
break;
PulseTab[i] = pulseWidth*20;
i++;
}
else //載波信號,偶數(shù)位為低電平(載波),奇數(shù)位為高電平(空閑)
{
pulseWidth = 0;
while(IR_RDATA == 0)
{
pulseWidth++;
delay_us(19);
}
if(pulseWidth<=15 || pulseWidth>=2000) // >40ms || <300sus 則結束記錄
break;
PulseTab[i] = pulseWidth*20;
i++;
}
}
PulseTab[i++] = pulseWidth;
PulseTab[i] = 0xffff;
Flag_LearnState = 1;
Infrared_Send();
EXTI_ClearITPendingBit(EXTI_Line1);
return;
}
#endif
#ifdef INFRARED_SEND
/**
* @name void Infrared_Send()
* @description 紅外發(fā)射,根據(jù) PulseTab[]內(nèi)的數(shù)據(jù)發(fā)波形
* @param
* @return
* @notice
*/
void Infrared_Send()
{
u16 i;
EXTI->IMR &= ~(0x00000002); //關中斷,避免發(fā)送的紅外線被自己接受
for(i=0; i<MAX_PULSE_LEN && PulseTab[i]!=0xffff; i++)
{
if(i%2 == 0)
{
TIM_Cmd(TIM2,ENABLE);
delay_us(PulseTab[i]);
TIM_Cmd(TIM2,DISABLE);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_0);
delay_us(PulseTab[i]);
}
}
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
EXTI->IMR |= (0x00000002); //開中斷
}
/**
* @name void TIM2_PWM_Init(u16 arr,u16 psc)
* @description 初始化定時器2的設置,將定時器2用于PWM調(diào)制,PWM輸出口為 PA.0
* @param arr -- u16,定時器重裝值
psc -- u16,定時器分頻值
* @return
* @notice PWM頻率 = 72M/((arr+1)*(psc+1)),這里用作紅外發(fā)射的載波,需要生成38kHz的方波,故取arr = 1895,psc = 0。
*/
void TIM2_PWM_Init(u16 arr,u16 psc)
{
/* 初始化結構體定義 */
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* 使能相應端口的時鐘 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定時器2時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外設時鐘
/* GPIOA.0初始化 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // TIM2 CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // PA.0 復用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
/* TIM2 初始化*/
TIM_TimeBaseInitStructure.TIM_Period = arr; //下一個更新事件裝入活動的自動重裝載寄存器周期的值
TIM_TimeBaseInitStructure.TIM_Prescaler = psc; //作為TIMx時鐘頻率除數(shù)的預分頻值
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0; //時鐘分割:TDTS = Tck_tim
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
/* 定時器TIM2 Ch1 PWM模式初始化 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM PWM1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
//TIM_OCInitStructure.TIM_Pulse = (arr+1)/2; //占空比 50%
TIM_OCInitStructure.TIM_Pulse = (arr+1)/3; //占空比1:3
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* 使能TIM2在CCR1上的預裝載寄存器 */
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* 使能定時器 */
// TIM_Cmd(TIM2,ENABLE);
}
#endif
/**
* @name
* @description
* @param
* @return
* @notice
*/
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
while (1)
{}
}
#endif
復制代碼
所有資料51hei提供下載:
空調(diào).7z
(195.4 KB, 下載次數(shù): 129)
2020-5-9 19:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
作者:
lyseg
時間:
2020-5-12 18:32
好東西,學習一下!!!
作者:
lbq691477940
時間:
2020-5-23 23:24
http://m.raoushi.com/bbs/dpj-127769-1.html
作者:
LIKEY1688
時間:
2020-5-24 15:17
好東西!!!多謝
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1