📄 ir.c
字号:
#include "stm32f10x_lib.h"
#include "MyType.h"
#include "config.h"
#include "globals.h"
#include "STM32_Reg.h"
#define NULL 0
#define IRIN PC12in
#define SERIES_TIME 20
uint8 ConsumerCode = 0;//用户码
/*
//红外数据格式
同步头 + 8bit用户码 + 8bit用户码反码 + 8bit数据 + 8bit数据反码
//同步头: 9ms低电平,4.5ms高电平
数据:
//0.5ms低电平,0.5ms高电平 ==BIT 0
//0.5ms低电平,1ms高电平 ==BIT 1
连续码:
9ms低电平+2.2ms高电平+0.6ms低电平
上面的时间是个大概值,不一定精确
*/
/*
等待端口状态
time:等待超时时间,单位毫秒
f:等待端口状态,1=高电平,0=低电平
gTimerI:等待端口状态成功的时间,单位100us
返回:0=端口状态是等待的值,1=端口状态不是等待的值
*/
uint8 WaitBit(uint32 time, uint8 f, uint32 *gTimerI)
{//uint32 timer;
uint8 over;
gTimer_100us=0;
//TIM1->CR1 |= TIMX_CR1_CEN;
time*=10;
while(1)
{
if(gTimer_100us>time)
{ over=1; break;}
//if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) == f)
if(IRIN==f)
{ over=0; break;}
}
//TIM1->CR1 &= ~TIMX_CR1_CEN;
if(gTimerI!=NULL)
*gTimerI=gTimer_100us;
return(over);
//return(1);
}
//头9ms高,4.5ms低
char IrHeader()
{uint32 timer;
//uint8 i=0;
//等待出现高电平立即返回,并且得到计数值
WaitBit(20, 1, &timer);
if(timer<60 || timer>150)//非头脉冲
return(0);
//等待出现低电平立即返回,并且得到计数值
WaitBit(20, 0, &timer);
if(timer<35 || timer>70)//非头脉冲
return(0);
return(1);
}
//接收8位数据
//0.5ms低电平,0.5ms高电平 ==BIT 0
//0.5ms低电平,1ms高电平 ==BIT 1
int Ir8Bit()
{uint32 timer;
char i;
int ir_data = 0;
for (i = 0; i < 8; i++)
{
if(WaitBit(1, 1, NULL))//1ms内不出现高电平就无效
return(-1);
if(WaitBit(1, 0, &timer))//1ms内不出现低电平Bit=1
{
ir_data <<= 1;
ir_data |= 0x01;
if(WaitBit(1, 0, &timer))//1ms内不出现低电平非法脉冲
return(-1);
}
else//0.5秒就出现低电平Bit=0
{
ir_data <<= 1;
ir_data &= 0xfe;
}
}
return(ir_data);
}
uint32 t1,t2,t3;
int IrIn(char check, char CheckF)
{uint32 timer;
int ir_data1, ir_data2;
char header;
// WaitBit(2, 0, &timer);
// if(timer>150/10)
// return(0);
// else
// return(-1);
//if(WaitBit(1, 1, &timer))
// return(-1);
if(WaitBit(1, 0, &timer))//2ms内不出现低电平就无效(用时1ms)
//if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_12) == 1)
return(-1);
header = IrHeader();
if (header == 0)//(用时13ms)
return(-1);//非法脉冲
ir_data1 = Ir8Bit();
if (CheckF == 1)
{
if (ir_data1 != check)
{
return(-1);
}
}
if (ir_data1 == -1)
return(-1);
ir_data2 = Ir8Bit();
if (ir_data2 == -1)
return(-1);
if ((ir_data1 + ir_data2) != 0xff )
{
return(-1);
}
else
{
ConsumerCode = (uint8) ir_data1; //设置用户码
}
ir_data1 = Ir8Bit();
if (ir_data1 == -1)
return(-1);
ir_data2 = Ir8Bit();
if (ir_data2 == -1)
return(-1);
if ((ir_data1 + ir_data2) != 0xff )
return(-1);
else
{
return(ir_data1);
}
}
extern void delay(uint32 time);
void IR_Run(void)
{
int temp;
static uint8 flag=0;
if(ConsumerCode==0)
temp = IrIn(ConsumerCode, 0);
else
temp = IrIn(ConsumerCode, 1);
if (temp != -1)
{
if(flag==0)
{
flag=1;
/*PC8=0;*/ PC9=0; PC10=0; PC11=0;
}
else
{
flag=0;
/*PC8=1;*/ PC9=1; PC10=1; PC11=1;
}
delay(500);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -