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

📄 main.c

📁 EM4001,EM4100等系列只读ID卡读取程序源码
💻 C
字号:
#include <sst/SST89x5x4.H>
#include <string.h>
#include <stdio.h>
//PCA REGS

sfr CH=0xF9;  //PCA COUNER/TIMER
sfr CL=0xE9;

sfr CCON=0xD8;
sfr CMOD=0xD9;

sfr CCAPM0=0xDA;
sfr CCAPM1=0xDB;
sfr CCAPM2=0xDC;
sfr CCAPM3=0xDD;
sfr CCAPM4=0xDE;

sfr CCAP0H=0xFA;
sfr CCAP0L=0xEA;
sfr CCAP1H=0xFB;
sfr CCAP1L=0xEB;
sfr CCAP2H=0xFC;
sfr CCAP2L=0xEC;
sfr CCAP3H=0xFD;
sfr CCAP3L=0xED;
sfr CCAP4H=0xFE;
sfr CCAP4L=0xEE;

sbit CCF0=CCON^0;
sbit CCF4=CCON^4;
sbit CR=CCON^6;


// This program is designed to use the PCA module to calculate the width of a
// detected pulse. The pulse must begin with a rising edge and end with a falling
// edge on the CEX0 pin.
// The HEADER FILE “SST89X5X4.h” is an SST proprietary header file that defines SST’s SFRs.
// This file can be found on the SST website, or the BSL Demo Kit.
bit flag = 0; // Rising or falling edge flag.
unsigned int pulse_width = 0x00; // Final pulse width calculation is stored here.
unsigned int capture1 = 0x00; // Rising edge captured here.
unsigned int capture2 = 0x00; // Falling edge captured here.
unsigned char buf_p=0,buf_p2=0;
unsigned char idata dat[129],iddat[10];

unsigned char code bitcode[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

/*
void PCA_ISR() interrupt 6 // PCA Interrupt Service routine
{
 unsigned int temp;

 CCF4 = 0; // Clear the PCA flag

 if (flag == 0)
 {
 	capture1 = CCAP4L | (CCAP4H << 8); // If positive edge, store in

   	CCAPM4 = 0x11; // capture1. Setup module

	flag = 1; // 0 for capturing falling edge
  
    temp=(capture1-capture2)/100;

}
else
{
	capture2 = CCAP4L | (CCAP4H << 8); // Capture falling edge

	CCAPM4 = 0x21; // Setup module 0 for capturing rising edge.

	flag = 0; // Reset flag	

	temp=(capture2-capture1)/100;

 }
 
   if(temp<2)return;
  if(temp>12)return;

  if( temp<6 )                //分解数据,脉宽判断
       dat[buf_p++]=flag;

  else 
  {
       dat[buf_p++]=flag;
       dat[buf_p++]=flag;
	


  }

 if( buf_p< 17)                        //起始位判断
 if(dat[buf_p-1] != ( (buf_p-1) & 1))  
   buf_p=0;                                      

 if( buf_p >=128 )
 {  
    buf_p=128;
    CCAPM4=0;
	CMOD=0;
	CR = 0; 
	IE = 0x00; 
 }        
}
*/
void PCA_intr()interrupt 6 using 1
{
	 
		CCF4=0;
		if(flag==0)
		{
		capture1 = CCAP4L|(CCAP4H<<8);
		CCAPM4=0x11;                                     //设置为下降沿中断
		flag=1;
		pulse_width=(capture1-capture2)/100;
		}
        else
		{
		capture2=CCAP4L|(CCAP4H<<8);
		pulse_width=(capture2-capture1)/100;
		CCAPM4 = 0x21;									//设置为上升沿中断
		flag=0;

		}

	 

	  	if((pulse_width<2)||(pulse_width>12))
			return;
  		 

  		if( pulse_width<6 )                //分解数据,脉宽判断
      		 dat[buf_p++]=flag;			
 		else 
  		{
	       dat[buf_p++]=flag;
	       dat[buf_p++]=flag;
  		}
 
 		if( buf_p< 17)                        //起始位判断
 		{
			if( dat[buf_p-1] != ( (buf_p-1) & 1))  //如果不是1,剔除
   				buf_p=0;                                      
		} 
 		if( buf_p >=128 )
		{  
		    buf_p=128;
		    CCAPM4=0;
			CMOD=0;
			CR = 0; 
			IE = 0x00; 						//停止中断使能
		}        
	

}


 /*
bit check_emid(unsigned char *dat,unsigned char *out,unsigned int len)
{
   unsigned int i,j,k;   
   unsigned char result=0;
//   unsigned char check;
   unsigned char code head[]={1,1,1,1,1,1,1,1,0};

   if( memcmp(head,dat,9) != 0 )return 0;    

   i=8;

   for(j=0;j<10;j++)
   {
     result=0;

     for(k=0;k<4;k++)
     {
      result <<=1;
      if(dat[i+j*5+k])
	       result |=0x01;
	 }
 
	 if(!(j % 2))out[j/2]=result;
	 else
     {
       out[j/2]=(out[j/2]<<4)+result; 	  
     }
   }

   return 1;
}*/
bit check_emid(unsigned char *dat,unsigned char *out )
{
   unsigned int i,j,k;   
   unsigned char result=0;
    
   unsigned char code head[]={1,1,1,1,1,1,1,1,0};

   if( memcmp(head,dat,9) != 0 )
		return 0;    

   i=8;

   for(j=0;j<10;j++)
   {
     result=0;

     for(k=0;k<4;k++)
     {
      result <<=1;
      if(dat[i+j*5+k])
	       result |=0x01;
	 }
 
	 if(!(j % 2))
		out[j/2]=result;
	 else
     {
       out[j/2]=(out[j/2]<<4)+result; 	  					//2组4bit 组成一个字节
     }
   }

   return 1;
}

void pcaint()
{
CMOD = 0x01; //Setting up the PCA counter
 CH = 0x00;
 CL = 0x00;
 CCAPM4 = 0x21; // Setup module zero in capture mode
 flag = 0;
 IE = 0xC0; // Enable PCA interrupt
 CR = 1; // Start PCA counter
  EA=1;
 


}

void GetID()											//读卡
{
	unsigned char i;
	unsigned long ID;

	  while(buf_p !=128);
	
	  for(i=0;i<65;i++)
	  {
	    dat[i]=(dat[i*2]==0) && (dat[i*2+1]==1);
	 
	  }   
	
	
	  if( check_emid(dat,iddat ) )
	  {
   		ID=*(unsigned long*)(iddat+1);					//获取的ID号放在iddat中,用一个LONG来表示
 	 	 putchar(ID);
   		putchar(ID);
	}
	 
 
	  CCAPM4 = 0x21; // Setup module zero in capture mode
	  flag = 0;
	  IE = 0xC0; // Enable PCA interrupt
	  CR = 1; // Start PCA counter
	  EA=1;
	  buf_p=0;

	 

}
void main()
{


 pcaint();
 while (1) // Software trap
 {
  
	GetID();

 }
}

⌨️ 快捷键说明

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