#include <16F688.h> #device *=16 #device adc=8 #fuses NOWDT,INTRC_IO, NOPROTECT, NOBROWNOUT, MCLR, NOCPD, PUT, NOIESO, FCMEN #use delay(clock=8000000) #use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8) #use fast_io(A) #use fast_io(C) //Registers and bits used for serial comms #bit CREN = 0x17.4 // RCSTA Habilitación de recepción continua 1 ==> Habilitado #bit OERR = 0x17.1 // RCSTA 1 ==> Error de sobreflujo #byte RCREG = 0x14 #bit RCIF = 0x0C.5 //PIR1 //Inputs and outputs definition #define InRD PIN_C0 #define InWR PIN_C1 #define OutRdy PIN_A0 #define OutOK PIN_A1 #define OutRst PIN_A2 //Station Indentifier definition modify to acomadate your station #define StID_Espected 0x03 #define StID_ToWrite 0x45 //Subrrutine declaration, implementation is at the bottom void Configuration(void); unsigned int ReadByte(void); void WriteByte(unsigned int); void ResetCommand(void); void ReadCommand(void); void WriteCommand(void); /*--------------------------------------------------------------------------------------------------------------- MAIN PROGRAM STARTS ----------------------------------------------------------------------------------------------------------------*/ void main() { Configuration(); // Start main cycle while(TRUE) { //if both inputs RD and WR are 1, then reset the HMS830 RFID system if(input(InWR) && input(InRD)) { ResetCommand(); } //Trigger the Read Command if(input(InRD) && !input(InWR)) { delay_ms(100); if(input(InRD) && !input(InWR)) { ReadCommand(); } } //Trigger Write Commad if(input(InWR) && !input(InRD)) { delay_ms(100); if(input(InWR) && !input(InRD)) { WriteCommand(); } } }//End Main Cycle }//End Main Program /*--------------------------------------------------------------------------------------------------------------------- SUBRUTINES IMPLEMENTATION ----------------------------------------------------------------------------------------------------------------------*/ void Configuration(void) { setup_adc_ports(NO_ANALOGS|VSS_VDD); setup_adc(ADC_OFF); setup_timer_0(RTCC_INTERNAL); setup_wdt(WDT_DIV_65536); setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); set_tris_A(0b11111000); set_tris_C(0b11100111); output_high(OutRdy); delay_ms(1000); output_low(OutRdy); delay_ms(1000); output_high(OutOK); delay_ms(1000); output_low(OutOK); delay_ms(1000); output_low(OutRst); } //Configuration subrrutine void ResetCommand(void) { delay_ms(100); if(input(InWR) && input(InRD)) { output_high(OutRst); delay_ms(500); output_low(OutRst); } while(input(InWR) && input(InRD)); } //Subrutine to write a single byte to serial port void WriteByte(unsigned int c) { putc(c); } //Subrrutine to read a single byte from serial port unsigned int ReadByte(void) { unsigned long ContEsp = 0; char c; while((ContEsp < 65534) && !RCIF) { ContEsp++; delay_cycles(1); } if (OERR) { CREN = 0; delay_cycles(1); CREN = 1; } c = RCREG; return(c); } //Performs a Read Block Command and checks for the correct Station ID void ReadCommand(void) { unsigned int8 Retransmit = 0; int1 StID_IsOK = FALSE; unsigned int8 Data,CommandEcho,TimeCounter; do{ WriteByte(0x02); //Header WriteByte(0x02); //Header WriteByte(0x00); //Command Size WriteByte(0x07); //Command Size WriteByte(0x05); //Command WriteByte(0x00); //Start Address WriteByte(0x03); //Start Address WriteByte(0x00); //Block Size WriteByte(0x01); //Block Size WriteByte(0x01); //Time Out 500ms WriteByte(0xF4); //Time Out 500ms WriteByte(0x03); //Terminator Data = 0; CommandEcho = 0; TimeCounter = 0; set_timer1(0); //Wait for a response from HMS830 do{ if(get_timer1() > 60000) { TimeCounter++; set_timer1(0); } }while((TimeCounter <= 5) && !RCIF); if(RCIF) { ReadByte();//STX ReadByte();//STX ReadByte();//Response Size ReadByte();//Response Size CommandEcho = ReadByte();//Command Echo Data = ReadByte();//Data ReadByte();//Checksum ReadByte();//Terminator if((CommandEcho == 5) && ((Data == StID_Espected) || (Data == StID_ToWrite))) { StID_IsOK = TRUE; } else { Retransmit++; delay_ms(200); } } else { Retransmit++; delay_ms(200); } }while(!StID_IsOK && (Retransmit<3));//End retrasnmit if(StID_IsOK) { output_high(OutOK); delay_ms(20); output_high(OutRdy); } else { output_high(OutRdy); } while(input(InRD) || input(InWR)); output_low(OutOK); output_low(OutRdy); }//EndReadCommand /*----------------------------------------------------------------------------------------------------------*/ //Performs the Station ID writing void WriteCommand(void) { unsigned int8 Retransmit = 0; unsigned int8 Data,CommandEcho,TimeCounter; int1 CmdOK = FALSE; do{ WriteByte(0x02); //Header WriteByte(0x02); //Header WriteByte(0x00); //Command Size WriteByte(0x08); //Command Size WriteByte(0x06); //Command WriteByte(0x00); //Start Address WriteByte(0x03); //Start Address WriteByte(0x00); //Block Size WriteByte(0x01); //Block Size WriteByte(0x01); //Time Out 500ms WriteByte(0xF4); //Time Out 500ms WriteByte(StID_ToWrite); //Data WriteByte(0x03); //Terminator Data = 0; CommandEcho = 0; TimeCounter = 0; set_timer1(0); //Waits for a response from HMS830 do{ if(get_timer1() > 60000) { TimeCounter++; set_timer1(0); } }while((TimeCounter <= 5) && !RCIF); if(RCIF) { ReadByte();//STX ReadByte();//STX ReadByte();//Response Size ReadByte();//Response Size CommandEcho = ReadByte();//Command Echo if(CommandEcho != 6) { Data = ReadByte();//Error ID } ReadByte();//Checksum ReadByte();//Terminator if(CommandEcho == 6) { CmdOK = TRUE; } else { Retransmit++; delay_ms(200); } } else { Retransmit++; delay_ms(200); } }while(!CmdOK && (Retransmit<3));//End retrasnmit if(CmdOK) { output_high(OutOK); delay_ms(20); output_high(OutRdy); } else { output_high(OutRdy); } while(input(InRD) || input(InWR)); output_low(OutOK); output_low(OutRdy); }//EndWriteCommand