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

📄 level3.c

📁 此程序为利用mage8 AVR 和EM4094实现读取ISO15693类型卡的C程序、读卡稳定、这个项目我们做了好久、非常不错的程序、很适合做这方面产品的朋友们参考
💻 C
字号:


#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include "level4.h"
#include "level3.h"
#include "level2.h"
#include "level1.h"

//--------------------------------------------------------------
//global variables

uint8_t maxCaptureTimeLow;           //lower part of current maximum capture time (used to initialise TCNT1)
uint8_t maxCaptureTimeHi;            //upper part of current maximum capture time (expected value 0xFF or 0xFE only)

//--------------------------------------------------------------
//local declarations

#define STD_WAIT 145                  //~318us = (145+13)x32 @ 16MHz , 13x32 uC clocks is a setup reserve

uint8_t clean_data_cnt;               //clean response data length

uint16_t maxTGeneral;                 //current datarate maximum response capture time
uint16_t maxTwa1ee;                   //

uint8_t signed_crc;                   //flag of signed response

uint8_t authSelectFlag;               //disable selection before HW Authentication
//uint8_t em4094_data_invalid_times;
//uint8_t history_weigan_data[ 5 ];
uint8_t em4094_data_valid_flag;
uint8_t data_same_times;

//void SendCaptureData( uint8_t cmd, uint8_t ack );
void AddCRC( uint8_t where );
void Compute_Timeouts( uint8_t flags );
uint8_t Compare_Weigan_Data( void );
//void Inventory( void );
int8_t InventoryStep_version4( void );
void main_receiver( void );
void Send( uint8_t len , uint16_t wait_time );
void Alarm_Check_And_Process( void );

//uint8_t HW_Authentication(void);
//uint8_t ToggleEAS(void);
//uint8_t GeneralRead(void);
//uint8_t GeneralWrite(void);
//void RF_Reset(void);
//uint8_t SwitchToNormalMode(void);

//--------------------------------------------------------------
// main loop routine
//--------------------------------------------------------------

void main_receiver( void ) 
{

    uint8_t fwd_bit_count;
//    uint8_t i;
    decode_number = 2;
    forward_link_type = 0x01;               //default value = 1 from 4 forward link    

    uint16_t write_4094_low = 0xCB33;
    uint16_t write_4094_hi  = 0x0280;
    
    WriteSPI( write_4094_low , write_4094_hi );
    Wait( 4000 );

    while ( 21 ) 
    {                              // infinite loop

        wdt_reset( );                            // reset the watchdog
#ifdef WDT
        WatchDog_Feed( );
#endif       
  
        if ( valid_data_flag == 0 )
        {
//       	 	  SendByte( 0xEE );                    //output error sign;
        }
        else
        { 
       	    valid_data_flag = 0x00;
      	    SendByte( 0x55 );                  //response to PC;
      	    SendByte( 0xAA );
      	    SendByte( 0x01 );
      	    SendByte( 0x02 );
      	    Beep( 1 );
      	    Parse_Command( uart_in_command , uart_in_param ); 
        } 
        
        Alarm_Check_And_Process( );   
                                        
        expectedResponseLen = 0x60;
        cmd_message_len = 5;
        cmd_message[ 0 ] = 0x26;
        cmd_message[ 1 ] = 0x01;
        cmd_message[ 2 ] = 0x00;
        cmd_message[ 3 ] = 0xF6;
        cmd_message[ 4 ] = 0x0A;
        message_flags = cmd_message[ 0 ];
        forward_ptr = data_buffer;            // initialize fwd pointer
        fwd_bit_count = 0;
        if ( InventoryStep_version4( ) )
        {
            Send_Card_ID( weigan_format , weigan_data ); 

          	Set_GREEN_LED( );
            Delayms( 150 );
            Set_RED_LED( );
            
            if ( alarm_enable == 0x01 )
            {
	              if ( PINB & ( 1 << CYE ) )
	              {	  	         
	  	              beep_times = 2;
	  	              unauth_card_times++;
	              }
	              else
	              {
	  	               beep_times = 1;
	  	               unauth_card_times = 0;
	              }             
                Beep( beep_times );
                if ( unauth_card_times > 3 )
                {
            	      unauth_card_times = 0x00;
            	      uint8_t i;
            	      for ( i = 0 ; i < alarm_time ; i++ )
            	      {
            	          cbi( PORTD , BEEP_PIN ); 
            	          Delayms( 600 );
            	          sbi( PORTD , BEEP_PIN );            	      
            	      }
                }	  
            }
            else
            {
	              if ( PINB & ( 1 << CYE ) )
	              {	  	         
	  	              beep_times = 2;
	              }
	              else
	              {
	  	              beep_times = 1;
	              }             
                Beep( beep_times );            	
            }                	
        }
    }
}

