project van rtos
Dependencies: mbed-os
Fork of MFRC522 by
main.cpp@3:80837b470692, 2018-05-08 (annotated)
- Committer:
- Onon
- Date:
- Tue May 08 12:52:09 2018 +0000
- Revision:
- 3:80837b470692
- Parent:
- 2:c719944e080b
- Child:
- 4:325954591d0f
display ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Onon | 2:c719944e080b | 1 | |
Onon | 2:c719944e080b | 2 | #include "mbed.h" |
Onon | 2:c719944e080b | 3 | #include "MFRC522.h" |
Onon | 2:c719944e080b | 4 | |
Onon | 2:c719944e080b | 5 | //KL25Z Pins for MFRC522 SPI interface |
Onon | 2:c719944e080b | 6 | #define SPI_MOSI p5 |
Onon | 2:c719944e080b | 7 | #define SPI_MISO p6 |
Onon | 2:c719944e080b | 8 | #define SPI_SCLK p7 |
Onon | 2:c719944e080b | 9 | #define SPI_CS p9 |
Onon | 2:c719944e080b | 10 | // KL25Z Pin for MFRC522 reset |
Onon | 2:c719944e080b | 11 | #define MF_RESET p8 |
Onon | 2:c719944e080b | 12 | /** |
Onon | 2:c719944e080b | 13 | // KL25Z Pins for Debug UART port |
Onon | 2:c719944e080b | 14 | #define UART_RX gnd |
Onon | 2:c719944e080b | 15 | #define UART_TX gnd |
Onon | 2:c719944e080b | 16 | |
Onon | 2:c719944e080b | 17 | Serial DebugUART(UART_TX, UART_RX); |
Onon | 2:c719944e080b | 18 | **/ |
Onon | 2:c719944e080b | 19 | MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET); |
Onon | 2:c719944e080b | 20 | Serial pc(USBTX, USBRX); |
Onon | 2:c719944e080b | 21 | |
Onon | 2:c719944e080b | 22 | DigitalOut led(LED1); |
Onon | 3:80837b470692 | 23 | DigitalOut dg0(p27); |
Onon | 3:80837b470692 | 24 | DigitalOut dg1(p28); |
Onon | 3:80837b470692 | 25 | DigitalOut dg2(p29); |
Onon | 3:80837b470692 | 26 | DigitalOut dg3(p30); |
Onon | 3:80837b470692 | 27 | |
Onon | 3:80837b470692 | 28 | DigitalOut da(p26); |
Onon | 3:80837b470692 | 29 | DigitalOut db(p25); |
Onon | 3:80837b470692 | 30 | DigitalOut dc(p24); |
Onon | 3:80837b470692 | 31 | DigitalOut dd(p23); |
Onon | 3:80837b470692 | 32 | DigitalOut de(p22); |
Onon | 3:80837b470692 | 33 | DigitalOut df(p21); |
Onon | 3:80837b470692 | 34 | DigitalOut dg(p19); |
Onon | 3:80837b470692 | 35 | DigitalOut ddp(p18); |
Onon | 3:80837b470692 | 36 | |
Onon | 3:80837b470692 | 37 | AnalogIn a0(p20); |
Onon | 3:80837b470692 | 38 | |
Onon | 3:80837b470692 | 39 | int sevenSeg; |
Onon | 3:80837b470692 | 40 | |
Onon | 3:80837b470692 | 41 | void light7Seg(int nr, int output) { |
Onon | 3:80837b470692 | 42 | da = 0; |
Onon | 3:80837b470692 | 43 | db = 0; |
Onon | 3:80837b470692 | 44 | dc = 0; |
Onon | 3:80837b470692 | 45 | dd = 0; |
Onon | 3:80837b470692 | 46 | de = 0; |
Onon | 3:80837b470692 | 47 | df = 0; |
Onon | 3:80837b470692 | 48 | dg = 0; |
Onon | 3:80837b470692 | 49 | ddp = 0; |
Onon | 3:80837b470692 | 50 | dg0 = 1; |
Onon | 3:80837b470692 | 51 | dg1 = 1; |
Onon | 3:80837b470692 | 52 | dg2 = 1; |
Onon | 3:80837b470692 | 53 | dg3 = 1; |
Onon | 3:80837b470692 | 54 | switch(output) { |
Onon | 3:80837b470692 | 55 | case 0: |
Onon | 3:80837b470692 | 56 | dg0 = 0; |
Onon | 3:80837b470692 | 57 | break; |
Onon | 3:80837b470692 | 58 | case 1: |
Onon | 3:80837b470692 | 59 | dg1 = 0; |
Onon | 3:80837b470692 | 60 | break; |
Onon | 3:80837b470692 | 61 | case 2: |
Onon | 3:80837b470692 | 62 | dg2 = 0; |
Onon | 3:80837b470692 | 63 | break; |
Onon | 3:80837b470692 | 64 | case 3: |
Onon | 3:80837b470692 | 65 | dg3 = 0; |
Onon | 3:80837b470692 | 66 | ddp = 1; |
Onon | 3:80837b470692 | 67 | break; |
Onon | 3:80837b470692 | 68 | default: |
Onon | 3:80837b470692 | 69 | break; |
Onon | 3:80837b470692 | 70 | } |
Onon | 3:80837b470692 | 71 | switch(nr) { |
Onon | 3:80837b470692 | 72 | case 0: |
Onon | 3:80837b470692 | 73 | da = 1; |
Onon | 3:80837b470692 | 74 | db = 1; |
Onon | 3:80837b470692 | 75 | dc = 1; |
Onon | 3:80837b470692 | 76 | dd = 1; |
Onon | 3:80837b470692 | 77 | de = 1; |
Onon | 3:80837b470692 | 78 | df = 1; |
Onon | 3:80837b470692 | 79 | dg = 0; |
Onon | 3:80837b470692 | 80 | break; |
Onon | 3:80837b470692 | 81 | case 1: |
Onon | 3:80837b470692 | 82 | da = 0; |
Onon | 3:80837b470692 | 83 | db = 1; |
Onon | 3:80837b470692 | 84 | dc = 1; |
Onon | 3:80837b470692 | 85 | dd = 0; |
Onon | 3:80837b470692 | 86 | de = 0; |
Onon | 3:80837b470692 | 87 | df = 0; |
Onon | 3:80837b470692 | 88 | dg = 0; |
Onon | 3:80837b470692 | 89 | break; |
Onon | 3:80837b470692 | 90 | case 2: |
Onon | 3:80837b470692 | 91 | da = 1; |
Onon | 3:80837b470692 | 92 | db = 1; |
Onon | 3:80837b470692 | 93 | dc = 0; |
Onon | 3:80837b470692 | 94 | dd = 1; |
Onon | 3:80837b470692 | 95 | de = 1; |
Onon | 3:80837b470692 | 96 | df = 0; |
Onon | 3:80837b470692 | 97 | dg = 1; |
Onon | 3:80837b470692 | 98 | break; |
Onon | 3:80837b470692 | 99 | case 3: |
Onon | 3:80837b470692 | 100 | da = 1; |
Onon | 3:80837b470692 | 101 | db = 1; |
Onon | 3:80837b470692 | 102 | dc = 1; |
Onon | 3:80837b470692 | 103 | dd = 1; |
Onon | 3:80837b470692 | 104 | de = 0; |
Onon | 3:80837b470692 | 105 | df = 0; |
Onon | 3:80837b470692 | 106 | dg = 1; |
Onon | 3:80837b470692 | 107 | break; |
Onon | 3:80837b470692 | 108 | case 4: |
Onon | 3:80837b470692 | 109 | da = 0; |
Onon | 3:80837b470692 | 110 | db = 1; |
Onon | 3:80837b470692 | 111 | dc = 1; |
Onon | 3:80837b470692 | 112 | dd = 0; |
Onon | 3:80837b470692 | 113 | de = 0; |
Onon | 3:80837b470692 | 114 | df = 1; |
Onon | 3:80837b470692 | 115 | dg = 1; |
Onon | 3:80837b470692 | 116 | break; |
Onon | 3:80837b470692 | 117 | case 5: |
Onon | 3:80837b470692 | 118 | da = 1; |
Onon | 3:80837b470692 | 119 | db = 0; |
Onon | 3:80837b470692 | 120 | dc = 1; |
Onon | 3:80837b470692 | 121 | dd = 1; |
Onon | 3:80837b470692 | 122 | de = 0; |
Onon | 3:80837b470692 | 123 | df = 1; |
Onon | 3:80837b470692 | 124 | dg = 1; |
Onon | 3:80837b470692 | 125 | break; |
Onon | 3:80837b470692 | 126 | case 6: |
Onon | 3:80837b470692 | 127 | da = 1; |
Onon | 3:80837b470692 | 128 | db = 0; |
Onon | 3:80837b470692 | 129 | dc = 1; |
Onon | 3:80837b470692 | 130 | dd = 1; |
Onon | 3:80837b470692 | 131 | de = 1; |
Onon | 3:80837b470692 | 132 | df = 1; |
Onon | 3:80837b470692 | 133 | dg = 1; |
Onon | 3:80837b470692 | 134 | break; |
Onon | 3:80837b470692 | 135 | case 7: |
Onon | 3:80837b470692 | 136 | da = 1; |
Onon | 3:80837b470692 | 137 | db = 1; |
Onon | 3:80837b470692 | 138 | dc = 1; |
Onon | 3:80837b470692 | 139 | dd = 0; |
Onon | 3:80837b470692 | 140 | de = 0; |
Onon | 3:80837b470692 | 141 | df = 0; |
Onon | 3:80837b470692 | 142 | dg = 0; |
Onon | 3:80837b470692 | 143 | break; |
Onon | 3:80837b470692 | 144 | case 8: |
Onon | 3:80837b470692 | 145 | da = 1; |
Onon | 3:80837b470692 | 146 | db = 1; |
Onon | 3:80837b470692 | 147 | dc = 1; |
Onon | 3:80837b470692 | 148 | dd = 1; |
Onon | 3:80837b470692 | 149 | de = 1; |
Onon | 3:80837b470692 | 150 | df = 1; |
Onon | 3:80837b470692 | 151 | dg = 1; |
Onon | 3:80837b470692 | 152 | break; |
Onon | 3:80837b470692 | 153 | case 9: |
Onon | 3:80837b470692 | 154 | da = 1; |
Onon | 3:80837b470692 | 155 | db = 1; |
Onon | 3:80837b470692 | 156 | dc = 1; |
Onon | 3:80837b470692 | 157 | dd = 1; |
Onon | 3:80837b470692 | 158 | de = 0; |
Onon | 3:80837b470692 | 159 | df = 1; |
Onon | 3:80837b470692 | 160 | dg = 1; |
Onon | 3:80837b470692 | 161 | break; |
Onon | 3:80837b470692 | 162 | case 10: |
Onon | 3:80837b470692 | 163 | da = 1; |
Onon | 3:80837b470692 | 164 | db = 1; |
Onon | 3:80837b470692 | 165 | dc = 1; |
Onon | 3:80837b470692 | 166 | dd = 0; |
Onon | 3:80837b470692 | 167 | de = 1; |
Onon | 3:80837b470692 | 168 | df = 1; |
Onon | 3:80837b470692 | 169 | dg = 1; |
Onon | 3:80837b470692 | 170 | break; |
Onon | 3:80837b470692 | 171 | case 11: |
Onon | 3:80837b470692 | 172 | da = 0; |
Onon | 3:80837b470692 | 173 | db = 0; |
Onon | 3:80837b470692 | 174 | dc = 1; |
Onon | 3:80837b470692 | 175 | dd = 1; |
Onon | 3:80837b470692 | 176 | de = 1; |
Onon | 3:80837b470692 | 177 | df = 1; |
Onon | 3:80837b470692 | 178 | dg = 1; |
Onon | 3:80837b470692 | 179 | break; |
Onon | 3:80837b470692 | 180 | case 12: |
Onon | 3:80837b470692 | 181 | da = 1; |
Onon | 3:80837b470692 | 182 | db = 0; |
Onon | 3:80837b470692 | 183 | dc = 0; |
Onon | 3:80837b470692 | 184 | dd = 1; |
Onon | 3:80837b470692 | 185 | de = 1; |
Onon | 3:80837b470692 | 186 | df = 1; |
Onon | 3:80837b470692 | 187 | dg = 0; |
Onon | 3:80837b470692 | 188 | break; |
Onon | 3:80837b470692 | 189 | case 13: |
Onon | 3:80837b470692 | 190 | da = 0; |
Onon | 3:80837b470692 | 191 | db = 1; |
Onon | 3:80837b470692 | 192 | dc = 1; |
Onon | 3:80837b470692 | 193 | dd = 1; |
Onon | 3:80837b470692 | 194 | de = 1; |
Onon | 3:80837b470692 | 195 | df = 0; |
Onon | 3:80837b470692 | 196 | dg = 1; |
Onon | 3:80837b470692 | 197 | break; |
Onon | 3:80837b470692 | 198 | case 14: |
Onon | 3:80837b470692 | 199 | da = 1; |
Onon | 3:80837b470692 | 200 | db = 0; |
Onon | 3:80837b470692 | 201 | dc = 0; |
Onon | 3:80837b470692 | 202 | dd = 1; |
Onon | 3:80837b470692 | 203 | de = 1; |
Onon | 3:80837b470692 | 204 | df = 1; |
Onon | 3:80837b470692 | 205 | dg = 1; |
Onon | 3:80837b470692 | 206 | break; |
Onon | 3:80837b470692 | 207 | case 15: |
Onon | 3:80837b470692 | 208 | da = 1; |
Onon | 3:80837b470692 | 209 | db = 0; |
Onon | 3:80837b470692 | 210 | dc = 0; |
Onon | 3:80837b470692 | 211 | dd = 0; |
Onon | 3:80837b470692 | 212 | de = 1; |
Onon | 3:80837b470692 | 213 | df = 1; |
Onon | 3:80837b470692 | 214 | dg = 1; |
Onon | 3:80837b470692 | 215 | break; |
Onon | 3:80837b470692 | 216 | default: |
Onon | 3:80837b470692 | 217 | da = 0; |
Onon | 3:80837b470692 | 218 | db = 0; |
Onon | 3:80837b470692 | 219 | dc = 0; |
Onon | 3:80837b470692 | 220 | dd = 0; |
Onon | 3:80837b470692 | 221 | de = 0; |
Onon | 3:80837b470692 | 222 | df = 0; |
Onon | 3:80837b470692 | 223 | dg = 0; |
Onon | 3:80837b470692 | 224 | ddp = 0; |
Onon | 3:80837b470692 | 225 | } |
Onon | 3:80837b470692 | 226 | } |
Onon | 3:80837b470692 | 227 | |
Onon | 3:80837b470692 | 228 | void displayNumber7Seg(float nr) { |
Onon | 3:80837b470692 | 229 | nr *= 1000; |
Onon | 3:80837b470692 | 230 | int nrint = (int) nr; |
Onon | 3:80837b470692 | 231 | int digits[4]; |
Onon | 3:80837b470692 | 232 | int i = 3; |
Onon | 3:80837b470692 | 233 | while (nrint > 0) { |
Onon | 3:80837b470692 | 234 | int digit = nrint % 10; |
Onon | 3:80837b470692 | 235 | digits[i] = digit; |
Onon | 3:80837b470692 | 236 | if(i == 0) { |
Onon | 3:80837b470692 | 237 | break; |
Onon | 3:80837b470692 | 238 | } |
Onon | 3:80837b470692 | 239 | i--; |
Onon | 3:80837b470692 | 240 | nrint /= 10; |
Onon | 3:80837b470692 | 241 | } |
Onon | 3:80837b470692 | 242 | for(int i = 0; i < 4; i++) { |
Onon | 3:80837b470692 | 243 | light7Seg(digits[3-i], i); |
Onon | 3:80837b470692 | 244 | wait_ms(100/24); //60Hz |
Onon | 3:80837b470692 | 245 | } |
Onon | 3:80837b470692 | 246 | } |
Onon | 2:c719944e080b | 247 | |
Onon | 2:c719944e080b | 248 | int main(void) { |
Onon | 2:c719944e080b | 249 | pc.printf("Start"); |
Onon | 2:c719944e080b | 250 | // Set debug UART speed |
Onon | 3:80837b470692 | 251 | //pc.baud(115200); |
Onon | 2:c719944e080b | 252 | |
Onon | 2:c719944e080b | 253 | // Init. RC522 Chip |
Onon | 2:c719944e080b | 254 | RfChip.PCD_Init(); |
Onon | 3:80837b470692 | 255 | |
Onon | 3:80837b470692 | 256 | dg0 = 0; |
Onon | 3:80837b470692 | 257 | dg1 = 0; |
Onon | 3:80837b470692 | 258 | dg2 = 0; |
Onon | 3:80837b470692 | 259 | dg3 = 0; |
Onon | 2:c719944e080b | 260 | |
Onon | 2:c719944e080b | 261 | while (true) { |
Onon | 3:80837b470692 | 262 | led = 0; |
Onon | 3:80837b470692 | 263 | |
Onon | 3:80837b470692 | 264 | displayNumber7Seg(a0.read()); |
Onon | 3:80837b470692 | 265 | |
Onon | 3:80837b470692 | 266 | //printf("%0.4f \n\r", a0.read()); |
Onon | 2:c719944e080b | 267 | // Look for new cards |
Onon | 3:80837b470692 | 268 | /*if ( ! RfChip.PICC_IsNewCardPresent()) |
Onon | 2:c719944e080b | 269 | { |
Onon | 2:c719944e080b | 270 | wait_ms(500); |
Onon | 2:c719944e080b | 271 | continue; |
Onon | 2:c719944e080b | 272 | } |
Onon | 2:c719944e080b | 273 | |
Onon | 2:c719944e080b | 274 | // Select one of the cards |
Onon | 2:c719944e080b | 275 | if ( ! RfChip.PICC_ReadCardSerial()) |
Onon | 2:c719944e080b | 276 | { |
Onon | 2:c719944e080b | 277 | wait_ms(500); |
Onon | 2:c719944e080b | 278 | continue; |
Onon | 2:c719944e080b | 279 | } |
Onon | 2:c719944e080b | 280 | |
Onon | 2:c719944e080b | 281 | // Print Card UID |
Onon | 2:c719944e080b | 282 | pc.printf("Card UID: "); |
Onon | 2:c719944e080b | 283 | for (uint8_t i = 0; i < RfChip.uid.size; i++) |
Onon | 2:c719944e080b | 284 | { |
Onon | 2:c719944e080b | 285 | led = 1; |
Onon | 2:c719944e080b | 286 | pc.printf(" %X02", RfChip.uid.uidByte[i]); |
Onon | 2:c719944e080b | 287 | } |
Onon | 2:c719944e080b | 288 | pc.printf("\n\r"); |
Onon | 2:c719944e080b | 289 | |
Onon | 2:c719944e080b | 290 | // Print Card type |
Onon | 2:c719944e080b | 291 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
Onon | 2:c719944e080b | 292 | pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
Onon | 2:c719944e080b | 293 | wait_ms(1000); |
Onon | 3:80837b470692 | 294 | }*/ |
Onon | 3:80837b470692 | 295 | } |
Onon | 2:c719944e080b | 296 | } |
Onon | 3:80837b470692 | 297 | |
Onon | 3:80837b470692 | 298 | |
Onon | 3:80837b470692 | 299 | |
Onon | 3:80837b470692 | 300 |