?? main_c.c
字號:
*(pbuf + 9) = 0xF4; *(pbuf + 10) = 0x23; *(pbuf + 11) = 0x00; *(pbuf + 12) = 0x00; *(pbuf + 13) = 0x07; *(pbuf + 14) = 0xE0; *(pbuf + 15) = 0x00; *(pbuf + 16) = 0x01; if(lenght > 12) lenght = 12; if(lenght == 0x00 && brokenBits != 0x00) { lenght = 1; RXTXstate = 1; } command = IRQStatus; RFID_ReadSingle(&command, 1); RFID_RAWwrite(pbuf, lenght + 5); /* send the request using RAW writing */ /* Write 12 bytes the first time you write to FIFO */// irqCLR; /* PORT2 interrupt flag clear */ irqON; RXTXstate = RXTXstate - 12; //RXTXstate = RXTXstate - lenght; index = 17; i_reg = 0x01; command = IRQStatus; RFID_ReadSingle(&command, 1); while(RXTXstate > 0) { //myDelay(20); //LPM0; /* enter low power mode and exit on interrupt */ //sleep_cpu(); if(RXTXstate > 9) { /* the number of unsent bytes is in the RXTXstate global */ lenght = 10; /* count variable has to be 10 : 9 bytes for FIFO and 1 address */ } else if(RXTXstate < 1) { break; /* return from interrupt if all bytes have been sent to FIFO */ } else { lenght = RXTXstate + 1; /* all data has been sent out */ } /* if */ buf[index - 1] = FIFO; /* writes 9 or less bytes to FIFO for transmitting */ RFID_WriteCont(&buf[index - 1], lenght); RXTXstate = RXTXstate - 3;//9 /* write 9 bytes to FIFO */ index = index + 9; } /* while */ RXTXstate = 1; /* the response will be stored in buf[1] upwards */ /* wait for end of transmit */ while(i_reg == 0x01) { myDelay(30);/* CounterSet(); countValue = 0xF000; // 60ms for TIMEOUT startCounter; // start timer up mode LPM0;*/ sleep_cpu(); } i_reg = 0x01; myDelay(30);/* CounterSet(); countValue = count1ms * 60; // 60ms for TIMEOUT startCounter; // start timer up mode */ if ( (((buf[5] & BIT6) == BIT6) && ((buf[6] == 0x21) || (buf[6] == 0x24) || (buf[6] == 0x27) || (buf[6] == 0x29))) || (buf[5] == 0x00 && ((buf[6] & 0xF0) == 0x20 || (buf[6] & 0xF0) == 0x30 || (buf[6] & 0xF0) == 0x40)) ) { delay_ms(20); command = Reset; RFID_DirectCommand(&command); command = TransmitNextSlot; RFID_DirectCommand(&command); } /* if */ while(i_reg == 0x01) { } /* wait for RX complete */ if(!POLLING) { switch(noCRC) { case 0: if(i_reg == 0xFF) { // recieved response // kputchar('['); for(j = 1; j < RXTXstate; j++) {// Put_byte(buf[j]); } // for // kputchar(']'); return(0); } else if(i_reg == 0x02) { // collision occured // kputchar('[');// kputchar('z');// kputchar(']'); return(0); } else if(i_reg == 0x00) { // timer interrupt // kputchar('[');// kputchar(']'); return(1); } else ; break;/* case 1: if(i_reg == 0xFF) { // recieved response kputchar('('); for(j = 1; j < RXTXstate; j++) { Put_byte(buf[j]); } // for kputchar(')'); return(0); } else if(i_reg == 0x02) { // collision occured kputchar('('); kputchar('z'); kputchar(')'); return(0); } else if(i_reg == 0x00) { // timer interrupt kputchar('('); kputchar(')'); return(1); } else ; break;*/ } // switch } // if irqOFF; return(1);} // RequestCommand void InterruptHandlerReader(unsigned char *Register){ /*~~~~~~~~~~~~~~~~*/ /* * char phello[20]; * //for testing */ unsigned char len,c; /*~~~~~~~~~~~~~~~~*/ /* * Register = Register & 0xF7; * //set the parity flag to 0 ; * parity is not used in 15693 and Tag-It */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Register = %x \n",*Register); if(*Register == 0xA0) { // TX active and only 3 bytes left in FIFO i_reg = 0x00;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"FIFO done\n"); } else if(*Register == BIT7) { /* TX complete */ i_reg = 0x00;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"TX completed\n"); *Register = Reset; /* reset the FIFO after TX */ RFID_DirectCommand(Register); } else if((*Register & BIT1) == BIT1) { /* collision error */ i_reg = 0x02; /* RX complete */ *Register = StopDecoders; /* reset the FIFO after TX */ RFID_DirectCommand(Register); CollPoss = CollisionPosition; RFID_ReadSingle(&CollPoss, 1);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Collision error = %x\n",CollPoss); /* * CollPoss &= 0xF7; */ len = CollPoss - 0x20; /* number of valid bytes if FIFO */ if((len & 0x0f) != 0x00) len = len + 0x10; /* add 1 byte if broken byte recieved */ len = len >> 4; if(len != 0x00) { buf[RXTXstate] = FIFO; /* write the recieved bytes to the correct place of the * buffer; * */ RFID_ReadCont(&buf[RXTXstate], len); RXTXstate = RXTXstate + len; } /* if */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"collision error \n"); *Register = Reset; RFID_DirectCommand(Register); *Register = IRQStatus; /* IRQ status register address */ RFID_ReadSingle(Register, 1); /* function call for single address read */ irqCLR; } else if(*Register == BIT6)//0x40 { /* RX flag means that EOF has been recieved */ /* and the number of unread bytes is in FIFOstatus regiter */ if(RXErrorFlag == 0x02) { i_reg = 0x02; return; } *Register = FIFOStatus; RFID_ReadSingle(Register, 1); /* determine the number of bytes left in FIFO */ *Register = (0x0F &*Register) + 0x01; for(c=0;c<=250;c++) buf[c] = 0x00; buf[RXTXstate] = FIFO; /* write the recieved bytes to the correct place of the * buffer; * */ RFID_ReadCont(&buf[RXTXstate], *Register); RXTXstate = RXTXstate +*Register; *Register = TXLenghtByte2; /* determine if there are broken bytes */ RFID_ReadSingle(Register, 1); /* determine the number of bits */ if((*Register & BIT0) == BIT0) { *Register = (*Register >> 1) & 0x07; /* mask the first 5 bits */ *Register = 8 -*Register; buf[RXTXstate - 1] &= 0xFF << *Register; } /* if */ *Register = Reset; /* reset the FIFO after last byte has been read out */ RFID_DirectCommand(Register);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"EOF received \n"); i_reg = 0xFF; /* signal to the recieve funnction that this are the last bytes */ } else if(*Register == 0x60) { /* RX active and 9 bytes allready in FIFO */ i_reg = 0x01; for(c=0;c<=250;c++) buf[c] = 0x00; buf[RXTXstate] = FIFO; RFID_ReadCont(&buf[RXTXstate], 9); /* read 9 bytes from FIFO */ RXTXstate = RXTXstate + 9;// delay_ms(100);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"RX active\n"); if(irqPORT & irqPIN) { *Register = IRQStatus; /* IRQ status register address */ RFID_ReadSingle(Register, 1); /* function call for single address read */ irqCLR; if (*Register == 0x40) { /* end of recieve */ *Register = FIFOStatus; RFID_ReadSingle(Register, 1); /* determine the number of bytes left in FIFO */ *Register = 0x0F & (*Register + 0x01); buf[RXTXstate] = FIFO; /* write the recieved bytes to the correct place of the * buffer; * */ RFID_ReadCont(&buf[RXTXstate], *Register); RXTXstate = RXTXstate +*Register; *Register = TXLenghtByte2; /* determine if there are broken bytes */ RFID_ReadSingle(Register, 1); /* determine the number of bits */ if((*Register & BIT0) == BIT0) { *Register = (*Register >> 1) & 0x07; /* mask the first 5 bits */ *Register = 8 -*Register; buf[RXTXstate - 1] &= 0xFF << *Register; } /* if */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"RX active 2\n"); i_reg = 0xFF; /* signal to the recieve funnction that this are the last bytes */ *Register = Reset; /* reset the FIFO after last byte has been read out */ RFID_DirectCommand(Register);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"register = %x \n",buf); } else if(*Register == 0x50) { // end of recieve and error i_reg = 0x02;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"End of receive error\n"); } } else { *Register = IRQStatus; RFID_ReadSingle(Register, 2); /* function call for single address read */ if(Register[0] == 0x00) i_reg = 0xFF; } } else if((*Register & BIT4) == BIT4) { /* CRC error */ if((*Register & BIT5) == BIT5) { i_reg = 0x01; /* RX active */ RXErrorFlag = 0x02; } else i_reg = 0x02; /* end of RX */ } else if((*Register & BIT2) == BIT2) { /* byte framing error */ if((*Register & BIT5) == BIT5) { i_reg = 0x01; /* RX active */ RXErrorFlag = 0x02; } else i_reg = 0x02; /* end of RX */ } else if(*Register == BIT0) { /* No response interrupt */ i_reg = 0x00;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"No Response interrupt\n"); } else { /* Interrupt register not properly set */ i_reg = 0x02; *Register = StopDecoders; /* reset the FIFO after TX */ RFID_DirectCommand(Register); *Register = Reset; RFID_DirectCommand(Register); *Register = IRQStatus; /* IRQ status register address */ RFID_ReadSingle(Register, 1); /* function call for single address read */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Interrupt error\n"); // irqCLR; }} /* InterruptHandlerReader */void delay_ms(unsigned int n_ms) { unsigned int ii1, ii0; for(ii0=n_ms; ii0>0; ii0--) { ii1 = 0x0033; // Delay do (ii1--); while (ii1 != 0); }}// end of delay_ms ISR(TIMER2_COMPA_vect){ unsigned char Register; StopCounter; Timer_Flag = 1; Register = IRQStatus; //IRQ status register address irqCLR; //PORT2 interrupt flag clear RFID_ReadSingle(&Register, 1); //function call for single address read //IRQ status register has to be read Register = Register & 0xF7; //set the parity flag to 0// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Timer Register = %x\n",Register); if(Register == 0x00) { i_reg = 0x00; } else i_reg = 0x01;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"IRQStatus 1 = %x\n",Register);}ISR(INT0_vect){ unsigned char Register[2]; StopCounter; /* stop timer mode */ do { irqCLR; Register[0] = IRQStatus; /* IRQ status register address */ RFID_ReadSingle(Register, 1); /* function call for single address read */ Last_Register[Last_Cnt] = Register[0]; Last_Cnt++;// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Interrupt Register = %x\n",Register[0]); /* IRQ status register has to be read */ if(*Register == 0xA0) { /* TX active and only 3 bytes left in FIFO */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"Interrupt FINISH\n"); goto FINISH; } else InterruptHandlerReader(&Register[0]); }while((irqPORT & irqPIN) == irqPIN);FINISH:// sleep_cpu(); Nop = 1;}int main(){ unsigned char c=0; unsigned char command[10]; init(); stdout = stdin = &uart_str; fprintf(&uart_str, "\nBefore main loop\n");/* CountValue = Count1ms*50; CounterSet(); StartCounter;*/ while(1) {/* // Timer_Flag = 0; if(Timer_Flag==1) { Timer_Flag = 0; fprintf(&uart_str, "Loop counter = %x \n",cnt);// StartCounter; } */// PORTC = cnt; for(c=0;c<=250;c++) buf[c] = 0x00; command[0] = Reset; /* reset the FIFO after last byte has been read out */ RFID_DirectCommand(command); command[0] = ChipStateControl; command[1] = 0x21; command[2] = ISOControl; command[3] = 0x02; RFID_WriteSingle(command, 4); delay_ms(5); flags = 0x06;//0x26 command[0] = 0x00; RFID_RequestCommand(buf, 0x00, 0x00, 0);// RFID_InventoryRequest(command, 0); command[0] = ChipStateControl; command[1] = 0x01; RFID_WriteSingle(command, 2); delay_ms(1); command[0] = IRQStatus;//IRQStatus RFID_ReadSingle(command, 1); fprintf(&uart_str,"\n"); for(c=0;c<=10;c++) { fprintf(&uart_str,"Last Register = %02x\n",Last_Register[c]); } Last_Cnt = 0; fprintf(&uart_str, "\n"); fprintf(&uart_str, "Buf = "); for(c=0;c<=20;c++) { fprintf(&uart_str, "%02x ",buf[c]); } /* fprintf(&uart_str, "\nAfter TX\n"); for(c=0;c<=0;c++) { delay_ms(100); }*/ /* RFID_Data[0] = TestSetting1; RFID_Data[1] = 0x00; RFID_WriteSingle(RFID_Data,2); RFID_Data[0] = 0x00; RFID_Data[1] = 0x00; RFID_Data[0] = TestSetting1; RFID_ReadSingle(&RFID_Data[0],1);*//* fprintf(&uart_str,"%c",0x0D); fprintf(&uart_str,"buf = %x\n",buf[0]); fprintf(&uart_str,"%c",0x0D); fprintf(&uart_str,"RFID i_reg = %x\n",i_reg);*//* PORTC--; if(PORTC<=1) PORTC = 0xFF;*/// PORTC = d;/* for(c=1;c<=4000;c++) { _delay_ms(1); }*/ }//while return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -