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

📄 s6700.c

📁 PIC18F4620、TIS6700下RFID程序
💻 C
字号:
#include "S6700.h"
#include "initial.h"
#include "timer.h"
#include "dealchars.h"
//#define PUSH 0x80
//#define DOUT 0x40
//#define DIN  0x20
//#define SCLOCK 0x10
//#define GLED 0x08
//#define RLED 0x04
//unsigned PUSH = PORTDbits.RD7;
//unsigned DOUT = PORTDbits.RD6;
//unsigned DIN = PORTDbits.RD5 ;
//unsigned GLED = PORTDbits.RD3;
//unsigned RLED = PORTDbits.RD2;
//unsigned SCLOCK = PORTDbits.RD4;
extern uchar timer_flag;
extern uchar cTemp;
//别处定义的带发送数据
extern uchar Data_buf[32];
uchar k;

///////////////////////////////////////////////////////////////////////////////
//Start signal , sent by CPU to S6700
//Start is a low-to-high transition on the PORTDbits.RD5 line while PORTDbits.RD4 is held high.
///////////////////////////////////////////////////////////////////////////////
void startS6700(void) 
{
	PORTD &= ~SCLOCK;                  //SCLOCK = 0
	PORTD &= ~DIN;                     //DIN = 0;
	PORTD |= SCLOCK;                   //SCLOCK = 1;
	PORTD |= DIN; 	                   //DIN = 1;
}
///////////////////////////////////////////////////////////////////////////////
//Stop signal , sent by CPU to S6700
//Stop is a high-to-low transition on the PORTDbits.RD5 line while PORTDbits.RD4 is held high.
///////////////////////////////////////////////////////////////////////////////
void stopS6700(void) 
{
    PORTD &= ~SCLOCK;  
    PORTD |= DIN;          
    PORTD |= SCLOCK;
    PORTD &= ~DIN;         
    PORTD &= ~SCLOCK;
}
//////////////////////////////////////////////////////////////////////////////
void tran1() 
{
	PORTD |= DIN;
	PORTD &= ~DIN;
	PORTD |= DIN;					   //TRAN1
}
//////////////////////////////////////////////////////////////////////////////
void tran2() 
{
	PORTD &= ~DIN;
	//PULSE_DELAY = 0;
	PORTD |= DIN;
	//PULSE_DELAY = 0;
	PORTD &= ~DIN;					   //TRAN2
	PORTD &= ~SCLOCK;		
}
///////////////////////////////////////////////////////////////////////////////
//Send data to S6700, include start, command, data, stop
///////////////////////////////////////////////////////////////////////////////
void SendDataS6700(uchar WholeByte_Length, uchar LastBit_Length) 
{
	uchar i=0, j;
	startS6700();
	while (i<WholeByte_Length) 
	{
		cTemp = Data_buf[i];
		//发送一个字节
		for (j=0; j<8; j++) 
		{
			PORTD &= ~SCLOCK;          //拉低SCLOCK
			while (PORTD & DOUT) ;	   //等待S6700FIFO为空,才能发送数据
			if (cTemp & 0x80)
				PORTD |= DIN;             
			else 
				PORTD &= ~DIN;	       //DIN = cTemp7;
			cTemp <<= 1;
			PORTD |= SCLOCK;           //PORTD |= SCLOCK;
		}
		i++;
	}
	//结束位发送
	if (LastBit_Length) 
	{
		cTemp = Data_buf[i];
		for (j=0; j<LastBit_Length; j++) 
		{
			PORTD &= ~SCLOCK;          //拉低SCLOCK
		    while (PORTD & DOUT);      //等待S6700FIFO为空,才能发送数据
			if (cTemp & 0x80)
				PORTD |= DIN;             
			else 
				PORTD &= ~DIN;         //DIN = cTemp7;
			cTemp <<= 1;
			PORTD |= SCLOCK;           //PORTD |= SCLOCK;
		}
	}
	stopS6700();	                   //ES1
							
}
///////////////////////////////////////////////////////////////////////////////
//Receive data returned by smart label via S6700
//the return value is the length of bits
///////////////////////////////////////////////////////////////////////////////
uchar RecDataS6700(void) 
{

	uchar  j=0,start_flag=0;
	timer_flag = 1;
	cTemp = 0;
	//启动定时器,此处需要重新改///////////////////////////////////////////////
	T0CON = 0x82;							//设定TMR0L工作于8位定时器方式
											//内部时钟,TMR0分频	 	
	INTCON = 0x20;                          //总中断禁止,TMR0中断允许,清除TMR0的中断标志
	INTCONbits.GIE = 1;                     //开总中断
   	//////////////////////////////////////////////////////////////////////////
	PORTD &= ~SCLOCK;         //
	PORTD |= DIN; 
	Nop();   								//0917 add
    PORTD &= ~DIN;
	 
	//PORTD &= ~SCLOCK;
	InputIO('D',SCLOCK);                    //改变为输入 
 	Nop();   								//0917 add
											//TRAN1
    PORTD |= DIN;     
	k=0;
	//InputIO('D',SCLOCK);                    //改变SCLOCK为输入
    while ((!start_flag) && (timer_flag) )  //等待S2
    {	
		while (PORTDbits.RD4 == 1)                //SCLOCK high 
		{
			if (PORTDbits.RD6 == 1)                 //DOUT high
		    {
				start_flag = 1;
				//PORTDbits.RD1 ^= 1;
			}		          					//start send data from s6700
		}
	}
    //Nop();
    while (start_flag && timer_flag) 
    {
		while (!(PORTD & SCLOCK));            //等待SCLOCK变高
		if (PORTD & DOUT)                   //读此时的DOUT
			cTemp|=0x01;
		else 
			cTemp&=~0x01;                   
		if ((cTemp&0x01)!=0x01)             //如果DOUT为低
			while (PORTD & SCLOCK);         //等待SCLOCK变低,就是发送了0
		else 
		{                                      
			while ((PORTD & DOUT) &&(PORTD & SCLOCK));//此时DOUT为高,如果在SCLOCK为高时
			if (PORTD & SCLOCK)						  //DOUT变低,为ES2,需要复位start_flag;
													  //否则SCLOCK先变低就是发送了1	
				start_flag = 0;
		}
		if (start_flag)                     //将读到的数据放到缓冲区
		{
			if (j<7) 
			{
				cTemp <<= 1;
				j++;
			}
			else 
			{
				Data_buf[k+1] = cTemp;	    //move the received char into buffer
				j = 0;
				k++;
			}
		}
	}//end of while (start_flag && timer_flag)
    INTCONbits.TMR0IE = 0;                        //关闭定时器
    
	PORTD &= ~DIN;
	Nop();  
	PORTD |= DIN;
	Nop();   
	//PORTD |= SCLOCK; // output//怎么改?
	OutputIO('D',SCLOCK);	   
	PORTD &= ~DIN ;							//TRAN2
	if (k==0 && j==0)
		return 0;
	else
		return k;
}

///////////////////////////////////////////////////////////////////////////////
//Read data from register of 6700
///////////////////////////////////////////////////////////////////////////////
uchar ReadTIC(void) 
{
	uchar i=0;
	startS6700();			      //S1
	cTemp = 0x71;                 //Read from Configuration Register
	while (i<8)
	{
		PORTD &= ~SCLOCK;         //拉低时钟,等待FIFO寄存器为空 
		while (PORTD & DOUT);
		if (cTemp & 0x80) 
		      PORTD |= DIN;             
		else 
		      PORTD &= ~DIN;   	  //DIN = cTemp7;
		cTemp <<= 1;
		PORTD |= SCLOCK;
		i++;
	}
	PORTD &= ~SCLOCK; 
    PORTD &= ~DIN;                //DIN= 0;
	i = 0; 
	cTemp = 0;
	while (i<8) 
	{
		PORTD |= SCLOCK;          //PORTD |= SCLOCK;
		cTemp <<= 1;
		if (PORTD & DOUT) 
			cTemp|=0x01;
		else 
			cTemp &=~0x01;        //cTemp0=PORTDbits.RD6
		PORTD &= ~SCLOCK;         //PORTD &= ~SCLOCK;
		i++;
	}// end of while
	stopS6700();				  //ES1
	return cTemp;
} 
///////////////////////////////////////////////////////////////////////////////
//Initiate the timer register of S6700
///////////////////////////////////////////////////////////////////////////////
void InitTIC(void) 
{
	Data_buf[0] = 0x7B;						//Initiate the communication between CPU and S6700,InitTIC_command = 7Bh
	Data_buf[1] = 0x80;						//MSB 8 bits 1000 0000 
	Data_buf[2] = 0xC0;						//LSB 5 bits 1100 0 
	SendDataS6700(2, 5);
} 
///////////////////////////////////////////////////////////////////////////
//PowerDown 
///////////////////////////////////////////////////////////////////////////
void SleepTIC(void)
{
	uchar i;
    //InitTIC();
    startS6700();
    cTemp = 0x72;                          //powerdomn commmand 0111 0010
    for(i=0;i<8;i++) 
    {
    	PORTD &= ~SCLOCK;                  //PORTD &= ~SCLOCK; 
        while (PORTD & DOUT);
        	if (cTemp & 0x80) 
            	PORTD |= DIN;             
            else 
                PORTD &= ~DIN;            //DIN = cTemp7;
            cTemp <<= 1;
            PORTD |= SCLOCK;
    }
  	cTemp = 0x00;                         //powerdomn commmand 0000 0000
   	for(i=0;i<8;i++) 
    {
    	PORTD &= ~SCLOCK;                 //PORTD &= ~SCLOCK; 
        while (PORTD & DOUT);
        if (cTemp & 0x80)
			PORTD |= DIN;             
        else
			PORTD &= ~DIN;   			  //DIN = cTemp7;;
        cTemp <<= 1;
        PORTD |= SCLOCK;
    }
    stopS6700();
    startS6700();
    cTemp = 0x7E;                        //powerdomn commmand 0111 1110
    for( i=0;i<8;i++) 
    {
    	PORTD &= ~SCLOCK;                //PORTD &= ~SCLOCK; 
        while (PORTD & DOUT);
        if (cTemp & 0x80)
			PORTD |= DIN;             
        else
			PORTD &= ~DIN;              //DIN = cTemp7;
        cTemp <<= 1;
        PORTD |= SCLOCK;
    }
    stopS6700();        
}
///////////////////////////////////////////////////////////////////////////////
//Wake up
//////////////////////////////////////////////////////////////////////////////
void WakeupTIC(void)
{
	startS6700();
    delay(400);
    Data_buf[0]=0x7d;
    Data_buf[1]=0x22;
    SendDataS6700(2,0);
}

