Andrew Bell
/
counter_assesment_creg
7 seg counter with 3 modes controlled by push button
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 // Global variables 00004 00005 int mode; // mode=1 on startup 00006 // mode=2 for keypad 00007 // mode=3 for serial 00008 00009 //Assign 8 pins digital bus object LED_Disp 00010 BusOut LED_Disp(p7,p11,p9,p8,p5,p6,p10,p12); 00011 00012 // Pin assignment for 4x3 or 12 key Hex keypad 00013 // Looking at keyboard front, the left most pin is pin1 00014 // keypad pin numbers 00015 // pin 1 = not connected 00016 // pin 2 = col 2 00017 // pin 3 = row 1 00018 // pin 4 = col 1 00019 // pin 5 = row 4 00020 // pin 6 = col 3 00021 // pin 7 = row 3 00022 // pin 8 = row 2 00023 // pin 9 = not connected 00024 // 00025 00026 Serial pc(USBTX, USBRX); // Create serial comms object 00027 00028 // mbed pin assignment for keypad 00029 DigitalIn row1(p27); 00030 DigitalIn row2(p22); 00031 DigitalIn row3(p23); 00032 DigitalIn row4(p25); 00033 DigitalOut col1(p26); 00034 DigitalOut col2(p28); 00035 DigitalOut col3(p24); 00036 00037 DigitalOut debug_led (LED1); // Debug LED for flashing 00038 00039 Serial s_port(USBTX, USBRX); 00040 00041 00042 // Interrupts 00043 InterruptIn cal_button(p14); // Assign interrupt input object to pin 14 00044 00045 // Prototypes for ISRs 00046 void cal_isr(void); // Declare ISR for cal button on p14 00047 00048 // Function Prototypes 00049 char Keypad(void); // Declare Keypad function will return character pressed 00050 void DisplayChar(char); // Declare function to display character on 7 segment 00051 void Flash_char(char); // Declare function to flash character on 7 segment 00052 00053 00054 00055 00056 00057 // ************ MAIN ************ 00058 int main() 00059 { 00060 mode=1; // On startup set mode=1 00061 00062 cal_button.rise(&cal_isr); //Attach address of ISR to interrupt 00063 00064 int counter=0; // General program or character counter 00065 00066 int kp_char = ' '; // Character read from keypad (ASCII) 00067 00068 00069 while(1) 00070 { 00071 if(mode==1) // Check if startup mode is set 00072 { 00073 if(counter>=5) 00074 counter=0; // Reset counter 00075 00076 if (counter==0) 00077 Flash_char('H'); // Flash 'H' 00078 else if (counter==1) 00079 Flash_char('E'); // Flash 'E' 00080 else if (counter==2) 00081 Flash_char('L'); // Flash 'L' 00082 else if (counter==3) 00083 Flash_char('L'); // Flash 'L' 00084 else if (counter==4) 00085 Flash_char('0'); // Flash '0' 00086 00087 00088 counter++; //Increment message counter 00089 } 00090 00091 00092 00093 else if(mode==2) // Check if keypad mode 00094 { 00095 00096 counter=0; // reset message counter 00097 00098 kp_char = Keypad(); // Poll keypad and check if key pressed 00099 00100 if(kp_char!=' ') // If keypad char == ' ' then no key pressed 00101 { 00102 00103 if(kp_char>='0' && kp_char<='9') 00104 { 00105 DisplayChar(kp_char); // Display character on 7 segment 00106 pc.printf("%c\n\r",kp_char); // Send character to serial port 00107 00108 } 00109 } 00110 } 00111 00112 00113 else if(mode==3) // Check if serial mode 00114 { 00115 kp_char = Keypad(); // Poll keypad and check if key pressed 00116 00117 if(s_port.readable()) 00118 { 00119 00120 kp_char = s_port.getc(); // Display character on 7 segment 00121 00122 00123 if(kp_char>='0' && kp_char<='9') 00124 { 00125 DisplayChar(kp_char); // Display character on 7 segment 00126 pc.printf("%c\n\r",kp_char); // Send character to serial port 00127 00128 } 00129 00130 } 00131 } 00132 } 00133 } 00134 00135 00136 // ISR function called by interrupt on push button 00137 void cal_isr(void) 00138 { 00139 // Switch modes 00140 if(mode==1) 00141 { 00142 mode=2; // Set mode to keypad mode 00143 DisplayChar(' '); // Turn off all segments 00144 //wait(0.1); //Debounce timer 00145 } 00146 else if(mode==2) 00147 { 00148 mode=3; // Set mode to serial mode 00149 DisplayChar(' '); // Turn off all segments 00150 //wait(0.1); //Debounce timer 00151 } 00152 else if(mode==3) 00153 { 00154 mode=1; // Set mode to startup mode 00155 DisplayChar(' '); // Turn off all segments 00156 //wait(0.1); //Debounce timer 00157 } 00158 } 00159 00160 00161 // Function to flash character on 7 segment 00162 void Flash_char(char new_char) 00163 { 00164 DisplayChar(' '); // Turn off all segments 00165 wait(0.1); 00166 DisplayChar(new_char); // Display chararacter on 7 segment 00167 wait(0.6); 00168 } 00169 00170 00171 00172 00173 char Keypad(void) 00174 { 00175 00176 // Test for any key pressed on column 1 00177 // Pull col1 high and keep columns 2 and 3 low 00178 col1=1; 00179 col2=0; 00180 col3=0; 00181 00182 00183 if(row1==1) // While col1 is high, if row 1 is high then '1' is pressed 00184 { 00185 while(row1==1){} // Loop while button is pressed down 00186 // On releasing the button return the button character and exit function 00187 00188 // Pull all colums low to end 00189 col1=0; 00190 col2=0; 00191 col3=0; 00192 return('1'); // return character 1 and exit function 00193 } 00194 else if(row2==1) // While col1 is high, if row 2 is high then '4' is pressed 00195 { 00196 while(row2==1){} 00197 // Pull all colums low to end 00198 col1=0; 00199 col2=0; 00200 col3=0; 00201 return('4'); // Exit function and return 00202 } 00203 else if(row3==1) // While col1 is high, if row 3 is high then '7' is pressed 00204 { 00205 while(row3==1){} 00206 // Pull all colums low to end 00207 col1=0; 00208 col2=0; 00209 col3=0; 00210 return('7'); 00211 } 00212 else if(row4==1) // While col1 is high, if row 4 is high then '*' is pressed 00213 { 00214 while(row4==1){} 00215 // Pull all colums low to end 00216 col1=0; 00217 col2=0; 00218 col3=0; 00219 return('*'); 00220 } 00221 00222 00223 00224 // Test for any key pressed on column 2 00225 // Pull col2 high and keep columns 1 and 3 low 00226 col1=0; 00227 col2=1; 00228 col3=0; 00229 00230 if(row1==1) // While col2 is high, if row 1 is high then '2' is pressed 00231 { 00232 while(row1==1){} // Loop 00233 // Pull all colums low to end 00234 col1=0; 00235 col2=0; 00236 col3=0; 00237 return('2'); 00238 } 00239 else if(row2==1) // While col2 is high, if row 2 is high then '5' is pressed 00240 { 00241 while(row2==1){} // Loop 00242 // Pull all colums low to end 00243 col1=0; 00244 col2=0; 00245 col3=0; 00246 return('5'); 00247 } 00248 else if(row3==1) // While col2 is high, if row 3 is high then '8' is pressed 00249 { 00250 while(row3==1){} // Loop 00251 // Pull all colums low to end 00252 col1=0; 00253 col2=0; 00254 col3=0; 00255 return('8'); 00256 } 00257 else if(row4==1) // While col2 is high, if row 4 is high then '0' is pressed 00258 { 00259 while(row4==1){} // Loop 00260 // Pull all colums low to end 00261 col1=0; 00262 col2=0; 00263 col3=0; 00264 return('0'); 00265 } 00266 00267 00268 // Test for any key pressed on column 3 00269 // Pull col3 high and keep columns 1 and 2 low 00270 col1=0; 00271 col2=0; 00272 col3=1; 00273 00274 if(row1==1) // While col3 is high, if row 1 is high then '3' is pressed 00275 { 00276 while(row1==1){} // Loop 00277 // Pull all colums low to end 00278 col1=0; 00279 col2=0; 00280 col3=0; 00281 return('3'); 00282 } 00283 else if(row2==1) // While col3 is high, if row 2 is high then '6' is pressed 00284 { 00285 while(row2==1){} // Loop 00286 // Pull all colums low to end 00287 col1=0; 00288 col2=0; 00289 col3=0; 00290 return('6'); 00291 } 00292 else if(row3==1) // While col3 is high, if row 3 is high then '9' is pressed 00293 { 00294 while(row3==1){} // Loop 00295 // Pull all colums low to end 00296 col1=0; 00297 col2=0; 00298 col3=0; 00299 return('9'); 00300 } 00301 else if(row4==1) // While col3 is high, if row 4 is high then '#' is pressed 00302 { 00303 while(row4==1){} // Loop 00304 // Pull all colums low to end 00305 col1=0; 00306 col2=0; 00307 col3=0; 00308 return('#'); 00309 } 00310 00311 // Pull all colums low to end 00312 col1=0; 00313 col2=0; 00314 col3=0; 00315 00316 00317 // ***** Return value ****** 00318 return(' '); // If no key is pressed then return ' ' a space character. 00319 00320 } // Keypad Function End 00321 00322 00323 00324 00325 // Function to display a character on 7 segment LED display 00326 void DisplayChar(char disp_char) 00327 { 00328 switch(disp_char) 00329 { 00330 case '0': 00331 LED_Disp = ~0x3F; // Bit pattern for '0' = ~0xC0 00332 break; 00333 case '1': 00334 LED_Disp = ~0x06; // Bit pattern for '1' = ~0xF9 00335 break; 00336 case '2': 00337 LED_Disp = ~0x5B; // Bit pattern for '2' = ~0xA4 00338 break; 00339 case '3': 00340 LED_Disp = ~0x4F; // Bit pattern for '3' = ~0xB0 00341 break; 00342 case '4': 00343 LED_Disp = ~0x66; // Bit pattern for '4' = ~0x99 00344 break; 00345 case '5': 00346 LED_Disp = ~0x6D; // Bit pattern for '5' = ~0x92 00347 break; 00348 case '6': 00349 LED_Disp = ~0x7D; // Bit pattern for '6' = ~0x82 00350 break; 00351 case '7': 00352 LED_Disp = ~0x07; // Bit pattern for '7' = ~0xF8 00353 break; 00354 case '8': 00355 LED_Disp = ~0x7F; // Bit pattern for '8' = ~0x80 00356 break; 00357 case '9': 00358 LED_Disp = ~0x67; // Bit pattern for '9' = ~0x98 00359 break; 00360 case 'A': 00361 LED_Disp = ~0x77; // Bit pattern for 'A' = ~0x77 00362 break; 00363 case 'B': 00364 LED_Disp = ~0x7F; // Bit pattern for 'B' = ~0x7F 00365 break; 00366 case 'C': 00367 LED_Disp = ~0x39; // Bit pattern for 'C' = ~0x39 00368 break; 00369 case 'D': 00370 LED_Disp = ~0x3F; // Bit pattern for 'D' = ~0x3F 00371 break; 00372 case 'E': 00373 LED_Disp = ~0x79; // Bit pattern for 'E' = ~0x79 00374 break; 00375 case 'F': 00376 LED_Disp = ~0x71; // Bit pattern for 'F' = ~0x71 00377 break; 00378 case 'G': 00379 LED_Disp = ~0x7D; // Bit pattern for 'G' = ~0x7D 00380 break; 00381 case 'H': // Display character 'H' = ~0x77 00382 LED_Disp = ~0x76; 00383 break; 00384 case 'I': // Display character 'I' = ~0x06 00385 LED_Disp = ~0x06; 00386 break; 00387 case 'L': // Display character 'L' = ~0x38 00388 LED_Disp = ~0x38; 00389 break; 00390 case 'S': 00391 LED_Disp = ~0x6D; // Bit pattern for 'S' = ~0x92 00392 break; 00393 case ' ': // Display character ' ' = ~0x00 00394 LED_Disp = ~0x00; 00395 break; 00396 case '*': // keypad '*' character 00397 LED_Disp = ~0x63; // Top 4 segments 00398 break; 00399 case '#': // keypad '#' character 00400 LED_Disp = ~0x5C; // Bottom 4 segments 00401 break; 00402 } 00403 }
Generated on Sun Jul 17 2022 01:36:20 by 1.7.2