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

📄 main.c

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


#include <avr/io.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/pgmspace.h>

#include "level4.h"
#include "level3.h"
#include "level2.h"
#include "level1.h"

//#define DEBUG 1 

// ==================================================================
// global variable declaration;

uint8_t counter1set;                  //counter1 settings
uint8_t forward_link_type;            //forward link modulation type
uint8_t watchdog_reset;               //watchdog activated
uint16_t write_4094_low = 0x6633;     // low word of SPI configuration word
uint16_t write_4094_hi  = 0x0280;      // hi word of SPI configuration word
uint8_t weigan_format;
//uint8_t weigan_enable;
//uint8_t keyboard_enable;              //keyboard enable, 1--keyboard scan, 0 -- not keyboard scan;

uint8_t ID __attribute__((section(".eeprom")))            = 0x01;
uint8_t SERIAL_NUMBER __attribute__((section(".eeprom"))) = 0xFF;
uint8_t WEIGAN_FORMAT __attribute__((section(".eeprom"))) = 0x01;
//uint8_t WEIGAN_EN_485 __attribute__((section(".eeprom"))) = 0x02;
uint8_t AUTH_CODE __attribute__((section(".eeprom")))     = 0x00;
uint8_t EEPROM_CHECK __attribute__((section(".eeprom")))  = 0x00;
uint8_t RESET_DEFAULT __attribute__((section(".eeprom"))) = 0x00;
uint8_t ALARM_LEVEL __attribute__((section(".eeprom")))   = 0x01;
uint8_t ALARM_TIME __attribute__((section(".eeprom")))    = 0x05;
uint8_t ALARM_ENABLE __attribute__((section(".eeprom")))  = 0x01;

uint8_t ID_CHK __attribute__((section(".eeprom")))            = 0x01;
uint8_t SERIAL_NUMBER_CHK __attribute__((section(".eeprom"))) = 0xFF;
uint8_t WEIGAN_FORMAT_CHK __attribute__((section(".eeprom"))) = 0x01;
//uint8_t WEIGAN_EN_485_CHK __attribute__((section(".eeprom"))) = 0x02;
uint8_t AUTH_CODE_CHK __attribute__((section(".eeprom")))     = 0x00;
uint8_t EEPROM_CHECK_CHK __attribute__((section(".eeprom")))  = 0x00;
uint8_t RESET_DEFAULT_CHK __attribute__((section(".eeprom"))) = 0x00;
uint8_t ALARM_LEVEL_CHK __attribute__((section(".eeprom")))   = 0x01;
uint8_t ALARM_TIME_CHK __attribute__((section(".eeprom")))    = 0x05;
uint8_t ALARM_ENABLE_CHK __attribute__((section(".eeprom")))  = 0x01;

uint8_t weigan_data[ 5 ];
//uint8_t history_weigan_data[ 5 ];
uint8_t data_same_times;

uint8_t beep_times;    //get a valid card ID , beep beep_times times;
uint8_t alarm_level;   //防拆开关有效电平,0--低电平有效,1--高电平有效;

uint8_t test;
uint8_t watchdog_feed_flag;        
uint8_t em4094_invalid_data_times;  //EM4094读出的数据是无效的次数,即没有读到卡号的次数;

uint8_t unauth_card_times;
uint8_t alarm_time;
uint8_t alarm_enable;

// ==================================================================
// global function declaration;
void UARTIni( void );                    // initialization of UART
void avr_ini( void );
void Beep( uint8_t times );
void Beep_Long( void );
void Delayms( uint16_t count );
void Delayus( uint16_t count );
void Parse_Command( uint8_t cmd , uint8_t param );
void main_receiver( void );
int main( void );
uint8_t Check_EEPROM( void );
void Reset_To_Default( void );
void WatchDog_Feed( void );
//uint8_t Compare_Weigan_Data( void );
void Send_Card_ID( uint8_t format , uint8_t *data );
void Alarm_Check_And_Process( void );


