/* DL2019 - WWU Digital Logic interface card * RLS - 2019-10-10 * * Scans an array of 16 pushbuttons to set or reset 8 outputs. * Also generates a 1Hz clock. */ #include #include "TimerOne.h" const byte rows = 4; const byte cols = 4; char keys[rows][cols] = { {0x40, 0x41, 0x42, 0x43}, {0x44, 0x45, 0x46, 0x47}, {0x20, 0x21, 0x22, 0x23}, {0x24, 0x25, 0x26, 0x27} }; byte rowPins[rows] = {5,4,3,2}; byte colPins[cols] = {9,8,7,6}; Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols); byte leds[] = {19,18,17,16,15,14,13,12}; // led pins byte bitval = 0; // leds=pb4, pb5, pc0, pc1, pc2, pc3, pc4, pc5 void write_leds(int bits) { PORTB = (PORTB & 0xcf) | ((bits&3) << 4); PORTC = (PORTC & 0xc0) | ((bits>>2) & 0x3f); } void clear_leds() { int tmp; for (tmp=0; tmp<8; tmp++) digitalWrite(leds[tmp],1); } void setup() { byte bitno; // setup ports for (bitno=0; bitno<8; bitno++) pinMode(leds[bitno], OUTPUT); // setup 1Hz clock pinMode(11,OUTPUT); Timer1.initialize(500000); // 2Hz frequency Timer1.pwm(11,512); // 50% duty cycle, pin 11 Timer1.attachInterrupt(callback); clear_leds(); } void callback() { digitalWrite(11,digitalRead(11) ^ 1); } void loop() { byte key; byte bitno; char str[80]; key = keypad.getKey(); // read keypad if (key != NO_KEY) { bitno = key & 7; if (key & 0x40) // ON key bitval |= 1 << bitno; if (key & 0x20) // OFF key bitval &= ~(1 << bitno); // update LEDs write_leds(~bitval); // inverted buffers } }