?? reader.c
字號:
{
//u_ram.host.RxBuffer[ReceivCNT]=SBUF;
ReceivCNT = 0;
Rec_Status=0;
if (SBUF == ETX)
{
if( CheckSum == 0 || CheckSum == 0x10)
DecodeFlag = 1;
}
}
//if(CheckSum == u_ram.host.RxBuffer[ReceivCNT-1] && SBUF == ETX ) DecodeFlag = 1;
//if(SBUF == ETX ) DecodeFlag = 1;
else
{
u_ram.host.RxBuffer[ReceivCNT]=SBUF;
ReceivCNT++;
if(ReceivCNT >= MaxRxd)Rec_Status=0; //when count out of range re-synchronous received
CheckSum ^=SBUF;
}
break;
default: //out received status
ReceivCNT = 0;
Rec_Status=0; //initial receiver buffer
} //end of switch case Rec_status
//Regetrc500EEPROMTimeOut=C_UART_TimeOut; //set byte time out value. The time count down at timer 0
} //end of protected
} /*-----------end of if(RI) */
else //------------------------------------Below code for transmit
{
TI=0;
if(TxFlag)
{
if(STX_Pointer != ETX_Pointer) //checking the end of transmit buffer
{
SBUF=u_ram.host.TxBuffer[STX_Pointer]; //output data to communication interface
STX_Pointer++; //pointer to next data
if(STX_Pointer >= MaxTxd)STX_Pointer=0;
}
else
{
STX_Pointer=0;
ETX_Pointer=0;
SBUF=ETX; //Transmit end of transmit code to host
TxFlag=0;
TxFlag1=0;
// TxFlag=0; //clear flag for end of transmit process
}
//if(TxFlag1 == 1 ) TxFlag=0;
} //end of if TxFlag
else
{
RS485 = 0;
TxFlag1=1;//UART_MCR &=0xf7; //Set RS485 to received mode, the control pin is located on the 16c550
REN=1;
}
}//----------------------------------------end of transmit function
}
//---------------------------------------------------------------------------------------------------------
// This function main work used for checking the control code. If parameter value match control code
// before value inserted DLE code in to transmit buffer.
// Send out data must be call this routine escape STX and ETX
//---------------------------------------------------------------------------------------------------------
void CheckControlCode(unsigned char value)
{
while(WaitTxBufferEmpty()); //waitting to transmit buffer empty
u_ram.host.TxBuffer[ETX_Pointer]=value;
ETX_Pointer++;
if(ETX_Pointer >= MaxTxd)
{
while(WaitTxBufferEmpty()); //waitting to transmit buffer empty
ETX_Pointer=0;
}
TxCheckSum ^= value ;
}
//---------------------------------------------------------------------------------------------------------
// This function used for protect transmit buffer
// return value = 0 indicated data can write to transmit buffer
// return value = 1 indicated can't write data to transmit buffer
// This routine call from CheckControlCode() routine
//---------------------------------------------------------------------------------------------------------
bit WaitTxBufferEmpty(void)
{
if(STX_Pointer == 0 && ETX_Pointer == MaxTxd) return 1; //buffer is full
else if((STX_Pointer-1)== ETX_Pointer) return 1; //buffer is full
return 0;
}
void ReturnStatus(unsigned char status)
{
StartTransmit();
CheckControlCode(0x01);
CheckControlCode(status);
CheckControlCode(TxCheckSum);
}
//---------------------------------------------------------------------------------------------------------
// This function used for send STX SQE and device address.
// When reply host command code before send data call this rountine.
//---------------------------------------------------------------------------------------------------------
void StartTransmit(void)
{
RS485 = 1; //UART_MCR |=0x08; //Set RS485 to transmit mode -16c550
delay_1ms(3);
if(u_ram.host.RxBuffer[2] != CMD_Authentication )delay(3);
Rec_Status=0;
REN=0;
TI=0;
TxFlag=1;
ETX_Pointer=0;
STX_Pointer=0;
TxCheckSum=0;
Regetrc500EEPROMTimeOut=C_UART_TimeOut; //set byte time out value. The time count down at timer 0
CheckControlCode(u_ram.host.RxBuffer[0]); //sequence number //seq 數據流水號
CheckControlCode(SlaveADR); // Device address
SBUF=STX; //start transmit //02
}
//---------------------------------------------------------------------------------------------------------
//This routine used for active I/O ports.
//call from the main routine decode command code
//---------------------------------------------------------------------------------------------------------
bit SetPortActive(unsigned char SelectPort)
{
unsigned char SelectPort0=0;
if(SelectPort>0x0F)
return 1; //other value out of range IO.
if(SelectPort==0)
{
Output0Flag=0;
OutputPort0=0; //Set to OFF
Output1Flag=0;
OutputPort1=0; //Set to OFF when port pull low
// Output2Flag=0;
// OutputPort2=0; //Set to OFF when port pull low
//Output3Flag=0;
//OutputPort3=0; //Set to OFF when port pull low
return 0;
}
else
{
SelectPort0=SelectPort&0x01;
if(SelectPort0)
{
Output0Flag=1;
Port.TimeOut[0]=0xFF;//set to ON forever
}
SelectPort0=SelectPort&0x02;
if(SelectPort0!=0)
{
Output1Flag=1;
Port.TimeOut[1]=0xFF; //set to ON forever
}
// SelectPort0=SelectPort&0x04;
// if(SelectPort0!=0)
// {
// Output2Flag=1;
// Port.TimeOut[2]=0xFF; //set to ON forever
// }
/* SelectPort0=SelectPort&0x08;
if(SelectPort0!=0)
{
Output3Flag=1;
Port.TimeOut[3]=0xFF; //set to ON forever
}*/
return 0;
}
}
unsigned char GetPortStatus(unsigned char SelectPort)
{
SelectPort=0;
switch(SelectPort)
{
case 0:
if(!OutputPort0)//get port0 in/output status
{
SelectPort|=0x01;
SelectPort&=0xEF;
}
else
{
SelectPort&=0xFE;
SelectPort|=0x10;
}
if(!OutputPort1)//get port1 in/output status
{
SelectPort|=0x02;
SelectPort&=0xDF;
}
else
{
SelectPort&=0xFD;
SelectPort|=0x20;
}
// if(!OutputPort2)//get port2 in/output status
// {
// SelectPort|=0x04;
// SelectPort&=0xBF;
// }
// else
// {
// SelectPort&=0xFB;
// SelectPort|=0x40;
// }
/*
if(!OutputPort3)//get port3 in/output status
{
SelectPort|=0x08;
SelectPort&=0x7F;
}
else
{
SelectPort&=0xF7;
SelectPort|=0x80;
}*/
break;
default:
SelectPort=0xff;
}
return(SelectPort);
}
//---------------------------------------------------------------------------------------------------------
//This routine used for checking trigger I/O time out. When time out to close I/O ports.
//call from the timer 0 interrupt routine.
//---------------------------------------------------------------------------------------------------------
/*void CheckTimeOutIO(void)
{
if(Output0Flag)//**********for IO port 0.
{
OutputPort0=1; //ON IO port
if(Port.TimeOut[0] < 0xFF)
{
if(Port.TimeOut[0]== 0)
{
Output0Flag=0; //time out to close IO port
OutputPort0=0;
}
Port.TimeOut[0]--;
}
} //***********end of IO port 0
if(Output1Flag)//**********for IO port 1.
{
OutputPort1=1; //ON IO port
if(Port.TimeOut[1] < 0xFF)
{
if(Port.TimeOut[1]== 0)
{
Output1Flag=0; //time out to close IO port
OutputPort1=0;
}
Port.TimeOut[1]--;
}
} //******************end of IO port 1
if(Output2Flag)//********for IO port 2.
{
OutputPort2=1; //ON IO port
if(Port.TimeOut[2] < 0xFF)
{
if(Port.TimeOut[2]== 0)
{
Output2Flag=0; //time out to close IO port
OutputPort2=0;
}
Port.TimeOut[2]--;
}
} //***************end of IO port 2
}
*/
//---------------------------------------------------------------------------------------------------------
// This routine used for toggle LED pattern.
// call from the main routine decode command code
// The parameter :RxBuffer[4] is indicated LED
// :RxBuffer[5] is duty time of LED ON
// :RxBuffer[6] is number of LED ON/OFF cycle
//---------------------------------------------------------------------------------------------------------
bit DecodeToggleLED(unsigned char *buffer)
{
switch(buffer[0])
{
case 0x01://For toggle red LED parameter value
DutyTimeLED_R=buffer[1];
NumPeriodLED_R=buffer[2];
RedLED_Flag=1; //Depend on parameter value auto OFF LED at timer 0 interrupt
break;
case 0x02://For toggle green LED parameter value
DutyTimeLED_G=buffer[1];
NumPeriodLED_G=buffer[2];
GreenLED_Flag=1; //Depend on parameter value auto OFF LED at timer 0 interrupt
break;
case 0x03://For toggle red and green LED parameter value
DutyTimeLED_R=DutyTimeLED_G=buffer[1];
NumPeriodLED_R=NumPeriodLED_G=buffer[2];
RedLED_Flag=1; //Depend on parameter value auto OFF LED at timer 0 interrupt
GreenLED_Flag=1;
break;
default:
return 1; //the data parameter out of range
}
return 0;
}
//---------------------------------------------------------------------------------------------------------
//This routine used for ON LED forever.
//call from the main routine decode command code
//---------------------------------------------------------------------------------------------------------
bit SetLED_Active(unsigned char SelectLED)
{
switch(SelectLED)
{
case 0x00:
LED_R=OFF;
LED_G=OFF;
GreenLED_Flag=0;
RedLED_Flag=0;
break;
case 0x01://For set red LED parameter value to forever ON
LED_R= ON;
LED_G=OFF;
DutyTimeLED_R=60;
NumPeriodLED_R=0xFF;
RedLED_Flag=1;
GreenLED_Flag=0;
break;
case 0x02://For set green LED parameter value to forever ON
LED_G = ON;
LED_R=OFF;
DutyTimeLED_G=60;
NumPeriodLED_G=0xFF;
GreenLED_Flag=1;
RedLED_Flag=0;
break;
case 0x03://For toggle red and green LED parameter value
LED_R = ON;
LED_G = ON;
DutyTimeLED_R=60;
DutyTimeLED_G=60;
NumPeriodLED_R=0xFF;
NumPeriodLED_G=0xFF;
RedLED_Flag=1; //Depend on parameter value auto OFF LED at timer 0 interrupt
GreenLED_Flag=1;
break;
default:
return 1; //the data parameter out of range
}
return 0;
}
bit C_Ext(unsigned char timelen)
{
unsigned char C_extbuff[4];
// if(timelen>255) return 1;
// else
if(timelen==0)
{
// P4=P4&0xF7;
P4=P4|0x08;
}
else if(timelen==255)
{
// P4=P4|0x08;
P4=P4&0xF7;
C_ExtFlag=0;
}
else
{
C_ext_CNT=timelen*225;
// P4=P4|0x08;
P4=P4&0xF7;
C_ExtFlag=1;
}
read_Flash(S_WIEGAND_MODE,C_extbuff,4);
weigandFlag = C_extbuff[3]; // 回到自動讀卡模式
return 0;
}
//=======================================================*/
void delay_50us_NOP(void)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
void delay_50us(unsigned char _50us)
{
while(--_50us)
{
delay_50us_NOP();
delay_50us_NOP();
delay_50us_NOP();
}
}
void delay_1ms(unsigned char _1ms)
{
while(--_1ms)
delay_50us(20);
}
//=======================================================
void delay(unsigned char time)
{
unsigned char i;
while(--time)
for(i=0;i<0x90;i++);
}
void SendNumber(unsigned char length, unsigned char *datas)
{
unsigned char i;
EA=0;
REN=0;
RS485 = 1;
for(i=0;i<length;i++)
{
TI=0;
SBUF=datas[i];
while(TI==0);
}
RS485 = 0;
REN=1;
EA=1;
}
//發送數據
void TxOut(unsigned char length, unsigned char status,unsigned char *datas)
{
unsigned char i;
StartTransmit();
CheckControlCode(length+1); //DATA LENGTH
CheckControlCode(status); //STATUS
for (i=0;i<length;i++)
{
CheckControlCode(*datas);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -