James Nagendran
/
4180_final_RFID
RFID code for SLVM
main.cpp@0:23e85e8989fc, 2014-12-03 (annotated)
- Committer:
- jnagendran3
- Date:
- Wed Dec 03 22:39:53 2014 +0000
- Revision:
- 0:23e85e8989fc
YAY
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jnagendran3 | 0:23e85e8989fc | 1 | #include "mbed.h" |
jnagendran3 | 0:23e85e8989fc | 2 | #include "ID12RFID.h" |
jnagendran3 | 0:23e85e8989fc | 3 | |
jnagendran3 | 0:23e85e8989fc | 4 | ID12RFID rfid(p27); // uart rx |
jnagendran3 | 0:23e85e8989fc | 5 | Serial pc(USBTX,USBRX); |
jnagendran3 | 0:23e85e8989fc | 6 | |
jnagendran3 | 0:23e85e8989fc | 7 | DigitalOut interrupt(p23); |
jnagendran3 | 0:23e85e8989fc | 8 | DigitalOut MSB(p21); |
jnagendran3 | 0:23e85e8989fc | 9 | DigitalOut LSB(p22); |
jnagendran3 | 0:23e85e8989fc | 10 | DigitalOut led1(LED1); |
jnagendran3 | 0:23e85e8989fc | 11 | DigitalOut led2(LED2); |
jnagendran3 | 0:23e85e8989fc | 12 | DigitalOut led3(LED3); |
jnagendran3 | 0:23e85e8989fc | 13 | |
jnagendran3 | 0:23e85e8989fc | 14 | float speedModifiers[] = { |
jnagendran3 | 0:23e85e8989fc | 15 | 1.0, 0.7, 0.5, 0.3 |
jnagendran3 | 0:23e85e8989fc | 16 | }; |
jnagendran3 | 0:23e85e8989fc | 17 | |
jnagendran3 | 0:23e85e8989fc | 18 | int speedTags[] = { |
jnagendran3 | 0:23e85e8989fc | 19 | 5452129, |
jnagendran3 | 0:23e85e8989fc | 20 | 5449063, |
jnagendran3 | 0:23e85e8989fc | 21 | 36902518 |
jnagendran3 | 0:23e85e8989fc | 22 | }; |
jnagendran3 | 0:23e85e8989fc | 23 | |
jnagendran3 | 0:23e85e8989fc | 24 | volatile int tag = 0; |
jnagendran3 | 0:23e85e8989fc | 25 | volatile bool tagRead = false; |
jnagendran3 | 0:23e85e8989fc | 26 | |
jnagendran3 | 0:23e85e8989fc | 27 | int intCorrection; |
jnagendran3 | 0:23e85e8989fc | 28 | |
jnagendran3 | 0:23e85e8989fc | 29 | void readCard(); |
jnagendran3 | 0:23e85e8989fc | 30 | void checkTag(); |
jnagendran3 | 0:23e85e8989fc | 31 | |
jnagendran3 | 0:23e85e8989fc | 32 | void readCard() { |
jnagendran3 | 0:23e85e8989fc | 33 | // First, disable interrupts to allow critical sections to run through |
jnagendran3 | 0:23e85e8989fc | 34 | NVIC_DisableIRQ(UART2_IRQn); |
jnagendran3 | 0:23e85e8989fc | 35 | //led3 = !led3; |
jnagendran3 | 0:23e85e8989fc | 36 | if(rfid.readable()) { |
jnagendran3 | 0:23e85e8989fc | 37 | tag = rfid.read(); |
jnagendran3 | 0:23e85e8989fc | 38 | tagRead = true; |
jnagendran3 | 0:23e85e8989fc | 39 | bool match = false; |
jnagendran3 | 0:23e85e8989fc | 40 | int length = (sizeof(speedTags)/sizeof(speedTags[0]) < sizeof(speedModifiers)/sizeof(speedModifiers[0])) ? (sizeof(speedTags)/sizeof(speedTags[0])) : (sizeof(speedModifiers)/(sizeof(speedModifiers[0]))); |
jnagendran3 | 0:23e85e8989fc | 41 | for (int i=0; i<length; i++) { |
jnagendran3 | 0:23e85e8989fc | 42 | if (speedTags[i] == tag) { |
jnagendran3 | 0:23e85e8989fc | 43 | printf("RFID Tag number : %d\nSpeed now at %d\n", tag, ((int)(speedModifiers[i]*100))); |
jnagendran3 | 0:23e85e8989fc | 44 | match = true; |
jnagendran3 | 0:23e85e8989fc | 45 | intCorrection++; |
jnagendran3 | 0:23e85e8989fc | 46 | } |
jnagendran3 | 0:23e85e8989fc | 47 | } |
jnagendran3 | 0:23e85e8989fc | 48 | |
jnagendran3 | 0:23e85e8989fc | 49 | // For some reason, this function is entered twice each time an RFID tag is read. |
jnagendran3 | 0:23e85e8989fc | 50 | // This bit here is to detect that case and just ignore it, while still catching |
jnagendran3 | 0:23e85e8989fc | 51 | // other cases and displaying a warning |
jnagendran3 | 0:23e85e8989fc | 52 | if (!match and intCorrection == 0) { |
jnagendran3 | 0:23e85e8989fc | 53 | printf("[ WARNING ] Tag number %d not recognized\n", tag); |
jnagendran3 | 0:23e85e8989fc | 54 | } |
jnagendran3 | 0:23e85e8989fc | 55 | else if (!match == 1) { |
jnagendran3 | 0:23e85e8989fc | 56 | intCorrection = 0; |
jnagendran3 | 0:23e85e8989fc | 57 | } |
jnagendran3 | 0:23e85e8989fc | 58 | } |
jnagendran3 | 0:23e85e8989fc | 59 | |
jnagendran3 | 0:23e85e8989fc | 60 | NVIC_EnableIRQ(UART2_IRQn); |
jnagendran3 | 0:23e85e8989fc | 61 | } |
jnagendran3 | 0:23e85e8989fc | 62 | |
jnagendran3 | 0:23e85e8989fc | 63 | void checkTag() { |
jnagendran3 | 0:23e85e8989fc | 64 | } |
jnagendran3 | 0:23e85e8989fc | 65 | |
jnagendran3 | 0:23e85e8989fc | 66 | int main() { |
jnagendran3 | 0:23e85e8989fc | 67 | interrupt=0; |
jnagendran3 | 0:23e85e8989fc | 68 | rfid.setInterrupt(&readCard); |
jnagendran3 | 0:23e85e8989fc | 69 | printf("Entering main loop\n"); |
jnagendran3 | 0:23e85e8989fc | 70 | while(1) { |
jnagendran3 | 0:23e85e8989fc | 71 | if (tagRead) { |
jnagendran3 | 0:23e85e8989fc | 72 | checkTag(); |
jnagendran3 | 0:23e85e8989fc | 73 | if(tag==5452129 || tag==5449063) LSB=1; |
jnagendran3 | 0:23e85e8989fc | 74 | else LSB=0; |
jnagendran3 | 0:23e85e8989fc | 75 | if(tag==5449063 || tag==36902518) MSB=1; |
jnagendran3 | 0:23e85e8989fc | 76 | else MSB=0; |
jnagendran3 | 0:23e85e8989fc | 77 | interrupt=1; |
jnagendran3 | 0:23e85e8989fc | 78 | led1=MSB; |
jnagendran3 | 0:23e85e8989fc | 79 | led2=LSB; |
jnagendran3 | 0:23e85e8989fc | 80 | led3=interrupt; |
jnagendran3 | 0:23e85e8989fc | 81 | wait(.5); |
jnagendran3 | 0:23e85e8989fc | 82 | interrupt=0; |
jnagendran3 | 0:23e85e8989fc | 83 | led3=interrupt; |
jnagendran3 | 0:23e85e8989fc | 84 | tagRead = false;} |
jnagendran3 | 0:23e85e8989fc | 85 | //led1 = 1; wait(0.5); |
jnagendran3 | 0:23e85e8989fc | 86 | //led1 = 0; wait(0.5); |
jnagendran3 | 0:23e85e8989fc | 87 | wait(.1); |
jnagendran3 | 0:23e85e8989fc | 88 | } |
jnagendran3 | 0:23e85e8989fc | 89 | } |