project van rtos

Dependencies:   mbed-os

Fork of MFRC522 by Martin Olejar

Committer:
Onon
Date:
Thu May 17 11:03:21 2018 +0000
Revision:
8:4638e3f14c41
Parent:
6:c034f08291ab
Parent:
7:1c712013123b
Child:
9:f1f71e42e267
merge complete :);

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Onon 2:c719944e080b 1
Onon 2:c719944e080b 2 #include "mbed.h"
Onon 2:c719944e080b 3 #include "MFRC522.h"
Bram122 4:325954591d0f 4 #include "rtos.h"
Onon 2:c719944e080b 5
Onon 2:c719944e080b 6 //KL25Z Pins for MFRC522 SPI interface
Onon 2:c719944e080b 7 #define SPI_MOSI p5
Onon 2:c719944e080b 8 #define SPI_MISO p6
Onon 2:c719944e080b 9 #define SPI_SCLK p7
Onon 2:c719944e080b 10 #define SPI_CS p9
Onon 2:c719944e080b 11 // KL25Z Pin for MFRC522 reset
Onon 2:c719944e080b 12 #define MF_RESET p8
Onon 2:c719944e080b 13 /**
Onon 2:c719944e080b 14 // KL25Z Pins for Debug UART port
Onon 2:c719944e080b 15 #define UART_RX gnd
Onon 2:c719944e080b 16 #define UART_TX gnd
Onon 2:c719944e080b 17
Onon 2:c719944e080b 18 Serial DebugUART(UART_TX, UART_RX);
Onon 2:c719944e080b 19 **/
Onon 2:c719944e080b 20 MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);
Onon 2:c719944e080b 21 Serial pc(USBTX, USBRX);
Onon 2:c719944e080b 22
Bram122 5:e45681989201 23 DigitalOut led1(LED1);
Bram122 5:e45681989201 24 DigitalOut led2(LED2);
Bram122 5:e45681989201 25 DigitalOut led3(LED3);
Bram122 5:e45681989201 26 DigitalOut led4(LED4);
Onon 3:80837b470692 27 DigitalOut dg0(p27);
Onon 3:80837b470692 28 DigitalOut dg1(p28);
Onon 3:80837b470692 29 DigitalOut dg2(p29);
Onon 3:80837b470692 30 DigitalOut dg3(p30);
Onon 3:80837b470692 31
Onon 3:80837b470692 32 DigitalOut da(p26);
Onon 3:80837b470692 33 DigitalOut db(p25);
Onon 3:80837b470692 34 DigitalOut dc(p24);
Onon 3:80837b470692 35 DigitalOut dd(p23);
Onon 3:80837b470692 36 DigitalOut de(p22);
Onon 3:80837b470692 37 DigitalOut df(p21);
Onon 3:80837b470692 38 DigitalOut dg(p19);
Onon 3:80837b470692 39 DigitalOut ddp(p18);
Onon 3:80837b470692 40
Onon 3:80837b470692 41 AnalogIn a0(p20);
Onon 3:80837b470692 42
Bram122 5:e45681989201 43 DigitalOut IN1(p11);
Bram122 5:e45681989201 44 DigitalOut IN2(p12);
Bram122 5:e45681989201 45 DigitalOut IN3(p13);
Bram122 5:e45681989201 46 DigitalOut IN4(p14);
Bram122 5:e45681989201 47
Bram122 5:e45681989201 48 float sensorValue = 0.0000;
Bram122 4:325954591d0f 49
Bram122 6:c034f08291ab 50 int speedcontrol = 0.00001;
Bram122 6:c034f08291ab 51
Onon 3:80837b470692 52 int sevenSeg;
Onon 3:80837b470692 53
Onon 3:80837b470692 54 void light7Seg(int nr, int output) {
Onon 3:80837b470692 55 da = 0;
Onon 3:80837b470692 56 db = 0;
Onon 3:80837b470692 57 dc = 0;
Onon 3:80837b470692 58 dd = 0;
Onon 3:80837b470692 59 de = 0;
Onon 3:80837b470692 60 df = 0;
Onon 3:80837b470692 61 dg = 0;
Onon 3:80837b470692 62 ddp = 0;
Onon 3:80837b470692 63 dg0 = 1;
Onon 3:80837b470692 64 dg1 = 1;
Onon 3:80837b470692 65 dg2 = 1;
Onon 3:80837b470692 66 dg3 = 1;
Onon 3:80837b470692 67 switch(output) {
Onon 3:80837b470692 68 case 0:
Onon 3:80837b470692 69 dg0 = 0;
Onon 3:80837b470692 70 break;
Onon 3:80837b470692 71 case 1:
Onon 3:80837b470692 72 dg1 = 0;
Onon 3:80837b470692 73 break;
Onon 3:80837b470692 74 case 2:
Onon 3:80837b470692 75 dg2 = 0;
Onon 3:80837b470692 76 break;
Onon 3:80837b470692 77 case 3:
Onon 3:80837b470692 78 dg3 = 0;
Onon 3:80837b470692 79 ddp = 1;
Onon 3:80837b470692 80 break;
Onon 3:80837b470692 81 default:
Onon 3:80837b470692 82 break;
Onon 3:80837b470692 83 }
Onon 3:80837b470692 84 switch(nr) {
Onon 3:80837b470692 85 case 0:
Onon 3:80837b470692 86 da = 1;
Onon 3:80837b470692 87 db = 1;
Onon 3:80837b470692 88 dc = 1;
Onon 3:80837b470692 89 dd = 1;
Onon 3:80837b470692 90 de = 1;
Onon 3:80837b470692 91 df = 1;
Onon 3:80837b470692 92 dg = 0;
Onon 3:80837b470692 93 break;
Onon 3:80837b470692 94 case 1:
Onon 3:80837b470692 95 da = 0;
Onon 3:80837b470692 96 db = 1;
Onon 3:80837b470692 97 dc = 1;
Onon 3:80837b470692 98 dd = 0;
Onon 3:80837b470692 99 de = 0;
Onon 3:80837b470692 100 df = 0;
Onon 3:80837b470692 101 dg = 0;
Onon 3:80837b470692 102 break;
Onon 3:80837b470692 103 case 2:
Onon 3:80837b470692 104 da = 1;
Onon 3:80837b470692 105 db = 1;
Onon 3:80837b470692 106 dc = 0;
Onon 3:80837b470692 107 dd = 1;
Onon 3:80837b470692 108 de = 1;
Onon 3:80837b470692 109 df = 0;
Onon 3:80837b470692 110 dg = 1;
Onon 3:80837b470692 111 break;
Onon 3:80837b470692 112 case 3:
Onon 3:80837b470692 113 da = 1;
Onon 3:80837b470692 114 db = 1;
Onon 3:80837b470692 115 dc = 1;
Onon 3:80837b470692 116 dd = 1;
Onon 3:80837b470692 117 de = 0;
Onon 3:80837b470692 118 df = 0;
Onon 3:80837b470692 119 dg = 1;
Onon 3:80837b470692 120 break;
Onon 3:80837b470692 121 case 4:
Onon 3:80837b470692 122 da = 0;
Onon 3:80837b470692 123 db = 1;
Onon 3:80837b470692 124 dc = 1;
Onon 3:80837b470692 125 dd = 0;
Onon 3:80837b470692 126 de = 0;
Onon 3:80837b470692 127 df = 1;
Onon 3:80837b470692 128 dg = 1;
Onon 3:80837b470692 129 break;
Onon 3:80837b470692 130 case 5:
Onon 3:80837b470692 131 da = 1;
Onon 3:80837b470692 132 db = 0;
Onon 3:80837b470692 133 dc = 1;
Onon 3:80837b470692 134 dd = 1;
Onon 3:80837b470692 135 de = 0;
Onon 3:80837b470692 136 df = 1;
Onon 3:80837b470692 137 dg = 1;
Onon 3:80837b470692 138 break;
Onon 3:80837b470692 139 case 6:
Onon 3:80837b470692 140 da = 1;
Onon 3:80837b470692 141 db = 0;
Onon 3:80837b470692 142 dc = 1;
Onon 3:80837b470692 143 dd = 1;
Onon 3:80837b470692 144 de = 1;
Onon 3:80837b470692 145 df = 1;
Onon 3:80837b470692 146 dg = 1;
Onon 3:80837b470692 147 break;
Onon 3:80837b470692 148 case 7:
Onon 3:80837b470692 149 da = 1;
Onon 3:80837b470692 150 db = 1;
Onon 3:80837b470692 151 dc = 1;
Onon 3:80837b470692 152 dd = 0;
Onon 3:80837b470692 153 de = 0;
Onon 3:80837b470692 154 df = 0;
Onon 3:80837b470692 155 dg = 0;
Onon 3:80837b470692 156 break;
Onon 3:80837b470692 157 case 8:
Onon 3:80837b470692 158 da = 1;
Onon 3:80837b470692 159 db = 1;
Onon 3:80837b470692 160 dc = 1;
Onon 3:80837b470692 161 dd = 1;
Onon 3:80837b470692 162 de = 1;
Onon 3:80837b470692 163 df = 1;
Onon 3:80837b470692 164 dg = 1;
Onon 3:80837b470692 165 break;
Onon 3:80837b470692 166 case 9:
Onon 3:80837b470692 167 da = 1;
Onon 3:80837b470692 168 db = 1;
Onon 3:80837b470692 169 dc = 1;
Onon 3:80837b470692 170 dd = 1;
Onon 3:80837b470692 171 de = 0;
Onon 3:80837b470692 172 df = 1;
Onon 3:80837b470692 173 dg = 1;
Onon 3:80837b470692 174 break;
Onon 3:80837b470692 175 case 10:
Onon 3:80837b470692 176 da = 1;
Onon 3:80837b470692 177 db = 1;
Onon 3:80837b470692 178 dc = 1;
Onon 3:80837b470692 179 dd = 0;
Onon 3:80837b470692 180 de = 1;
Onon 3:80837b470692 181 df = 1;
Onon 3:80837b470692 182 dg = 1;
Onon 3:80837b470692 183 break;
Onon 3:80837b470692 184 case 11:
Onon 3:80837b470692 185 da = 0;
Onon 3:80837b470692 186 db = 0;
Onon 3:80837b470692 187 dc = 1;
Onon 3:80837b470692 188 dd = 1;
Onon 3:80837b470692 189 de = 1;
Onon 3:80837b470692 190 df = 1;
Onon 3:80837b470692 191 dg = 1;
Onon 3:80837b470692 192 break;
Onon 3:80837b470692 193 case 12:
Onon 3:80837b470692 194 da = 1;
Onon 3:80837b470692 195 db = 0;
Onon 3:80837b470692 196 dc = 0;
Onon 3:80837b470692 197 dd = 1;
Onon 3:80837b470692 198 de = 1;
Onon 3:80837b470692 199 df = 1;
Onon 3:80837b470692 200 dg = 0;
Onon 3:80837b470692 201 break;
Onon 3:80837b470692 202 case 13:
Onon 3:80837b470692 203 da = 0;
Onon 3:80837b470692 204 db = 1;
Onon 3:80837b470692 205 dc = 1;
Onon 3:80837b470692 206 dd = 1;
Onon 3:80837b470692 207 de = 1;
Onon 3:80837b470692 208 df = 0;
Onon 3:80837b470692 209 dg = 1;
Onon 3:80837b470692 210 break;
Onon 3:80837b470692 211 case 14:
Onon 3:80837b470692 212 da = 1;
Onon 3:80837b470692 213 db = 0;
Onon 3:80837b470692 214 dc = 0;
Onon 3:80837b470692 215 dd = 1;
Onon 3:80837b470692 216 de = 1;
Onon 3:80837b470692 217 df = 1;
Onon 3:80837b470692 218 dg = 1;
Onon 3:80837b470692 219 break;
Onon 3:80837b470692 220 case 15:
Onon 3:80837b470692 221 da = 1;
Onon 3:80837b470692 222 db = 0;
Onon 3:80837b470692 223 dc = 0;
Onon 3:80837b470692 224 dd = 0;
Onon 3:80837b470692 225 de = 1;
Onon 3:80837b470692 226 df = 1;
Onon 3:80837b470692 227 dg = 1;
Onon 3:80837b470692 228 break;
Onon 3:80837b470692 229 default:
Onon 3:80837b470692 230 da = 0;
Onon 3:80837b470692 231 db = 0;
Onon 3:80837b470692 232 dc = 0;
Onon 3:80837b470692 233 dd = 0;
Onon 3:80837b470692 234 de = 0;
Onon 3:80837b470692 235 df = 0;
Onon 3:80837b470692 236 dg = 0;
Onon 3:80837b470692 237 ddp = 0;
Onon 3:80837b470692 238 }
Onon 3:80837b470692 239 }
Onon 3:80837b470692 240
Bram122 5:e45681989201 241 void control() {
Bram122 5:e45681989201 242 while (true) {
Bram122 5:e45681989201 243 led1 = IN1;
Bram122 5:e45681989201 244 led2 = IN2;
Bram122 5:e45681989201 245 led3 = IN3;
Bram122 5:e45681989201 246 led4 = IN4;
Bram122 5:e45681989201 247 }
Bram122 5:e45681989201 248 }
Bram122 5:e45681989201 249
Bram122 5:e45681989201 250 void motorControl() {
Bram122 5:e45681989201 251 while(true) {
Bram122 6:c034f08291ab 252 if (sensorValue != 0) {
Bram122 6:c034f08291ab 253 IN1 = 1;
Bram122 6:c034f08291ab 254 IN2 = 0;
Bram122 6:c034f08291ab 255 IN3 = 1;
Bram122 6:c034f08291ab 256 IN4 = 0;
Bram122 6:c034f08291ab 257
Bram122 6:c034f08291ab 258 Thread::wait(speedcontrol - speedcontrol/sensorValue);
Bram122 6:c034f08291ab 259
Bram122 6:c034f08291ab 260 IN1 = 1;
Bram122 6:c034f08291ab 261 IN2 = 0;
Bram122 6:c034f08291ab 262 IN3 = 0;
Bram122 6:c034f08291ab 263 IN4 = 1;
Bram122 6:c034f08291ab 264
Bram122 6:c034f08291ab 265 Thread::wait(speedcontrol - speedcontrol/sensorValue);
Bram122 6:c034f08291ab 266
Bram122 6:c034f08291ab 267 IN1 = 0;
Bram122 6:c034f08291ab 268 IN2 = 1;
Bram122 6:c034f08291ab 269 IN3 = 0;
Bram122 6:c034f08291ab 270 IN4 = 1;
Bram122 6:c034f08291ab 271
Bram122 6:c034f08291ab 272 Thread::wait(speedcontrol - speedcontrol/sensorValue);
Bram122 6:c034f08291ab 273
Bram122 6:c034f08291ab 274 IN1 = 0;
Bram122 6:c034f08291ab 275 IN2 = 1;
Bram122 6:c034f08291ab 276 IN3 = 1;
Bram122 6:c034f08291ab 277 IN4 = 0;
Bram122 6:c034f08291ab 278
Bram122 6:c034f08291ab 279 Thread::wait(speedcontrol - speedcontrol/sensorValue);
Bram122 6:c034f08291ab 280 }
Bram122 5:e45681989201 281 }
Bram122 5:e45681989201 282 }
Bram122 5:e45681989201 283
Bram122 5:e45681989201 284 void displayNumber7Seg() {
Bram122 5:e45681989201 285 while (true) {
Bram122 5:e45681989201 286 float nr = sensorValue;
Bram122 5:e45681989201 287 nr *= 1000;
Bram122 5:e45681989201 288 int nrint = (int) nr;
Bram122 5:e45681989201 289 int digits[4];
Bram122 5:e45681989201 290 int i = 3;
Bram122 5:e45681989201 291 while (nrint >= 0) {
Bram122 5:e45681989201 292 int digit = nrint % 10;
Bram122 5:e45681989201 293 digits[i] = digit;
Bram122 5:e45681989201 294 if(i == 0) {
Bram122 5:e45681989201 295 break;
Bram122 5:e45681989201 296 }
Bram122 5:e45681989201 297 --i;
Bram122 5:e45681989201 298 nrint /= 10;
Bram122 5:e45681989201 299 }
Bram122 5:e45681989201 300 for(int i = 0; i < 4; i++) {
Bram122 5:e45681989201 301 light7Seg(digits[3-i], i);
Bram122 5:e45681989201 302 Thread::wait(100/24); //60Hz
Onon 3:80837b470692 303 }
Onon 3:80837b470692 304 }
Onon 3:80837b470692 305 }
Onon 2:c719944e080b 306
Bram122 4:325954591d0f 307 void samplePotentio() {
Bram122 4:325954591d0f 308 while (true) {
Bram122 4:325954591d0f 309 float samples[20];
Bram122 4:325954591d0f 310 float sum = 0;
Bram122 4:325954591d0f 311 for(int i = 0; i < 20; i++) {
Bram122 4:325954591d0f 312 samples[i] = a0.read();
Bram122 5:e45681989201 313 Thread::wait(5);
Bram122 4:325954591d0f 314 }
Bram122 5:e45681989201 315 for (int i = 0; i < 20; i++) {
Bram122 4:325954591d0f 316 sum = sum + samples[i];
Bram122 4:325954591d0f 317 }
Bram122 4:325954591d0f 318 sensorValue = sum / 20;
Bram122 4:325954591d0f 319 }
Bram122 4:325954591d0f 320 }
Bram122 4:325954591d0f 321
Onon 7:1c712013123b 322 void checkRFID() {
Onon 7:1c712013123b 323 while (true) {
Onon 7:1c712013123b 324
Onon 7:1c712013123b 325 // Look for new cards
Onon 7:1c712013123b 326 if ( ! RfChip.PICC_IsNewCardPresent())
Onon 7:1c712013123b 327 {
Onon 7:1c712013123b 328 Thread::wait(500);
Onon 7:1c712013123b 329 continue;
Onon 7:1c712013123b 330 }
Onon 7:1c712013123b 331
Onon 7:1c712013123b 332 // Select one of the cards
Onon 7:1c712013123b 333 if ( ! RfChip.PICC_ReadCardSerial())
Onon 7:1c712013123b 334 {
Onon 7:1c712013123b 335 Thread::wait(500);
Onon 7:1c712013123b 336 continue;
Onon 7:1c712013123b 337 }
Onon 7:1c712013123b 338
Onon 7:1c712013123b 339 // Print Card UID
Onon 7:1c712013123b 340 pc.printf("Card UID: ");
Onon 7:1c712013123b 341 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Onon 7:1c712013123b 342 {
Onon 7:1c712013123b 343 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Onon 7:1c712013123b 344 }
Onon 7:1c712013123b 345 pc.printf("\n\r");
Onon 7:1c712013123b 346
Onon 7:1c712013123b 347 // Print Card type
Onon 7:1c712013123b 348 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Onon 7:1c712013123b 349 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Onon 7:1c712013123b 350 Thread::wait(1000);
Onon 7:1c712013123b 351 }
Onon 7:1c712013123b 352 }
Onon 7:1c712013123b 353
Onon 7:1c712013123b 354
Onon 2:c719944e080b 355 int main(void) {
Onon 2:c719944e080b 356 pc.printf("Start");
Bram122 4:325954591d0f 357
Bram122 5:e45681989201 358 Thread thread_sample;
Bram122 5:e45681989201 359 thread_sample.start(samplePotentio);
Bram122 5:e45681989201 360
Bram122 5:e45681989201 361 Thread thread_control;
Bram122 5:e45681989201 362 thread_control.start(control);
Bram122 5:e45681989201 363
Bram122 5:e45681989201 364 Thread thread_motor(osPriorityRealtime);
Bram122 5:e45681989201 365 thread_motor.start(motorControl);
Bram122 5:e45681989201 366
Bram122 5:e45681989201 367 Thread thread_display(osPriorityRealtime);
Bram122 5:e45681989201 368 thread_display.start(displayNumber7Seg);
Bram122 4:325954591d0f 369
Onon 7:1c712013123b 370 Thread thread_rfid;
Onon 7:1c712013123b 371 thread_rfid.start(checkRFID);
Onon 2:c719944e080b 372 // Set debug UART speed
Onon 3:80837b470692 373 //pc.baud(115200);
Onon 2:c719944e080b 374
Onon 2:c719944e080b 375 // Init. RC522 Chip
Onon 2:c719944e080b 376 RfChip.PCD_Init();
Onon 3:80837b470692 377
Onon 3:80837b470692 378 dg0 = 0;
Onon 3:80837b470692 379 dg1 = 0;
Onon 3:80837b470692 380 dg2 = 0;
Onon 3:80837b470692 381 dg3 = 0;
Onon 2:c719944e080b 382
Bram122 5:e45681989201 383 while (true) {
Onon 3:80837b470692 384
Onon 2:c719944e080b 385 // Look for new cards
Onon 3:80837b470692 386 /*if ( ! RfChip.PICC_IsNewCardPresent())
Onon 2:c719944e080b 387 {
Onon 2:c719944e080b 388 wait_ms(500);
Onon 2:c719944e080b 389 continue;
Onon 2:c719944e080b 390 }
Onon 2:c719944e080b 391
Onon 2:c719944e080b 392 // Select one of the cards
Onon 2:c719944e080b 393 if ( ! RfChip.PICC_ReadCardSerial())
Onon 2:c719944e080b 394 {
Onon 2:c719944e080b 395 wait_ms(500);
Onon 2:c719944e080b 396 continue;
Onon 2:c719944e080b 397 }
Onon 2:c719944e080b 398
Onon 2:c719944e080b 399 // Print Card UID
Onon 2:c719944e080b 400 pc.printf("Card UID: ");
Onon 2:c719944e080b 401 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Onon 2:c719944e080b 402 {
Onon 2:c719944e080b 403 led = 1;
Onon 2:c719944e080b 404 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Onon 2:c719944e080b 405 }
Onon 2:c719944e080b 406 pc.printf("\n\r");
Onon 2:c719944e080b 407
Onon 2:c719944e080b 408 // Print Card type
Onon 2:c719944e080b 409 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Onon 2:c719944e080b 410 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Onon 2:c719944e080b 411 wait_ms(1000);
Onon 3:80837b470692 412 }*/
Onon 3:80837b470692 413 }
Onon 2:c719944e080b 414 }
Onon 3:80837b470692 415
Onon 3:80837b470692 416
Onon 3:80837b470692 417
Onon 3:80837b470692 418