📄 main.c
字号:
#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"
//#define DEBUG 1
// ==================================================================
// capture variables
uint16_t halfDataRate; //half period downlink data rate modul
uint8_t pulsesPerBit; //8 pulses for low data rate, 32 for high
uint8_t volatile sof; //sof and eof indication
uint8_t forward_link_type; //forward link modulation type
uint8_t decode_number; //decoder routine number
void (*decode)(void); //decoder routine variable
uint16_t last_capture; //counter1 is now reset at capture start, we make a difference
//between current and last value
register uint8_t currentMaxTimeHi asm ("r10"); //actual hi value of maximum capture time
uint16_t EM4006_bitRate; //bit rate of EM4006 tag
uint8_t EM4006_scale; //scale of EM4006 bit rate
uint8_t watchdog_reset; //watchdog activated
uint8_t weigan_format;
//uint8_t weigan_enable;
//uint8_t keyboard_enable; //keyboard enable, 1--keyboard scan, 0 -- not keyboard scan;
uint8_t beep_times;
uint8_t alarm_level;
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 KEYBORD_EN __attribute__((section(".eeprom"))) = 0x00;
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 KEYBORD_EN_CHK __attribute__((section(".eeprom"))) = 0x00;
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 ] ;//= { 0x23 , 0x34 , 0x45 , 0x56 ,0x67 };
uint8_t watchdog_feed_flag;
uint8_t unauth_card_times;
uint8_t alarm_time;
uint8_t alarm_enable;
// ==================================================================
// level 1 local variables
register uint8_t captured_byte asm ("r6"); //store_bit current capture bits
register uint8_t captured_valid asm ("r7"); //store_bit current capture valid bits
register uint8_t capture_cnt asm ("r8"); //store_bit current capture byte index
register uint8_t captured_bit_count asm ("r9"); //store_bit current capture bit counter
uint8_t bit_pos; //bit position in demodulation
uint8_t edge; //bit_phase FSK
uint8_t last_valid; //last valid value for invalid sequence compression
uint8_t last_z; //FSK last pulse number
uint16_t old_capture; //one subcarrier variable
uint16_t previous_delay; //noise delay storage variable
uint8_t pulsesPerBit2;
uint8_t pulsesPerBit3;
#define TOLERANCE (3)
// ==================================================================
// function declarations
void avr_ini( void );
void Beep( uint8_t times );
void Beep_Long( void );
void Capture( uint8_t style );
uint8_t Check_EEPROM( void );
void Delayms( uint16_t count );
void Delayus( uint16_t count );
int main( void );
void Reset_To_Default( void );
void Parse_Command( uint8_t cmd , uint8_t param );
void store_bit( uint8_t b , uint8_t v );
void Wait( uint16_t period );
void WatchDog_Feed( void );
// ==================================================================
void avr_ini( void )
{
// ---------------------------------------------------------------
// UART initialization
// ---------------------------------------------------------------
UARTIni( ); // level4 uart init routine
// ---------------------------------------------------------------
// 16-bit Timer/Counter1 initialization
// ---------------------------------------------------------------
TCCR1A = 0; // no compare otuput, no PWM mode ...
TCCR1B = 0; // stopped on startup
// ---------------------------------------------------------------
// I/O Ports initialization
// ---------------------------------------------------------------
/*
PORTB = 0;
PORTC = (1 << DCLK_PIN) | (0 << MOD_PIN); //set SPI active == EM4094 normal function blocked
PORTD = 0; // notmodulation == startup antenna on
// LED On
DDRB = 0x02; // PORTB.0,2-7 as input, PORTB.1 = SDA = output
DDRC = 0x0F; // PORTC.(3-0) = (SCK, LED, DCLK, DIN=MOD) = output, rest = input
#ifdef DEBUG
DDRD = 0x44; // PORTD.(7,6,2) = (SSN, DBG_TRANS, DBG_FORWARD) = output, rest = input, SSN=off
#else
DDRD = 0x00; // PORTD.(7,6,2) = (SSN, DBG_TRANS, DBG_FORWARD) = output, rest = input, SSN=off
#endif
*/
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 );
while( Check_EEPROM( ) == 0x00 )
Reset_To_Default( );
// ---------------------------------------------------------------
// General Interrupt Enable
// ---------------------------------------------------------------
MCUCR = ( ( 1 << ISC11 ) | ( 1 << ISC10 ) ); //INT1 triggered on rising edge
GICR = 0; //INT0 & INT1 disabled
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( );
sei( ); // General Interrupt Enable
}
// ==================================================================
// R E C E I V E R C O D E
// ==================================================================
// ---------------------------------------------------------------
// store bit into capture buffer bytes
void store_bit( uint8_t b , uint8_t v )
{
captured_byte = ( captured_byte << 1 ) | b;
captured_valid = ( captured_valid << 1 ) | v;
if ( captured_bit_count == 7 )
{
captured_bit_count = 0;
if ( capture_cnt < CAPTURE_SIZE )
{
capture.capture_data[ capture_cnt ] = captured_byte;
capture.capture_valid[ capture_cnt++ ] = captured_valid;
}
else
{
TIMSK = 0; //finished, disable all
}
}
else
{
captured_bit_count++;
}
}
// ==================================================================
// void one_subcarrier_capture(void)
SIGNAL ( SIG_INPUT_CAPTURE1 )
{
uint8_t z;
uint16_t capt , y;
uint16_t icr;
icr = ICR1;
capt = icr - last_capture;
if ( capt > halfDataRate ) //quite good pause? //halfDataRate = 288 or 4* 288;
{
z = TCNT0;
TCNT0 = 0;
y = icr - old_capture; //y
if ( debug_mode != 1 ) //data capture
{
capt += previous_delay;
previous_delay = 0;
if ( z < ( uint8_t )( pulsesPerBit - TOLERANCE ) ) //kludge because of noise
{
previous_delay = capt;
store_bit( 0, 1 );
store_bit( 1 , 1 );
} //pulsePerBit = 8 or 32;
else if ( ( sof == 0 ) && ( z >= ( uint8_t )( pulsesPerBit3 - TOLERANCE ) ) && ( z <= ( uint8_t )( pulsesPerBit3 + TOLERANCE ) ) )
sof = 1; //SOF;
else if ( sof == 1 )
{
if ( ( z >= ( uint8_t )( pulsesPerBit2 - TOLERANCE ) ) && ( z <= ( uint8_t )( pulsesPerBit2 + TOLERANCE ) ) )
{
store_bit( 1 , 0 );
store_bit( 0 , 0 );
if ( capt < ( 2 * halfDataRate ) )
bit_pos = 0;
else
bit_pos = 1;
}
else if ( ( z >= ( uint8_t )( pulsesPerBit - TOLERANCE ) ) && ( z <= ( uint8_t )( pulsesPerBit + TOLERANCE ) ) )
{
store_bit( bit_pos , 0 ); //0!
if ( capt >= ( 2 * halfDataRate ) )
bit_pos = 1;
}
else if ( ( z >= ( uint8_t )( pulsesPerBit3 - TOLERANCE ) ) && ( z <= ( uint8_t )( pulsesPerBit2 + TOLERANCE ) ) && ( capt > ( 3 * halfDataRate ) ) )
{
sof = 2;
}
else
{
if ( ( bit_pos == 1 ) && ( z >= ( uint8_t )( pulsesPerBit2 + TOLERANCE ) ) )
store_bit( 1 , 0 );
store_bit( 0 , 1 );
}
}
else
{
if ( ( bit_pos == 1 ) && ( z >= ( uint8_t )( pulsesPerBit2 + TOLERANCE ) ) )
store_bit(1, 0);
store_bit(0, 1);
}
}
else
{ //raw capture , used to debug;
capt >>= 5;
// store_pulse(capt); //x
// store_pulse(z);
}
old_capture = icr;
}
last_capture = icr;
}
// ==================================================================
//void dual_subcarrier_polling( void ) {
//}
// ==================================================================
// Down Link setup function
// Requires: maxCaptureTimeLow and maxCaptureTimeHi
// (located here instead of level2 because of register variables)
void Capture( uint8_t style )
{
uint8_t last_cnt;
TCCR0 = 0; //disable Counter0
TCCR1B = 0; //disable Counter1
pulsesPerBit2 = 2 * pulsesPerBit;
pulsesPerBit3 = 3 * pulsesPerBit;
if ( !bufferClean )
ClearCaptureBuffers( );
bufferClean = 0;
previous_delay = 0;
captured_bit_count = 0; //reset some values
capture_cnt = 0;
bit_pos = 1;
sof = 0;
last_valid = 0;
last_capture = TCNT1;
old_capture = TCNT1;
currentMaxTimeHi = ~maxCaptureTimeHi; //set hi byte of maximum capture timer2 timeout
TCNT2 = ~maxCaptureTimeLow; //set low byte timer2 timeout
TCNT0 = 0; //clear timer0
TIFR = TIFR | ( 1 << ICF1 ) | ( 1 << TOV1 ) | ( 1 << OCF2 ) | ( 1 << TOV2 ); //clear pending interrupts
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -