📄 main.c
字号:
{
check_stat=0xf1;
return;
}
check_stat = UART_MESSAGE_OK;
if(capture_time[j+2]<(halfDataRate*2+8))
{
//bit_begin = 1;
store_bit(0);
b_edge = 0;
j=j+4;
}
else if((capture_time[j+2]>(halfDataRate*2+8))&&(capture_time[j+2]<(halfDataRate*3+8)))
{
//bit_begin = 0;
store_bit(1);
b_edge = 1;
j=j+3;
}
else
{
check_stat=0xf2;
return;
}
captured_bits_count=1;
captured_bits_count++;
if (check_stat==UART_MESSAGE_OK)
{
//解码
for (i=0;i<150;i++)
{
if (capture_time[j]<MaxCaptureHalfDateRate)
{
if((i%2)==0)
{
store_bit(b_edge);
}
else
{
store_bit(!b_edge);
}
i++;
j++;
captured_bits_count++;
if (captured_bits_count>64) //32位数据结构
break;
}
else if((capture_time[j]>MaxCaptureHalfDateRate) &&(capture_time[j]<MaxCaptureDateRate))
{
if((i%2)==0)
{
store_bit(!b_edge);
}
else
{
store_bit(b_edge);
}
captured_bits_count++;
if (captured_bits_count>64) //45位数据结构
break;
}
else
{
check_stat=0xf3;
break;
}
j++;
}
}
for(i=0;i<150;i++) //2006-5-13 14:19
{
capture_time[i]=0xff;
}
//trace(capture_time,120);
//trace(capture_data,32);
}
//写卡程序,Manchester解码
void ManchesterWrite(void)
{
uchar i,j;
ClearCaptureBuffers();
captured_bit_count = 0;
capture_cnt = 0;
captured_byte=0;
capture_check=0;
capture_check_count=0;
buffer_capture_check=0;
compute_capture_check=0;
check_stat=0;
TR2 = 0;
TF2=0; //clear pending interrupts
TL2=(uchar)(~maxCaptureTimeLow); TH2=(uchar)((~maxCaptureTimeLow)>>8) ; //set timer with initial time
currentMaxTimeHi = ~maxCaptureTimeHi;
last_capture=~maxCaptureTimeLow;
capture_read_time_data_ptr=capture_time;
flag_wait=1;ET2=1;
TR2=1;
while ( flag_wait )
{
for(i=0;i<0xf0;i++)
{
if(DEMOD_OUT == 0) //等待低电平,超时退出
break;
}
if(i>=0xf0)
{
for(i=0;i<120;i++)
{
capture_time[i]=0xff;
}
return;
}
capture();
for(i=0;i<0xf0;i++)
{
if(DEMOD_OUT == 1) //等待高电平,超时退出
break;
}
if(i>=0xf0)
{
for(i=0;i<120;i++)
{
capture_time[i]=0xff;
}
return;
}
capture();
}
check_stat=ERR_EM4469_NEITHER_ACK;
for (j=0;j<10;j++)
{
if(((halfDataRate*2+8)<capture_time[j])&&(capture_time[j]<(halfDataRate*3+8))
&&(capture_time[j+1]<MaxCaptureHalfDateRate)&&(capture_time[j+2]>MaxCaptureHalfDateRate)
&&(capture_time[j+2]<(halfDataRate*3+8)))
break;
}
if(j==5)
{
check_stat = 0xf1;
return;
}
check_stat = UART_MESSAGE_OK;
for(i=0;i<120;i++)
{
capture_time[i]=0xff;
}
}
void ClearCaptureBuffers(void)
{
uchar i;
for(i=0; i<CAPTURE_SIZE; i++)
{
capture_data[i] = 0;
}
}
void capture(void)
{
uint capt;
uint icr = count_module();
capt = icr - last_capture;
last_capture = icr;
*capture_read_time_data_ptr++=capt; //保存高低电平的时间(以125K的脉冲个数),ptr++
}
//保存解码数据
void store_bit(bit b)
{
if(b==1)
captured_byte = captured_byte|(0x80>>captured_bit_count);
if (captured_bit_count==7)
{
capture_data[capture_cnt++] = captured_byte;
captured_byte=0;
captured_bit_count = 0;
}
else captured_bit_count++;
}
uint count_module(void)
{
do
{
count_timer2.bytes.high2 = TH2;
count_timer2.bytes.low2 = TL2;
}while(count_timer2.bytes.high2 != TH2);
return count_timer2.word;
}
//从串口发送数据出去
void trace(uchar *str,uchar len)
{
uint i;
MYTI = 0;
for(i=0;i<len;i++)
{
myputchar(*str);
str++;
}
}
//trace调用
void myputchar(uchar c)
{
//ES = 0;
SBUF = c;
while (MYTI == 0);
MYTI = 0;
//ES = 1;
}
void ResetT5557(void)
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(0x00);
SendForward(fwd_bit_count);
}
void WakeUpT5557(void)
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(0x01);
fwd_bit_count += Prepare_Data(rx_buff);
SendForward(fwd_bit_count);
}
void GeneralRead_T5557()//发送时序 Opcode
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(rx_buff[1]);
SendForward(fwd_bit_count);
maxCaptureTimeLow = maxTRead;
maxCaptureTimeHi = 0;
ManchesterRead();
if (check_stat==UART_MESSAGE_OK)
{
tx_buff[0]=capture_data[0]; //数据
tx_buff[1]=capture_data[1]; //数据
tx_buff[2]=capture_data[2]; //数据
tx_buff[3]=capture_data[3]; //数据
tx_buff[4]=capture_data[4]; //数据
tx_buff[5]=capture_data[5]; //数据
tx_buff[6]=capture_data[6]; //数据
tx_buff[7]=capture_data[7]; //数据
;
trace(tx_buff,8); //发送到PC 11个字节
}
else
{
tx_buff[0]= check_stat; //数据
tx_buff[1]=0xee; //数据
tx_buff[2]=0xee; //数据
tx_buff[3]=0xee; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
}
void ReadT5557(void)//发送时序 Opcode + Lock + Addr
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(rx_buff[1]);
fwd_bit_count += Prepare_Lock();
fwd_bit_count += Prepare_Addr(rx_buff[2]);
SendForward(fwd_bit_count);
maxCaptureTimeLow = maxTRead;
maxCaptureTimeHi = 0;
ManchesterRead();
if (check_stat==UART_MESSAGE_OK)
{
tx_buff[0]=capture_data[0]; //数据
tx_buff[1]=capture_data[1]; //数据
tx_buff[2]=capture_data[2]; //数据
tx_buff[3]=capture_data[3]; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
else
{
tx_buff[0]= check_stat; //数据
tx_buff[1]=0xee; //数据
tx_buff[2]=0xee; //数据
tx_buff[3]=0xee; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
}
void WriteT5557(void)//发送时序 Opcode + Lock + Data + Addr
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(0x01);
fwd_bit_count += Prepare_Lock();
fwd_bit_count += Prepare_Data(rx_buff);
fwd_bit_count += Prepare_Addr(rx_buff[5]);
SendForward(fwd_bit_count);
maxCaptureTimeLow = maxTWrite;
maxCaptureTimeHi = 0;
ManchesterWrite();
if (check_stat==UART_MESSAGE_OK)
{
tx_buff[0]=0xe0; //数据
tx_buff[1]=0x00; //数据
tx_buff[2]=0x00; //数据
tx_buff[3]=0x00; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
else
{
tx_buff[0]= check_stat; //数据
tx_buff[1]=0xee; //数据
tx_buff[2]=0xee; //数据
tx_buff[3]=0xee; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
}
void ReadT5557_PWD(void) //发送时序 Opcode + PassWord + Lock + Addr
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(0x01);
fwd_bit_count += Prepare_Data(rx_buff);
fwd_bit_count += Prepare_Lock();
fwd_bit_count += Prepare_Addr(rx_buff[5]);
SendForward(fwd_bit_count);
maxCaptureTimeLow = maxTRead;
maxCaptureTimeHi = 0;
ManchesterRead();
if (check_stat==UART_MESSAGE_OK)
{
tx_buff[0]=capture_data[0]; //数据
tx_buff[1]=capture_data[1]; //数据
tx_buff[2]=capture_data[2]; //数据
tx_buff[3]=capture_data[3]; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
else
{
tx_buff[0]= check_stat; //数据
tx_buff[1]=0xee; //数据
tx_buff[2]=0xee; //数据
tx_buff[3]=0xee; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
}
void WriteT5557_PWD(void) //发送时序 Opcode + PassWord + Lock + Data + Addr
{
uchar fwd_bit_count;
fwd_bit_count = Prepare_Cmd(0x01);
fwd_bit_count += Prepare_Data(rx_buff);
fwd_bit_count += Prepare_Lock();
fwd_bit_count += Prepare_Data(rx_buff+4);
fwd_bit_count += Prepare_Addr(rx_buff[9]);
SendForward(fwd_bit_count);
maxCaptureTimeLow = maxTWrite;
maxCaptureTimeHi = 0;
ManchesterWrite();
if (check_stat==UART_MESSAGE_OK)
{
tx_buff[0]=0xe0; //数据
tx_buff[1]=0x00; //数据
tx_buff[2]=0x00; //数据
tx_buff[3]=0x00; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
else
{
tx_buff[0]= check_stat; //数据
tx_buff[1]=0xee; //数据
tx_buff[2]=0xee; //数据
tx_buff[3]=0xee; //数据
trace(tx_buff,4); //发送到PC 11个字节
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -