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

📄 main.c

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

/*
*--change on 2007-02-27,increase the sensitive of the card-reader,decrease the beep and led display time;
*--version 1.1 
*--add one command 0x07 and remove two old command 0x04 and 0x05;
*--command 0x07 is used to configure the alarm level of ALARM1;
*--add CYE power check, if CYE is high voltage, beep 2 when get a valid card no.
*/

#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 

// ==================================================================
// capture variables


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 = 0x681F;     // low word of SPI configuration word
uint16_t write_4094_hi = 0x0260;      // hi word of SPI configuration word

uint8_t cmd_message[5] = { 0x05,0x00,0x00,0x71,0xFF};
uint16_t fwd_B_timing;                //forward link pulse timing
uint8_t cmd_message_len;              // length of current cmd_message

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 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 TYPE_B_SETTING __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 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 TYPE_B_SETTING_CHK __attribute__((section(".eeprom")))  = 0x01;


uint8_t weigan_data[ 5 ] ;//= { 0x23 , 0x34 , 0x45 , 0x56 ,0x67 };
uint8_t history_weigan_data[ 5 ];
uint8_t watchdog_feed_flag;    
uint8_t data_same_times;   
uint8_t alarm_level;
uint8_t beep_times;
uint8_t type_b_setting; //1--PUPI,2--APP,3--APP+PUPI,4--PUPI+APP;
uint8_t unauth_card_times;
uint8_t alarm_time;
uint8_t alarm_enable;
// ==================================================================
// function declarations
void AVR_Init( void );
void Beep( uint8_t times );
void Beep_Long( void );
uint8_t Check_EEPROM( void );
uint8_t Compare_Weigan_Data( void );
void Delayms( uint16_t count );
void Delayus( uint16_t count );
int main( void );
void Main_Receiver( void );
void Parse_Command( uint8_t cmd , uint8_t param );
void Reset_To_Default( void );
void UARTIni( void );
void WatchDog_Feed( void );
void Send_Card_ID( uint8_t format , uint8_t *data );
void Alarm_Check_And_Process( void );



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

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

void AVR_Init( 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;
    
    watchdog_feed_flag = 0 ;  //feed dog sign,0 for not feed dog , other value for has feed dog;

    while( Check_EEPROM( ) == 0x00 )
        Reset_To_Default( );


//    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;
    	  
	  if( eeprom_rb( &TYPE_B_SETTING ) == eeprom_rb( &TYPE_B_SETTING_CHK ) )   
	  	  type_b_setting = eeprom_rb( &TYPE_B_SETTING );   
	  else
	  	  type_b_setting = 0x01;	 	
	  	    	  	  	  	  	   
    // --------------------------------------------------------------- 
    // General Interrupt Enable                                        
    // --------------------------------------------------------------- 

    MCUCR = ( ( 1 << ISC11 ) | ( 1 << ISC10 ) );          //INT1 triggered on rising edge
    GICR = 0;                                   //INT0 & INT1 disabled
    
    
    Beep( 2 );
    
    Set_GREEN_LED( );
    Delayms( 300 );
    Set_RED_LED( ); 
    
    sei( );                                      // General Interrupt Enable                    
}

// ********************************************************************************************
// 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 - 25 for 38k4Bd 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;
//    uart_valid_data_num = 0;
}




// ==================================================================
// ==================================================================
// ==================================================================
int main(void)
{
    watchdog_reset = bit_is_set( MCUCSR , WDRF );     //capture watchdog resets

    AVR_Init( );			                               // initialization

    Main_Receiver( );

    return( 0 );
}
//--------------------------------------------------------------
// main loop routine
//--------------------------------------------------------------

void Main_Receiver( void ) 
{
	
//    uint8_t i;

    bufferClean = 1;                     //on startup, make buffer not clean

    forward_link_type = 0x01;            //default value, 1=type B, 2=type A
    fwd_B_timing = -111;

    counter1set = ( 1 << ICNC1 ) | ( 1 << CS10 );

    WriteSPI( write_4094_low , write_4094_hi );
    Wait( 4000 );

    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( );   
 
        if ( Read_TypeB_Card( ) )
        {        
            Send_Card_ID( weigan_format , weigan_data );

	          Set_GREEN_LED( );
            Delayms( 150 );    //200
            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 WatchDog_Feed( void )                       //Feed watchdog of the MAX705;
{
	  if ( watchdog_feed_flag == 0 )

⌨️ 快捷键说明

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