⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h4001.c

📁 EM4100卡PIC单片机读写程序
💻 C
字号:
// --------------------------------------
// Function: H4001 64位RFID只读卡读头
// MCU:   ATmega32
// Crystal: 7.3728Mhz
// Date:  2005/01/16
// --------------------------------------
// Compiler:
// ICCAVR 6.28C,6.31A
// RFID基站芯片:U2270B 
// --------------------------------------

#include <iom32v.h>
#include <macros.h>
//variable definition
unsigned char timer_count,error=0,post=0,data1,rxdata,run=0;
unsigned char decode[192],head=0,rfiddata[54];
unsigned int  tcomp=0;
unsigned char lastbit=0,half=0; 
const unsigned char ascii[16]="0123456789ABCDEF";
//pin definition
#define led() (PORTD^=0x80)
#define RFW (1<<6)                        
//RFW, RFID输出载波控制
#define RFW_1() (PORTC |=RFW)          
#define RFW_0() (PORTC &=~RFW)
#define beep (1<<4)                        
#define beep_1() (PORTC |=beep)
#define beep_0() (PORTC &=~beep)
#define okled (1<<0)
#define okled1() (PORTC |=okled)
#define okled0() (PORTC &=~okled)
//RFID_IN U2270B返回卡的未解调码
#define RFID_IN  (PINC&0x80)

void port_init(void)
{
 PORTA = 0xFF;
 DDRA  = 0x00;
 PORTB = 0xFF;
 DDRB  = 0x00;
 PORTC = 0xFF; 
 DDRC  = 0x51;
 PORTD = 0x7F;
 DDRD  = 0x80;
}

//TIMER0 initialisation - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value:  9.861mSec (1.4%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 TCNT0 = 0xB9; //set count
 OCR0  = 0x47;  //set compare
 TCCR0 = 0x05; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:12
void timer0_ovf_isr(void)
{
     TCNT0 = 0xB9; //reload counter value
     timer_count++;
	 if (timer_count>50) {     // 0.01*50=0.5sec,每0.5秒扫描一次是否有ID卡
//      led();
	    timer_count=0;
        run=1;
        }
}

//TIMER1 initialisation - prescale:1
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: Out of range
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0x00 /*INVALID SETTING*/; //setup
 TCNT1L = 0x00 /*INVALID SETTING*/;
 OCR1AH = 0x00 /*INVALID SETTING*/;
 OCR1AL = 0x00 /*INVALID SETTING*/;
 OCR1BH = 0x00 /*INVALID SETTING*/;
 OCR1BL = 0x00 /*INVALID SETTING*/;
 ICR1H  = 0x00 /*INVALID SETTING*/;
 ICR1L  = 0x00 /*INVALID SETTING*/;
 TCCR1A = 0x00;
 TCCR1B = 0x01; //start Timer
}

//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:57600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)         //Uart初始化   
{
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x06;
 UBRRL = 0x07; //set baud rate lo,9600=0x2F,57600=0x07
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x98; 
}

#pragma interrupt_handler uart0_rx_isr:14  
void uart0_rx_isr(void)        //接收中断处理           
{
 //uart has received a character in UDR
   rxdata=UDR;
}


//call this routine to initialise all peripherals
void init_devices(void)              //初始化
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer0_init();
 timer1_init();
 uart0_init();
 MCUCR = 0x02;
 GICR  = 0x40;
 TIMSK = 0x01; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialised
}


void ldelay(unsigned int i)       //长延时
     {
     while(i--);
     }

void beeps(unsigned char i) {     //讯响 
     while(i--) { 
	       beep_0();
	       ldelay(60000);
		   ldelay(60000);
		   ldelay(60000);
		   beep_1();
	       ldelay(60000);
		   ldelay(60000);
		   ldelay(60000);
		   }		   
     }	    
   
void wait_tx() {	              //等待发送完一个字节
	while(!(UCSRA&0x40));
    UCSRA|=0x40; 
	}    

	
