Pre-class Assignment
Complete the participation activities in sections 4.1 and 4.2 (you do not need to do the blue challenge activities). Start working on lab 1 (help is available during lab Weds.)
Announcements
- Lab 1 is out and will be due next Tuesday Feb 7
- I have a conflict with lab hours Weds. Feb 1, so I will have lab hours 2pm-4pm Fri. Feb 3 in Singer 221. You can also schedule office hours with me or post on Ed.
- Office hours: by appointment (email me if none of those times work)
Learning Objectives and Outcomes
In this lecture you will:
- listen to a lecture about how to configure GPIOs using individual pins and using busses in Mbed OS and manipulate bits using bitwise manipulation, and apply that knowledge to reading key presses on a 4 x 4 matrix membrane key pad
By the end of this lecture you should be able to:
- Complete exercises 4 and 5 on the lab
Lecture Content
- Introduction to key pad
- GPIOs
DigitalIn
, DigitalOut
BusIn
, BusOut
- See code examples from class
- Programming the keypad
- Setup
- Set up each row as an input with pullup
- Set up each column as an output
- Set column one low.
- Check each row. If the row is low, return button press.
- Repeat until all columns have been tested.
- Practice: bitwise manipulation
- Challenge activities 4.2.1, 4.2.2
- Example bitwise manipulation use cases:
- key pad
- getting a digit (0 to 9)
- converting from upper case to lower case in ASCII (and vice versa)
- Bitwise manipulation cheat sheet. To perform an operation on the nth bit for a variable named
test
:
- test bit n: test & (1 << n)
- set bit n: test |= (1 << n);
- clear bit n: test &= ~(1 << n);
- toggle bit n: test ^= (1 << n);
Example codes
#include "mbed.h"
DigitalIn row1(ARDUINO_UNO_D2,PullUp);
DigitalIn row2(ARDUINO_UNO_D3,PullUp);
DigitalIn row3(ARDUINO_UNO_D4,PullUp);
DigitalIn row4(ARDUINO_UNO_D5,PullUp);
DigitalOut col1(ARDUINO_UNO_D6);
int main() {
col1 = 1;
// wait indefinitely for keypress
while (1){
col1 = 0;
if (!row1){
printf("1");
}
else if (!row2){
printf("4");
}
else if (!row3){
printf("7");
}
else if (!row4){
printf("*");
}
}
}