?? main_c.c
字號:
#include <avr/io.h>//#include <util/delay.h>// Standard Input/Output functions#include <stdio.h>#include <avr/interrupt.h>#include <avr/sleep.h>#include "init.h" #define BIT0 0x01#define BIT1 0x02#define BIT2 0x04#define BIT3 0x08#define BIT4 0x10#define BIT5 0x20#define BIT6 0x40#define BIT7 0x80#define LOW 0#define HIGH 1 /*Timer Defines*/#define Count1ms 8#define CountValue OCR2A//#define CountValue OCR1A#define StartCounter TCCR2B |= ((1 << CS20) | (1 << CS22)); // Start timer at Fcpu/64//#define StartCounter TCCR1B |= (1 << CS11)//Start Counter#define StopCounter TCCR2B &= ((0 << CS20) | (0 << CS22)); // Start timer at Fcpu/64//#define StopCounter TCCR1B &= (0<<CS11)//Stop counter //todo/*UART defines*/#define UDRE 5#define RXC 7/*RFID Defines*/#define RFID_Write PORTB#define RFID_Read PINB#define RFID_CLK_High PORTA |= 0x01; //CLK HIGH#define RFID_CLK_Low PORTA &= 0xFE; //CLK LOW#define ChipStateControl 0x00#define ISOControl 0x01#define ISO14443Boptions 0x02#define ISO14443Aoptions 0x03#define TXtimerEPChigh 0x04#define TXtimerEPClow 0x05#define TXPulseLenghtControl 0x06#define RXNoResponseWaitTime 0x07#define RXWaitTime 0x08#define ModulatorControl 0x09#define RXSpecialSettings 0x0A#define RegulatorControl 0x0B#define IRQStatus 0x0C#define IRQMask 0x0D#define CollisionPosition 0x0E#define RSSILevels 0x0F#define RAMStartAddress 0x10 //RAM is 7 bytes long (0x10 - 0x16)#define TestSetting1 0x1A#define TestSetting2 0x1B#define FIFOStatus 0x1C#define TXLenghtByte1 0x1D#define TXLenghtByte2 0x1E#define FIFO 0x1F//Reader commands-------------------------------------------#define Idle 0x00#define SoftInit 0x03#define InitialRFCollision 0x04#define ResponseRFCollisionN 0x05#define ResponseRFCollision0 0x06#define Reset 0x0F#define TransmitNoCRC 0x10#define TransmitCRC 0x11#define DelayTransmitNoCRC 0x12#define DelayTransmitCRC 0x13#define TransmitNextSlot 0x14#define CloseSlotSequence 0x15#define StopDecoders 0x16#define RunDecoders 0x17#define ChectInternalRF 0x18#define CheckExternalRF 0x19#define AdjustGain 0x1A//==========================================================/* bits common to all USARTs */#define DATA_REGISTER_EMPTY (1<<UDRE0)//#define DATA_REGISTER_EMPTY UDRE0#define RX_COMPLETE (1<<RXC0)//#define RX_COMPLETE RXC0#define irqCLR EIFR |= (1<<INTF0)#define irqON EIMSK |= (1<<INT0) #define irqOFF EIMSK &= (0<<INT0)#define irqPORT PIND#define irqPIN 0x04/*#define irqCLR PCIFR |= (1<<PCIF0)#define irqON PCICR |= (1<<PCIE0)#define irqOFF PCICR &= (0<<PCIE0)#define irqPORT PINA#define irqPIN 0x02*/unsigned char flags;unsigned char buf[300];unsigned char RXTXstate;unsigned char i_reg;unsigned char CollPoss,d=0;unsigned char RXErrorFlag;unsigned char POLLING=0x00;unsigned char temp_flag = 0x00;unsigned char cnt = 0,PCINT0_Flag = 0,PORTA_Value= 0x00,Nop;volatile char Timer_Flag ;volatile unsigned char Last_Register[10],Last_Cnt = 0x00;unsigned char RequestCommand = 0x00;void myDelay(unsigned int delay);void delay_ms(unsigned int n_ms);/* * Send character c down the UART Tx, wait until tx holding register * is empty. */int uart_putchar(char c, FILE *stream){ if (c == '\a') { fputs("*ring*\n", stderr); return 0; } if (c == '\n') uart_putchar('\r', stream); loop_until_bit_is_set(UCSR0A, UDRE); UDR0 = c; return 0;}intuart_getchar(FILE *stream){ uint8_t c=0; return c;}FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);void RFID_Start(void){ PORTB = 0x00; DDRB = 0xFF; //PORTB OUT PORTB = 0x00; PORTA |= 0x01; //CLK HIGH// delay_ms(1); //_delay_ms(1); PORTB |= 0xFF; //AD 7 HIGH// delay_ms(1); //_delay_ms(1); PORTA &= 0xFE; //CLK LOW// delay_ms(1); //_delay_ms(1);}void RFID_Stop(void){ PORTB = 0x00; DDRB = 0xFF; //PORTB OUT PORTB |= 0x80; //AD 7 HIGH RFID_CLK_High; PORTB = 0x00; RFID_CLK_Low;}void RFID_Stop_cont(void){ PORTB = 0x00; DDRB = 0xFF; //PORTB OUT PORTB = 0x80; //AD 7 HIGH Nop = 1; //delay_ms(1); PORTB = 0x00;}void RFID_ReadSingle(unsigned char *RFID_Buf, unsigned char length){ RFID_Start(); while(length > 0) { *RFID_Buf = (0x40 | *RFID_Buf); *RFID_Buf = (0x5F & *RFID_Buf); RFID_Write = *RFID_Buf; RFID_CLK_High; RFID_CLK_Low; DDRB = 0x00; //PORTB IN RFID_CLK_High; Nop = 1; //delay_ms(1); *RFID_Buf = RFID_Read; RFID_CLK_Low; RFID_Write = 0x00; DDRB = 0xFF; //PORTB OUT RFID_Buf++; length--; }//While RFID_Stop();}void RFID_WriteSingle(unsigned char *RFID_Buf,unsigned char length){ unsigned char i; RFID_Start(); while (length > 0 ) { *RFID_Buf = (0x1F & *RFID_Buf); for(i=0;i< 2;i++) { RFID_Write = *RFID_Buf; RFID_CLK_High; RFID_CLK_Low; RFID_Buf++; length--; }//for }//while RFID_Stop();}void RFID_WriteCont(unsigned char *RFID_Buf,unsigned char length){ RFID_Start(); *RFID_Buf = (0x20 | *RFID_Buf); *RFID_Buf = (0x3F & *RFID_Buf); while (length > 0 ) { RFID_Write = *RFID_Buf; RFID_CLK_High; RFID_CLK_Low; RFID_Buf++; length--; }//while RFID_Stop_cont();}void RFID_ReadCont(unsigned char *RFID_Buf, unsigned char length){ RFID_Start(); *RFID_Buf = (0x60 | *RFID_Buf); *RFID_Buf = (0x7F & *RFID_Buf); RFID_Write = *RFID_Buf; RFID_CLK_High; RFID_CLK_Low; DDRB = 0x00; //PORTB IN while(length > 0) { RFID_CLK_High; Nop = 1; //delay_ms(1); *RFID_Buf = RFID_Read; RFID_CLK_Low; RFID_Buf++; length--; }//While RFID_Stop_cont();}/* ======================================================================================================================= Function used for direct writing to reader chip ; ======================================================================================================================= */void RFID_RAWwrite(unsigned char *pbuf, unsigned char length){ RFID_Start(); while(length > 0) { RFID_Write = *pbuf; /* send command */ RFID_CLK_High; RFID_CLK_Low; pbuf++; length--; } /* while */ RFID_Stop_cont();} /* RAWwrite *//* ======================================================================================================================= Function DirectCommand transmits a command to the reader chip ======================================================================================================================= */void RFID_DirectCommand(unsigned char *pbuf){ RFID_Start(); *pbuf = (0x80 | *pbuf); /* command */ *pbuf = (0x9f &*pbuf); /* command code */ RFID_Write = *pbuf; /* send command */ RFID_CLK_High; RFID_CLK_Low; RFID_Stop();} /* DirectCommand */void RFID_EnableSlotCounter(void) // enable no-response interupt{ buf[41] = IRQMask; /* next slot counter */ buf[40] = IRQMask; RFID_ReadSingle(&buf[41], 1); buf[41] |= 0x01; /* set BIT0 in register 0x01 */ RFID_WriteSingle(&buf[40], 2);}void RFID_DisableSlotCounter(void) // disable no-response interupt{ buf[41] = IRQMask; /* next slot counter */ buf[40] = IRQMask; RFID_ReadSingle(&buf[41], 1); buf[41] &= 0xfe; /* clear BIT0 in register 0x01 */ RFID_WriteSingle(&buf[40], 2);}void RFID_InventoryRequest(unsigned char *mask, unsigned char lenght) /* host command 0x14 */{ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* 010800030414(req.packet)[00ff] */ unsigned char i = 1,j, command, NoSlots, found = 0; unsigned char *PslotNo, slotNo[17]; unsigned char NewMask[8], NewLenght, masksize; int size; unsigned int k = 0; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ slotNo[0] = 0x00; if((flags & 0x20) == 0x00) { /* flag bit5 is the number of slots indicator */ NoSlots = 17; /* 16 slots if bit is cleared */ RFID_EnableSlotCounter(); } else NoSlots = 2; /* 1 slot if bit is set */ PslotNo = &slotNo[0]; /* slot number pointer */ masksize = (((lenght >> 2) + 1) >> 1); /* masksize is 1 for lenght = 4 or 8 */ /* * masksize is 2 for lenght = 12 or 16 ; * and so on */ size = masksize + 3; /* mask value + mask lenght + command code + flags */ buf[0] = 0x8f; //Reset fifo command buf[1] = 0x91; /* send with CRC */ buf[2] = 0x3d; /* write continous from 1D */ buf[3] = (char) (size >> 8); buf[4] = (char) (size << 4); buf[5] = flags; /* ISO15693 flags */ buf[6] = 0x01; /* anticollision command code */ buf[7] = lenght; /* masklenght */ if(lenght > 0) { for(i = 0; i < masksize; i++) buf[i + 8] = *(mask + i); } /* if */ command = IRQStatus; RFID_ReadSingle(&command, 1); RFID_RAWwrite(&buf[0], masksize + 8); /* writing to FIFO */// irqCLR; /* PORT2 interrupt flag clear */ irqON; i_reg = 0x01; // LPM0; /* wait for end of TX interrupt */ myDelay(20); sleep_cpu(); for(i = 1; i < NoSlots; i++) { /* 1 or 16 available timeslots */ RXTXstate = 1; /* prepare the global counter */ /* the first UID will be stored from buf[1] upwards */ k = 0; //LPM0; myDelay(20); sleep_cpu(); while(i_reg == 0x01) { /* wait for RX complete */ k++; if(k == 0xFFF0) { i_reg = 0x00; RXErrorFlag = 0x00; break; } } command = RSSILevels; /* read RSSI levels */ RFID_ReadSingle(&command, 1);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"RSSILevels = %x\n",command); command = IRQStatus; /* IRQ status register address */ RFID_ReadSingle(&command, 1); /* function call for single address read */// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str,"IRQStatus = %x\n",command); if(i_reg == 0xFF) { /* recieved UID in buffer */ found = 1;/* fprintf(&uart_str,"%c",0x0D); fprintf(&uart_str,"Buf = "); for(j=3;j<=10;j++) fprintf(&uart_str,"%x",buf[j]); fprintf(&uart_str,"\n");*/ } else if(i_reg == 0x02) { /* collision occured */ PslotNo++; *PslotNo = i; } else if(i_reg == 0x00) { /* timer interrupt */ } else ; command = Reset; /* FIFO has to be reset before recieving the next response */ RFID_DirectCommand(&command);// fprintf(&uart_str,"%c",0x0D); // fprintf(&uart_str," Reset \n"); if((NoSlots == 17) && (i < 16)) { /* if 16 slots used send EOF(next slot) */ command = StopDecoders; RFID_DirectCommand(&command); command = RunDecoders; RFID_DirectCommand(&command); command = TransmitNextSlot; RFID_DirectCommand(&command); } else if((NoSlots == 17) && (i == 16)) { /* at the end of slot 16 stop the slot counter */ RFID_DisableSlotCounter(); } else if(NoSlots == 2) break; } /* for */ if(found) { /* turn on LED */ //LED15693ON; } else { //LED15693OFF; } NewLenght = lenght + 4; /* the mask lenght is a multiple of 4 bits */ masksize = (((NewLenght >> 2) + 1) >> 1) - 1; while((*PslotNo != 0x00) && (NoSlots == 17)) { *PslotNo = *PslotNo - 1; for(i = 0; i < 8; i++) NewMask[i] = *(mask + i); /* first the whole mask is copied */ if((NewLenght & BIT2) == 0x00) *PslotNo = *PslotNo << 4; /* * Put_byte(*PslotNo); * *put_crlf(); */ NewMask[masksize] |= *PslotNo; /* the mask is changed */ RFID_InventoryRequest(&NewMask[0], NewLenght); /* recursive call */ PslotNo--; } /* while */ irqOFF; } /* InventoryRequest */void CounterSet(void){ TCNT2 = 0x00; TCCR2A |= (1 << WGM21); TIMSK2 |= (1 << OCIE2A); // Enable CTC interrupt// TCCR2B |= ((1 << CS20) | (1 << CS22)); // Start timer at Fcpu/64 ASSR=0x00; /* TCNT1 = 0x00; TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt*/}void myDelay(unsigned int delay){ Timer_Flag = 0; CountValue = Count1ms*delay; CounterSet(); StartCounter;// while(!Timer_Flag);}/* ======================================================================================================================= The function RequestCommand() is used for request and // ; response handling and timing for VCD to VICC // ; communication. // ; Host command = 0x18 // ; ======================================================================================================================= */unsigned char RFID_RequestCommand(unsigned char *pbuf, unsigned char lenght, unsigned char brokenBits, char noCRC){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ unsigned char index, command,j; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ lenght = 12; //4 read no uid //8 write single //12 read with uid //15 write multi with uid RXTXstate = lenght; /* RXTXstate global wariable is the main transmit counter */ *pbuf = 0x8f; if(noCRC) *(pbuf + 1) = 0x90; /* buffer setup for FIFO writing */ else *(pbuf + 1) = 0x91; /* buffer setup for FIFO writing */ *(pbuf + 2) = 0x3d; *(pbuf + 3) = (RXTXstate >> 8);//4 *(pbuf + 4) = (RXTXstate << 4) | brokenBits;// Write multi block/* *(pbuf + 5) = 0x63;//22 *(pbuf + 6) = 0x21; *(pbuf + 7) = 0x94; *(pbuf + 8) = 0x03; *(pbuf + 9) = 0xF4; *(pbuf + 10) = 0x23; *(pbuf + 11) = 0x00; *(pbuf + 12) = 0x00; *(pbuf + 13) = 0x07; *(pbuf + 14) = 0xE0; *(pbuf + 15) = 0x00; *(pbuf + 16) = 0x0A; *(pbuf + 17) = 0x0B; *(pbuf + 18) = 0x0C; *(pbuf + 19) = 0x0D;*/// Read Multi Block *(pbuf + 5) = 0x22; *(pbuf + 6) = 0x23; *(pbuf + 7) = 0x94;//94 *(pbuf + 8) = 0x03;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -