Andrew Bell
/
counter_assesment_creg
7 seg counter with 3 modes controlled by push button
main.cpp@0:7d75e4df9577, 2021-03-03 (annotated)
- Committer:
- andrewbw01
- Date:
- Wed Mar 03 16:24:55 2021 +0000
- Revision:
- 0:7d75e4df9577
cregans assesment solution
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewbw01 | 0:7d75e4df9577 | 1 | #include "mbed.h" |
andrewbw01 | 0:7d75e4df9577 | 2 | |
andrewbw01 | 0:7d75e4df9577 | 3 | // Global variables |
andrewbw01 | 0:7d75e4df9577 | 4 | |
andrewbw01 | 0:7d75e4df9577 | 5 | int mode; // mode=1 on startup |
andrewbw01 | 0:7d75e4df9577 | 6 | // mode=2 for keypad |
andrewbw01 | 0:7d75e4df9577 | 7 | // mode=3 for serial |
andrewbw01 | 0:7d75e4df9577 | 8 | |
andrewbw01 | 0:7d75e4df9577 | 9 | //Assign 8 pins digital bus object LED_Disp |
andrewbw01 | 0:7d75e4df9577 | 10 | BusOut LED_Disp(p7,p11,p9,p8,p5,p6,p10,p12); |
andrewbw01 | 0:7d75e4df9577 | 11 | |
andrewbw01 | 0:7d75e4df9577 | 12 | // Pin assignment for 4x3 or 12 key Hex keypad |
andrewbw01 | 0:7d75e4df9577 | 13 | // Looking at keyboard front, the left most pin is pin1 |
andrewbw01 | 0:7d75e4df9577 | 14 | // keypad pin numbers |
andrewbw01 | 0:7d75e4df9577 | 15 | // pin 1 = not connected |
andrewbw01 | 0:7d75e4df9577 | 16 | // pin 2 = col 2 |
andrewbw01 | 0:7d75e4df9577 | 17 | // pin 3 = row 1 |
andrewbw01 | 0:7d75e4df9577 | 18 | // pin 4 = col 1 |
andrewbw01 | 0:7d75e4df9577 | 19 | // pin 5 = row 4 |
andrewbw01 | 0:7d75e4df9577 | 20 | // pin 6 = col 3 |
andrewbw01 | 0:7d75e4df9577 | 21 | // pin 7 = row 3 |
andrewbw01 | 0:7d75e4df9577 | 22 | // pin 8 = row 2 |
andrewbw01 | 0:7d75e4df9577 | 23 | // pin 9 = not connected |
andrewbw01 | 0:7d75e4df9577 | 24 | // |
andrewbw01 | 0:7d75e4df9577 | 25 | |
andrewbw01 | 0:7d75e4df9577 | 26 | Serial pc(USBTX, USBRX); // Create serial comms object |
andrewbw01 | 0:7d75e4df9577 | 27 | |
andrewbw01 | 0:7d75e4df9577 | 28 | // mbed pin assignment for keypad |
andrewbw01 | 0:7d75e4df9577 | 29 | DigitalIn row1(p27); |
andrewbw01 | 0:7d75e4df9577 | 30 | DigitalIn row2(p22); |
andrewbw01 | 0:7d75e4df9577 | 31 | DigitalIn row3(p23); |
andrewbw01 | 0:7d75e4df9577 | 32 | DigitalIn row4(p25); |
andrewbw01 | 0:7d75e4df9577 | 33 | DigitalOut col1(p26); |
andrewbw01 | 0:7d75e4df9577 | 34 | DigitalOut col2(p28); |
andrewbw01 | 0:7d75e4df9577 | 35 | DigitalOut col3(p24); |
andrewbw01 | 0:7d75e4df9577 | 36 | |
andrewbw01 | 0:7d75e4df9577 | 37 | DigitalOut debug_led (LED1); // Debug LED for flashing |
andrewbw01 | 0:7d75e4df9577 | 38 | |
andrewbw01 | 0:7d75e4df9577 | 39 | Serial s_port(USBTX, USBRX); |
andrewbw01 | 0:7d75e4df9577 | 40 | |
andrewbw01 | 0:7d75e4df9577 | 41 | |
andrewbw01 | 0:7d75e4df9577 | 42 | // Interrupts |
andrewbw01 | 0:7d75e4df9577 | 43 | InterruptIn cal_button(p14); // Assign interrupt input object to pin 14 |
andrewbw01 | 0:7d75e4df9577 | 44 | |
andrewbw01 | 0:7d75e4df9577 | 45 | // Prototypes for ISRs |
andrewbw01 | 0:7d75e4df9577 | 46 | void cal_isr(void); // Declare ISR for cal button on p14 |
andrewbw01 | 0:7d75e4df9577 | 47 | |
andrewbw01 | 0:7d75e4df9577 | 48 | // Function Prototypes |
andrewbw01 | 0:7d75e4df9577 | 49 | char Keypad(void); // Declare Keypad function will return character pressed |
andrewbw01 | 0:7d75e4df9577 | 50 | void DisplayChar(char); // Declare function to display character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 51 | void Flash_char(char); // Declare function to flash character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 52 | |
andrewbw01 | 0:7d75e4df9577 | 53 | |
andrewbw01 | 0:7d75e4df9577 | 54 | |
andrewbw01 | 0:7d75e4df9577 | 55 | |
andrewbw01 | 0:7d75e4df9577 | 56 | |
andrewbw01 | 0:7d75e4df9577 | 57 | // ************ MAIN ************ |
andrewbw01 | 0:7d75e4df9577 | 58 | int main() |
andrewbw01 | 0:7d75e4df9577 | 59 | { |
andrewbw01 | 0:7d75e4df9577 | 60 | mode=1; // On startup set mode=1 |
andrewbw01 | 0:7d75e4df9577 | 61 | |
andrewbw01 | 0:7d75e4df9577 | 62 | cal_button.rise(&cal_isr); //Attach address of ISR to interrupt |
andrewbw01 | 0:7d75e4df9577 | 63 | |
andrewbw01 | 0:7d75e4df9577 | 64 | int counter=0; // General program or character counter |
andrewbw01 | 0:7d75e4df9577 | 65 | |
andrewbw01 | 0:7d75e4df9577 | 66 | int kp_char = ' '; // Character read from keypad (ASCII) |
andrewbw01 | 0:7d75e4df9577 | 67 | |
andrewbw01 | 0:7d75e4df9577 | 68 | |
andrewbw01 | 0:7d75e4df9577 | 69 | while(1) |
andrewbw01 | 0:7d75e4df9577 | 70 | { |
andrewbw01 | 0:7d75e4df9577 | 71 | if(mode==1) // Check if startup mode is set |
andrewbw01 | 0:7d75e4df9577 | 72 | { |
andrewbw01 | 0:7d75e4df9577 | 73 | if(counter>=5) |
andrewbw01 | 0:7d75e4df9577 | 74 | counter=0; // Reset counter |
andrewbw01 | 0:7d75e4df9577 | 75 | |
andrewbw01 | 0:7d75e4df9577 | 76 | if (counter==0) |
andrewbw01 | 0:7d75e4df9577 | 77 | Flash_char('H'); // Flash 'H' |
andrewbw01 | 0:7d75e4df9577 | 78 | else if (counter==1) |
andrewbw01 | 0:7d75e4df9577 | 79 | Flash_char('E'); // Flash 'E' |
andrewbw01 | 0:7d75e4df9577 | 80 | else if (counter==2) |
andrewbw01 | 0:7d75e4df9577 | 81 | Flash_char('L'); // Flash 'L' |
andrewbw01 | 0:7d75e4df9577 | 82 | else if (counter==3) |
andrewbw01 | 0:7d75e4df9577 | 83 | Flash_char('L'); // Flash 'L' |
andrewbw01 | 0:7d75e4df9577 | 84 | else if (counter==4) |
andrewbw01 | 0:7d75e4df9577 | 85 | Flash_char('0'); // Flash '0' |
andrewbw01 | 0:7d75e4df9577 | 86 | |
andrewbw01 | 0:7d75e4df9577 | 87 | |
andrewbw01 | 0:7d75e4df9577 | 88 | counter++; //Increment message counter |
andrewbw01 | 0:7d75e4df9577 | 89 | } |
andrewbw01 | 0:7d75e4df9577 | 90 | |
andrewbw01 | 0:7d75e4df9577 | 91 | |
andrewbw01 | 0:7d75e4df9577 | 92 | |
andrewbw01 | 0:7d75e4df9577 | 93 | else if(mode==2) // Check if keypad mode |
andrewbw01 | 0:7d75e4df9577 | 94 | { |
andrewbw01 | 0:7d75e4df9577 | 95 | |
andrewbw01 | 0:7d75e4df9577 | 96 | counter=0; // reset message counter |
andrewbw01 | 0:7d75e4df9577 | 97 | |
andrewbw01 | 0:7d75e4df9577 | 98 | kp_char = Keypad(); // Poll keypad and check if key pressed |
andrewbw01 | 0:7d75e4df9577 | 99 | |
andrewbw01 | 0:7d75e4df9577 | 100 | if(kp_char!=' ') // If keypad char == ' ' then no key pressed |
andrewbw01 | 0:7d75e4df9577 | 101 | { |
andrewbw01 | 0:7d75e4df9577 | 102 | |
andrewbw01 | 0:7d75e4df9577 | 103 | if(kp_char>='0' && kp_char<='9') |
andrewbw01 | 0:7d75e4df9577 | 104 | { |
andrewbw01 | 0:7d75e4df9577 | 105 | DisplayChar(kp_char); // Display character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 106 | pc.printf("%c\n\r",kp_char); // Send character to serial port |
andrewbw01 | 0:7d75e4df9577 | 107 | |
andrewbw01 | 0:7d75e4df9577 | 108 | } |
andrewbw01 | 0:7d75e4df9577 | 109 | } |
andrewbw01 | 0:7d75e4df9577 | 110 | } |
andrewbw01 | 0:7d75e4df9577 | 111 | |
andrewbw01 | 0:7d75e4df9577 | 112 | |
andrewbw01 | 0:7d75e4df9577 | 113 | else if(mode==3) // Check if serial mode |
andrewbw01 | 0:7d75e4df9577 | 114 | { |
andrewbw01 | 0:7d75e4df9577 | 115 | kp_char = Keypad(); // Poll keypad and check if key pressed |
andrewbw01 | 0:7d75e4df9577 | 116 | |
andrewbw01 | 0:7d75e4df9577 | 117 | if(s_port.readable()) |
andrewbw01 | 0:7d75e4df9577 | 118 | { |
andrewbw01 | 0:7d75e4df9577 | 119 | |
andrewbw01 | 0:7d75e4df9577 | 120 | kp_char = s_port.getc(); // Display character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 121 | |
andrewbw01 | 0:7d75e4df9577 | 122 | |
andrewbw01 | 0:7d75e4df9577 | 123 | if(kp_char>='0' && kp_char<='9') |
andrewbw01 | 0:7d75e4df9577 | 124 | { |
andrewbw01 | 0:7d75e4df9577 | 125 | DisplayChar(kp_char); // Display character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 126 | pc.printf("%c\n\r",kp_char); // Send character to serial port |
andrewbw01 | 0:7d75e4df9577 | 127 | |
andrewbw01 | 0:7d75e4df9577 | 128 | } |
andrewbw01 | 0:7d75e4df9577 | 129 | |
andrewbw01 | 0:7d75e4df9577 | 130 | } |
andrewbw01 | 0:7d75e4df9577 | 131 | } |
andrewbw01 | 0:7d75e4df9577 | 132 | } |
andrewbw01 | 0:7d75e4df9577 | 133 | } |
andrewbw01 | 0:7d75e4df9577 | 134 | |
andrewbw01 | 0:7d75e4df9577 | 135 | |
andrewbw01 | 0:7d75e4df9577 | 136 | // ISR function called by interrupt on push button |
andrewbw01 | 0:7d75e4df9577 | 137 | void cal_isr(void) |
andrewbw01 | 0:7d75e4df9577 | 138 | { |
andrewbw01 | 0:7d75e4df9577 | 139 | // Switch modes |
andrewbw01 | 0:7d75e4df9577 | 140 | if(mode==1) |
andrewbw01 | 0:7d75e4df9577 | 141 | { |
andrewbw01 | 0:7d75e4df9577 | 142 | mode=2; // Set mode to keypad mode |
andrewbw01 | 0:7d75e4df9577 | 143 | DisplayChar(' '); // Turn off all segments |
andrewbw01 | 0:7d75e4df9577 | 144 | //wait(0.1); //Debounce timer |
andrewbw01 | 0:7d75e4df9577 | 145 | } |
andrewbw01 | 0:7d75e4df9577 | 146 | else if(mode==2) |
andrewbw01 | 0:7d75e4df9577 | 147 | { |
andrewbw01 | 0:7d75e4df9577 | 148 | mode=3; // Set mode to serial mode |
andrewbw01 | 0:7d75e4df9577 | 149 | DisplayChar(' '); // Turn off all segments |
andrewbw01 | 0:7d75e4df9577 | 150 | //wait(0.1); //Debounce timer |
andrewbw01 | 0:7d75e4df9577 | 151 | } |
andrewbw01 | 0:7d75e4df9577 | 152 | else if(mode==3) |
andrewbw01 | 0:7d75e4df9577 | 153 | { |
andrewbw01 | 0:7d75e4df9577 | 154 | mode=1; // Set mode to startup mode |
andrewbw01 | 0:7d75e4df9577 | 155 | DisplayChar(' '); // Turn off all segments |
andrewbw01 | 0:7d75e4df9577 | 156 | //wait(0.1); //Debounce timer |
andrewbw01 | 0:7d75e4df9577 | 157 | } |
andrewbw01 | 0:7d75e4df9577 | 158 | } |
andrewbw01 | 0:7d75e4df9577 | 159 | |
andrewbw01 | 0:7d75e4df9577 | 160 | |
andrewbw01 | 0:7d75e4df9577 | 161 | // Function to flash character on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 162 | void Flash_char(char new_char) |
andrewbw01 | 0:7d75e4df9577 | 163 | { |
andrewbw01 | 0:7d75e4df9577 | 164 | DisplayChar(' '); // Turn off all segments |
andrewbw01 | 0:7d75e4df9577 | 165 | wait(0.1); |
andrewbw01 | 0:7d75e4df9577 | 166 | DisplayChar(new_char); // Display chararacter on 7 segment |
andrewbw01 | 0:7d75e4df9577 | 167 | wait(0.6); |
andrewbw01 | 0:7d75e4df9577 | 168 | } |
andrewbw01 | 0:7d75e4df9577 | 169 | |
andrewbw01 | 0:7d75e4df9577 | 170 | |
andrewbw01 | 0:7d75e4df9577 | 171 | |
andrewbw01 | 0:7d75e4df9577 | 172 | |
andrewbw01 | 0:7d75e4df9577 | 173 | char Keypad(void) |
andrewbw01 | 0:7d75e4df9577 | 174 | { |
andrewbw01 | 0:7d75e4df9577 | 175 | |
andrewbw01 | 0:7d75e4df9577 | 176 | // Test for any key pressed on column 1 |
andrewbw01 | 0:7d75e4df9577 | 177 | // Pull col1 high and keep columns 2 and 3 low |
andrewbw01 | 0:7d75e4df9577 | 178 | col1=1; |
andrewbw01 | 0:7d75e4df9577 | 179 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 180 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 181 | |
andrewbw01 | 0:7d75e4df9577 | 182 | |
andrewbw01 | 0:7d75e4df9577 | 183 | if(row1==1) // While col1 is high, if row 1 is high then '1' is pressed |
andrewbw01 | 0:7d75e4df9577 | 184 | { |
andrewbw01 | 0:7d75e4df9577 | 185 | while(row1==1){} // Loop while button is pressed down |
andrewbw01 | 0:7d75e4df9577 | 186 | // On releasing the button return the button character and exit function |
andrewbw01 | 0:7d75e4df9577 | 187 | |
andrewbw01 | 0:7d75e4df9577 | 188 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 189 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 190 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 191 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 192 | return('1'); // return character 1 and exit function |
andrewbw01 | 0:7d75e4df9577 | 193 | } |
andrewbw01 | 0:7d75e4df9577 | 194 | else if(row2==1) // While col1 is high, if row 2 is high then '4' is pressed |
andrewbw01 | 0:7d75e4df9577 | 195 | { |
andrewbw01 | 0:7d75e4df9577 | 196 | while(row2==1){} |
andrewbw01 | 0:7d75e4df9577 | 197 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 198 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 199 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 200 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 201 | return('4'); // Exit function and return |
andrewbw01 | 0:7d75e4df9577 | 202 | } |
andrewbw01 | 0:7d75e4df9577 | 203 | else if(row3==1) // While col1 is high, if row 3 is high then '7' is pressed |
andrewbw01 | 0:7d75e4df9577 | 204 | { |
andrewbw01 | 0:7d75e4df9577 | 205 | while(row3==1){} |
andrewbw01 | 0:7d75e4df9577 | 206 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 207 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 208 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 209 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 210 | return('7'); |
andrewbw01 | 0:7d75e4df9577 | 211 | } |
andrewbw01 | 0:7d75e4df9577 | 212 | else if(row4==1) // While col1 is high, if row 4 is high then '*' is pressed |
andrewbw01 | 0:7d75e4df9577 | 213 | { |
andrewbw01 | 0:7d75e4df9577 | 214 | while(row4==1){} |
andrewbw01 | 0:7d75e4df9577 | 215 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 216 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 217 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 218 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 219 | return('*'); |
andrewbw01 | 0:7d75e4df9577 | 220 | } |
andrewbw01 | 0:7d75e4df9577 | 221 | |
andrewbw01 | 0:7d75e4df9577 | 222 | |
andrewbw01 | 0:7d75e4df9577 | 223 | |
andrewbw01 | 0:7d75e4df9577 | 224 | // Test for any key pressed on column 2 |
andrewbw01 | 0:7d75e4df9577 | 225 | // Pull col2 high and keep columns 1 and 3 low |
andrewbw01 | 0:7d75e4df9577 | 226 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 227 | col2=1; |
andrewbw01 | 0:7d75e4df9577 | 228 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 229 | |
andrewbw01 | 0:7d75e4df9577 | 230 | if(row1==1) // While col2 is high, if row 1 is high then '2' is pressed |
andrewbw01 | 0:7d75e4df9577 | 231 | { |
andrewbw01 | 0:7d75e4df9577 | 232 | while(row1==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 233 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 234 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 235 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 236 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 237 | return('2'); |
andrewbw01 | 0:7d75e4df9577 | 238 | } |
andrewbw01 | 0:7d75e4df9577 | 239 | else if(row2==1) // While col2 is high, if row 2 is high then '5' is pressed |
andrewbw01 | 0:7d75e4df9577 | 240 | { |
andrewbw01 | 0:7d75e4df9577 | 241 | while(row2==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 242 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 243 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 244 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 245 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 246 | return('5'); |
andrewbw01 | 0:7d75e4df9577 | 247 | } |
andrewbw01 | 0:7d75e4df9577 | 248 | else if(row3==1) // While col2 is high, if row 3 is high then '8' is pressed |
andrewbw01 | 0:7d75e4df9577 | 249 | { |
andrewbw01 | 0:7d75e4df9577 | 250 | while(row3==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 251 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 252 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 253 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 254 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 255 | return('8'); |
andrewbw01 | 0:7d75e4df9577 | 256 | } |
andrewbw01 | 0:7d75e4df9577 | 257 | else if(row4==1) // While col2 is high, if row 4 is high then '0' is pressed |
andrewbw01 | 0:7d75e4df9577 | 258 | { |
andrewbw01 | 0:7d75e4df9577 | 259 | while(row4==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 260 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 261 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 262 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 263 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 264 | return('0'); |
andrewbw01 | 0:7d75e4df9577 | 265 | } |
andrewbw01 | 0:7d75e4df9577 | 266 | |
andrewbw01 | 0:7d75e4df9577 | 267 | |
andrewbw01 | 0:7d75e4df9577 | 268 | // Test for any key pressed on column 3 |
andrewbw01 | 0:7d75e4df9577 | 269 | // Pull col3 high and keep columns 1 and 2 low |
andrewbw01 | 0:7d75e4df9577 | 270 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 271 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 272 | col3=1; |
andrewbw01 | 0:7d75e4df9577 | 273 | |
andrewbw01 | 0:7d75e4df9577 | 274 | if(row1==1) // While col3 is high, if row 1 is high then '3' is pressed |
andrewbw01 | 0:7d75e4df9577 | 275 | { |
andrewbw01 | 0:7d75e4df9577 | 276 | while(row1==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 277 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 278 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 279 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 280 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 281 | return('3'); |
andrewbw01 | 0:7d75e4df9577 | 282 | } |
andrewbw01 | 0:7d75e4df9577 | 283 | else if(row2==1) // While col3 is high, if row 2 is high then '6' is pressed |
andrewbw01 | 0:7d75e4df9577 | 284 | { |
andrewbw01 | 0:7d75e4df9577 | 285 | while(row2==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 286 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 287 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 288 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 289 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 290 | return('6'); |
andrewbw01 | 0:7d75e4df9577 | 291 | } |
andrewbw01 | 0:7d75e4df9577 | 292 | else if(row3==1) // While col3 is high, if row 3 is high then '9' is pressed |
andrewbw01 | 0:7d75e4df9577 | 293 | { |
andrewbw01 | 0:7d75e4df9577 | 294 | while(row3==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 295 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 296 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 297 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 298 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 299 | return('9'); |
andrewbw01 | 0:7d75e4df9577 | 300 | } |
andrewbw01 | 0:7d75e4df9577 | 301 | else if(row4==1) // While col3 is high, if row 4 is high then '#' is pressed |
andrewbw01 | 0:7d75e4df9577 | 302 | { |
andrewbw01 | 0:7d75e4df9577 | 303 | while(row4==1){} // Loop |
andrewbw01 | 0:7d75e4df9577 | 304 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 305 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 306 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 307 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 308 | return('#'); |
andrewbw01 | 0:7d75e4df9577 | 309 | } |
andrewbw01 | 0:7d75e4df9577 | 310 | |
andrewbw01 | 0:7d75e4df9577 | 311 | // Pull all colums low to end |
andrewbw01 | 0:7d75e4df9577 | 312 | col1=0; |
andrewbw01 | 0:7d75e4df9577 | 313 | col2=0; |
andrewbw01 | 0:7d75e4df9577 | 314 | col3=0; |
andrewbw01 | 0:7d75e4df9577 | 315 | |
andrewbw01 | 0:7d75e4df9577 | 316 | |
andrewbw01 | 0:7d75e4df9577 | 317 | // ***** Return value ****** |
andrewbw01 | 0:7d75e4df9577 | 318 | return(' '); // If no key is pressed then return ' ' a space character. |
andrewbw01 | 0:7d75e4df9577 | 319 | |
andrewbw01 | 0:7d75e4df9577 | 320 | } // Keypad Function End |
andrewbw01 | 0:7d75e4df9577 | 321 | |
andrewbw01 | 0:7d75e4df9577 | 322 | |
andrewbw01 | 0:7d75e4df9577 | 323 | |
andrewbw01 | 0:7d75e4df9577 | 324 | |
andrewbw01 | 0:7d75e4df9577 | 325 | // Function to display a character on 7 segment LED display |
andrewbw01 | 0:7d75e4df9577 | 326 | void DisplayChar(char disp_char) |
andrewbw01 | 0:7d75e4df9577 | 327 | { |
andrewbw01 | 0:7d75e4df9577 | 328 | switch(disp_char) |
andrewbw01 | 0:7d75e4df9577 | 329 | { |
andrewbw01 | 0:7d75e4df9577 | 330 | case '0': |
andrewbw01 | 0:7d75e4df9577 | 331 | LED_Disp = ~0x3F; // Bit pattern for '0' = ~0xC0 |
andrewbw01 | 0:7d75e4df9577 | 332 | break; |
andrewbw01 | 0:7d75e4df9577 | 333 | case '1': |
andrewbw01 | 0:7d75e4df9577 | 334 | LED_Disp = ~0x06; // Bit pattern for '1' = ~0xF9 |
andrewbw01 | 0:7d75e4df9577 | 335 | break; |
andrewbw01 | 0:7d75e4df9577 | 336 | case '2': |
andrewbw01 | 0:7d75e4df9577 | 337 | LED_Disp = ~0x5B; // Bit pattern for '2' = ~0xA4 |
andrewbw01 | 0:7d75e4df9577 | 338 | break; |
andrewbw01 | 0:7d75e4df9577 | 339 | case '3': |
andrewbw01 | 0:7d75e4df9577 | 340 | LED_Disp = ~0x4F; // Bit pattern for '3' = ~0xB0 |
andrewbw01 | 0:7d75e4df9577 | 341 | break; |
andrewbw01 | 0:7d75e4df9577 | 342 | case '4': |
andrewbw01 | 0:7d75e4df9577 | 343 | LED_Disp = ~0x66; // Bit pattern for '4' = ~0x99 |
andrewbw01 | 0:7d75e4df9577 | 344 | break; |
andrewbw01 | 0:7d75e4df9577 | 345 | case '5': |
andrewbw01 | 0:7d75e4df9577 | 346 | LED_Disp = ~0x6D; // Bit pattern for '5' = ~0x92 |
andrewbw01 | 0:7d75e4df9577 | 347 | break; |
andrewbw01 | 0:7d75e4df9577 | 348 | case '6': |
andrewbw01 | 0:7d75e4df9577 | 349 | LED_Disp = ~0x7D; // Bit pattern for '6' = ~0x82 |
andrewbw01 | 0:7d75e4df9577 | 350 | break; |
andrewbw01 | 0:7d75e4df9577 | 351 | case '7': |
andrewbw01 | 0:7d75e4df9577 | 352 | LED_Disp = ~0x07; // Bit pattern for '7' = ~0xF8 |
andrewbw01 | 0:7d75e4df9577 | 353 | break; |
andrewbw01 | 0:7d75e4df9577 | 354 | case '8': |
andrewbw01 | 0:7d75e4df9577 | 355 | LED_Disp = ~0x7F; // Bit pattern for '8' = ~0x80 |
andrewbw01 | 0:7d75e4df9577 | 356 | break; |
andrewbw01 | 0:7d75e4df9577 | 357 | case '9': |
andrewbw01 | 0:7d75e4df9577 | 358 | LED_Disp = ~0x67; // Bit pattern for '9' = ~0x98 |
andrewbw01 | 0:7d75e4df9577 | 359 | break; |
andrewbw01 | 0:7d75e4df9577 | 360 | case 'A': |
andrewbw01 | 0:7d75e4df9577 | 361 | LED_Disp = ~0x77; // Bit pattern for 'A' = ~0x77 |
andrewbw01 | 0:7d75e4df9577 | 362 | break; |
andrewbw01 | 0:7d75e4df9577 | 363 | case 'B': |
andrewbw01 | 0:7d75e4df9577 | 364 | LED_Disp = ~0x7F; // Bit pattern for 'B' = ~0x7F |
andrewbw01 | 0:7d75e4df9577 | 365 | break; |
andrewbw01 | 0:7d75e4df9577 | 366 | case 'C': |
andrewbw01 | 0:7d75e4df9577 | 367 | LED_Disp = ~0x39; // Bit pattern for 'C' = ~0x39 |
andrewbw01 | 0:7d75e4df9577 | 368 | break; |
andrewbw01 | 0:7d75e4df9577 | 369 | case 'D': |
andrewbw01 | 0:7d75e4df9577 | 370 | LED_Disp = ~0x3F; // Bit pattern for 'D' = ~0x3F |
andrewbw01 | 0:7d75e4df9577 | 371 | break; |
andrewbw01 | 0:7d75e4df9577 | 372 | case 'E': |
andrewbw01 | 0:7d75e4df9577 | 373 | LED_Disp = ~0x79; // Bit pattern for 'E' = ~0x79 |
andrewbw01 | 0:7d75e4df9577 | 374 | break; |
andrewbw01 | 0:7d75e4df9577 | 375 | case 'F': |
andrewbw01 | 0:7d75e4df9577 | 376 | LED_Disp = ~0x71; // Bit pattern for 'F' = ~0x71 |
andrewbw01 | 0:7d75e4df9577 | 377 | break; |
andrewbw01 | 0:7d75e4df9577 | 378 | case 'G': |
andrewbw01 | 0:7d75e4df9577 | 379 | LED_Disp = ~0x7D; // Bit pattern for 'G' = ~0x7D |
andrewbw01 | 0:7d75e4df9577 | 380 | break; |
andrewbw01 | 0:7d75e4df9577 | 381 | case 'H': // Display character 'H' = ~0x77 |
andrewbw01 | 0:7d75e4df9577 | 382 | LED_Disp = ~0x76; |
andrewbw01 | 0:7d75e4df9577 | 383 | break; |
andrewbw01 | 0:7d75e4df9577 | 384 | case 'I': // Display character 'I' = ~0x06 |
andrewbw01 | 0:7d75e4df9577 | 385 | LED_Disp = ~0x06; |
andrewbw01 | 0:7d75e4df9577 | 386 | break; |
andrewbw01 | 0:7d75e4df9577 | 387 | case 'L': // Display character 'L' = ~0x38 |
andrewbw01 | 0:7d75e4df9577 | 388 | LED_Disp = ~0x38; |
andrewbw01 | 0:7d75e4df9577 | 389 | break; |
andrewbw01 | 0:7d75e4df9577 | 390 | case 'S': |
andrewbw01 | 0:7d75e4df9577 | 391 | LED_Disp = ~0x6D; // Bit pattern for 'S' = ~0x92 |
andrewbw01 | 0:7d75e4df9577 | 392 | break; |
andrewbw01 | 0:7d75e4df9577 | 393 | case ' ': // Display character ' ' = ~0x00 |
andrewbw01 | 0:7d75e4df9577 | 394 | LED_Disp = ~0x00; |
andrewbw01 | 0:7d75e4df9577 | 395 | break; |
andrewbw01 | 0:7d75e4df9577 | 396 | case '*': // keypad '*' character |
andrewbw01 | 0:7d75e4df9577 | 397 | LED_Disp = ~0x63; // Top 4 segments |
andrewbw01 | 0:7d75e4df9577 | 398 | break; |
andrewbw01 | 0:7d75e4df9577 | 399 | case '#': // keypad '#' character |
andrewbw01 | 0:7d75e4df9577 | 400 | LED_Disp = ~0x5C; // Bottom 4 segments |
andrewbw01 | 0:7d75e4df9577 | 401 | break; |
andrewbw01 | 0:7d75e4df9577 | 402 | } |
andrewbw01 | 0:7d75e4df9577 | 403 | } |