A library for interfacing with the SN74HC595N Shift register. Includes functions for writing bits, bytes, animation and bits at spesified positions.
ShiftOut.cpp@0:e576e892f0ca, 2015-12-25 (annotated)
- Committer:
- benrammok
- Date:
- Fri Dec 25 11:59:15 2015 +0000
- Revision:
- 0:e576e892f0ca
- Child:
- 3:a0df8989ffa2
Added ability to write a bit to spesified position
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benrammok | 0:e576e892f0ca | 1 | #include "mbed.h" |
benrammok | 0:e576e892f0ca | 2 | #include "ShiftOut.h" |
benrammok | 0:e576e892f0ca | 3 | |
benrammok | 0:e576e892f0ca | 4 | |
benrammok | 0:e576e892f0ca | 5 | #define SET_LATCH() (LATCH = 0) |
benrammok | 0:e576e892f0ca | 6 | #define RESET_LATCH() (LATCH = 1) |
benrammok | 0:e576e892f0ca | 7 | |
benrammok | 0:e576e892f0ca | 8 | #define ENABLE_RESET() (RESET = 0) |
benrammok | 0:e576e892f0ca | 9 | #define DISABLE_RESET() (RESET = 1) |
benrammok | 0:e576e892f0ca | 10 | |
benrammok | 0:e576e892f0ca | 11 | static char stateArr[8] = {0}; |
benrammok | 0:e576e892f0ca | 12 | |
benrammok | 0:e576e892f0ca | 13 | ShiftOut::ShiftOut(PinName ser, PinName srclk, PinName rclk, |
benrammok | 0:e576e892f0ca | 14 | PinName oe, PinName reset) : DSERIAL(ser), LATCH(oe), SRCLK(srclk), RCLK(rclk), RESET(reset) |
benrammok | 0:e576e892f0ca | 15 | { |
benrammok | 0:e576e892f0ca | 16 | writeByte(0x00); // Reset the values of the registers to 0 |
benrammok | 0:e576e892f0ca | 17 | if(RESET != NC){ |
benrammok | 0:e576e892f0ca | 18 | DISABLE_RESET(); |
benrammok | 0:e576e892f0ca | 19 | } |
benrammok | 0:e576e892f0ca | 20 | } |
benrammok | 0:e576e892f0ca | 21 | |
benrammok | 0:e576e892f0ca | 22 | //Pulses the register |
benrammok | 0:e576e892f0ca | 23 | void ShiftOut::updateRegister(){ |
benrammok | 0:e576e892f0ca | 24 | SRCLK = 1; |
benrammok | 0:e576e892f0ca | 25 | wait_us(2); |
benrammok | 0:e576e892f0ca | 26 | SRCLK = 0; |
benrammok | 0:e576e892f0ca | 27 | } |
benrammok | 0:e576e892f0ca | 28 | |
benrammok | 0:e576e892f0ca | 29 | void ShiftOut::updateOutput(){ |
benrammok | 0:e576e892f0ca | 30 | RCLK = 1; |
benrammok | 0:e576e892f0ca | 31 | wait_us(2); |
benrammok | 0:e576e892f0ca | 32 | RCLK = 0; |
benrammok | 0:e576e892f0ca | 33 | } |
benrammok | 0:e576e892f0ca | 34 | |
benrammok | 0:e576e892f0ca | 35 | void ShiftOut::writeByte(unsigned char byte){ |
benrammok | 0:e576e892f0ca | 36 | for(int i = 0; i<8; i++){ |
benrammok | 0:e576e892f0ca | 37 | DSERIAL = (byte & 0x01<<i)>>i; |
benrammok | 0:e576e892f0ca | 38 | updateRegister(); |
benrammok | 0:e576e892f0ca | 39 | } |
benrammok | 0:e576e892f0ca | 40 | updateOutput(); |
benrammok | 0:e576e892f0ca | 41 | } |
benrammok | 0:e576e892f0ca | 42 | |
benrammok | 0:e576e892f0ca | 43 | void ShiftOut::writeBit(unsigned char bit){ |
benrammok | 0:e576e892f0ca | 44 | DSERIAL = bit & 0x01; |
benrammok | 0:e576e892f0ca | 45 | updateRegister(); |
benrammok | 0:e576e892f0ca | 46 | updateOutput(); |
benrammok | 0:e576e892f0ca | 47 | } |
benrammok | 0:e576e892f0ca | 48 | |
benrammok | 0:e576e892f0ca | 49 | void ShiftOut::animate(int arr[][8], int lines, int delay_ms){ |
benrammok | 0:e576e892f0ca | 50 | for(int i = 0; i < lines; i++){ |
benrammok | 0:e576e892f0ca | 51 | for(int j = 0; j < 8; j++){ |
benrammok | 0:e576e892f0ca | 52 | writeBit(arr[i][j]); |
benrammok | 0:e576e892f0ca | 53 | } |
benrammok | 0:e576e892f0ca | 54 | wait_ms(delay_ms); |
benrammok | 0:e576e892f0ca | 55 | } |
benrammok | 0:e576e892f0ca | 56 | } |
benrammok | 0:e576e892f0ca | 57 | |
benrammok | 0:e576e892f0ca | 58 | void ShiftOut::animationExample(){ |
benrammok | 0:e576e892f0ca | 59 | int strobe[][8]= {{1,0,0,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 60 | {0,1,0,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 61 | {0,0,1,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 62 | {0,0,0,1,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 63 | {0,0,0,0,1,0,0,0}, |
benrammok | 0:e576e892f0ca | 64 | {0,0,0,0,0,1,0,0}, |
benrammok | 0:e576e892f0ca | 65 | {0,0,0,0,0,0,1,0}, |
benrammok | 0:e576e892f0ca | 66 | {0,0,0,0,0,0,0,1}}; |
benrammok | 0:e576e892f0ca | 67 | |
benrammok | 0:e576e892f0ca | 68 | int nightrider[18][8]= {{1,0,0,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 69 | {1,1,0,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 70 | {1,1,1,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 71 | {0,1,1,1,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 72 | {0,0,1,1,1,0,0,0}, |
benrammok | 0:e576e892f0ca | 73 | {0,0,0,1,1,1,0,0}, |
benrammok | 0:e576e892f0ca | 74 | {0,0,0,0,1,1,1,0}, |
benrammok | 0:e576e892f0ca | 75 | {0,0,0,0,0,1,1,1}, |
benrammok | 0:e576e892f0ca | 76 | {0,0,0,0,0,0,1,1}, |
benrammok | 0:e576e892f0ca | 77 | {0,0,0,0,0,0,0,1}, |
benrammok | 0:e576e892f0ca | 78 | {0,0,0,0,0,0,1,1}, |
benrammok | 0:e576e892f0ca | 79 | {0,0,0,0,0,1,1,1}, |
benrammok | 0:e576e892f0ca | 80 | {0,0,0,0,1,1,1,0}, |
benrammok | 0:e576e892f0ca | 81 | {0,0,0,1,1,1,0,0}, |
benrammok | 0:e576e892f0ca | 82 | {0,0,1,1,1,0,0,0}, |
benrammok | 0:e576e892f0ca | 83 | {0,1,1,1,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 84 | {1,1,1,0,0,0,0,0}, |
benrammok | 0:e576e892f0ca | 85 | {1,1,0,0,0,0,0,0}}; |
benrammok | 0:e576e892f0ca | 86 | |
benrammok | 0:e576e892f0ca | 87 | animate(nightrider, 18, 50); |
benrammok | 0:e576e892f0ca | 88 | wait(1); |
benrammok | 0:e576e892f0ca | 89 | animate(strobe, 8, 200); |
benrammok | 0:e576e892f0ca | 90 | } |
benrammok | 0:e576e892f0ca | 91 | |
benrammok | 0:e576e892f0ca | 92 | void ShiftOut::writeBitAtPos(unsigned char pin, bool state){ |
benrammok | 0:e576e892f0ca | 93 | |
benrammok | 0:e576e892f0ca | 94 | |
benrammok | 0:e576e892f0ca | 95 | if(pin < 8){ |
benrammok | 0:e576e892f0ca | 96 | stateArr[pin] = state; |
benrammok | 0:e576e892f0ca | 97 | } |
benrammok | 0:e576e892f0ca | 98 | writeArray(stateArr); |
benrammok | 0:e576e892f0ca | 99 | } |
benrammok | 0:e576e892f0ca | 100 | |
benrammok | 0:e576e892f0ca | 101 | void ShiftOut::writeArray(char arr[8]){ |
benrammok | 0:e576e892f0ca | 102 | for(int i = 7; i >= 0; i--) { |
benrammok | 0:e576e892f0ca | 103 | writeBit(arr[i]); |
benrammok | 0:e576e892f0ca | 104 | } |
benrammok | 0:e576e892f0ca | 105 | } |