project van rtos

Dependencies:   mbed-os

Fork of MFRC522 by Martin Olejar

Committer:
Onon
Date:
Thu May 17 11:53:09 2018 +0000
Revision:
9:f1f71e42e267
Parent:
8:4638e3f14c41
Child:
10:65c046928e5d
bram zijn slechte code opgelost :)

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 IN1 = 1;
Bram122 6:c034f08291ab 253 IN2 = 0;
Bram122 6:c034f08291ab 254 IN3 = 1;
Bram122 6:c034f08291ab 255 IN4 = 0;
Bram122 6:c034f08291ab 256
Onon 9:f1f71e42e267 257 //Thread::wait(speedcontrol - speedcontrol/sensorValue);
Onon 9:f1f71e42e267 258 Thread::wait(100-100*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
Onon 9:f1f71e42e267 265 //Thread::wait(speedcontrol - speedcontrol/sensorValue);
Onon 9:f1f71e42e267 266 Thread::wait(100-100*sensorValue);
Bram122 6:c034f08291ab 267
Bram122 6:c034f08291ab 268 IN1 = 0;
Bram122 6:c034f08291ab 269 IN2 = 1;
Bram122 6:c034f08291ab 270 IN3 = 0;
Bram122 6:c034f08291ab 271 IN4 = 1;
Bram122 6:c034f08291ab 272
Onon 9:f1f71e42e267 273 //Thread::wait(speedcontrol - speedcontrol/sensorValue);
Onon 9:f1f71e42e267 274 Thread::wait(100-100*sensorValue);
Bram122 6:c034f08291ab 275
Bram122 6:c034f08291ab 276 IN1 = 0;
Bram122 6:c034f08291ab 277 IN2 = 1;
Bram122 6:c034f08291ab 278 IN3 = 1;
Bram122 6:c034f08291ab 279 IN4 = 0;
Bram122 6:c034f08291ab 280
Onon 9:f1f71e42e267 281 //Thread::wait(speedcontrol - speedcontrol/sensorValue);
Onon 9:f1f71e42e267 282 Thread::wait(100-100*sensorValue);
Bram122 5:e45681989201 283 }
Bram122 5:e45681989201 284 }
Bram122 5:e45681989201 285
Bram122 5:e45681989201 286 void displayNumber7Seg() {
Bram122 5:e45681989201 287 while (true) {
Bram122 5:e45681989201 288 float nr = sensorValue;
Bram122 5:e45681989201 289 nr *= 1000;
Bram122 5:e45681989201 290 int nrint = (int) nr;
Bram122 5:e45681989201 291 int digits[4];
Bram122 5:e45681989201 292 int i = 3;
Bram122 5:e45681989201 293 while (nrint >= 0) {
Bram122 5:e45681989201 294 int digit = nrint % 10;
Bram122 5:e45681989201 295 digits[i] = digit;
Bram122 5:e45681989201 296 if(i == 0) {
Bram122 5:e45681989201 297 break;
Bram122 5:e45681989201 298 }
Bram122 5:e45681989201 299 --i;
Bram122 5:e45681989201 300 nrint /= 10;
Bram122 5:e45681989201 301 }
Bram122 5:e45681989201 302 for(int i = 0; i < 4; i++) {
Bram122 5:e45681989201 303 light7Seg(digits[3-i], i);
Bram122 5:e45681989201 304 Thread::wait(100/24); //60Hz
Onon 3:80837b470692 305 }
Onon 3:80837b470692 306 }
Onon 3:80837b470692 307 }
Onon 2:c719944e080b 308
Bram122 4:325954591d0f 309 void samplePotentio() {
Bram122 4:325954591d0f 310 while (true) {
Onon 9:f1f71e42e267 311 pc.printf("ik sample. %f ", sensorValue);
Bram122 4:325954591d0f 312 float samples[20];
Bram122 4:325954591d0f 313 float sum = 0;
Bram122 4:325954591d0f 314 for(int i = 0; i < 20; i++) {
Bram122 4:325954591d0f 315 samples[i] = a0.read();
Bram122 5:e45681989201 316 Thread::wait(5);
Bram122 4:325954591d0f 317 }
Bram122 5:e45681989201 318 for (int i = 0; i < 20; i++) {
Bram122 4:325954591d0f 319 sum = sum + samples[i];
Bram122 4:325954591d0f 320 }
Bram122 4:325954591d0f 321 sensorValue = sum / 20;
Bram122 4:325954591d0f 322 }
Bram122 4:325954591d0f 323 }
Bram122 4:325954591d0f 324
Onon 9:f1f71e42e267 325
Onon 7:1c712013123b 326 void checkRFID() {
Onon 9:f1f71e42e267 327 // Init. RC522 Chip
Onon 9:f1f71e42e267 328 RfChip.PCD_Init();
Onon 7:1c712013123b 329 while (true) {
Onon 7:1c712013123b 330
Onon 7:1c712013123b 331 // Look for new cards
Onon 7:1c712013123b 332 if ( ! RfChip.PICC_IsNewCardPresent())
Onon 7:1c712013123b 333 {
Onon 7:1c712013123b 334 Thread::wait(500);
Onon 7:1c712013123b 335 continue;
Onon 7:1c712013123b 336 }
Onon 7:1c712013123b 337
Onon 7:1c712013123b 338 // Select one of the cards
Onon 7:1c712013123b 339 if ( ! RfChip.PICC_ReadCardSerial())
Onon 7:1c712013123b 340 {
Onon 7:1c712013123b 341 Thread::wait(500);
Onon 7:1c712013123b 342 continue;
Onon 7:1c712013123b 343 }
Onon 7:1c712013123b 344
Onon 7:1c712013123b 345 // Print Card UID
Onon 7:1c712013123b 346 pc.printf("Card UID: ");
Onon 7:1c712013123b 347 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Onon 7:1c712013123b 348 {
Onon 9:f1f71e42e267 349 pc.printf(" %X02 ", RfChip.uid.uidByte[i]);
Onon 7:1c712013123b 350 }
Onon 7:1c712013123b 351 pc.printf("\n\r");
Onon 7:1c712013123b 352
Onon 7:1c712013123b 353 // Print Card type
Onon 7:1c712013123b 354 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Onon 7:1c712013123b 355 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Onon 7:1c712013123b 356 Thread::wait(1000);
Onon 7:1c712013123b 357 }
Onon 7:1c712013123b 358 }
Onon 7:1c712013123b 359
Onon 7:1c712013123b 360
Onon 2:c719944e080b 361 int main(void) {
Onon 2:c719944e080b 362 pc.printf("Start");
Bram122 4:325954591d0f 363
Bram122 5:e45681989201 364 Thread thread_sample;
Bram122 5:e45681989201 365 thread_sample.start(samplePotentio);
Bram122 5:e45681989201 366
Bram122 5:e45681989201 367 Thread thread_control;
Bram122 5:e45681989201 368 thread_control.start(control);
Bram122 5:e45681989201 369
Bram122 5:e45681989201 370 Thread thread_motor(osPriorityRealtime);
Bram122 5:e45681989201 371 thread_motor.start(motorControl);
Bram122 5:e45681989201 372
Bram122 5:e45681989201 373 Thread thread_display(osPriorityRealtime);
Bram122 5:e45681989201 374 thread_display.start(displayNumber7Seg);
Bram122 4:325954591d0f 375
Onon 7:1c712013123b 376 Thread thread_rfid;
Onon 7:1c712013123b 377 thread_rfid.start(checkRFID);
Onon 2:c719944e080b 378 // Set debug UART speed
Onon 3:80837b470692 379 //pc.baud(115200);
Onon 3:80837b470692 380
Onon 3:80837b470692 381 dg0 = 0;
Onon 3:80837b470692 382 dg1 = 0;
Onon 3:80837b470692 383 dg2 = 0;
Onon 3:80837b470692 384 dg3 = 0;
Onon 2:c719944e080b 385
Bram122 5:e45681989201 386 while (true) {
Onon 3:80837b470692 387
Onon 2:c719944e080b 388 // Look for new cards
Onon 3:80837b470692 389 /*if ( ! RfChip.PICC_IsNewCardPresent())
Onon 2:c719944e080b 390 {
Onon 2:c719944e080b 391 wait_ms(500);
Onon 2:c719944e080b 392 continue;
Onon 2:c719944e080b 393 }
Onon 2:c719944e080b 394
Onon 2:c719944e080b 395 // Select one of the cards
Onon 2:c719944e080b 396 if ( ! RfChip.PICC_ReadCardSerial())
Onon 2:c719944e080b 397 {
Onon 2:c719944e080b 398 wait_ms(500);
Onon 2:c719944e080b 399 continue;
Onon 2:c719944e080b 400 }
Onon 2:c719944e080b 401
Onon 2:c719944e080b 402 // Print Card UID
Onon 2:c719944e080b 403 pc.printf("Card UID: ");
Onon 2:c719944e080b 404 for (uint8_t i = 0; i < RfChip.uid.size; i++)
Onon 2:c719944e080b 405 {
Onon 2:c719944e080b 406 led = 1;
Onon 2:c719944e080b 407 pc.printf(" %X02", RfChip.uid.uidByte[i]);
Onon 2:c719944e080b 408 }
Onon 2:c719944e080b 409 pc.printf("\n\r");
Onon 2:c719944e080b 410
Onon 2:c719944e080b 411 // Print Card type
Onon 2:c719944e080b 412 uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
Onon 2:c719944e080b 413 pc.printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
Onon 2:c719944e080b 414 wait_ms(1000);
Onon 3:80837b470692 415 }*/
Onon 3:80837b470692 416 }
Onon 2:c719944e080b 417 }
Onon 3:80837b470692 418
Onon 3:80837b470692 419
Onon 3:80837b470692 420
Onon 3:80837b470692 421