// ********************************************************************************************
// Description : UART initialize routinue.
// Return value: None.   
// parameter   : None.  
// ********************************************************************************************

void UARTIni( void ) 
{
    // UCSRA is not necessary to set up, using initial valuses         
    // no double transmission speed, no multi-processor mode           
    //UCSRA: RXC , TXC , UDRE , FE , DOR , PE , U2X , MPCM ;
    //UCSRB: RXCIE, TXCIE, UDRIE, RXEN, TXEN, UCSZ2, RXB8,TXB8 ;
//    UCSRB  = ( 1 << RXCIE ) | ( 0 << UDRIE ) | ( 1 << RXEN ) | ( 1 << TXEN ); 
    UCSRB  = ( 1 << RXCIE ) | ( 0 << TXCIE ) | ( 1 << RXEN ) | ( 1 << TXEN );  
                       // set up : RXCIE, UDRIE, RXEN, TXEN           
                       // (interrupt enable from receiver,            
                       // UART receiver enable, UART transmit enable) 
    // UCSRC: URSEL, UMSEL, UPM1, UPM0, USBS, UCSZ1, UCSZ0, UCPOL ;
    UCSRC = ( 1 << URSEL ) | ( 1 << UCSZ1 ) | ( 1 << UCSZ0 ); 
                       // asynchronous operation, 8 data bits,        
                       // no parity, 1 stop bit                       
//      UCSRC = ( 1 << URSEL ) | ( 1 << UPM1 ) | ( 1 << UPM0 ) | ( 1 << UCSZ1 ) | ( 1 << UCSZ0 );
                       // asynchronous operation, 8 data bits, odd parity, 1 stop bit;
    UBRRL = 103;        // baud rate - see UBRR register setting table 
                       //             in AVR ATmega8 specification    
                       // UBRRH - using initial values                
                       // UBRRL - 103 for 9.6Kbps and frequency f=16 MHz 
                       // (using SINGLE transmission speed)   
//      UBRRL = 51;        // baud rate - 19200;                          

    rx_number = 0;
    tx_number = 0;
    valid_data_flag = 0;         //This sign indicate if there is a valid command received;

}

// ********************************************************************************************
// Description : Atmega8 device initialize.
// Return value: None.   
// parameter   : None.
// ********************************************************************************************

void avr_ini( void )
{

    UARTIni(); 
             
    // --------------------------------------------------------------- 
    // 16-bit Timer/Counter1 initialization                            
    // --------------------------------------------------------------- 

    TCCR1A = 0;         // no compare otuput, no PWM mode ...  
    TCCR1B = 0;         // stopped on startup

    // --------------------------------------------------------------- 
    // I/O Ports initialization                                        
    // --------------------------------------------------------------- 

    DDRB  = 0x06;
    DDRC  = 0x37;
    DDRD  = 0xC2;
    
    PORTB = 0x00;        //如果weigan0和weigan1不置低,则wd0和wd1第一次输出会有问题;
    PORTC = 0x01;
    PORTD = 0xC0;       
                                       
    // --------------------------------------------------------------- 
    // Watch Dog Setup
    // --------------------------------------------------------------- 
    cbi( MCUCSR , WDRF );  //clear watchdog reset flag
    wdt_enable( 0x07 );    //set timeout of Watchdog is 2.1s;

    while ( Check_EEPROM( ) == 0x00 )       //Ensure EEPROM is ok;
    	  Reset_To_Default( );

    watchdog_feed_flag = 0 ;  //feed dog sign,0 for not feed dog , other value for has feed dog;
//    weigan_format = 1 ;     //1--Weigan26 , 2--Weigan34 , 3--Weigan36 , 4--Weigan44 , 5--9600bps;
	  if( eeprom_rb( &WEIGAN_FORMAT ) == eeprom_rb( &WEIGAN_FORMAT_CHK ) )
	  {	
	      weigan_format = eeprom_rb( &WEIGAN_FORMAT );
	  } 
	  else
	  	  weigan_format = 0x01;
	  	   	  
	  if( eeprom_rb( &ALARM_LEVEL ) == eeprom_rb( &ALARM_LEVEL_CHK ) )   
	  	  alarm_level = eeprom_rb( &ALARM_LEVEL );
	  else
	  	  alarm_level = 0x01;
	  	  	  
	  if( eeprom_rb( &ALARM_TIME ) == eeprom_rb( &ALARM_TIME_CHK ) )   
	  	  alarm_time = eeprom_rb( &ALARM_TIME );   
	  else
	  	  alarm_time = 0x05;		  

	  if( eeprom_rb( &ALARM_ENABLE ) == eeprom_rb( &ALARM_ENABLE_CHK ) )   
	  	  alarm_enable = eeprom_rb( &ALARM_ENABLE );   
	  else
	  	  alarm_enable = 0x01;
	  	  	  	   
    Beep( 2 );
    
    Set_GREEN_LED( );
    Delayms( 300 );
    Set_RED_LED( );
    Delayms( 300 );

    sei( );                                      // General Interrupt Enable                    
}