void R_H4001()                    //读卡 
    {              
     unsigned char i=0,j=0;
     unsigned int bits=0;
	 data1=0;
	 for (i=0;i<192;i++) {
         decode[i]=1;
		}   
	 for (i=0;i<64;i++) {
         rfiddata[i]=0;
		}   
	 RFW_1();
     data1=0;
	 lastbit=0;
	 TCNT1=0;
	 half=0;
	 j=0;
	 for (bits=0;bits<256;bits++) {
    //	 while (((!(PINC&0x80))==lastbit)& (TCNT1<20000)) { }            //low level  
    //   while (((!(PINC&0x80))==lastbit)& (TCNT1<20000)) { }
         while (((!(RFID_IN))==lastbit)& (TCNT1<20000)) { }              
	     tcomp=TCNT1;
         TCNT1=0;
		   if ((tcomp>1500 & tcomp<2500)|(tcomp>3000 & tcomp<5000)) {    //判断是否为合法跳变
			if (tcomp>1500 & tcomp<2500) {
		        if (half==1) {
			        half=0;
				    decode[j]=lastbit;
				    j++;
				    } 
			    else
			        half=1;	  
			   } 
		    else 
			    {                 // (tcomp>3000 & tcomp<5000)   
		        if (half==1) 
				   {
                   if (lastbit==0) {
			           decode[j]=1;
				       }
			       else
			          { 
					   if (lastbit==1) {
					      decode[j]=0;
	            	      } 
					  }
                   } 
			    else
				   {
				    decode[j]=0x0E;
				    if (j<127) {
                       bits=0;
					   }					
					else
					   break;
					}
			    j++;
			    half=1;
		        }
	      } 
		 lastbit^=0x01;
         } 

     j=0;
	 head=0;
	 post=0;
	 error=0;
	 for (i=0;i<128;i++)
	     {                        //查找最后一个终止位(0),及9位(1)引导位
	     if ((decode[i]==0)&(decode[i+1]==1)&(decode[i+2]==1)&(decode[i+3]==1)&(decode[i+4]==1)&(decode[i+5]==1)&(decode[i+6]==1)&(decode[i+7]==1)&(decode[i+8]==1)&(decode[i+9]==1)) {
		     head=1;
			 post=i+10;
			 break;
		     } 
		 }
	  if (head==1) {
		   for (i=0;i<54;i++) {
		     if (decode[post+i]==0x0e) error++;
			 rfiddata[i]=decode[post+i];
			 } 
         if (error==0) {          //H4001卡采用的二维行列偶校验码
			 for (i=0;i<10;i++) { //先对10行数据进行校验
                 if (!(rfiddata[i*5]^rfiddata[i*5+1]^rfiddata[i*5+2]^rfiddata[i*5+3]==rfiddata[i*5+4])) error++;
				 }
             for (i=0;i<4;i++) {  //再对4列数据进行校验
                 if (!(rfiddata[i]^rfiddata[i+5]^rfiddata[i+10]^rfiddata[i+15]^rfiddata[i+20]^rfiddata[i+25]^rfiddata[i+30]^rfiddata[i+35]^rfiddata[i+40]^rfiddata[i+45]==rfiddata[i+50])) error++;
                 }
			 if (error==0) {
			     beeps(1);
                 UDR=10;
                 wait_tx();
	             UDR=13;
	             wait_tx();
				 for (i=0;i<10;i++) {    //对10行数据进行校验
			         data1=(rfiddata[i*5]*8)+(rfiddata[i*5+1]*4)+(rfiddata[i*5+2]*2)+rfiddata[i*5+3];
                     UDR=ascii[data1%16];
					 wait_tx();
				 } 
				} 
		    }
		 }
	} 

	
void int0_isr(void)
{
 //external interupt on INT0
}


void main(void)
{
    unsigned char i,j,t_i;
    init_devices();
    RFW_1();
    okled0();
	while (1) {
		if (run==1) { 
			R_H4001();
			led();  
			run=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
*/     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -