📄 main.c
字号:
sbi( TIMSK , TOIE2 ); //enable timer2 overflow for both
if ( style != 5 )
{
if ( ( decode_number & 1 ) == 0 )
{
sbi( TIMSK , TICIE1 ); //enable timer1 capture for ASK only
}
TCCR0 = ( 1 << CS02 ) | ( 1 << CS01 ); //external clock source, falling edge,T0 PIN input
TCCR1B = ( 1 << ICNC1 ) | ( 1 << CS10 ); //noise canceler, clk_io clock, falling edge
TCCR2 = ( 1 << CS21 ) | ( 1 << CS20 ); //run! at clk/32
}
else
{
TCCR1B = ( 1 << ICNC1 ) | ( 1 << ICES1 ) | ( 1 << CS11 ); //noise canceler, clk/8 clock, start with rising edge
TCCR2 = ( 1 << CS22 ); //run! at clk/64
}
if ( style == 5 )
{
// EM4006_polling( );
}
else if( ( decode_number & 1 ) == 0 )
{
while ( TIMSK & ( 1 << TOIE2 ) ) //wait until done for ASK
{}
}
else
{
// dual_subcarrier_polling(); //do polling until done for FSK
}
TCCR0 = 0; //stop all
TCCR1B = 0;
TCCR2 = 0;
TIMSK = TIMSK & ( 0xFF ^ ( ( 1 << TICIE1 ) | ( 1 << TOIE2 ) ) ); //disable timer1 capture, timer2 overflow
TIFR = TIFR | ( 1 << ICF1 ) | ( 1 << TOV1 ) | ( 1 << OCF2 ) | ( 1 << TOV2 ); //clear pending interrupts
last_cnt = TCNT0;
if ( debug_mode != 1 )
{
while ( captured_bit_count != 0 ) //flush captured bits
store_bit( 0 , 1 );
}
else
{
}
if ( ( style != 5 ) && ( decode_number & 1 ) == 0 ) //ASK EOF resolution
{
if ( ( sof == 1 ) && ( last_cnt >= ( 3 * pulsesPerBit - TOLERANCE ) ) && ( last_cnt <= ( 3 * pulsesPerBit + TOLERANCE ) ) ) //kludge for very long delays
sof = 2;
}
}
// ==================================================================
// Wait
void Wait( uint16_t period )
{
period = -period;
TCCR2 = 0; //disable Counter2
TCNT2 = period; //set timer with initial time
TIFR = TIFR | ( 1 << OCF2 ) | ( 1 << TOV2 ); //clear pending interrupts
currentMaxTimeHi = period >> 8;
sbi( TIMSK , TOIE2 ); //enable overflow
TCCR2 = ( 1 << CS21 ) | ( 1 << CS20 ); //run! at clk/32
while ( TIMSK & ( 1 << TOIE2 ) ) //wait until done
{}
TCCR2 = 0; //stop
}
// ==================================================================
// ==================================================================
// INTERRUPT ROUTINES
// ==================================================================
SIGNAL ( SIG_OVERFLOW2 )
{
if ( currentMaxTimeHi++ == 0xFF ) //hi datarate
{
TIMSK = 0; //finished, disable all
}
}
// ==================================================================
// ==================================================================
// EM4006 capture routine
//void EM4006_polling( void )
//{ }
// ==================================================================
// ==================================================================
// ==================================================================
int main( void )
{
// SetLEDOn();
// watchdog_reset = bit_is_set( MCUCSR , WDRF ); //capture watchdog resets
avr_ini( ); // initialization
main_receiver( );
return( 0 );
}
void Delayms( uint16_t count )
{
while( count-- > 0 )
{
Delayus( 1000 );
}
wdt_reset( );
#ifdef WDT
WatchDog_Feed( );
#endif
}
void Delayus( uint16_t count )
{
while( count-- > 0 )
{
asm ("nop");asm ("nop");asm ("nop");asm ("nop");
asm ("nop");asm ("nop");asm ("nop");asm ("nop");
asm ("nop");asm ("nop");asm ("nop");asm ("nop");
asm ("nop");asm ("nop");asm ("nop");asm ("nop");
}
}
void Beep_Long( void )
{
cbi( PORTD , BEEP_PIN );
Delayms( 90 );
sbi( PORTD , BEEP_PIN );
}
void Beep( uint8_t times )
{
if ( times == 0x01 )
Beep_Long( );
else
{
while( times-- > 1 )
{
cbi( PORTD , BEEP_PIN );
Delayms( 50 );
sbi( PORTD , BEEP_PIN );
Delayms( 30 );
}
cbi( PORTD , BEEP_PIN );
Delayms( 50 );
sbi( PORTD , BEEP_PIN );
}
}
void WatchDog_Feed( void ) //Feed watchdog of the MAX705;
{
if ( watchdog_feed_flag == 0 )
{
watchdog_feed_flag = 0x55;
sbi( PORTC , WATCHDOG );
}
else
{
watchdog_feed_flag = 0;
cbi( PORTC , WATCHDOG );
}
}
void Parse_Command( uint8_t cmd , uint8_t param )
{
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 );
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;
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &AUTH_CODE , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &AUTH_CODE_CHK , param );
break;
case 0x05 : //Modify EEPROM check enable;
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &EEPROM_CHECK , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &EEPROM_CHECK_CHK , param );
if ( param )
{
if ( Check_EEPROM( ) )
{
SendByte( 'O' );
SendByte( 'K' );
}
else
{
SendByte( 'E' );
}
}
break;
case 0x06 : //Recovery default value, 0 --not , 1 -- reset to default value;
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &RESET_DEFAULT , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &RESET_DEFAULT_CHK , param );
if ( param )
Reset_To_Default( );
break;
case 0x07 : //Modify keybord enable, ( 0 -- disable , 1 -- enable );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_LEVEL , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_LEVEL , param );
alarm_level = param ;
break;
case 0x08 :
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_TIME , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_TIME_CHK , param );
alarm_time = param;
break;
case 0x09 :
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_ENABLE , param );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_ENABLE_CHK , param );
alarm_enable = param;
break;
default :
break;
}
}
uint8_t Check_EEPROM( void )
{
if( eeprom_rb( &ID ) != eeprom_rb( &ID_CHK ) )
return 0;
if( eeprom_rb( &SERIAL_NUMBER ) != eeprom_rb( &SERIAL_NUMBER_CHK ) )
return 0;
if( eeprom_rb( &WEIGAN_FORMAT ) != eeprom_rb( &WEIGAN_FORMAT_CHK ) )
return 0;
if( eeprom_rb( &AUTH_CODE ) != eeprom_rb( &AUTH_CODE_CHK ) )
return 0;
if( eeprom_rb( &EEPROM_CHECK ) != eeprom_rb( &EEPROM_CHECK_CHK ) )
return 0;
if( eeprom_rb( &RESET_DEFAULT ) != eeprom_rb( &RESET_DEFAULT_CHK ) )
return 0;
if( eeprom_rb( &ALARM_LEVEL ) != eeprom_rb( &ALARM_LEVEL_CHK ) )
return 0;
if( eeprom_rb( &ALARM_TIME ) != eeprom_rb( &ALARM_TIME_CHK ) )
return 0;
if( eeprom_rb( &ALARM_ENABLE ) != eeprom_rb( &ALARM_ENABLE_CHK ) )
return 0;
return 1;
}
void Reset_To_Default( void )
{
wdt_reset( );
#ifdef WDT
WatchDog_Feed( );
#endif
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ID , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ID_CHK , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &SERIAL_NUMBER , 0xFF );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &SERIAL_NUMBER_CHK , 0xFF );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &WEIGAN_FORMAT , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &WEIGAN_FORMAT_CHK , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &AUTH_CODE , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &AUTH_CODE_CHK , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &EEPROM_CHECK , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &EEPROM_CHECK_CHK , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &RESET_DEFAULT , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &RESET_DEFAULT_CHK , 0x00 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_LEVEL , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_LEVEL_CHK , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_TIME , 0x05 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_TIME_CHK , 0x05 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_ENABLE , 0x01 );
while ( !eeprom_is_ready( ) ){};
eeprom_wb( &ALARM_ENABLE_CHK , 0x01 );
wdt_reset( );
#ifdef WDT
WatchDog_Feed( );
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -