|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#ifndef __RED_H
#define __RED_H
#include "sys.h"
#define RDATA PAin(1) //ºìÍâÊý¾ÝÊäÈë½Å
//ºìÍâÒ£¿Øʶ±ðÂë(ID),ÿ¿îÒ£¿ØÆ÷µÄ¸ÃÖµ»ù±¾¶¼²»Ò»Ñù,µ«Ò²ÓÐÒ»ÑùµÄ.
//ÎÒÃÇÑ¡ÓõÄÒ£¿ØÆ÷ʶ±ðÂëΪ0
#define REMOTE_ID 0
extern u8 RmtCnt; //°´¼ü°´ÏµĴÎÊý
void Remote_Init(void); //ºìÍâ´«¸ÐÆ÷½ÓÊÕÍ·Òý½Å³õʼ»¯
u8 Remote_Scan(void);
#endif
#include "remote.h"#include "delay.h"
#include "usart.h"
//ºìÍâÒ£¿Ø³õʼ»¯
//ÉèÖÃIOÒÔ¼°¶¨Ê±Æ÷4µÄÊäÈ벶»ñ
void Remote_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //ʹÄÜPORTBʱÖÓ PA1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5 ʱÖÓʹÄÜ TIM5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1 ÊäÈë
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //ÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_1); //³õʼ»¯GPIOA1
TIM_TimeBaseStructure.TIM_Period = 10000; //É趨¼ÆÊýÆ÷×Ô¶¯ÖØ×°Öµ ×î´ó10msÒç³ö
TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //Ô¤·ÖƵÆ÷,1MµÄ¼ÆÊýƵÂÊ,1us¼Ó1.
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMx
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; // Ñ¡ÔñÊäÈë¶Ë IC2Ó³Éäµ½TI5ÉÏ
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //ÉÏÉýÑز¶»ñ
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃÊäÈë·ÖƵ,²»·ÖƵ
TIM_ICInitStructure.TIM_ICFilter = 0x03;//IC4F=0011 ÅäÖÃÊäÈëÂ˲¨Æ÷ 8¸ö¶¨Ê±Æ÷ʱÖÓÖÜÆÚÂ˲¨
TIM_ICInit(TIM5, &TIM_ICInitStructure);//³õʼ»¯¶¨Ê±Æ÷ÊäÈ벶»ñͨµÀ
TIM_Cmd(TIM5,ENABLE ); //ʹÄܶ¨Ê±Æ÷5
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏÈÕ¼ÓÅÏȼ¶0¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //´ÓÓÅÏȼ¶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀ±»Ê¹ÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷
TIM_ITConfig( TIM5,TIM_IT_Update|TIM_IT_CC2,ENABLE);//ÔÊÐí¸üÐÂÖÐ¶Ï ,ÔÊÐíCC2IE²¶»ñÖжÏ
}
//Ò£¿ØÆ÷½ÓÊÕ״̬
//[7]:ÊÕµ½ÁËÒýµ¼Âë±êÖ¾
//[6]:µÃµ½ÁËÒ»¸ö°´¼üµÄËùÓÐÐÅÏ¢
//[5]:±£Áô
//[4]:±ê¼ÇÉÏÉýÑØÊÇ·ñÒѾ±»²¶»ñ
//[3:0]:Òç³ö¼ÆʱÆ÷
u8 RmtSta=0;
u16 Dval; //ϽµÑØʱ¼ÆÊýÆ÷µÄÖµ
u32 RmtRec=0; //ºìÍâ½ÓÊÕµ½µÄÊý¾Ý
u8 RmtCnt=0; //°´¼ü°´ÏµĴÎÊý
//¶¨Ê±Æ÷5ÖжϷþÎñ³ÌÐò
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)//¸üÐÂÖжÏ
{
if(RmtSta&0x80)//ÉÏ´ÎÓÐÊý¾Ý±»½ÓÊÕµ½ÁË
{
RmtSta&=~0X10; //È¡ÏûÉÏÉýÑØÒѾ±»²¶»ñ±ê¼Ç
if((RmtSta&0X0F)==0X00)RmtSta|=1<<6;//±ê¼ÇÒѾÍê³ÉÒ»´Î°´¼üµÄ¼üÖµÐÅÏ¢²É¼¯
if((RmtSta&0X0F)<14)RmtSta++; //130ms
else //³¬¹ý130ms
{
RmtSta&=~(1<<7);//Çå¿ÕÒýµ¼±êʶ
RmtSta&=0XF0; //Çå¿Õ¼ÆÊýÆ÷
}
}
}
if(TIM_GetITStatus(TIM5,TIM_IT_CC2)!=RESET)//²¶»ñÖжÏ
{
if(RDATA)//ÉÏÉýÑز¶»ñ
{
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Falling); //CC1P=1 ÉèÖÃΪϽµÑز¶»ñ
TIM_SetCounter(TIM5,0); //Çå¿Õ¶¨Ê±Æ÷Öµ
RmtSta|=0X10; //±ê¼ÇÉÏÉýÑØÒѾ±»²¶»ñ
}else //ϽµÑز¶»ñ
{
Dval=TIM_GetCapture2(TIM5);//¶ÁÈ¡CCR1Ò²¿ÉÒÔÇåCC1IF±ê־λ
TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Rising); //CC4P=0 ÉèÖÃΪÉÏÉýÑز¶»ñ
if(RmtSta&0X10) //Íê³ÉÒ»´Î¸ßµçƽ²¶»ñ
{
if(RmtSta&0X80)//½ÓÊÕµ½ÁËÒýµ¼Âë
{
if(Dval>300&&Dval<800) //560Ϊ±ê×¼Öµ,560us
{
RmtRec<<=1; //×óÒÆһλ.
RmtRec|=0; //½ÓÊÕµ½0
}else if(Dval>1400&&Dval<1800) //1680Ϊ±ê×¼Öµ,1680us
{
RmtRec<<=1; //×óÒÆһλ.
RmtRec|=1; //½ÓÊÕµ½1
}else if(Dval>2200&&Dval<2600) //µÃµ½°´¼ü¼üÖµÔö¼ÓµÄÐÅÏ¢ 2500Ϊ±ê×¼Öµ2.5ms
{
RmtCnt++; //°´¼ü´ÎÊýÔö¼Ó1´Î
RmtSta&=0XF0; //Çå¿Õ¼ÆʱÆ÷
}
}else if(Dval>4200&&Dval<4700) //4500Ϊ±ê×¼Öµ4.5ms
{
RmtSta|=1<<7; //±ê¼Ç³É¹¦½ÓÊÕµ½ÁËÒýµ¼Âë
RmtCnt=0; //Çå³ý°´¼ü´ÎÊý¼ÆÊýÆ÷
}
}
RmtSta&=~(1<<4);
}
}
TIM_ClearFlag(TIM5,TIM_IT_Update|TIM_IT_CC2);
}
//´¦ÀíºìÍâ¼üÅÌ
//·µ»ØÖµ:
// 0,ûÓÐÈκΰ´¼ü°´ÏÂ
//ÆäËû,°´Ïµİ´¼ü¼üÖµ
u8 Remote_Scan(void)
{
u8 sta=0;
u8 t1,t2;
if(RmtSta&(1<<6))//µÃµ½Ò»¸ö°´¼üµÄËùÓÐÐÅÏ¢ÁË
{
t1=RmtRec>>24; //µÃµ½µØÖ·Âë
t2=(RmtRec>>16)&0xff; //µÃµ½µØÖ··´Âë
if((t1==(u8)~t2)&&t1==REMOTE_ID)//¼ìÑéÒ£¿Øʶ±ðÂë(ID)¼°µØÖ·
{
t1=RmtRec>>8;
t2=RmtRec;
if(t1==(u8)~t2)sta=t1;//¼üÖµÕýÈ·
}
if((sta==0)||((RmtSta&0X80)==0))//°´¼üÊý¾Ý´íÎó/Ò£¿ØÒѾûÓа´ÏÂÁË
{
RmtSta&=~(1<<6);//Çå³ý½ÓÊÕµ½ÓÐЧ°´¼ü±êʶ
RmtCnt=0; //Çå³ý°´¼ü´ÎÊý¼ÆÊýÆ÷
}
}
return sta;
}
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
#include "remote.h"
int main(void) {
u8 key; u8 t=0;
u8 *str=0; delay_init(); //ÑÓʱº¯Êý³õʼ»
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é2 uart_init(9600); //´®¿Ú³õʼ»¯Îª9600
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú LCD_Init();
Remote_Init(); //ºìÍâ½ÓÊÕ³õʼ»¯ POINT_COLOR=RED;//ÉèÖÃ×ÖÌåΪºìÉ«
LCD_ShowString(60,50,200,16,16,"Mini STM32"); LCD_ShowString(60,70,200,16,16,"REMOTE TEST");
LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK"); LCD_ShowString(60,110,200,16,16,"2014/3/12");
LCD_ShowString(60,130,200,16,16,"KEYVAL:");
LCD_ShowString(60,150,200,16,16,"KEYCNT:"); LCD_ShowString(60,170,200,16,16,"SYMBOL:");
while(1) {
key=Remote_Scan(); if(key)
{ LCD_ShowNum(116,130,key,3,16); //ÏÔʾ¼üÖµ
LCD_ShowNum(116,150,RmtCnt,3,16); //ÏÔʾ°´¼ü´ÎÊý switch(key)
{ case 0:str="ERROR";break;
case 162:str="OWER";break; case 98:str="UP";break;
case 2:str="LAY";break; case 226:str="ALIENTEK";break;
case 194:str="RIGHT";break; case 34:str="LEFT";break;
case 224:str="VOL-";break; case 168:str="DOWN";break;
case 144:str="VOL+";break; case 104:str="1";break;
case 152:str="2";break; case 176:str="3";break;
case 48:str="4";break; case 24:str="5";break;
case 122:str="6";break; case 16:str="7";break;
case 56:str="8";break; case 90:str="9";break;
case 66:str="0";break; case 82:str="DELETE";break;
} LCD_Fill(116,170,116+8*8,170+16,WHITE); //Çå³þ֮ǰµÄÏÔʾ
LCD_ShowString(116,170,200,16,16,str); //ÏÔʾSYMBOL }else delay_ms(10);
t++; if(t==20)
{ t=0;
LED0=!LED0; }
}}
|
|