///////////////////////////////////////////////////////////////////////////////
//Communication between CPU and smart label via S6700 
///////////////////////////////////////////////////////////////////////////////
uchar SendINC(uchar inc_length) 
{
	uint  Crc, wCrc;
	uchar i,rbyte=0;
	SendDataS6700(inc_length, 0);				//Send data to S6700
        rbyte = RecDataS6700();					//receive data from S6700
	if (rbyte == 0)
		return 0;
    for (i=1; i<=rbyte; i++)
		Data_buf[i] = reverse_byte(Data_buf[i]);
	Crc = (uint)Data_buf[rbyte];
	Crc <<= 8;
	Crc |= (uint)Data_buf[rbyte-1];				//CRC read from smartlabel
	wCrc = CRC16CheckSum(Data_buf+1, rbyte-2);		//CRC computed
	if (Crc == wCrc)
		return rbyte;
	else
		return 0;
}

//////////////////////////////////////////////////////////////////////////////////
//read Uid of smartlabel
/////////////////////////////////////////////////////////////////////////////////
uchar Read_UID(void)
{ 
	uint  wCrc;
    uchar i;
    InitRFID();        

    Data_buf[30]=ReadTIC();
    if (Data_buf[30]!=34)
        return 0;
    InitTIC();


    Data_buf[0] = 0x2d;         //    03command length  2d 00 2b read UID
    Data_buf[1] = 0x00;
    Data_buf[2] = 0x2b;
   
    wCrc = CRC16CheckSum(Data_buf+1, 2);
    Data_buf[3] = (uchar)(wCrc & 0x00ff);			//LSB of CRC
    Data_buf[4] = (uchar)((wCrc & 0xff00) >> 8);	        //HSB of CRC
          
    for (i=1; i<=4; i++)
		Data_buf[i] = reverse_byte(Data_buf[i]);
    i = SendINC(5);
    return i;	 //length of read saved in Data_buf[0]; Databuf[3..10] is the UID;
}

