//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
/*------宏定义------*/
#define BIT(x) (1<<(x))
void put_c(unsigned char dat);
void put_s(unsigned char *dat,unsigned char enter);
void find_start(void);
volatile unsigned char bit_array[256];
volatile unsigned char test_end = 0;
//端口初始化
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x08;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x03;
DDRD = 0x02;
}
//定时T1初始化
void timer1_init(void)
{
TCCR1B = 0x00;//停止定时器
TIMSK |= 0x20;//中断允许
TCNT1H = 0xFF;
TCNT1L = 0xF8;//初始值
TCCR1A = 0x00;
TCCR1B = 0xC5;//启动定时器
}
//定时器T1输入捕捉中断服务程序
ISR(TIMER1_CAPT_vect)
{
static unsigned char tr = 0;
static unsigned char bit_no = 0;
static unsigned char one_time = 0;
TCCR1B = 0;
TCNT1 = 0;
if(ICR1 > 6)
{
bit_no = 0;
}
if(test_end == 0)
{
if(tr == 1)
{
bit_array[bit_no] = 1;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
if(ICR1L > 4)
{
bit_array[bit_no] = 1;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
}
TCCR1B = 0xC5; //上升沿触发
tr = 0;
}
else
{
bit_array[bit_no] = 0;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
if(ICR1L > 4)
{
bit_array[bit_no] = 0;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
}
TCCR1B = 0x85; //下降沿触发
tr = 1;
}
}
}
//定时器T2初始化
void timer2_init(void)
{
TCCR2 = 0x00;//停止定时器
ASSR = 0x00;//异步时钟模式
TCNT2 = 0x00;//初始值
OCR2 = 0x2B;//匹配值
TIMSK |= 0x00;//中断允许
TCCR2 = 0x19;//启动定时器
}
//串口通信初始化
void usart_init(void)
{
UCSRB = 0x00;//禁止中断
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x05;
UBRRH = 0x00;
UCSRB = 0x98;
}
void put_c(unsigned char dat)
{
while(!(UCSRA & (1 << UDRE)))
{
}
UDR = dat;
}
void put_s(unsigned char *dat,unsigned char enter)
{
while(*dat)
{
put_c(*dat);
dat++;
}
if(enter == 1)
{
put_c(0x0d);
put_c(0x0a);
}
}
void put_long(unsigned long dat)
{
unsigned long temp = 1000000000;
unsigned char i = 0;
unsigned char out = 0;
for(i = 0; i < 10; i++)
{
out = dat / temp;
dat %= temp;
temp /= 10;
put_c(out + 48);
}
// out = dat % temp;
// put_c(out + 48);
}
void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
timer1_init();
timer2_init();
usart_init();
sei();//开全局中断
}
//主函数
int main(void)
{
unsigned char i = 0;
init_devices();
//在这继续添加你的代码
put_s("RFID Card Reader",1);
while(1)
{
if(test_end == 1)
{
decode();
test_end = 0;
}
}
return 0;
}
void decode(void)
{
unsigned char i,j,k;
unsigned char id_code[11] = {0,0,0,0,0,0,0,0,0,0,0};
unsigned char col_parity = 0;
unsigned char row_parity = 0;
unsigned char temp = 0;
unsigned char start_data[20] = {1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
unsigned long rfid = 0;
for(i = 0; i < 128; i++)
{
for(j = 0; j < 20; j++)
{
if(bit_array[i+j] != start_data[j])
{
break;
}
}
if(j == 20)
{
put_s("",1);
put_s("Find RFID Card.",1);
i += 20;
for(k = 0; k < 11; k++)
{
row_parity = 0;
// 读出五位数据
temp = 0;
for(j = 0; j < 5; j++)
{
temp <<= 1;
if((bit_array == 0) && (bit_array[i+1] == 1))
{
temp |= 0x01;
if(j < 4)
{
row_parity += 1;
}
}
else if((bit_array == 1) && (bit_array[i+1] == 0))
{
temp &= 0xfe;
}
else
{
return;
}
i += 2;
}
id_code[k] = (temp >> 1);
temp &= 0x01;
row_parity %= 2;
if(k < 10)
{
if(row_parity != temp)
{
return;
}
}
else
{
if(temp != 0)
{
return;
}
}
}
if(k == 11)
{
put_s("ID Card :",0);
for(j = 2; j < 10; j++)
{
rfid += (((unsigned long)(id_code[j])) << (4 * (9 - j)));
}
}
put_long(rfid);
return;
}
}
}
200字以内,仅用于支线交流,主线讨论请采用回复功能。