Its a program

Dependencies:   mbed beep ds3231 RC522

Committer:
attila0970
Date:
Sun Apr 05 13:51:08 2020 +0000
Revision:
0:9f3afc173a43
asd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
attila0970 0:9f3afc173a43 1 #include "mbed.h"
attila0970 0:9f3afc173a43 2 #include "MFRC522.h"
attila0970 0:9f3afc173a43 3 #include "beep.h"
attila0970 0:9f3afc173a43 4 #include "ds3231.h"
attila0970 0:9f3afc173a43 5
attila0970 0:9f3afc173a43 6 //KL25Z Pins for MFRC522 SPI3 interface
attila0970 0:9f3afc173a43 7 #define SPI_MOSI PB_5
attila0970 0:9f3afc173a43 8 #define SPI_MISO PB_4
attila0970 0:9f3afc173a43 9 #define SPI_SCLK PB_3
attila0970 0:9f3afc173a43 10 #define SPI_CS PA_10
attila0970 0:9f3afc173a43 11
attila0970 0:9f3afc173a43 12 #define address 0xd1
attila0970 0:9f3afc173a43 13 // KL25Z Pin for MFRC522 reset
attila0970 0:9f3afc173a43 14 #define MF_RESET PB_1
attila0970 0:9f3afc173a43 15
attila0970 0:9f3afc173a43 16 DigitalOut LedRed (PC_5);
attila0970 0:9f3afc173a43 17 DigitalOut LedGreen (PC_6);
attila0970 0:9f3afc173a43 18 DigitalOut led (LED1);
attila0970 0:9f3afc173a43 19
attila0970 0:9f3afc173a43 20 uint16_t cards[2][4] = {
attila0970 0:9f3afc173a43 21 {0x51, 0xf2, 0x05, 0x20},
attila0970 0:9f3afc173a43 22 {0x01, 0x02, 0x03, 0x04}
attila0970 0:9f3afc173a43 23 };
attila0970 0:9f3afc173a43 24
attila0970 0:9f3afc173a43 25 Beep buzzer(PC_8);
attila0970 0:9f3afc173a43 26
attila0970 0:9f3afc173a43 27 Serial pc(USBTX, USBRX);
attila0970 0:9f3afc173a43 28 MFRC522 RfChip (PB_5, PB_4, PB_3, PB_10, PA_8);
attila0970 0:9f3afc173a43 29
attila0970 0:9f3afc173a43 30 I2C rtc(PB_9, PB_8);
attila0970 0:9f3afc173a43 31
attila0970 0:9f3afc173a43 32 void guttentag();
attila0970 0:9f3afc173a43 33 int get_hour();
attila0970 0:9f3afc173a43 34 int get_min();
attila0970 0:9f3afc173a43 35 int set_time(int hour, int min);
attila0970 0:9f3afc173a43 36
attila0970 0:9f3afc173a43 37 int main(void) {
attila0970 0:9f3afc173a43 38 // Set debug UART speed
attila0970 0:9f3afc173a43 39 pc.baud(115200);
attila0970 0:9f3afc173a43 40
attila0970 0:9f3afc173a43 41 // Init. RC522 Chip
attila0970 0:9f3afc173a43 42 RfChip.PCD_Init();
attila0970 0:9f3afc173a43 43
attila0970 0:9f3afc173a43 44 rtc.frequency(100000);
attila0970 0:9f3afc173a43 45
attila0970 0:9f3afc173a43 46 //set_time(0x23,0x58);
attila0970 0:9f3afc173a43 47 int joez = 0;
attila0970 0:9f3afc173a43 48 while (1) {
attila0970 0:9f3afc173a43 49
attila0970 0:9f3afc173a43 50
attila0970 0:9f3afc173a43 51 // Look for new cards
attila0970 0:9f3afc173a43 52 if ( ! RfChip.PICC_IsNewCardPresent())
attila0970 0:9f3afc173a43 53 {
attila0970 0:9f3afc173a43 54
attila0970 0:9f3afc173a43 55 wait_ms(100);
attila0970 0:9f3afc173a43 56 led = !led;
attila0970 0:9f3afc173a43 57 LedGreen = LedRed;
attila0970 0:9f3afc173a43 58 LedRed = !LedRed;
attila0970 0:9f3afc173a43 59
attila0970 0:9f3afc173a43 60
attila0970 0:9f3afc173a43 61 //pc.printf("%d:%d\n", get_hour(), get_min());
attila0970 0:9f3afc173a43 62 continue;
attila0970 0:9f3afc173a43 63 }
attila0970 0:9f3afc173a43 64
attila0970 0:9f3afc173a43 65 LedRed = 0;
attila0970 0:9f3afc173a43 66
attila0970 0:9f3afc173a43 67 // Select one of the cards
attila0970 0:9f3afc173a43 68 if ( ! RfChip.PICC_ReadCardSerial())
attila0970 0:9f3afc173a43 69 {
attila0970 0:9f3afc173a43 70 wait_ms(500);
attila0970 0:9f3afc173a43 71
attila0970 0:9f3afc173a43 72
attila0970 0:9f3afc173a43 73
attila0970 0:9f3afc173a43 74 continue;
attila0970 0:9f3afc173a43 75 }
attila0970 0:9f3afc173a43 76 //wait(0.5);
attila0970 0:9f3afc173a43 77 LedRed = 0;
attila0970 0:9f3afc173a43 78 LedGreen = 1;
attila0970 0:9f3afc173a43 79 buzzer.beep(5000,0.2);
attila0970 0:9f3afc173a43 80
attila0970 0:9f3afc173a43 81 // Print Card UID
attila0970 0:9f3afc173a43 82 pc.printf("Card UID: ");
attila0970 0:9f3afc173a43 83 for (uint8_t i = 0; i < RfChip.uid.size; i++)
attila0970 0:9f3afc173a43 84 {
attila0970 0:9f3afc173a43 85 pc.printf(" %X ", RfChip.uid.uidByte[i]);
attila0970 0:9f3afc173a43 86 if (RfChip.uid.uidByte[i] == cards[0][i]) joez++;
attila0970 0:9f3afc173a43 87 }
attila0970 0:9f3afc173a43 88
attila0970 0:9f3afc173a43 89 pc.printf("\n\r");
attila0970 0:9f3afc173a43 90
attila0970 0:9f3afc173a43 91 if(joez == 4) {
attila0970 0:9f3afc173a43 92 wait(0.8);
attila0970 0:9f3afc173a43 93 buzzer.beep(4900, 3);
attila0970 0:9f3afc173a43 94 pc.printf("\nbelephet\n");
attila0970 0:9f3afc173a43 95 wait(3);
attila0970 0:9f3afc173a43 96 }
attila0970 0:9f3afc173a43 97
attila0970 0:9f3afc173a43 98
attila0970 0:9f3afc173a43 99 else{
attila0970 0:9f3afc173a43 100 LedGreen= 0;
attila0970 0:9f3afc173a43 101 for(int i = 0; i < 6; i++){
attila0970 0:9f3afc173a43 102 buzzer.beep(5000, 0.5);
attila0970 0:9f3afc173a43 103 LedRed = !LedRed;
attila0970 0:9f3afc173a43 104 wait(1);
attila0970 0:9f3afc173a43 105 }
attila0970 0:9f3afc173a43 106 }
attila0970 0:9f3afc173a43 107 joez = 0;
attila0970 0:9f3afc173a43 108 LedRed = 1;
attila0970 0:9f3afc173a43 109
attila0970 0:9f3afc173a43 110 char buf[3];
attila0970 0:9f3afc173a43 111 char data[10];
attila0970 0:9f3afc173a43 112 buf[0] = 0x0;
attila0970 0:9f3afc173a43 113 rtc.write(address, buf, 1, true);
attila0970 0:9f3afc173a43 114 rtc.read(address, data, 8, false);
attila0970 0:9f3afc173a43 115 for ( int i = 0; i < 8; i++){
attila0970 0:9f3afc173a43 116 pc.printf(" %02X", data[i]);
attila0970 0:9f3afc173a43 117 }
attila0970 0:9f3afc173a43 118
attila0970 0:9f3afc173a43 119 //wait_ms(3000);
attila0970 0:9f3afc173a43 120
attila0970 0:9f3afc173a43 121 }
attila0970 0:9f3afc173a43 122 }
attila0970 0:9f3afc173a43 123
attila0970 0:9f3afc173a43 124 void guttentag(){
attila0970 0:9f3afc173a43 125
attila0970 0:9f3afc173a43 126 }
attila0970 0:9f3afc173a43 127 int set_time(int hour, int min){
attila0970 0:9f3afc173a43 128 char buf[3];
attila0970 0:9f3afc173a43 129 buf[0] = 0x01;
attila0970 0:9f3afc173a43 130 buf[1] = min;
attila0970 0:9f3afc173a43 131 rtc.write(0xd0, buf, 2, false);
attila0970 0:9f3afc173a43 132 buf[0] = 0x02;
attila0970 0:9f3afc173a43 133 buf[1] = hour;
attila0970 0:9f3afc173a43 134 rtc.write(0xd0, buf, 2, false);
attila0970 0:9f3afc173a43 135
attila0970 0:9f3afc173a43 136 return 1;
attila0970 0:9f3afc173a43 137 }
attila0970 0:9f3afc173a43 138 int get_min()
attila0970 0:9f3afc173a43 139 {
attila0970 0:9f3afc173a43 140 char buf[3];
attila0970 0:9f3afc173a43 141 char data[10];
attila0970 0:9f3afc173a43 142 int min_val;
attila0970 0:9f3afc173a43 143 int min_hex;
attila0970 0:9f3afc173a43 144 buf[0] = 0x0;
attila0970 0:9f3afc173a43 145
attila0970 0:9f3afc173a43 146 rtc.write(address, buf, 1, true);
attila0970 0:9f3afc173a43 147 rtc.read(address, data, 8, false);
attila0970 0:9f3afc173a43 148 min_hex = data[1];
attila0970 0:9f3afc173a43 149
attila0970 0:9f3afc173a43 150 //-------------------------------------
attila0970 0:9f3afc173a43 151 //----transfer hexa data to decimal----
attila0970 0:9f3afc173a43 152 //-------------------------------------
attila0970 0:9f3afc173a43 153
attila0970 0:9f3afc173a43 154 if ( min_hex >= 0x00 & min_hex < 0x10)
attila0970 0:9f3afc173a43 155 {
attila0970 0:9f3afc173a43 156 min_val = min_hex;
attila0970 0:9f3afc173a43 157 }
attila0970 0:9f3afc173a43 158 if ( min_hex >= 0x10 & min_hex < 0x20)
attila0970 0:9f3afc173a43 159 {
attila0970 0:9f3afc173a43 160 min_val = min_hex - 6;
attila0970 0:9f3afc173a43 161 }
attila0970 0:9f3afc173a43 162 if ( min_hex >=0x20 & min_hex <= 0x29)
attila0970 0:9f3afc173a43 163 {
attila0970 0:9f3afc173a43 164 min_val = min_hex - 12;
attila0970 0:9f3afc173a43 165 }
attila0970 0:9f3afc173a43 166 if ( min_hex >=0x30 & min_hex <= 0x39)
attila0970 0:9f3afc173a43 167 {
attila0970 0:9f3afc173a43 168 min_val = min_hex - 18;
attila0970 0:9f3afc173a43 169 }
attila0970 0:9f3afc173a43 170 if ( min_hex >=0x40 & min_hex <= 0x49)
attila0970 0:9f3afc173a43 171 {
attila0970 0:9f3afc173a43 172 min_val = min_hex - 24;
attila0970 0:9f3afc173a43 173 }
attila0970 0:9f3afc173a43 174 if ( min_hex >=0x50 & min_hex <= 0x59)
attila0970 0:9f3afc173a43 175 {
attila0970 0:9f3afc173a43 176 min_val = min_hex - 30;
attila0970 0:9f3afc173a43 177 }
attila0970 0:9f3afc173a43 178
attila0970 0:9f3afc173a43 179 return min_val;
attila0970 0:9f3afc173a43 180 }
attila0970 0:9f3afc173a43 181
attila0970 0:9f3afc173a43 182 int get_hour()
attila0970 0:9f3afc173a43 183 {
attila0970 0:9f3afc173a43 184
attila0970 0:9f3afc173a43 185 char buf[3];
attila0970 0:9f3afc173a43 186 char data[10];
attila0970 0:9f3afc173a43 187 int hour_val;
attila0970 0:9f3afc173a43 188 int hour_hex;
attila0970 0:9f3afc173a43 189 buf[0] = 0x00;
attila0970 0:9f3afc173a43 190
attila0970 0:9f3afc173a43 191 rtc.write(address, buf, 1, true);
attila0970 0:9f3afc173a43 192 rtc.read(address, data, 8, false);
attila0970 0:9f3afc173a43 193 hour_hex = data[2];
attila0970 0:9f3afc173a43 194
attila0970 0:9f3afc173a43 195 //-------------------------------------
attila0970 0:9f3afc173a43 196 //----transfer hexa data to decimal----
attila0970 0:9f3afc173a43 197 //-------------------------------------
attila0970 0:9f3afc173a43 198
attila0970 0:9f3afc173a43 199 if ( hour_hex >= 0x00 & hour_hex < 0x10)
attila0970 0:9f3afc173a43 200 {
attila0970 0:9f3afc173a43 201 hour_val = hour_hex;
attila0970 0:9f3afc173a43 202 }
attila0970 0:9f3afc173a43 203 if ( hour_hex >= 0x10 & hour_hex < 0x20)
attila0970 0:9f3afc173a43 204 {
attila0970 0:9f3afc173a43 205 hour_val = hour_hex - 6;
attila0970 0:9f3afc173a43 206 }
attila0970 0:9f3afc173a43 207 if ( hour_hex >=0x20 & hour_hex <= 0x24)
attila0970 0:9f3afc173a43 208 {
attila0970 0:9f3afc173a43 209 hour_val = hour_hex - 12;
attila0970 0:9f3afc173a43 210 }
attila0970 0:9f3afc173a43 211
attila0970 0:9f3afc173a43 212 return hour_val;
attila0970 0:9f3afc173a43 213 }