Attila Fazekas / Mbed 2 deprecated RFID_with_ds3231

Dependencies:   mbed beep ds3231 RC522

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  #include "mbed.h"
00002  #include "MFRC522.h"
00003  #include "beep.h"
00004 #include "ds3231.h"
00005  
00006  //KL25Z Pins for MFRC522 SPI3 interface
00007  #define SPI_MOSI    PB_5
00008  #define SPI_MISO    PB_4
00009  #define SPI_SCLK    PB_3
00010  #define SPI_CS      PA_10
00011  
00012  #define address     0xd1
00013  // KL25Z Pin for MFRC522 reset
00014  #define MF_RESET    PB_1
00015  
00016  DigitalOut LedRed   (PC_5);
00017  DigitalOut LedGreen (PC_6);
00018  DigitalOut led (LED1);
00019  
00020  uint16_t cards[2][4] = {
00021                         {0x51, 0xf2, 0x05, 0x20},
00022                         {0x01, 0x02, 0x03, 0x04}
00023                         };
00024  
00025  Beep buzzer(PC_8);
00026  
00027  Serial     pc(USBTX, USBRX);
00028  MFRC522    RfChip   (PB_5, PB_4, PB_3, PB_10, PA_8);
00029  
00030  I2C rtc(PB_9, PB_8);
00031  
00032  void guttentag();
00033  int get_hour();
00034  int get_min();
00035  int set_time(int hour, int min);
00036  
00037  int main(void) {
00038    // Set debug UART speed
00039    pc.baud(115200);
00040  
00041    // Init. RC522 Chip
00042    RfChip.PCD_Init();
00043    
00044    rtc.frequency(100000);
00045    
00046    //set_time(0x23,0x58);
00047    int joez = 0;
00048    while (1) {
00049      
00050  
00051      // Look for new cards
00052      if ( ! RfChip.PICC_IsNewCardPresent())
00053      {
00054        
00055        wait_ms(100);
00056        led = !led;
00057        LedGreen = LedRed;
00058        LedRed = !LedRed;
00059        
00060        
00061        //pc.printf("%d:%d\n", get_hour(), get_min());
00062        continue;
00063      }
00064  
00065      LedRed   = 0;
00066  
00067      // Select one of the cards
00068      if ( ! RfChip.PICC_ReadCardSerial())
00069      {
00070        wait_ms(500);
00071        
00072        
00073        
00074        continue;
00075      }
00076     //wait(0.5);
00077     LedRed   = 0;
00078      LedGreen = 1;
00079      buzzer.beep(5000,0.2);
00080  
00081      // Print Card UID
00082      pc.printf("Card UID: ");
00083      for (uint8_t i = 0; i < RfChip.uid.size; i++)
00084      {
00085        pc.printf(" %X  ", RfChip.uid.uidByte[i]);
00086        if (RfChip.uid.uidByte[i] == cards[0][i]) joez++;
00087      }
00088      
00089      pc.printf("\n\r");
00090     
00091     if(joez == 4) {
00092         wait(0.8);
00093         buzzer.beep(4900, 3);
00094         pc.printf("\nbelephet\n");
00095         wait(3);
00096     }
00097     
00098     
00099     else{
00100         LedGreen= 0;
00101         for(int i = 0; i < 6; i++){
00102             buzzer.beep(5000, 0.5);
00103             LedRed = !LedRed;
00104             wait(1);
00105         }
00106     }
00107     joez = 0;
00108     LedRed = 1;
00109     
00110     char buf[3];    
00111         char data[10];
00112         buf[0] = 0x0; 
00113          rtc.write(address, buf, 1, true);
00114         rtc.read(address, data, 8, false);         
00115         for ( int i = 0; i < 8; i++){
00116             pc.printf("     %02X", data[i]);
00117         }
00118      
00119      //wait_ms(3000);
00120      
00121    }
00122  }
00123  
00124  void guttentag(){
00125      
00126 }
00127 int set_time(int hour, int min){
00128     char buf[3];
00129     buf[0] = 0x01;
00130     buf[1] = min;
00131     rtc.write(0xd0, buf, 2, false);
00132     buf[0] = 0x02;
00133     buf[1] = hour;
00134     rtc.write(0xd0, buf, 2, false);
00135     
00136     return 1;
00137 }
00138 int get_min()
00139 {
00140     char buf[3];    
00141     char data[10];
00142     int min_val; 
00143     int min_hex; 
00144     buf[0] = 0x0;    
00145 
00146     rtc.write(address, buf, 1, true);
00147     rtc.read(address, data, 8, false);         
00148     min_hex = data[1];
00149 
00150     //-------------------------------------
00151     //----transfer hexa data to decimal----
00152     //-------------------------------------
00153     
00154     if ( min_hex >= 0x00 & min_hex < 0x10)
00155     {
00156         min_val = min_hex;
00157     }
00158     if ( min_hex >= 0x10 & min_hex < 0x20)
00159     {
00160         min_val = min_hex - 6;
00161     }
00162     if ( min_hex >=0x20 & min_hex <= 0x29)
00163     {
00164         min_val = min_hex - 12;
00165     }
00166     if ( min_hex >=0x30 & min_hex <= 0x39)
00167     {
00168         min_val = min_hex - 18;
00169     }
00170     if ( min_hex >=0x40 & min_hex <= 0x49)
00171     {
00172         min_val = min_hex - 24;
00173     }
00174     if ( min_hex >=0x50  & min_hex <= 0x59)
00175     {
00176         min_val = min_hex - 30;
00177     }
00178       
00179     return min_val;
00180 }
00181 
00182 int get_hour()
00183 {
00184     
00185     char buf[3];    
00186     char data[10];
00187     int hour_val; 
00188     int hour_hex; 
00189     buf[0] = 0x00;    
00190 
00191     rtc.write(address, buf, 1, true);
00192     rtc.read(address, data, 8, false);         
00193     hour_hex = data[2];
00194 
00195     //-------------------------------------
00196     //----transfer hexa data to decimal----
00197     //-------------------------------------
00198     
00199     if ( hour_hex >= 0x00 & hour_hex < 0x10)
00200     {
00201         hour_val = hour_hex;
00202     }
00203     if ( hour_hex >= 0x10 & hour_hex < 0x20)
00204     {
00205         hour_val = hour_hex - 6;
00206     }
00207     if ( hour_hex >=0x20 & hour_hex <= 0x24)
00208     {
00209         hour_val = hour_hex - 12;
00210     }
00211     
00212     return hour_val;
00213 }