han back / Mbed OS CLEO_UART_KEYPAD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 struct UART_buf
00004 { 
00005     uint8_t STA;
00006     uint8_t MODE; 
00007     uint8_t CMD;
00008     uint8_t LEN;
00009     uint8_t DATA[32];
00010     uint8_t END; 
00011      
00012 };
00013  
00014 // C1, C2, C3, C4
00015 PinName pin_KEYPAD_C[4] = {PC_4, PC_5, PC_6, PC_7};
00016 // R1, R2, R3, R4
00017 PinName pin_KEYPAD_R[4] = {PC_8, PC_9, PC_10, PC_11};
00018     
00019 DigitalInOut Keypad_C1(pin_KEYPAD_C[0]);
00020 DigitalInOut Keypad_C2(pin_KEYPAD_C[1]);
00021 DigitalInOut Keypad_C3(pin_KEYPAD_C[2]);
00022 DigitalInOut Keypad_C4(pin_KEYPAD_C[3]);
00023 
00024 // R1, R2, R3, R4
00025 BusIn Keypad_R(pin_KEYPAD_R[0], pin_KEYPAD_R[1], pin_KEYPAD_R[2], pin_KEYPAD_R[3]);
00026 
00027 char Key_value[16] = {'1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'}; 
00028 
00029 Serial SerialUART(PA_2, PA_3);
00030 
00031 uint8_t Buffer[37];
00032 
00033 //UART_buf RX_BUF;
00034 
00035 //Ticker Sensor_Timer;
00036 
00037 //void SerialUARTRX_ISR(void);
00038 //void Timer_setting(uint8_t cmd, uint8_t value);
00039 //void Sensor_Read(void);
00040 void Sensor_Read_Data(uint8_t data);
00041 uint16_t Keypad_Read(void);
00042 
00043 int main() {
00044 
00045     uint8_t Key_flag_pre = 0, i;
00046     uint16_t Keypad;
00047         
00048     SerialUART.baud(115200);
00049     
00050     //SerialUART.attach(&SerialUARTRX_ISR);
00051     
00052     //Timer_setting(0x06, 2);
00053     
00054     while(1)
00055     {
00056         Keypad = Keypad_Read();
00057         if(Keypad != 0)
00058         {
00059             for(i=0; i<16; i++)
00060             {
00061                 if(Keypad & (0x0001 << i))
00062                 {
00063                     if(Key_flag_pre != Key_value[i]);
00064                     {
00065                         Key_flag_pre = Key_value[i];
00066                         Sensor_Read_Data(Key_flag_pre);
00067                     }
00068                     break;
00069                 }
00070             }
00071         }
00072         else
00073             Key_flag_pre = 0;
00074         wait(0.1);
00075     }
00076 }
00077 
00078 /*void SerialUARTRX_ISR(void)
00079 {
00080     static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0;
00081     uint8_t rx_da = SerialUART.getc();
00082     switch(RX_Status)
00083     {
00084         case 0:
00085             if(rx_da == 0x76)
00086             {
00087                 RX_BUF.STA = rx_da;
00088                 RX_Status++;
00089             }
00090             break;
00091         case 1:
00092             RX_BUF.MODE = rx_da;
00093             RX_Status++;
00094             break;
00095         case 2:
00096             RX_BUF.CMD = rx_da;
00097             RX_Status++;
00098             break;
00099         case 3:
00100             RX_BUF.LEN = rx_da;
00101             RX_Len = RX_BUF.LEN;
00102             RX_Status++;
00103             if(RX_Len == 0)
00104                 RX_Status++;
00105             break;
00106         case 4:
00107             RX_BUF.DATA[RX_count] = rx_da;
00108             RX_count++;
00109             if(RX_count == RX_Len)
00110             {
00111                 RX_Status++;
00112                 RX_count = 0;
00113                 RX_Len = 32;
00114             }
00115             break;
00116         case 5:
00117             if(rx_da == 0x3E)
00118             {
00119                 RX_BUF.END = rx_da;
00120                 RX_Status = 0;
00121                 switch(RX_BUF.MODE)
00122                 {
00123                     case 0x04:
00124                         Timer_setting(RX_BUF.CMD, RX_BUF.DATA[0]);
00125                         break;
00126                 }
00127             }
00128             break;
00129     }
00130 }*/
00131 
00132 /*void Timer_setting(uint8_t cmd, uint8_t value)
00133 {
00134     double Time_value = 0;
00135     switch(cmd)
00136     {
00137         case 0x01:
00138             Time_value = 30;
00139             break;
00140         case 0x02:
00141             Time_value = 60;
00142             break;
00143         case 0x03:
00144             Time_value = 120;
00145             break;
00146         case 0x04:
00147             Time_value = 300;
00148             break;
00149         case 0x05:
00150             Time_value = 600;
00151             break;
00152         case 0x06:
00153             Time_value = value;
00154             Time_value = 1.0/Time_value;
00155             break;
00156     }
00157     Sensor_Timer.attach(&Sensor_Read, Time_value);
00158 }*/
00159 //
00160 // bit   | 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
00161 // -------------------------------------------------------
00162 // value |  D  #  0  *  C  9  8  7  B  6  5  4  A  3  2  1
00163 //
00164 //  1  2  3  A
00165 //  4  5  6  B
00166 //  7  8  9  C
00167 //  *  0  #  D
00168 //
00169 uint16_t Keypad_Read(void)
00170 {
00171     uint16_t keypad_tmp, Read_data = 0, i;
00172     for(i=0; i<4; i++)
00173     {
00174         switch(i)
00175         {
00176             case 0:
00177                 Keypad_C1.output();
00178                 Keypad_C1 = 1;
00179                 break;
00180             case 1:
00181                 Keypad_C2.output();
00182                 Keypad_C2 = 1;
00183                 break;
00184             case 2:
00185                 Keypad_C3.output();
00186                 Keypad_C3 = 1;
00187                 break;
00188             case 3:
00189                 Keypad_C4.output();
00190                 Keypad_C4 = 1;
00191                 break;
00192         }
00193         
00194         wait(0.001);
00195         keypad_tmp = Keypad_R;
00196         Read_data |= (keypad_tmp << (i*4));
00197         switch(i)
00198         {
00199             case 0:
00200                 Keypad_C1 = 0;
00201                 Keypad_C1.input();
00202                 break;
00203             case 1:
00204                 Keypad_C2 = 0;
00205                 Keypad_C2.input();
00206                 break;
00207             case 2:
00208                 Keypad_C3 = 0;
00209                 Keypad_C3.input();
00210                 break;
00211             case 3:
00212                 Keypad_C4 = 0;
00213                 Keypad_C4.input();
00214                 break;
00215         }
00216     }
00217     return Read_data;
00218 }
00219 
00220 void Sensor_Read_Data(uint8_t data)
00221 {
00222     Buffer[0] = 0x76;
00223     Buffer[1] = 0x01;
00224     Buffer[2] = 0x19;
00225     Buffer[3] = 0x01;
00226     Buffer[4] = data;
00227     Buffer[5] = 0x3E;
00228     for(int i=0; i<6; i++)
00229         SerialUART.putc(Buffer[i]);
00230 }
00231 /*
00232 void Sensor_Read(void)
00233 {
00234     Buffer[0] = 0x76;
00235     Buffer[1] = 0x01;
00236     Buffer[2] = 0x05;
00237     Buffer[3] = 0x01;
00238     Buffer[4] = IR_Detect;
00239     Buffer[5] = 0x3E;
00240     for(int i=0; i<6; i++)
00241         SerialUART.putc(Buffer[i]);
00242 }
00243 */