void WatchDog_Feed( void )                       //Feed watchdog of the MAX705;
{
	  if ( watchdog_feed_flag == 0 )
	  {
	  	  watchdog_feed_flag = 1;
	  	  sbi( PORTC , WATCHDOG ); 
	  }
	  else
	  {
	  	  watchdog_feed_flag = 0;
	  	  cbi( PORTC , WATCHDOG );
	  }
}
// ********************************************************************************************
// Description : Main loop.
// Return value: None.   
// parameter   : None.
// ********************************************************************************************

void main_receiver( void ) 
{
//    uint8_t i;
    bufferClean       = 1;                              //on startup, make buffer not clean
    forward_link_type = 2;                              //default value, 1=type B, 2=type A

    counter1set       = ( 1 << ICNC1 ) | ( 1 << CS10 ); //Timer1 input capture at falling edge;
    
    WriteSPI( write_4094_low , write_4094_hi );    //Configure EM4094 option bit;
    Wait( 4000 );                                  //Wait some time for EM4094 enter in normal state;

    while ( 1 )                                    // infinite loop
    {
    	  wdt_reset( );
    	  
#ifdef WDT
        WatchDog_Feed( );
#endif
 	  
    	  cbi( PORTC , TEN_485 );

        if ( valid_data_flag == 0 )
        {

        }
        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( );
       
        TypeA_GetUID( );              //Get UID of TypeA;

  
        if ( em4094_data_valid == 0x55 )
        {
    	      em4094_data_valid = 0x00;
        	  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 );            	
            }
        } 
    }
}

void Parse_Command( uint8_t cmd , uint8_t param )
{
//	  cli( );
	  switch ( cmd )
    {
        case 0x01 :             //Modify ID number;
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &ID , param );
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &ID_CHK , param );
        	  
        	  break;
        	  
        case 0x02 :             //Modify serial number;
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &SERIAL_NUMBER , param );
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &SERIAL_NUMBER_CHK , param );
        	  
        	  break;
        	  
        case 0x03 :             //Modify format of weigan output,( 1 -- 26, 2 -- 34, 3 -- 36, 4 -- 44 , 5 -- 9600bps);
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &WEIGAN_FORMAT , param );
        	  while ( !eeprom_is_ready( ) ){};
        	  eeprom_wb( &WEIGAN_FORMAT_CHK , param ); 
        	  weigan_format = param;       	
        	 
        	  break;   
        	       	  
        case 0x04 :             //Modify authentication code;
        	  eeprom_wb( &AUTH_CODE , param );
        	  eeprom_wb( &AUTH_CODE_CHK , param );        	           	
        	  break;
        	  
        case 0x05 :             //Modify EEPROM check enable;
        	  while ( !eeprom_is_ready( ) ){};

⌨️ 快捷键说明

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