Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ShiftOut.cpp@3:a0df8989ffa2, 2015-12-25 (annotated)
- Committer:
- benrammok
- Date:
- Fri Dec 25 21:40:48 2015 +0000
- Revision:
- 3:a0df8989ffa2
- Parent:
- 0:e576e892f0ca
- Child:
- 4:61198ffab8ca
Small update to the cpp file, added a clear function for the stateArray.
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 | 3:a0df8989ffa2 | 12 | static bool hasChanged = false; |
| benrammok | 0:e576e892f0ca | 13 | |
| benrammok | 0:e576e892f0ca | 14 | ShiftOut::ShiftOut(PinName ser, PinName srclk, PinName rclk, |
| benrammok | 0:e576e892f0ca | 15 | PinName oe, PinName reset) : DSERIAL(ser), LATCH(oe), SRCLK(srclk), RCLK(rclk), RESET(reset) |
| benrammok | 0:e576e892f0ca | 16 | { |
| benrammok | 0:e576e892f0ca | 17 | writeByte(0x00); // Reset the values of the registers to 0 |
| benrammok | 0:e576e892f0ca | 18 | if(RESET != NC){ |
| benrammok | 0:e576e892f0ca | 19 | DISABLE_RESET(); |
| benrammok | 0:e576e892f0ca | 20 | } |
| benrammok | 0:e576e892f0ca | 21 | } |
| benrammok | 0:e576e892f0ca | 22 | |
| benrammok | 0:e576e892f0ca | 23 | //Pulses the register |
| benrammok | 0:e576e892f0ca | 24 | void ShiftOut::updateRegister(){ |
| benrammok | 0:e576e892f0ca | 25 | SRCLK = 1; |
| benrammok | 0:e576e892f0ca | 26 | wait_us(2); |
| benrammok | 0:e576e892f0ca | 27 | SRCLK = 0; |
| benrammok | 0:e576e892f0ca | 28 | } |
| benrammok | 0:e576e892f0ca | 29 | |
| benrammok | 0:e576e892f0ca | 30 | void ShiftOut::updateOutput(){ |
| benrammok | 0:e576e892f0ca | 31 | RCLK = 1; |
| benrammok | 0:e576e892f0ca | 32 | wait_us(2); |
| benrammok | 0:e576e892f0ca | 33 | RCLK = 0; |
| benrammok | 0:e576e892f0ca | 34 | } |
| benrammok | 0:e576e892f0ca | 35 | |
| benrammok | 0:e576e892f0ca | 36 | void ShiftOut::writeByte(unsigned char byte){ |
| benrammok | 3:a0df8989ffa2 | 37 | hasChanged = true; |
| benrammok | 0:e576e892f0ca | 38 | for(int i = 0; i<8; i++){ |
| benrammok | 0:e576e892f0ca | 39 | DSERIAL = (byte & 0x01<<i)>>i; |
| benrammok | 0:e576e892f0ca | 40 | updateRegister(); |
| benrammok | 0:e576e892f0ca | 41 | } |
| benrammok | 0:e576e892f0ca | 42 | updateOutput(); |
| benrammok | 0:e576e892f0ca | 43 | } |
| benrammok | 0:e576e892f0ca | 44 | |
| benrammok | 0:e576e892f0ca | 45 | void ShiftOut::writeBit(unsigned char bit){ |
| benrammok | 0:e576e892f0ca | 46 | DSERIAL = bit & 0x01; |
| benrammok | 0:e576e892f0ca | 47 | updateRegister(); |
| benrammok | 0:e576e892f0ca | 48 | updateOutput(); |
| benrammok | 0:e576e892f0ca | 49 | } |
| benrammok | 0:e576e892f0ca | 50 | |
| benrammok | 0:e576e892f0ca | 51 | void ShiftOut::animate(int arr[][8], int lines, int delay_ms){ |
| benrammok | 3:a0df8989ffa2 | 52 | hasChanged = true; |
| benrammok | 0:e576e892f0ca | 53 | for(int i = 0; i < lines; i++){ |
| benrammok | 0:e576e892f0ca | 54 | for(int j = 0; j < 8; j++){ |
| benrammok | 0:e576e892f0ca | 55 | writeBit(arr[i][j]); |
| benrammok | 0:e576e892f0ca | 56 | } |
| benrammok | 0:e576e892f0ca | 57 | wait_ms(delay_ms); |
| benrammok | 0:e576e892f0ca | 58 | } |
| benrammok | 0:e576e892f0ca | 59 | } |
| benrammok | 0:e576e892f0ca | 60 | |
| benrammok | 0:e576e892f0ca | 61 | void ShiftOut::animationExample(){ |
| benrammok | 3:a0df8989ffa2 | 62 | hasChanged = true; |
| benrammok | 3:a0df8989ffa2 | 63 | int strobe[][8]= {{1,0,0,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 64 | {0,1,0,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 65 | {0,0,1,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 66 | {0,0,0,1,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 67 | {0,0,0,0,1,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 68 | {0,0,0,0,0,1,0,0}, |
| benrammok | 3:a0df8989ffa2 | 69 | {0,0,0,0,0,0,1,0}, |
| benrammok | 3:a0df8989ffa2 | 70 | {0,0,0,0,0,0,0,1}}; |
| benrammok | 0:e576e892f0ca | 71 | |
| benrammok | 3:a0df8989ffa2 | 72 | int nightrider[18][8]= {{1,0,0,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 73 | {1,1,0,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 74 | {1,1,1,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 75 | {0,1,1,1,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 76 | {0,0,1,1,1,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 77 | {0,0,0,1,1,1,0,0}, |
| benrammok | 3:a0df8989ffa2 | 78 | {0,0,0,0,1,1,1,0}, |
| benrammok | 3:a0df8989ffa2 | 79 | {0,0,0,0,0,1,1,1}, |
| benrammok | 3:a0df8989ffa2 | 80 | {0,0,0,0,0,0,1,1}, |
| benrammok | 3:a0df8989ffa2 | 81 | {0,0,0,0,0,0,0,1}, |
| benrammok | 3:a0df8989ffa2 | 82 | {0,0,0,0,0,0,1,1}, |
| benrammok | 3:a0df8989ffa2 | 83 | {0,0,0,0,0,1,1,1}, |
| benrammok | 3:a0df8989ffa2 | 84 | {0,0,0,0,1,1,1,0}, |
| benrammok | 3:a0df8989ffa2 | 85 | {0,0,0,1,1,1,0,0}, |
| benrammok | 3:a0df8989ffa2 | 86 | {0,0,1,1,1,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 87 | {0,1,1,1,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 88 | {1,1,1,0,0,0,0,0}, |
| benrammok | 3:a0df8989ffa2 | 89 | {1,1,0,0,0,0,0,0}}; |
| benrammok | 0:e576e892f0ca | 90 | |
| benrammok | 0:e576e892f0ca | 91 | animate(nightrider, 18, 50); |
| benrammok | 0:e576e892f0ca | 92 | wait(1); |
| benrammok | 0:e576e892f0ca | 93 | animate(strobe, 8, 200); |
| benrammok | 0:e576e892f0ca | 94 | } |
| benrammok | 0:e576e892f0ca | 95 | |
| benrammok | 0:e576e892f0ca | 96 | void ShiftOut::writeBitAtPos(unsigned char pin, bool state){ |
| benrammok | 3:a0df8989ffa2 | 97 | if(!hasChanged){ |
| benrammok | 3:a0df8989ffa2 | 98 | clearStateArray(); |
| benrammok | 3:a0df8989ffa2 | 99 | hasChanged = false; |
| benrammok | 3:a0df8989ffa2 | 100 | } |
| benrammok | 0:e576e892f0ca | 101 | if(pin < 8){ |
| benrammok | 0:e576e892f0ca | 102 | stateArr[pin] = state; |
| benrammok | 0:e576e892f0ca | 103 | } |
| benrammok | 0:e576e892f0ca | 104 | writeArray(stateArr); |
| benrammok | 0:e576e892f0ca | 105 | } |
| benrammok | 0:e576e892f0ca | 106 | |
| benrammok | 0:e576e892f0ca | 107 | void ShiftOut::writeArray(char arr[8]){ |
| benrammok | 0:e576e892f0ca | 108 | for(int i = 7; i >= 0; i--) { |
| benrammok | 0:e576e892f0ca | 109 | writeBit(arr[i]); |
| benrammok | 0:e576e892f0ca | 110 | } |
| benrammok | 3:a0df8989ffa2 | 111 | |
| benrammok | 3:a0df8989ffa2 | 112 | } |
| benrammok | 3:a0df8989ffa2 | 113 | |
| benrammok | 3:a0df8989ffa2 | 114 | void ShiftOut::clearStateArray(){ |
| benrammok | 3:a0df8989ffa2 | 115 | for(int i = 0; i < 8; i++){ |
| benrammok | 3:a0df8989ffa2 | 116 | stateArr[i] = 0; |
| benrammok | 3:a0df8989ffa2 | 117 | } |
| benrammok | 3:a0df8989ffa2 | 118 | } |