This code works on FRDM but not on nRF boards.

Dependencies:   mbed

Committer:
blark
Date:
Thu Dec 04 16:08:04 2014 +0000
Revision:
0:35c0300ee644
first commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
blark 0:35c0300ee644 1 #include "mbed.h"
blark 0:35c0300ee644 2
blark 0:35c0300ee644 3 #define MAX_BITS 100
blark 0:35c0300ee644 4 #define WIEGAND_WAIT_TIME 3000 // wait for 3000us for the next Wiegand signal
blark 0:35c0300ee644 5 #define DATA0_PIN PTC2 // see http://developer.mbed.org/platforms/FRDM-K64F/ for pin locations
blark 0:35c0300ee644 6 #define DATA1_PIN PTC3 // customize for board as appropriate.
blark 0:35c0300ee644 7 #define DATA0_OUT_PIN PTC7
blark 0:35c0300ee644 8 #define DATA1_OUT_PIN PTC5
blark 0:35c0300ee644 9
blark 0:35c0300ee644 10 Serial pc(USBTX, USBRX);
blark 0:35c0300ee644 11 Timeout wiegandTimer;
blark 0:35c0300ee644 12
blark 0:35c0300ee644 13 InterruptIn sw(SW2);
blark 0:35c0300ee644 14 InterruptIn data0_int(DATA0_PIN);
blark 0:35c0300ee644 15 InterruptIn data1_int(DATA1_PIN);
blark 0:35c0300ee644 16 DigitalOut data0_out(DATA0_OUT_PIN);
blark 0:35c0300ee644 17 DigitalOut data1_out(DATA1_OUT_PIN);
blark 0:35c0300ee644 18
blark 0:35c0300ee644 19 volatile unsigned char dataBits[MAX_BITS]; // stores all of the data bits
blark 0:35c0300ee644 20 volatile unsigned char bitCount = 0; // number of bits captured
blark 0:35c0300ee644 21 volatile bool flagDataIn = false; // false when data is currently being captured
blark 0:35c0300ee644 22 volatile bool readComplete = false; // true when a card read has been completed
blark 0:35c0300ee644 23 volatile bool sendData = false;
blark 0:35c0300ee644 24
blark 0:35c0300ee644 25 void txWiegand(uint8_t *data, uint8_t size) {
blark 0:35c0300ee644 26 // sends wiegand data for testing purposes.
blark 0:35c0300ee644 27 for (uint8_t i=0; i<size; i++) {
blark 0:35c0300ee644 28 // use a ternary operator to toggle the correct pin
blark 0:35c0300ee644 29 data[i] ? data1_out=0 : data0_out=0;
blark 0:35c0300ee644 30 wait_us(40);
blark 0:35c0300ee644 31 data[i] ? data1_out=1 : data0_out=1;
blark 0:35c0300ee644 32 wait_us(2249);
blark 0:35c0300ee644 33 }
blark 0:35c0300ee644 34 }
blark 0:35c0300ee644 35
blark 0:35c0300ee644 36 void parseWiegand (void) {
blark 0:35c0300ee644 37 // tell the main while loop the signal to process data
blark 0:35c0300ee644 38 readComplete = true;
blark 0:35c0300ee644 39 }
blark 0:35c0300ee644 40
blark 0:35c0300ee644 41 void DATA0_ISR() {
blark 0:35c0300ee644 42 bitCount++;
blark 0:35c0300ee644 43 flagDataIn = true;
blark 0:35c0300ee644 44 }
blark 0:35c0300ee644 45
blark 0:35c0300ee644 46 void DATA1_ISR() {
blark 0:35c0300ee644 47 dataBits[bitCount] = 1;
blark 0:35c0300ee644 48 bitCount++;
blark 0:35c0300ee644 49 flagDataIn = true;
blark 0:35c0300ee644 50 }
blark 0:35c0300ee644 51
blark 0:35c0300ee644 52 void sw_isr() {
blark 0:35c0300ee644 53 sendData = true;
blark 0:35c0300ee644 54 }
blark 0:35c0300ee644 55
blark 0:35c0300ee644 56 int main()
blark 0:35c0300ee644 57 {
blark 0:35c0300ee644 58 pc.baud (115200);
blark 0:35c0300ee644 59
blark 0:35c0300ee644 60 data0_int.fall(&DATA0_ISR); // assign ISRs
blark 0:35c0300ee644 61 data1_int.fall(&DATA1_ISR);
blark 0:35c0300ee644 62 sw.fall(&sw_isr);
blark 0:35c0300ee644 63
blark 0:35c0300ee644 64 data0_out = 1;
blark 0:35c0300ee644 65 data1_out = 1;
blark 0:35c0300ee644 66
blark 0:35c0300ee644 67 while (true) {
blark 0:35c0300ee644 68 if ((bitCount > 0) && flagDataIn) {
blark 0:35c0300ee644 69 // we got more valid data so reset the timer
blark 0:35c0300ee644 70 wiegandTimer.attach_us(&parseWiegand, WIEGAND_WAIT_TIME);
blark 0:35c0300ee644 71 flagDataIn = false;
blark 0:35c0300ee644 72 }
blark 0:35c0300ee644 73 if (readComplete) {
blark 0:35c0300ee644 74 // After the Wiegand data has been read we need to parse it
blark 0:35c0300ee644 75 pc.printf("\nRead %d bits: ", bitCount);
blark 0:35c0300ee644 76 // Print the card value in binary
blark 0:35c0300ee644 77 for (uint8_t i=0; i<bitCount; i++) {
blark 0:35c0300ee644 78 pc.printf("%d", dataBits[i]);
blark 0:35c0300ee644 79 }
blark 0:35c0300ee644 80 pc.printf("\n");
blark 0:35c0300ee644 81
blark 0:35c0300ee644 82 /* in the future parse card code and facility code here.
blark 0:35c0300ee644 83 */
blark 0:35c0300ee644 84
blark 0:35c0300ee644 85 // reset variables for next read
blark 0:35c0300ee644 86 readComplete = false;
blark 0:35c0300ee644 87 bitCount = 0;
blark 0:35c0300ee644 88 for (uint8_t i=0; i<MAX_BITS; i++) {
blark 0:35c0300ee644 89 dataBits[i] = 0;
blark 0:35c0300ee644 90 }
blark 0:35c0300ee644 91 }
blark 0:35c0300ee644 92 if (sendData) {
blark 0:35c0300ee644 93 // do things
blark 0:35c0300ee644 94 uint8_t testData[26] = { 0,0,1,1,0,0,1,0,0,0,0,1,0,0,1,1,0,0,0,1,0,1,0,1,0,0 };
blark 0:35c0300ee644 95 txWiegand(testData, 26);
blark 0:35c0300ee644 96 sendData = false;
blark 0:35c0300ee644 97 }
blark 0:35c0300ee644 98 }
blark 0:35c0300ee644 99 }