|
本帖最后由 andy 于 2016-11-14 10:24 编辑
参考以下本网站老陈的资料制作的
MEGA8-T2-CTC产生125KHz方波,ICP中断方式读125KHz的RFID卡(EM4100系列)成功,发贴祝贺!
http://www.amobbs.com/forum.php? ... d%2B125k&page=1
// ATMEGA8 16MHz Arduino IDE
// ----------------------------------------------------------------------------
// | 125 KHz RFID Functions |
// ----------------------------------------------------------------------------
// Data Parity example:
// 1 1 1 1 1 1 1 1 1 - header
// - received / decoded
// 0 0 0 0 |0 - 0x00 0x00
// 1 1 1 1 |0 - 0x1e 0x0F
// 0 0 0 0 |0 - 0x00 0x00
// 0 0 1 0 |1 - 0x05 0x02
// 0 1 1 0 |0 - 0x0c 0x06
// 1 0 1 1 |1 - 0x17 0x0B
// 1 1 1 1 |0 - 0x1e 0x0F
// 1 1 1 1 |0 - 0x1e 0x0F
// 1 0 1 0 |0 - 0x14 0x0A
// 1 0 1 1 |1 - 0x17 0x0B
// -------------
// ver parity 0 0 0 1 |0 - 0x02 (hor parity always 0)
// H4001卡有关信息,采用曼切斯特编码,125Khz,RF/64,位周期512uS
// 激光编程64位ID,前9位头全"1",10位行检验,4位列检验,1个终止位,
// 8位CustomID,32位ID信息,即10个十六进制编码
// 一个典型数据 111111111
// CustomID 0 00000
// CustomID F 11111
// 0 00000
// 0 00000
// 4 01001
// A 10100
// D 11011
// E 11101
// 1 00011
// 9 10010
// 10100
//PB.0(ICP) <----- 曼切斯特编码输入
//PB.3(OC2) ------> 135.6KHZ输出
volatile unsigned char bit_array[256];
volatile unsigned char test_end = 0;
void put_c(unsigned char dat)
{
while(!(UCSRA & (1 << UDRE)));
UDR = dat;
}
void put_s(char *dat,char enter)
{
while(*dat)
{
put_c(*dat);
dat++;
}
if(enter == 1)
{
put_c(0x0d);
put_c(0x0a);
}
}
void put_long(unsigned long dat)
{
long temp = 1000000000;
char i,out;
for(i = 0;i < 10;i++)
{
out = dat / temp;
dat %= temp;
temp /= 10;
put_c(out + '0');
}
}
//解码RFID码函数
void decode()
{
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); //串口打印 RFID 码的内容
return;
}
}
}
//定时器T1输入捕捉中断服务程序
ISR(TIMER1_CAPT_vect)
{
static unsigned char tr = 0;
static unsigned char bit_no = 0;
TCCR1B = 0;
TCNT1 = 0;
if(ICR1L > 9)
{
bit_no = 0;
}
if(test_end == 0)
{
if(tr == 1)
{
bit_array[bit_no] = 0;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
if(ICR1L > 6)
{
bit_array[bit_no] = 0;
//put_long (bit_array[bit_no]);
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
}
TCCR1B = 0xC5; //上升沿触发
tr = 0;
}
else
{
bit_array[bit_no] = 1;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
if(ICR1L > 6)
{
bit_array[bit_no] = 1;
bit_no++;
if(bit_no == 0)
{
test_end = 1;
}
}
TCCR1B = 0x85; //下降沿触发
tr = 1;
}
}
}
void setup()
{
// put your setup code here, to run once:
MCUCR = 0x00;
GICR = 0x00;
//端口初始化
PORTB = 0x00;
DDRB |= (1<<PB3); //OC2_OUTPUT
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x03; //RXD TXD 上拉电阻
DDRD = 0x02; //TXD OUTPUT
//定时T1初始化
TCCR1B = 0x00; //停止定时器
TIMSK = (1<<TICIE1); //允许T1输入捕捉中中断允许
TCNT1H = 0xFF; //初始值 0xFF
TCNT1L = 0xF8; //初始值 0xF8 /8
TCCR1A = 0x00;
TCCR1B = 0xC5; //启动定时器
//定时器T2初始化
TCCR2 = 0x00; //停止定时器
ASSR = 0x00; //异步时钟模式
TCNT2 = 0x00; //初始值
OCR2 = 63; //63 0x3F匹配值
TCCR2 = 0x19; //启动定时器 16MHz/1=250KHz/2=125KHz
//串口通信初始化
UCSRB = 0x00; //禁止中断
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UBRRL = (16000000/8/115200)-1;
UBRRH = 0x00;
UCSRA = (1<<U2X);
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
pinMode(2, OUTPUT);
}
void loop()
{
sei();
put_s("RFID 125KHz Card Reader",1);
while(1)
{
if(test_end==1) //有要RFID解码旗标是1
{
test_end = 0;
digitalWrite(2, LOW);
decode();
digitalWrite(2, HIGH);
}
}
}
用现成RFID 125KHz读卡器,但是,买了回来不合适使用,要改装:
1 > 硬件改装,C14焊离(不使用),R22改换1k电阻,R6-R7 2个电阻焊离(不使用),C14电容下焊点焊上1个104P电容(自动Bootload功能),这个电容下面焊接1条柒皮线连接到R6上焊点,
Mega8_30Pin_RXD焊接1条柒皮线连接到R7上焊点,将晶振8MHz换为16MHz(不换掉达不到115200),硬件改装完成了,可参考看第2张图片。
2 > 将Mega8写入Bootload,就可在Arduino IDE玩了。
可将Mega8 改换为 Mega328P 单片机
也很方便改回AVRGCC C源程序的,但是,以后用到AVR 8Bit单片机都将全面用Arduino IDE 来執行編譯。
右边5位排针:
1 > +5V
2 > GND
3 > DTR (DI)
4 > RXD (DO)
5 > TXD
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|