//--------------------------------------------------------------
//compute the timeouts according to the flag byte
//
void Compute_Timeouts( uint8_t flags ) 
{
    register uint16_t pom;
    register uint16_t corr;

    pom = expectedResponseLen * 16;        //512RF/32 = 16 TCNT0 pulses per bit
    pom += 128;                            //reserve
    corr = expectedResponseLen / 2 + 2;    //correction for dual carrier

    if ( ( flags & 2 ) == 0 )
        pom *= 4;
    else
        corr >>= 2;

    if ( ( flags & 1 ) == 1 )
        pom -= corr;

    pom = ( 19 * ( uint32_t )pom ) / 16 + 1;

    decode_number = flags & 3;//////decode_number = 0x02;

    if ( decode_number & 2 ) 
    {
        halfDataRate    = 288;             //high data rates
        pulsesPerBit    = 8;
    } 
    else 
    {
        halfDataRate    = 288 * 4;         //low data rates
        pulsesPerBit    = 32;
    }
    maxTGeneral     = pom;
    maxTwa1ee       = write_tag_memory_delay + pom;

}

//--------------------------------------------------------------
//send command and wait
//
void Send( uint8_t len , uint16_t wait_time ) 
{
    uint8_t fwd_bit_count;
    int i;
    forward_ptr = data_buffer;
    fwd_bit_count = 0;

    for ( i = 0 ; i < len ; i++ ) 
        fwd_bit_count += Prepare_Data( cmd_message[ i ] );

    Compute_Timeouts( cmd_message[ 0 ] );
    ClearCaptureBuffers( );

    maxCaptureTimeLow = ( uint8_t )maxTGeneral; 
    maxCaptureTimeHi = maxTGeneral >> 8; 

    SendForward( fwd_bit_count );

    if ( wait_time != 0 ) 
    	  Wait( wait_time );
}


//--------------------------------------------------------------
// Appends crc to ISO15693 command
void AddCRC( uint8_t where ) 
{  
    uint16_t crc = CRC( cmd_message , where );
    cmd_message[ where++ ] = crc & 255;
    cmd_message[ where ] = crc >> 8;

}

//--------------------------------------------------------------
//--------------------------------------------------------------
//1TS Inventory

//#define DBG_1TS

#define STACK_LENGTH 8                //stack size

typedef struct {
    uint8_t mask_len;
    uint8_t value;
    uint8_t mask[ 12 ];
    uint8_t vata[ 2 ];                    //to keep this structure size being 2^n
} TINVENTORY;

TINVENTORY found[ STACK_LENGTH ];

int8_t found_ptr;

uint8_t dbg_counter;

//--------------------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------

// ****************** 1TS inventory version 4 ******************

TINVENTORY search;

int8_t InventoryStep_version4( void ) 
{
    uint8_t i, j, k, l;
    uint8_t result;
    uint16_t crc;
    expectedResponseLen = 96;

    j = search.mask_len;

    //set collision bit
    if ( j > 0 ) 
    {
        l = 2 + ( j - 1 ) / 8;
        for ( k = 0xFE , i = 0 ; i < ( ( j - 1 ) % 8 ) ; i++ ) 
            k <<= 1;   //!!!
        search.mask[ l ] = search.mask[ l ] & ( ~k );
        k = search.value << ( ( j - 1 ) % 8 );
        search.mask[ l ] = search.mask[ l ] | k;
    }
    cmd_message[ 0 ] = message_flags;
    cmd_message[ 1 ] = 0x01;
    cmd_message[ 2 ] = j;

    //copy uid as a mask
    if ( j != 0 ) 
    {
        l = ( j - 1 ) / 8;
        Copy( &cmd_message[ 3 ] , &search.mask[ 2 ] , l + 1 );
        j = ( j + 7 ) / 8;
    }  
    //compute CRC
    j += 3;
    AddCRC( j );
    j += 2;

    //form & send command & wait
    Send( j , STD_WAIT );

    //listen for response
    maxCaptureTimeLow = ( uint8_t )maxTGeneral;
    maxCaptureTimeHi = maxTGeneral >> 8;

    Capture( 2 );
    clean_data_cnt = 0;
    result = 0;

    if ( ( sof == 0 ) || ( capture_cnt < 8 ) )
    {

        return 0;
    }
    j = ExtractData( expectedResponseLen );        //find first data bit

//    Copy( found[ found_ptr ].mask , data_buffer , 12 );   //always copy data regardless the result

    if ( j == 0 ) 
    {

        //one valid response received
        clean_data_cnt = 12;

        crc = CRC( data_buffer , clean_data_cnt );
        if ( crc == ~0xF0B8 ) 
        {
            uint8_t check_data = 0;
 
            weigan_data[ 3 ] = data_buffer[ 2 ];
            weigan_data[ 2 ] = data_buffer[ 3 ];
            weigan_data[ 1 ] = data_buffer[ 4 ];
            weigan_data[ 0 ] = data_buffer[ 5 ];
            for ( i = 0 ; i < 4 ; i++ )
                 check_data ^= weigan_data[ i ];  
            weigan_data[ 4 ] = check_data;
//            em4094_data_valid_flag = 0x55;  
            return 1;          
        }
        else
        {
//       	    em4094_data_valid_flag = 0x00;
//       	    SendByte( 0x00 );
        	  return 0;
        }
    }
    else
    {
//        em4094_data_valid_flag = 0x00;
//        SendByte( 0x00 );
        return 0;	
    }
}


void Alarm_Check_And_Process( void )
{
	  if ( alarm_level == 0 )
	  {
	      if ( PINC & ( 1 << ALARM_IN ) )
	      {
	  	      sbi( PORTC , ALARM_OUT );
	  	      cbi( PORTD , BEEP_PIN );
	  	  }
	      else
	      {
	      	  cbi( PORTC , ALARM_OUT );	 
	      	  sbi( PORTD , BEEP_PIN ); 	
	      }  
	  }
	  else if ( alarm_level == 1 )
	  {
	      if ( PINC & ( 1 << ALARM_IN ) )
	      {
	      	  sbi( PORTD , BEEP_PIN );
	      	  cbi( PORTC , ALARM_OUT );
	      }
	      else
	      {
	      	  sbi( PORTC , ALARM_OUT );	
	      	  cbi( PORTD , BEEP_PIN );
	      }
	  }	  
}

/*
uint8_t Compare_Weigan_Data( void )
{
	  uint8_t i;
	  InventoryStep_version4( );
  
	  if ( em4094_data_valid_flag == 0x00 )
	  {
	  	  data_same_times++;
	  	  if ( data_same_times > 31 )
	  	  {
	  	      data_same_times = 0;
	  	      for ( i = 0 ; i < 5 ; i++ )
	  	          history_weigan_data[ i ] = 0x00;
	  	  }
	  	  return 0;
	  }
	  else
	  {
	  	  em4094_data_valid_flag = 0x00;
	  	  if ( data_same_times > 30 )
	  	      data_same_times -= 30;
	  	  else if ( data_same_times > 20 )
	  	  	  data_same_times -= 20;
	  	  else if ( data_same_times > 10 )
	  	      data_same_times -= 10;
	  	  for ( i = 0 ; i < 5 ; i++ )
	  	  {
	  	      if ( history_weigan_data[ i ] != weigan_data[ i ] )
	  	      	  break;	
	  	  }
	  	  if ( i != 5 )
	  	  {
	  	      for ( i = 0 ; i < 5 ; i++ )
	  	          history_weigan_data[ i ] = weigan_data[ i ];
	  	      return 1;
	  	  }
	  	  else
	  	  {
	  	  	  return 0;
	  	  }
	  }

}
*/

⌨️ 快捷键说明

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