-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplexer.c
More file actions
72 lines (60 loc) · 1.46 KB
/
Multiplexer.c
File metadata and controls
72 lines (60 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Multiplexer.c
*
* Created on: Jul 1, 2015
* Author: John
*/
#include "Multiplexer.h"
uint8_t nextRow;
void Multiplexer_Init()
{
nextRow = 0;
// Initialize pins.
setHigh(P2OUT, QE_PIN);
enableGPIO(SELECT_1, SER_PIN);
enableGPIO(SELECT_2, QE_PIN);
enableGPIO(SELECT_2, RCLK_PIN);
setOutput(P1DIR, SER_PIN);
setOutput(P2DIR, RCLK_PIN);
setOutput(P2DIR, QE_PIN);
// Clear all rows and prepare the active "travelling" bit.
// Since there are two flip flops between the input and first
// output, three between the input and the second, four between
// the input and third, etc.. we need to buffer the active bit
// in the first flip flop.
Multiplexer_clearRows();
Multiplexer_setupRows();
}
void Multiplexer_setupRows()
{
// Buffer the active travelling bit.
setLow(P1OUT, SER_PIN);
pulse(P2OUT, RCLK_PIN);
// Switch from high impedance to output mode on register pins.
setLow(P2OUT, QE_PIN);
}
void Multiplexer_clearRows()
{
int i = 0;
// Switch to high impedance mode.
setHigh(P2OUT, QE_PIN);
// Turn off all rows.
for(i = 0; i < NUM_REGISTERS + 1; i++) {
setHigh(P1OUT, SER_PIN);
pulse(P2OUT, RCLK_PIN);
}
}
uint8_t Multiplexer_changeRow()
{
// The active travelling bit is delayed by one cycle so
// it needs to be clocked in at NUM_ROWS - 1 instead of 0.
if(nextRow == NUM_ROWS - 1) {
setLow(P1OUT, SER_PIN);
}
else {
setHigh(P1OUT, SER_PIN);
}
pulse(P2OUT, RCLK_PIN);
nextRow = (nextRow + 1) % NUM_ROWS;
return nextRow;
}