shift register
Shifter.cpp@0:7af9b977e6e9, 2018-02-21 (annotated)
- Committer:
- billycorgan123
- Date:
- Wed Feb 21 19:37:59 2018 +0000
- Revision:
- 0:7af9b977e6e9
rev1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
billycorgan123 | 0:7af9b977e6e9 | 1 | // Include the standard types |
billycorgan123 | 0:7af9b977e6e9 | 2 | #include "Shifter.h" |
billycorgan123 | 0:7af9b977e6e9 | 3 | |
billycorgan123 | 0:7af9b977e6e9 | 4 | #define LOW 0 |
billycorgan123 | 0:7af9b977e6e9 | 5 | #define HIGH 1 |
billycorgan123 | 0:7af9b977e6e9 | 6 | DigitalOut SER_Pin(D4); |
billycorgan123 | 0:7af9b977e6e9 | 7 | DigitalOut RCLK_Pin(D3); |
billycorgan123 | 0:7af9b977e6e9 | 8 | DigitalOut SRCLK_Pin(D2); |
billycorgan123 | 0:7af9b977e6e9 | 9 | int Number_of_Registers=2; |
billycorgan123 | 0:7af9b977e6e9 | 10 | |
billycorgan123 | 0:7af9b977e6e9 | 11 | void Shifter::write() |
billycorgan123 | 0:7af9b977e6e9 | 12 | { |
billycorgan123 | 0:7af9b977e6e9 | 13 | //Set and display registers |
billycorgan123 | 0:7af9b977e6e9 | 14 | //Only call AFTER all values are set how you would like (slow otherwise) |
billycorgan123 | 0:7af9b977e6e9 | 15 | RCLK_Pin.write(LOW); |
billycorgan123 | 0:7af9b977e6e9 | 16 | |
billycorgan123 | 0:7af9b977e6e9 | 17 | //iterate through the registers |
billycorgan123 | 0:7af9b977e6e9 | 18 | for(int i = Number_of_Registers - 1; i >= 0; i--) |
billycorgan123 | 0:7af9b977e6e9 | 19 | { |
billycorgan123 | 0:7af9b977e6e9 | 20 | //iterate through the bits in each registers |
billycorgan123 | 0:7af9b977e6e9 | 21 | for(int j = 8 - 1; j >= 0; j--) |
billycorgan123 | 0:7af9b977e6e9 | 22 | { |
billycorgan123 | 0:7af9b977e6e9 | 23 | SRCLK_Pin.write(LOW); |
billycorgan123 | 0:7af9b977e6e9 | 24 | wait_us(1); |
billycorgan123 | 0:7af9b977e6e9 | 25 | int val = shiftRegisters[i] & (1 << j); |
billycorgan123 | 0:7af9b977e6e9 | 26 | SER_Pin.write(val); |
billycorgan123 | 0:7af9b977e6e9 | 27 | wait_us(1); |
billycorgan123 | 0:7af9b977e6e9 | 28 | SRCLK_Pin.write(HIGH); |
billycorgan123 | 0:7af9b977e6e9 | 29 | } |
billycorgan123 | 0:7af9b977e6e9 | 30 | |
billycorgan123 | 0:7af9b977e6e9 | 31 | } |
billycorgan123 | 0:7af9b977e6e9 | 32 | RCLK_Pin.write(HIGH); |
billycorgan123 | 0:7af9b977e6e9 | 33 | } |
billycorgan123 | 0:7af9b977e6e9 | 34 | |
billycorgan123 | 0:7af9b977e6e9 | 35 | void Shifter::setPin(int index, bool val) |
billycorgan123 | 0:7af9b977e6e9 | 36 | { |
billycorgan123 | 0:7af9b977e6e9 | 37 | int byteIndex = index/8; |
billycorgan123 | 0:7af9b977e6e9 | 38 | int bitIndex = index % 8; |
billycorgan123 | 0:7af9b977e6e9 | 39 | char current = shiftRegisters[byteIndex]; |
billycorgan123 | 0:7af9b977e6e9 | 40 | current &= ~(1 << bitIndex); //clear the bit |
billycorgan123 | 0:7af9b977e6e9 | 41 | current |= val << bitIndex; //set the bit |
billycorgan123 | 0:7af9b977e6e9 | 42 | shiftRegisters[byteIndex] = current; //set the value |
billycorgan123 | 0:7af9b977e6e9 | 43 | } |
billycorgan123 | 0:7af9b977e6e9 | 44 | |
billycorgan123 | 0:7af9b977e6e9 | 45 | void Shifter::setAll(bool val) |
billycorgan123 | 0:7af9b977e6e9 | 46 | { |
billycorgan123 | 0:7af9b977e6e9 | 47 | //set all register pins to LOW |
billycorgan123 | 0:7af9b977e6e9 | 48 | for(int i = Number_of_Registers * 8 - 1; i >= 0; i--){ setPin(i, val); } |
billycorgan123 | 0:7af9b977e6e9 | 49 | } |
billycorgan123 | 0:7af9b977e6e9 | 50 | |
billycorgan123 | 0:7af9b977e6e9 | 51 | void Shifter::clear() |
billycorgan123 | 0:7af9b977e6e9 | 52 | { |
billycorgan123 | 0:7af9b977e6e9 | 53 | //set all register pins to LOW |
billycorgan123 | 0:7af9b977e6e9 | 54 | for(int i = Number_of_Registers * 8 - 1; i >= 0; i--){ setPin(i, LOW); } |
billycorgan123 | 0:7af9b977e6e9 | 55 | } |