📄 rfid.c
字号:
//******************************************************************************************
// 头文件处理
//******************************************************************************************
#include <18F252.H>
#device ICD=TRUE
#fuses HS,NOWDT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#use fast_io(c)
#use fast_io(a)
#use fast_io(b)
//******************************************************************************************
// IO口处理
//******************************************************************************************
#define RF_DATA_IN PIN_C2
#bit Time_Out= 0xff2.2
#bit Star_Timer= 0xfd5.7
//******************************************************************************************
// 变量处理
//******************************************************************************************
// LCD 模块命令
#define DISP_ON 0x0C //开显示
#define DISP_ON_C 0x0E //开显示, 开指针
#define DISP_ON_B 0x0F //开显示, 开指针,开闪烁
#define DISP_OFF 0x08 //关显示
#define CLR_DISP 0x01 //清显示的操作数
#define ENTRY_INC 0x06 //操作数
#define ENTRY_INC_S 0x07 //操作数
#define ENTRY_DEC 0x04 //操作数
#define ENTRY_DEC_S 0x05 //操作数
#define DD_RAM_ADDR 0x80 //初始化第一行显示的操作数
#define DD_RAM_UL 0xc0 //初始化第二行显示的操作数
//*******************************************************
int16 TIMER_1=0;
int8 moniter_Data[11],normal_Data[11];
int8 sendData[11];
int1 Check_Data_bit;
int8 temp;
int8 flag,jing;
int16 last_timer,width,haha;
int8 timeout=0,bit_over=1,bitdata=0;
int8 a=0,b=0;
//******************************************************************************************
// 寄存器处理
//******************************************************************************************
#byte ADCON0=0xfc2
#byte ADCON1=0xfc1
#byte TRISA=0xf92
#byte ADRESH=0xfc4
#byte STATUS=0xfd8
#byte PORTA=0xf80
#byte PORTB=0xf81
#byte TRISA=0xf92
#byte TRISB=0xf93
#byte SPBRG=0xfaf
#byte TXSTA=0xfac
#byte RCSTA=0xfab
#byte TRISC=0xf94
#byte PIE1=0xf9d
#byte RCREG=0xfae
#define GO 2
#define C 0
#define RCIE 5
#bit tim3Str= 0xfb1.0
#bit over= flag.0
#bit LCD_DB7= PORTB.5
#bit LCD_DB6= PORTB.4
#bit LCD_DB5= PORTB.3
#bit LCD_DB4= PORTB.2
#bit LCD_E= PORTA.5
#bit LCD_RS= PORTB.1
#bit LCD_DB7_DIR= TRISB.5
#bit LCD_DB6_DIR= TRISB.4
#bit LCD_DB5_DIR= TRISB.3
#bit LCD_DB4_DIR= TRISB.2
#bit LCD_E_DIR= TRISA.5
#bit LCD_RS_DIR= TRISB.1
#bit CFE_DIR= TRISA.2
#bit CFE = PORTA.2
//******************************************************************************************
// 函数声明处理
//******************************************************************************************
void moniter(void);
void moniter_Inti(void);
void moniter_Clern_Number(void);
void moniter_Recive(void);
//******************************************************************************************
void normal(void);
void normal_Inti(void);
void normal_Clern_Number(void);
void normal_Recive(void);
//*******************************************************************************
void simulation_Inti(void);
void simulation(void);
void TIMER3_isr(void);
//*******************************************************************************
void InitADKEY(void);
int8 GetADKEY(void);
void InitLCD(void);
void putcLCD(int8 Byte);
void SendCmd(int8 Byte);
void clrLCD(void);
void write(int8 Byte);
//******************************************************************************************
// 监听模块
//******************************************************************************************
#INT_CCP1
void rise (void)
{
if(a==1)
{
b=1;
if(CCP_1<CCP_2)
{
haha=CCP_1+65536-CCP_2;
}
else
{
haha=CCP_1-CCP_2;
}
}
if(CCP_1<last_timer)
{
width=65536+CCP_1-last_timer;
}
else
{
width=CCP_1-last_timer;
}
delay_us(1);
if(width<150||width>320)
{
if(width>330&&width<800)
{
bitdata=1;
bit_over=1;
last_timer=CCP_1;
}
else
{
timeout=1;
}
}
}
#INT_CCP2
void fall (void)
{
a=1;
if(CCP_2<last_timer)
{
width=65536+CCP_2-last_timer;
}
else
{
width=CCP_2-last_timer;
}
delay_us(1);
if(width<150||width>320)
{
if(width>330&&width<800)
{
bitdata=0;
bit_over=1;
last_timer=CCP_2;
}
else
{
timeout=1;
}
}
}
void moniter_Inti(void)
{
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // Start timer 1
setup_timer_0(RTCC_INTERNAL|RTCC_OFF|RTCC_8_BIT|RTCC_DIV_1);
set_tris_c(0x86);
disable_interrupts(GLOBAL);
enable_interrupts(INT_CCP1);
enable_interrupts(INT_CCP2);
}
//************************************************************
void moniter_Clern_Number(void)
{
moniter_Data[0]=0;
moniter_Data[1]=0;
moniter_Data[2]=0;
moniter_Data[3]=0;
moniter_Data[4]=0;
moniter_Data[5]=0;
moniter_Data[6]=0;
moniter_Data[7]=0;
moniter_Data[8]=0;
moniter_Data[9]=0;
moniter_Data[10]=0;
}
//************************************************************
void moniter_Recive(void)
{
int8 header,pc;
int8 temp,temp1;
int16 temp2;
int8 ByteCounter;
int8 *PData,*PData1;
int8 BitCounter;
int16 fall,rise,num;
int16 value,delay_time;
str:
disable_interrupts(GLOBAL);
if(input(RF_DATA_IN)==1)
if(input(RF_DATA_IN)==1)
if(input(RF_DATA_IN)==1)
if(input(RF_DATA_IN)==1)
if(input(RF_DATA_IN)==1)
{
enable_interrupts(GLOBAL);
while(b==0);
a=0;b=0;
delay_us(1);
if(haha<430||haha>550)
{
goto str;
}
last_timer=CCP_1;
header=0;
value=0;
do
{
jing=header;
bit_over=0;
timeout=0;
num=0;
while(!bit_over)
{
delay_us(1);
num++;
if(num>600)
{
timeout=1;
break;
}
}
bit_over=0;
if(!(timeout==0&&bitdata==1))
{
timeout=0;
goto str;
}
// value+=TIMER_1;
header=header+1;
}while(header<8);
// delay_time=value*0.2;
if(header==8)
{
ByteCounter=0;
PData=moniter_Data;
do
{
BitCounter=5;
temp1=0;
do
{
bit_over=0;
timeout=0;
num=0;
while(!bit_over)
{
delay_us(1);
num++;
if(num>600)
{
timeout=1;
break;
}
}
bit_over=0;
if(timeout==1)
{
timeout=0;
goto str;
}
temp=bitdata;
temp1=(temp1<<1)|temp;
BitCounter--;
}while(BitCounter);
*(PData+ByteCounter)=temp1;
ByteCounter=ByteCounter+1;
}while(ByteCounter<11);
if(temp==0)
{
PData=moniter_Data;
PData1=sendData;
for(ByteCounter=0;ByteCounter<11;ByteCounter++)//有5个字节数据
{
temp=(((*(PData+ByteCounter))&0x10)>>4)+(((*(PData+ByteCounter))&0x08)>>3)+(((*(PData+ByteCounter))&0x04)>>2)+(((*(PData+ByteCounter))&0x02)>>1);
if(ByteCounter==10)
{
temp1=*(PData+ByteCounter)&0x1f;
*(PData1+ByteCounter)=temp1;
}
else
{
if((temp&0x01)==((*(PData+ByteCounter))&0x01))//效验高4位
{
temp1=*(PData+ByteCounter)&0x1f;
*(PData1+ByteCounter)=temp1;
temp1=(temp1&0x1e)>>1;
*(PData+ByteCounter)=temp1;
}
else
{
moniter_Clern_Number();//并清所有数据
goto str;
}
}
}
output_high(PIN_C5);
delay_ms(1000);
output_low(PIN_C5);
Check_Data_bit=1;
disable_interrupts(GLOBAL);
disable_interrupts(INT_CCP1);
disable_interrupts(INT_CCP2);
SendCmd(0xc0);
putcLCD(' ');
putcLCD(' ');
putcLCD(' ');
putcLCD(' ');
putcLCD(' ');
putcLCD('s');
putcLCD('u');
putcLCD('c');
putcLCD('c');
putcLCD('e');
putcLCD('s');
putcLCD('s');
}
else
{
moniter_Clern_Number();
goto str;
}
}
}
}
//***************************************************************
void moniter(void)
{
int8 temp;
delay_ms(100);
moniter_Inti();
wo:
moniter_Recive();
if(Check_Data_bit==0)
goto wo;
else
{
Check_Data_bit=0;
simulation_Inti();
while(1)
{
simulation();
}
}
}
//******************************************************************************************
// 读卡模块
//******************************************************************************************
void normal_Inti(void)
{
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // Start timer 1
setup_timer_0(RTCC_INTERNAL|RTCC_OFF|RTCC_8_BIT|RTCC_DIV_1);
set_tris_c(0x86);
}
//************************************************************
void normal_Clern_Number(void)
{
normal_Data[0]=0;
normal_Data[1]=0;
normal_Data[2]=0;
normal_Data[3]=0;
normal_Data[4]=0;
normal_Data[5]=0;
normal_Data[6]=0;
normal_Data[7]=0;
normal_Data[8]=0;
normal_Data[9]=0;
normal_Data[10]=0;
}
//************************************************************
void normal_Recive(void)
{
int8 header,pc;
int8 temp,temp1;
int16 temp2;
int8 ByteCounter;
int8 *PData,*PData1;
int8 BitCounter;
int16 fall,rise;
str:
if(input(RF_DATA_IN)==0)
if(input(RF_DATA_IN)==0)
if(input(RF_DATA_IN)==0)
if(input(RF_DATA_IN)==0)
if(input(RF_DATA_IN)==0)
{
while(input(RF_DATA_IN)==0);
rise=CCP_1;
delay_us(10);
while(input(RF_DATA_IN)==1);
fall=CCP_2;
TIMER_1=fall-rise;
if(fall<rise)
TIMER_1=TIMER_1+65536;
temp2=560;
if(TIMER_1>temp2)
{
goto str;
}
temp2=540;
if(TIMER_1<temp2)
{
goto str;
}
header=0;
do
{
TIMER_1=0;
while(input(RF_DATA_IN)==0);
rise=CCP_1;
delay_us(10);
while(input(RF_DATA_IN)==1);
fall=CCP_2;
TIMER_1=fall-rise;
if(fall<rise)
TIMER_1=TIMER_1+65536;
temp2=280;
if(TIMER_1>temp2)
{
header=0;
goto str;
}
temp2=230;
if(TIMER_1<temp2)
{
header=0;
goto str;
}
header++;
}while(header<8);
//delay_us(65);
if(header==8)
{
set_timer0(106);//对计数器0赋初值
Time_Out=0;
ByteCounter=0;
PData=normal_Data;
do
{
BitCounter=5;
temp1=0;
do
{
delay_us(445);
if(input(RF_DATA_IN)==1)
temp=1;
else
temp=0;
Star_Timer=1;
while(input(RF_DATA_IN)==temp)
{
if(Time_Out==1)
{
Star_Timer=0;//停止计数器
Time_Out=0;
goto str;
}
}
Star_Timer=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -