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

📄 level3.c

📁 此程序为利用mage8 AVR 和EM4094实现读取TYPE B类型卡的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"

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

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

uint8_t clean_data_cnt;               //clean response data length
uint8_t maxCaptureTime;                 //current maximum capture time (used to initialise TCNT2)
uint8_t maxCaptureTimeScale;            //scale of current maximum capture time (expected value 3 - 7 only)
uint8_t temp;
uint8_t number;
uint8_t sec;
uint16_t maxTGeneral;                 //current datarate maximum response capture time
uint8_t expectedResponseLen;          // expected number of response bits


//--------------------------------------------------------------
//Local functions declaration
void Capture( void );
void Compute_Timeouts( void );
uint16_t ScaleOnce( uint16_t period );
uint8_t ScaleTime( uint16_t period );
void Send( uint8_t len , uint16_t wait_time );
void Wait( uint16_t period );
uint8_t Read_TypeB_Card( void );
//--------------------------------------------------------------
//compute the timeouts according to the flag byte
//
void Compute_Timeouts( void ) 
{
    uint16_t pom = 0;
    //type B
    pom = ( uint16_t )expectedResponseLen * 64 + 128;  //128RF/32 = 4 TCNT2 pulses per bit + reserve
    pom = ( 19 * ( uint32_t )pom ) / 16 + 1;   //13.56 to 16 correction
    maxTGeneral     = pom;
}



//--------------------------------------------------------------
//send command and wait
//
void Send( uint8_t len , uint16_t wait_time ) 
{
    uint8_t fwd_bit_count;
    uint8_t i;

    fwd_bit_count = Prepare_SOF( );

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

    fwd_bit_count += Prepare_EOF( len );

    Compute_Timeouts( );
    ClearCaptureBuffers( );
    maxCaptureTime = ScaleTime( maxTGeneral );

    SendForward( fwd_bit_count );

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


// ==================================================================
// Down Link setup function
// Requires: maxCaptureTime and maxCaptureTimeScale
//   (located here instead of level2 because of register variables)


void Capture( void ) 
{
    TCCR0 = 0;                                //disable Counter0
    TCCR1B = 0;                               //disable Counter1
    TCNT1 = 0;

    if ( !bufferClean ) 
    	  ClearCaptureBuffers( );
    bufferClean = 0;
    captured_bit_count = 0;                   //reset some values
//    last_valid = 0;

    //set hi byte of maximum capture timer2 timeout
    TCNT2 = ~maxCaptureTime;                  //set timer2 timeout
    TCNT0 = 0;                                //clear timer0
    TIFR = TIFR | ( 1 << ICF1 ) | ( 1 << TOV1 ) | ( 1 << OCF2 ) | ( 1 << TOV2 );      //clear pending interrupts

    sbi( TIMSK , TOIE2 );                      //enable timer2 overflow
    sbi( SFIOR , PSR2 );                       //clear timer2 prescaler

    TCCR2 = maxCaptureTimeScale;              //run T2


    number = 0;
    //type B
    sbi( GIFR , INTF1 );                       //clear any pending interrupt
    sbi( GICR , INT1 );                        //enable interrupt 1
    while ( TIMSK & ( 1 << TOIE2 ) )              //wait until done for BPSK
    { }
    cbi( GICR , INT1 );                        //disable interrupt 1
    TCCR2 = 0;
    cbi( TIMSK , TOIE2 );                        //disable timer2 overflow
    sbi( TIFR , TOV2 );                          //clear pending interrupt
}

// ==================================================================
// Wait

void Wait( uint16_t period ) 
{
    uint8_t time = ScaleTime( period );
    TCCR2 = 0;                                //disable Counter2
    TCNT2 = -time;                            //set timer with initial time
    TIFR = TIFR | ( 1 << OCF2 ) | ( 1 << TOV2 );      //clear pending interrupts
    sbi( SFIOR , PSR2 );                       //clear T2 prescaler
    sbi( TIMSK , TOIE2 );                      //enable overflow

    TCCR2 = maxCaptureTimeScale;              //run! at clkio/scale
    while ( TIMSK & ( 1 << TOIE2 ) )              //wait until done
    { }
    TCCR2 = 0;                                //stop
}

// ==================================================================
// Scale Once - minimum scale is RF/32
// fixed parameters - maxCaptureTimeScale

uint16_t ScaleOnce( uint16_t period ) 
{  
    if ( period > 255 ) 
    {
        period /= 2;
        if ( maxCaptureTimeScale <= 5 ) 
        {
            maxCaptureTimeScale++;
        } 
        else if ( maxCaptureTimeScale == 6 ) 
        {
            period /= 2;
            maxCaptureTimeScale++;
        } 
        else if ( period > 255 ) 
        {
            period = 255;
        }
    }
    return period;
}


// ==================================================================

uint8_t ScaleTime( uint16_t period ) 
{
    uint8_t i;
    maxCaptureTimeScale = 3;
    for ( i = 0 ; i < 4 ; i++ ) 
    {
        period = ScaleOnce( period );
    }
    return period;
}

// ==================================================================
// ==================================================================
// INTERRUPT ROUTINES
// ==================================================================

SIGNAL ( SIG_OVERFLOW2 )
{
  	    
    TIMSK = 0;                                    //finished, disable all
}

// ==================================================================
// PBSK valid data 

SIGNAL ( SIG_INTERRUPT1 )
{ 
	  sec = 0 ;
    temp = ( ( PIND >> 4 ) & 1 ) ^ 1;
    while ( ( PIND & 0x08 ) == 0x08 )
	  {
	      sec++;
	  }
	  if ( sec >= 5 )
	  {
		    raw_data[ number++ ] = temp ;		 
	  }
}

uint8_t Read_TypeB_Card( void )
{
	  uint8_t i;
 
    expectedResponseLen = 14 ;    
	          
    Send( 5 , 0 );          
          
    maxCaptureTime = ScaleTime( maxTGeneral );     
  	          
    Capture( );
   	    
    clean_data_cnt = ExtractTypeBData( 248 );

    if ( clean_data_cnt == ( expectedResponseLen + 1 ) * 8 ) 
    {
        switch( type_b_setting )
        {
            case 1 :
                weigan_data[ 3 ] = data_buffer[ 4 ];
                weigan_data[ 2 ] = data_buffer[ 3 ];
                weigan_data[ 1 ] = data_buffer[ 2 ];
                weigan_data[ 0 ] = data_buffer[ 1 ];	
            break;
            
            case 2 :
                weigan_data[ 3 ] = data_buffer[ 8 ];
                weigan_data[ 2 ] = data_buffer[ 7 ];
                weigan_data[ 1 ] = data_buffer[ 6 ];
                weigan_data[ 0 ] = data_buffer[ 5 ];            	  
            break;
            
            case 3 :
                weigan_data[ 3 ] = data_buffer[ 4 ];
                weigan_data[ 2 ] = data_buffer[ 3 ];
                weigan_data[ 1 ] = data_buffer[ 8 ];
                weigan_data[ 0 ] = data_buffer[ 7 ];            	
            break;
            
            case 4 :
                weigan_data[ 3 ] = data_buffer[ 8 ];
                weigan_data[ 2 ] = data_buffer[ 7 ];
                weigan_data[ 1 ] = data_buffer[ 4 ];
                weigan_data[ 0 ] = data_buffer[ 3 ];            	
            break;
            
            default:
            break;	
        }
        uint8_t check_data = 0;
        for ( i = 0 ; i < 4 ; i++ )
          check_data ^= weigan_data[ i ];
        weigan_data[ 4 ] = check_data; 
        return 1;     
    }
    else
    {
        return 0 ;
    }	
}




⌨️ 快捷键说明

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