📄 timecardrecord.c
字号:
if( uc_cycle_r++ >= CYCLE_TRY ) goto error;
if( TWI_SLA_R() == 0x50 )
{
uc_pc_buff[uc_index++] = TWDR;
}
else
{
goto re_read;
}
}
trans_msg(_READ_EEPROM, uc_data_len, uc_pc_buff);
TWI_Stop();
return;
}
else
{
goto begin;
}
error:
TWI_Stop();
trans_err_msg(_READ_EEPROM);
}
void write_eeprom(volatile unsigned char *uc_data)
{
unsigned int ui_data_addr;
unsigned char uc_device_addr;
unsigned char uc_data_len;
unsigned char uc_cycle;
ui_data_addr = (*(uc_data+1))*256 + (*(uc_data+2));
uc_device_addr = *uc_data;
uc_data_len = *(uc_data+3);
if( (uc_device_addr != 0xA0) && (uc_device_addr != 0xA4) && (uc_device_addr != 0xA6) ) goto error;
if( uc_data_len > 128 ) goto error;
if( ((ui_data_addr%128) + uc_data_len) > 128 ) goto error;
uc_cycle=0;
begin:
if( uc_cycle++ >= CYCLE_TRY ) goto error;
if( I2C_eeprom_write(uc_device_addr, ui_data_addr, uc_data_len, (uc_data+4)) == RW_SUC )
{
trans_suc_msg(_WRITE_EEPROM);
return;
}
else
{
goto begin;
}
error:
trans_err_msg(_WRITE_EEPROM);
}
SIGNAL(SIG_UART0_RECV)
{
unsigned char uc_temp = UDR0;
if( ((uc_pc_counter==0) && (uc_temp==_COMMAND_HEAD)) || (uc_pc_buff[0]==_COMMAND_HEAD) )
{
uc_pc_buff[uc_pc_counter] = uc_temp;
if( uc_pc_buff[uc_pc_counter] == _COMMAND_TAIL )
{
switch( uc_pc_buff[1] )
{
case _SET_EEPROM_AS: // PC to MCU :初始化设置所有员工ID的进出权限
if( uc_pc_counter != 4 )
{
trans_err_msg(_SET_EEPROM_AS);
break;
}
command_start();
set_eeprom_as( uc_pc_buff+2 );
command_finish();
break;
case _SET_TIME: // PC to MCU :设置系统时间
if( uc_pc_counter != 9 )
{
trans_err_msg(_SET_TIME);
break;
}
command_start();
set_system_time(7, uc_pc_buff+2);
command_finish();
break;
case _READ_TIME: // PC to MCU :设置系统时间
if( uc_pc_counter != 2 )
{
trans_err_msg(_READ_TIME);
break;
}
command_start();
read_system_time(_READ_TIME);
command_finish();
break;
case _SET_SINGLE: // PC to MCU :设置单个成员的进出权限
if( uc_pc_counter != 8 )
{
trans_err_msg(_SET_SINGLE);
break;
}
command_start();
set_single(uc_pc_buff+2);
command_finish();
break;
case _READ_SINGLE: // PC to MCU :读取单个成员的进出权限
if( uc_pc_counter != 7 )
{
trans_err_msg(_READ_SINGLE);
break;
}
command_start();
read_single_from_pc_or_bc(uc_pc_buff+2, READ_FROM_PC);
command_finish();
break;
case _READ_EEPROM:
if( uc_pc_counter != 6 )
{
trans_err_msg(_READ_EEPROM);
break;
}
command_start();
read_eeprom(uc_pc_buff+2);
command_finish();
break;
case _WRITE_EEPROM:
if( uc_pc_counter != (uc_pc_buff[5] + 6) )
{
trans_err_msg(_WRITE_EEPROM);
break;
}
command_start();
write_eeprom(uc_pc_buff+2);
command_finish();
break;
case _BC_INTERRUPT:
cbi(uc_SF, SF_BE_PC_RET);
uc_be_pc_ack = 0;
cbi(PORTC,5);
break;
default:
trans_err_msg(uc_pc_buff[1]);
break;
}
uc_pc_counter = 0;
uc_pc_valid_time = 0;
set_buff_as(0, 192, uc_pc_buff);
}
else
{
if( ++uc_pc_counter >= 192 )
{
uc_pc_counter = 0;
uc_pc_valid_time = 0;
set_buff_as(0, 192, uc_pc_buff);
}
}
}
else
{
uc_file_buff[uc_file_pos] = uc_temp;
if( uc_file_buff[uc_file_pos] == '\n' )
{
if( uc_file_pos > 1 )
{
if( uc_file_buff[uc_file_pos-1] == '\r' )
{
if( (uc_file_buff[0] >= '0') && (uc_file_buff[0] <= '9') && (uc_file_pos <= 5) )
{
if( uc_file_pos < 5)
{
for(uc_temp=0; uc_temp<=uc_file_pos; uc_temp++)
{
uc_file_buff[5-uc_temp] = uc_file_buff[4-uc_temp];
}
for(uc_temp=0; uc_temp<(5-uc_file_pos); uc_temp++)
{
uc_file_buff[uc_temp] = 0x30;
}
}
format_number_id(uc_file_buff, READ_FROM_FILE);
}
else if( (uc_file_buff[0] >= 'A') && (uc_file_buff[0] <= 'Z') && (uc_file_pos == 6) )
{
uc_file_buff[5] = 0x01;
set_single(uc_file_buff);
}
}
}
uc_file_pos=0;
set_buff_as(0,64,uc_file_buff);
}
else
{
if( ++uc_file_pos >= 64 )
{
uc_file_pos=0;
set_buff_as(0,64,uc_file_buff);
}
}
}
}
SIGNAL(SIG_UART1_RECV)
{
uc_bc_buff[uc_bc_counter] = UDR1;
if( uc_bc_buff[uc_bc_counter] == 0x0D )
{
if( (uc_bc_buff[0] >= '0') && (uc_bc_buff[0] <= '9') && (uc_bc_counter <= 4) )
{
unsigned char uc_temp;
if( uc_bc_counter < 4)
{
for(uc_temp=0; uc_temp<=uc_bc_counter; uc_temp++)
{
uc_bc_buff[4-uc_temp] = uc_bc_buff[3-uc_temp];
}
for(uc_temp=0; uc_temp<(4-uc_bc_counter); uc_temp++)
{
uc_bc_buff[uc_temp] = 0x30;
}
}
format_number_id(uc_bc_buff, READ_FROM_BC);
}
else if( (uc_bc_buff[0] >= 'A') && (uc_bc_buff[0] <= 'Z') && (uc_bc_counter == 5) )
{
read_single_from_pc_or_bc(uc_bc_buff, READ_FROM_BC);
}
else
{
sbi(PORTC, 6); // 无效BC显示
}
uc_bc_counter = 0;
uc_bc_valid_time = 0;
set_buff_as(0, 64, uc_bc_buff);
}
else
{
if( ++uc_bc_counter >= 64 )
{
uc_bc_counter = 0;
uc_bc_valid_time = 0;
set_buff_as(0, 64, uc_bc_buff);
}
}
}
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
eoi(PORTC, 0);
if( uc_pc_counter )
{
if( uc_pc_valid_time >= CMD_VALID_TIME)
{
uc_pc_counter = 0;
set_buff_as(0, 192, uc_pc_buff);
}
++uc_pc_valid_time;
}
if( uc_bc_counter )
{
if( uc_bc_valid_time >= CMD_VALID_TIME)
{
uc_bc_counter = 0;
set_buff_as(0, 64, uc_bc_buff);
}
++uc_bc_valid_time;
}
gate_closed();
cbi(PORTC, 6); // 无效BC显示
}
void Init_Digital_IO(void)
{
DDRC = 0xFF; // Digital Output
gate_closed();
sbi(PORTE, PIN5); // Digital Input 0: INT5
sbi(PORTE, PIN6); // Digital Input 1: INT6
sbi(PORTE, PIN7); // Digital Input 2: INT7
sbi(PORTF, PIN3); // Digital Input 3
sbi(PORTF, PIN4); // Digital Input 4
sbi(PORTF, PIN5); // Digital Input 5
sbi(PORTF, PIN6); // Digital Input 6
sbi(PORTF, PIN7); // Digital Input 7
}
void Init_Variable(void)
{
uc_bc_valid_time = 0;
uc_pc_valid_time = 0;
uc_bc_counter = 0;
uc_pc_counter = 0;
uc_file_pos = 0;
uc_be_pc_ack = 0;
ul_list_counter = eeprom_read_word((unsigned int *)0x0000)*65536;
ul_list_counter += eeprom_read_word((unsigned int *)0x0004);
if(ul_list_counter == 0xFFFFFFFF) ul_list_counter = 0;
}
void save_record(unsigned char uc_len, volatile unsigned char *uc_data)
{
unsigned char uc_cycle;
unsigned int ui_addr;
unsigned char uc_device_addr = 0xA4;
if( (ul_list_counter > 0x0000FFFF) && (ul_list_counter <= 0x0001FFEF) )
{
uc_device_addr = 0xA6;
ui_addr = (unsigned int)(ul_list_counter-0x00010000);
}
else if( ul_list_counter <= 0x0000FFFF )
{
uc_device_addr = 0xA4;
ui_addr = (unsigned int)ul_list_counter;
}
else if( ul_list_counter > 0x0001FFEF )
{
ul_list_counter = 0;
uc_device_addr = 0xA4;
ui_addr = 0;
}
uc_cycle=0;
start:
if( uc_cycle++ > CYCLE_TRY ) return;
if( I2C_eeprom_write(uc_device_addr, ul_list_counter, uc_len, uc_data) == RW_SUC )
{
eeprom_write_word((unsigned int *)0x0000, (ul_list_counter/65536));
eeprom_write_word((unsigned int *)0x0004, (ul_list_counter%65536));
ul_list_counter += 16;
}
else
{
goto start;
}
}
int main(void)
{
Init_Variable();
Init_Digital_IO();
Init_BarcodeUsart();
Init_PCUsart();
Init_Timer1();
asm("sei");
while(1)
{
if( bit_is_set(uc_SF, SF_BE_PC_RET) )
{
sbi(PORTC,5);
if( uc_be_pc_ack++ > PC_ACK_OVERTIME )
{
cbi(uc_SF, SF_BE_PC_RET);
uc_be_pc_ack = 0;
cbi(PORTC,5);
save_record(12, uc_id_buff);
}
_delay_ms(1);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -