|
ATMEGA8,11.0592MHZ,T2工作于CTC模式,OC2生产125.673KHz的方波,8050/8550两个三极管放大,358调制出来的信号加到M8的ICP脚上。串口输出读卡信息。
效果不错。现在有效距离5CM,有空再调一下电感,距离应该可以达到15CM,因为以前调到过,只是程序没写好。
晶振如果用16M/8M/4M的能生产准确的125KHz方波。
先把程序拿来晒晒。原理图稍后上传。实物是自己用万用板搭的,就不照了。
另:现在程序中只用的行奇偶校验,还未用列奇偶校验,后面会加上。
补上原理图:
原理图-Protel99SE格式ourdev_393898.rar(文件大小:14K) (原文件名:m8_rfid.rar)
原理图-JPG格式,清析ourdev_393896.jpg(文件大小:523K,只有300K以内的图片才能直接显示) (原文件名:all-good.jpg)
![](http://cache.amobbs.com/bbs_upload782111/files_10/ourdev_393897.jpg)
原理图-RF部分 (原文件名:jb.jpg)
//包含所需头文件
#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;
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|