![](/media/cache/group/nUCLEO.png.50x50_q85.jpg)
hmc5883l
Dependencies: HMC5883L LCD4884 MFRC522 eeprom mbed
project.cpp@4:b56a8a051fdd, 2015-12-08 (annotated)
- Committer:
- 1918june
- Date:
- Tue Dec 08 16:11:25 2015 +0000
- Revision:
- 4:b56a8a051fdd
- Parent:
- 3:987e54d48f77
- Child:
- 5:4af4865f462b
csada
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
1918june | 0:930d2bc16788 | 1 | #include "mbed.h" |
1918june | 0:930d2bc16788 | 2 | #include "LCD4884.h" |
1918june | 0:930d2bc16788 | 3 | #include "MFRC522.h" |
1918june | 0:930d2bc16788 | 4 | #include "eeprom.h" |
1918june | 0:930d2bc16788 | 5 | #include "string.h" |
1918june | 0:930d2bc16788 | 6 | |
1918june | 0:930d2bc16788 | 7 | |
1918june | 0:930d2bc16788 | 8 | // KL25Z Pin for MFRC522 reset |
1918june | 0:930d2bc16788 | 9 | #define MF_RESET D9 |
1918june | 0:930d2bc16788 | 10 | |
1918june | 0:930d2bc16788 | 11 | |
1918june | 0:930d2bc16788 | 12 | EEPROM eeprom(I2C_SDA,I2C_SCL,0); |
1918june | 0:930d2bc16788 | 13 | Serial pc(SERIAL_TX,SERIAL_RX); |
1918june | 0:930d2bc16788 | 14 | LCD4884 lcd; |
1918june | 0:930d2bc16788 | 15 | MFRC522 RFID(D11, D12, D13, D10, MF_RESET); |
1918june | 3:987e54d48f77 | 16 | InterruptIn Sw1(PC_5); |
1918june | 3:987e54d48f77 | 17 | AnalogIn Rnd(A0); |
1918june | 3:987e54d48f77 | 18 | DigitalIn Switch1(PC_5); |
1918june | 3:987e54d48f77 | 19 | DigitalIn Switch2(PC_6); |
1918june | 3:987e54d48f77 | 20 | InterruptIn Sw2(PC_6); |
1918june | 4:b56a8a051fdd | 21 | |
1918june | 3:987e54d48f77 | 22 | uint8_t mode=0; |
1918june | 3:987e54d48f77 | 23 | uint8_t selectitem=0,place=0; |
1918june | 0:930d2bc16788 | 24 | |
1918june | 3:987e54d48f77 | 25 | void ItrSw1() |
1918june | 3:987e54d48f77 | 26 | { |
1918june | 3:987e54d48f77 | 27 | mode=1; |
1918june | 3:987e54d48f77 | 28 | } |
1918june | 3:987e54d48f77 | 29 | |
1918june | 3:987e54d48f77 | 30 | void ItrSw2() |
1918june | 3:987e54d48f77 | 31 | { |
1918june | 3:987e54d48f77 | 32 | mode=2; |
1918june | 3:987e54d48f77 | 33 | } |
1918june | 3:987e54d48f77 | 34 | |
1918june | 3:987e54d48f77 | 35 | void ItrSw3() |
1918june | 3:987e54d48f77 | 36 | { |
1918june | 3:987e54d48f77 | 37 | mode=0; |
1918june | 3:987e54d48f77 | 38 | } |
1918june | 3:987e54d48f77 | 39 | void printstring(int x,int y,string s,int n,int m) |
1918june | 3:987e54d48f77 | 40 | { |
1918june | 2:38376c7c1685 | 41 | int k=0; |
1918june | 2:38376c7c1685 | 42 | lcd.LCD_set_XY(x,y); |
1918june | 3:987e54d48f77 | 43 | while(k<n) { |
1918june | 3:987e54d48f77 | 44 | lcd.LCD_write_char(s[k],m); |
1918june | 3:987e54d48f77 | 45 | k++; |
1918june | 2:38376c7c1685 | 46 | } |
1918june | 2:38376c7c1685 | 47 | } |
1918june | 0:930d2bc16788 | 48 | |
1918june | 0:930d2bc16788 | 49 | int main() |
1918june | 0:930d2bc16788 | 50 | { |
1918june | 0:930d2bc16788 | 51 | pc.baud(9600); |
1918june | 3:987e54d48f77 | 52 | Sw1.rise(&ItrSw1); |
1918june | 3:987e54d48f77 | 53 | Sw2.rise(&ItrSw2); |
1918june | 4:b56a8a051fdd | 54 | |
1918june | 3:987e54d48f77 | 55 | string NameJune="Natthanicha",recieveNameJune,SurJune="Jamroonpan",recieveSurJune,IdJune="57340500023",recieveIdJune; //June start @ 1-36 |
1918june | 3:987e54d48f77 | 56 | string NameO="Sirawat",recieveNameO,SurO="Sok",recieveSurO,IdO="57340500071",recieveIdO; |
1918june | 3:987e54d48f77 | 57 | uint8_t UID[8],recieveUID[8]; |
1918june | 2:38376c7c1685 | 58 | UID[0]=106; |
1918june | 2:38376c7c1685 | 59 | UID[1]=224; |
1918june | 2:38376c7c1685 | 60 | UID[2]=37; |
1918june | 2:38376c7c1685 | 61 | UID[3]=231; |
1918june | 3:987e54d48f77 | 62 | UID[4]=106; |
1918june | 3:987e54d48f77 | 63 | UID[5]=76; |
1918june | 3:987e54d48f77 | 64 | UID[6]=39; |
1918june | 3:987e54d48f77 | 65 | UID[7]=231; |
1918june | 3:987e54d48f77 | 66 | |
1918june | 3:987e54d48f77 | 67 | eeprom.write(1,&NameJune,11); |
1918june | 3:987e54d48f77 | 68 | wait_us(1); |
1918june | 3:987e54d48f77 | 69 | eeprom.write(12,&SurJune,10); |
1918june | 3:987e54d48f77 | 70 | wait_us(1); |
1918june | 3:987e54d48f77 | 71 | eeprom.write(22,&IdJune,11); |
1918june | 3:987e54d48f77 | 72 | wait_us(1); |
1918june | 3:987e54d48f77 | 73 | eeprom.write(41,&NameO,7); |
1918june | 3:987e54d48f77 | 74 | wait_us(1); |
1918june | 3:987e54d48f77 | 75 | eeprom.write(48,&SurO,3); |
1918june | 3:987e54d48f77 | 76 | wait_us(1); |
1918june | 3:987e54d48f77 | 77 | eeprom.write(51,&IdO,11); |
1918june | 3:987e54d48f77 | 78 | wait_us(1); |
1918june | 3:987e54d48f77 | 79 | eeprom.write(81,UID,8); |
1918june | 3:987e54d48f77 | 80 | wait_us(1); |
1918june | 3:987e54d48f77 | 81 | eeprom.read(1,&recieveNameJune,11); |
1918june | 3:987e54d48f77 | 82 | eeprom.read(12,&recieveSurJune,10); |
1918june | 3:987e54d48f77 | 83 | eeprom.read(22,&recieveIdJune,11); |
1918june | 3:987e54d48f77 | 84 | eeprom.read(41,&recieveNameO,7); |
1918june | 3:987e54d48f77 | 85 | eeprom.read(48,&recieveSurO,3); |
1918june | 3:987e54d48f77 | 86 | eeprom.read(51,&recieveIdO,11); |
1918june | 3:987e54d48f77 | 87 | eeprom.read(81,recieveUID,8); |
1918june | 3:987e54d48f77 | 88 | //pc.printf("%s\n",recieveNameJune); |
1918june | 3:987e54d48f77 | 89 | //pc.printf("%s\n",recieveSurJune); |
1918june | 3:987e54d48f77 | 90 | //pc.printf("%s\n",recieveIdJune); |
1918june | 3:987e54d48f77 | 91 | //pc.printf("%s\n",recieveNameO); |
1918june | 3:987e54d48f77 | 92 | //pc.printf("%s\n",recieveSurO); |
1918june | 3:987e54d48f77 | 93 | //pc.printf("%s\n",recieveIdO); |
1918june | 3:987e54d48f77 | 94 | //pc.printf("%d %d %d %d %d %d %d %d\n",recieveUID[0],recieveUID[1],recieveUID[2],recieveUID[3],recieveUID[4],recieveUID[5],recieveUID[6],recieveUID[7]); |
1918june | 2:38376c7c1685 | 95 | RFID.PCD_Init(); |
1918june | 0:930d2bc16788 | 96 | lcd.LCD_init(); |
1918june | 0:930d2bc16788 | 97 | lcd.backlight(ON); |
1918june | 2:38376c7c1685 | 98 | wait(2); |
1918june | 0:930d2bc16788 | 99 | lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 100 | while(1) { |
1918june | 4:b56a8a051fdd | 101 | lcd.LCD_write_string(0,0,"SCAN ID CARD",0); |
1918june | 0:930d2bc16788 | 102 | if ( ! RFID.PICC_IsNewCardPresent()) // Look for new cards |
1918june | 0:930d2bc16788 | 103 | continue; |
1918june | 0:930d2bc16788 | 104 | if ( ! RFID.PICC_ReadCardSerial()) // Select one of the cards |
1918june | 4:b56a8a051fdd | 105 | |
1918june | 0:930d2bc16788 | 106 | continue; |
1918june | 0:930d2bc16788 | 107 | pc.printf("Card UID: "); |
1918june | 0:930d2bc16788 | 108 | for (uint8_t i = 0; i < RFID.uid.size; i++) |
1918june | 0:930d2bc16788 | 109 | pc.printf(" %d", RFID.uid.uidByte[i]); |
1918june | 0:930d2bc16788 | 110 | printf("\n\r"); |
1918june | 3:987e54d48f77 | 111 | if( RFID.uid.uidByte[0]==recieveUID[0] && RFID.uid.uidByte[1]==recieveUID[1] && RFID.uid.uidByte[2]==recieveUID[2] && RFID.uid.uidByte[3]==recieveUID[3] ) { |
1918june | 3:987e54d48f77 | 112 | //จำ |
1918june | 3:987e54d48f77 | 113 | pc.printf("%s\t%s\t%s\n",recieveNameJune,recieveSurJune,recieveIdJune); |
1918june | 4:b56a8a051fdd | 114 | lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 115 | string name,sur; |
1918june | 3:987e54d48f77 | 116 | eeprom.read(1,&name,11); |
1918june | 3:987e54d48f77 | 117 | eeprom.read(12,&sur,10); |
1918june | 3:987e54d48f77 | 118 | printstring(0,0,name,11,0); |
1918june | 3:987e54d48f77 | 119 | printstring(0,1,sur,10,0); |
1918june | 3:987e54d48f77 | 120 | //lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 121 | } |
1918june | 3:987e54d48f77 | 122 | if( RFID.uid.uidByte[0]==recieveUID[4] && RFID.uid.uidByte[1]==recieveUID[5] && RFID.uid.uidByte[2]==recieveUID[6] && RFID.uid.uidByte[3]==recieveUID[7] ) { |
1918june | 3:987e54d48f77 | 123 | pc.printf("%s\t%s\t%s\n",recieveNameO,recieveSurO,recieveIdO); |
1918june | 2:38376c7c1685 | 124 | |
1918june | 3:987e54d48f77 | 125 | //จำ |
1918june | 3:987e54d48f77 | 126 | string name,sur; |
1918june | 4:b56a8a051fdd | 127 | lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 128 | eeprom.read(41,&name,7); |
1918june | 3:987e54d48f77 | 129 | eeprom.read(48,&sur,3); |
1918june | 3:987e54d48f77 | 130 | printstring(0,0,name,7,0); |
1918june | 3:987e54d48f77 | 131 | printstring(0,1,sur,3,0); |
1918june | 0:930d2bc16788 | 132 | } |
1918june | 3:987e54d48f77 | 133 | //wait(2); |
1918june | 3:987e54d48f77 | 134 | //lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 135 | lcd.LCD_write_string(0,2,"1 select item",0); |
1918june | 3:987e54d48f77 | 136 | lcd.LCD_write_string(0,3,"2 select place",0); |
1918june | 4:b56a8a051fdd | 137 | //lcd.LCD_write_string(40,5,"cancel",0); |
1918june | 3:987e54d48f77 | 138 | while(1) { |
1918june | 3:987e54d48f77 | 139 | if(mode==1) { |
1918june | 3:987e54d48f77 | 140 | printf("1"); |
1918june | 3:987e54d48f77 | 141 | lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 142 | lcd.LCD_write_string(0,0,"mode 1",0); |
1918june | 3:987e54d48f77 | 143 | lcd.LCD_write_string(0,1,"select item",0); |
1918june | 3:987e54d48f77 | 144 | while(1) { |
1918june | 3:987e54d48f77 | 145 | if(Switch1.read()==1) { |
1918june | 3:987e54d48f77 | 146 | char str[10]; |
1918june | 3:987e54d48f77 | 147 | selectitem++; |
1918june | 3:987e54d48f77 | 148 | if(selectitem > 9) |
1918june | 3:987e54d48f77 | 149 | selectitem=1; |
1918june | 3:987e54d48f77 | 150 | wait(0.2); |
1918june | 3:987e54d48f77 | 151 | sprintf(str,"item : %d",selectitem); |
1918june | 3:987e54d48f77 | 152 | printstring(3,2,str,8,0); |
1918june | 3:987e54d48f77 | 153 | } |
1918june | 4:b56a8a051fdd | 154 | if(Switch2.read()==1 && selectitem !=0) { |
1918june | 4:b56a8a051fdd | 155 | mode=1; |
1918june | 3:987e54d48f77 | 156 | char str[10]; |
1918june | 3:987e54d48f77 | 157 | place=((int) (Rnd.read()*1000))%4 +1; |
1918june | 3:987e54d48f77 | 158 | wait(0.2); |
1918june | 3:987e54d48f77 | 159 | sprintf(str,"place :%d",place); |
1918june | 3:987e54d48f77 | 160 | printstring(3,3,str,8,0); |
1918june | 3:987e54d48f77 | 161 | break; |
1918june | 3:987e54d48f77 | 162 | } |
1918june | 3:987e54d48f77 | 163 | } |
1918june | 3:987e54d48f77 | 164 | while(1) { //อ่านค่าจากแผ่นนั่นว่ามันจิ้มตรงมั้ย |
1918june | 0:930d2bc16788 | 165 | |
1918june | 3:987e54d48f77 | 166 | } |
1918june | 3:987e54d48f77 | 167 | } |
1918june | 3:987e54d48f77 | 168 | if(mode==2) {//selectplace |
1918june | 3:987e54d48f77 | 169 | printf("2"); |
1918june | 3:987e54d48f77 | 170 | lcd.LCD_clear(); |
1918june | 3:987e54d48f77 | 171 | lcd.LCD_write_string(0,0,"mode 2",0); |
1918june | 3:987e54d48f77 | 172 | lcd.LCD_write_string(0,1,"select place",0); |
1918june | 3:987e54d48f77 | 173 | while(1) { |
1918june | 3:987e54d48f77 | 174 | if(Switch1.read()==1) { |
1918june | 4:b56a8a051fdd | 175 | mode=2; |
1918june | 3:987e54d48f77 | 176 | char str[10]; |
1918june | 3:987e54d48f77 | 177 | place++; |
1918june | 3:987e54d48f77 | 178 | if(place > 4) |
1918june | 3:987e54d48f77 | 179 | place=1; |
1918june | 3:987e54d48f77 | 180 | wait(0.2); |
1918june | 3:987e54d48f77 | 181 | sprintf(str,"place : %d",place); |
1918june | 3:987e54d48f77 | 182 | printstring(3,2,str,9,0); |
1918june | 3:987e54d48f77 | 183 | } |
1918june | 4:b56a8a051fdd | 184 | if(Switch2.read()==1 && place!=0) { |
1918june | 3:987e54d48f77 | 185 | char str[10]; |
1918june | 3:987e54d48f77 | 186 | selectitem=((int) (Rnd.read()*1000))%9 +1; |
1918june | 3:987e54d48f77 | 187 | wait(0.2); |
1918june | 3:987e54d48f77 | 188 | sprintf(str,"item : %d",selectitem); |
1918june | 3:987e54d48f77 | 189 | printstring(3,3,str,8,0); |
1918june | 3:987e54d48f77 | 190 | break; |
1918june | 3:987e54d48f77 | 191 | } |
1918june | 4:b56a8a051fdd | 192 | } |
1918june | 4:b56a8a051fdd | 193 | while(1) {} |
1918june | 4:b56a8a051fdd | 194 | } |
1918june | 4:b56a8a051fdd | 195 | if(mode==3) { |
1918june | 4:b56a8a051fdd | 196 | printf("0"); |
1918june | 2:38376c7c1685 | 197 | |
1918june | 4:b56a8a051fdd | 198 | } |
1918june | 4:b56a8a051fdd | 199 | printf("mode=%d",mode); |
1918june | 3:987e54d48f77 | 200 | |
1918june | 4:b56a8a051fdd | 201 | } //end while mode loop |
1918june | 4:b56a8a051fdd | 202 | } |
1918june | 3:987e54d48f77 | 203 | |
1918june | 3:987e54d48f77 | 204 | } |
1918june | 4:b56a8a051fdd | 205 |