//////////////////////////////////////////////////////////////////////////////////
//read a block of smartlabel
/////////////////////////////////////////////////////////////////////////////////

uchar Read_block(uchar block_num)
{
	uint  wCrc;
    uchar i;
    InitRFID();    
    Data_buf[30]=ReadTIC();
    InitTIC();  
    Data_buf[0] = 0x2d;         //    03command length  2d 40 20 read block
    Data_buf[1] = 0x40;         //    optional_flag is seted for block securety status
    Data_buf[2] = 0x20;
    Data_buf[3] = block_num;         //    block number is block_num
    wCrc = CRC16CheckSum(Data_buf+1, 3);
    Data_buf[4] = (uchar)(wCrc & 0x00ff);			//LSB of CRC
    Data_buf[5] = (uchar)((wCrc & 0xff00) >> 8);	        //HSB of CRC
    for (i=1; i<=5; i++)
		Data_buf[i] = reverse_byte(Data_buf[i]);
    //IE2 &=~ URXIE0;
    //changeDCO();
    i = SendINC(6);
        
	//ResetDoc();
    //SleepTIC();
    //InitSeriel();
    //IE2 |= URXIE0;
    return i;	//length of read saved in Data_buf[0]; Databuf[3..6] is the block_data; 
}

//////////////////////////////////////////////////////////////////////////////////
//write block2 of smartlabel, the data must be store in Data_buf[20]...Data_buf[23]
/////////////////////////////////////////////////////////////////////////////////
uchar Write_block(uchar block_num)
{
	uint  wCrc;
    uchar i;
    InitRFID();        

    Data_buf[30]=ReadTIC();
    if (Data_buf[30]!=34)
    	return 0;

    InitTIC();

    Data_buf[0] = 0x2d;         //    03command length  2d 40 21 write block
    Data_buf[1] = 0x40;
    Data_buf[2] = 0x21;
    
    Data_buf[3] = block_num;         //    block number is 00
              
    Data_buf[4] = Data_buf[20];         //    data to write
    Data_buf[5] = Data_buf[21];
    Data_buf[6] = Data_buf[22];
    Data_buf[7] = Data_buf[23];
         
    wCrc = CRC16CheckSum(Data_buf+1, 7);
    Data_buf[8] = (uchar)(wCrc & 0x00ff);			//LSB of CRC
    Data_buf[9] = (uchar)((wCrc & 0xff00) >> 8);	
    for (i=1; i<=9; i++)
		Data_buf[i] = reverse_byte(Data_buf[i]);
    //IE2 &=~ URXIE0;
    //changeDCO();
    i = SendINC(10);
        
    //ResetDoc();
    //SleepTIC();
    //InitSeriel();
    //IE2 |= URXIE0;
    return i;	
}
///////////////////////////////////////////////////////////////
///////写S6700Configuration寄存器
///////////////////////////////////////////////////////////////
void WriteConfigReg()
{
	Data_buf[0] = 0x7D;						//Write Configuration Register
	Data_buf[1] = 0x34;						//MSB 8 bits 1000 0000 
	SendDataS6700(2,0);
}

⌨️ 快捷键说明

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