Board Basis für Auftrag
Embed:
(wiki syntax)
Show/hide line numbers
ShiftOut.cpp
00001 #include "mbed.h" 00002 #include "ShiftOut.h" 00003 00004 00005 #define SET_LATCH() (LATCH = 0) 00006 #define RESET_LATCH() (LATCH = 1) 00007 00008 #define ENABLE_RESET() (RESET = 0) 00009 #define DISABLE_RESET() (RESET = 1) 00010 00011 static char stateArr[8*REGISTER_CNT] = {0}; 00012 static bool hasChanged = false; 00013 00014 ShiftOut::ShiftOut(PinName ser, PinName srclk, PinName rclk, 00015 PinName oe, PinName reset) : DSERIAL(ser), SRCLK(srclk), RCLK(rclk), LATCH(oe), RESET(reset) 00016 { 00017 writeByte(0x00); // Reset the values of the registers to 0 00018 if(RESET != NC){ 00019 DISABLE_RESET(); 00020 } 00021 } 00022 00023 //Pulses the register 00024 void ShiftOut::updateRegister(){ 00025 SRCLK = 1; 00026 wait_us(2); 00027 SRCLK = 0; 00028 } 00029 //Updates the output register 00030 void ShiftOut::updateOutput(){ 00031 RCLK = 1; 00032 wait_us(2); 00033 RCLK = 0; 00034 } 00035 //Writes a byte to the shift register 00036 void ShiftOut::writeByte(unsigned char byte){ 00037 hasChanged = true; 00038 for(int i = 0; i<8*REGISTER_CNT; i++){ 00039 DSERIAL = (byte & 0x01<<i)>>i; 00040 updateRegister(); 00041 } 00042 updateOutput(); 00043 } 00044 //Writes a byte to the shift register 00045 void ShiftOut::write2Byte(unsigned int word){ 00046 hasChanged = true; 00047 for(int i = 0; i<8*REGISTER_CNT; i++){ 00048 DSERIAL = (word & 0x01<<i)>>i; 00049 updateRegister(); 00050 } 00051 updateOutput(); 00052 } 00053 //Writes a bit to the shift register 00054 void ShiftOut::writeBit(unsigned char bit){ 00055 DSERIAL = bit & 0x01; 00056 updateRegister(); 00057 updateOutput(); 00058 } 00059 //Writes multiple bits from an array to create an animation 00060 void ShiftOut::animate(int arr[][8], int lines, int delay_ms){ 00061 hasChanged = true; 00062 for(int i = 0; i < lines; i++){ 00063 for(int j = 0; j < 8; j++){ 00064 writeBit(arr[i][j]); 00065 } 00066 wait_ms(delay_ms); 00067 } 00068 } 00069 00070 void ShiftOut::animationExample(){ 00071 hasChanged = true; 00072 int strobe[][8]= {{1,0,0,0,0,0,0,0}, 00073 {0,1,0,0,0,0,0,0}, 00074 {0,0,1,0,0,0,0,0}, 00075 {0,0,0,1,0,0,0,0}, 00076 {0,0,0,0,1,0,0,0}, 00077 {0,0,0,0,0,1,0,0}, 00078 {0,0,0,0,0,0,1,0}, 00079 {0,0,0,0,0,0,0,1}}; 00080 00081 int nightrider[18][8]= {{1,0,0,0,0,0,0,0}, 00082 {1,1,0,0,0,0,0,0}, 00083 {1,1,1,0,0,0,0,0}, 00084 {0,1,1,1,0,0,0,0}, 00085 {0,0,1,1,1,0,0,0}, 00086 {0,0,0,1,1,1,0,0}, 00087 {0,0,0,0,1,1,1,0}, 00088 {0,0,0,0,0,1,1,1}, 00089 {0,0,0,0,0,0,1,1}, 00090 {0,0,0,0,0,0,0,1}, 00091 {0,0,0,0,0,0,1,1}, 00092 {0,0,0,0,0,1,1,1}, 00093 {0,0,0,0,1,1,1,0}, 00094 {0,0,0,1,1,1,0,0}, 00095 {0,0,1,1,1,0,0,0}, 00096 {0,1,1,1,0,0,0,0}, 00097 {1,1,1,0,0,0,0,0}, 00098 {1,1,0,0,0,0,0,0}}; 00099 00100 animate(nightrider, 18, 50); 00101 wait(1); 00102 animate(strobe, 8, 200); 00103 } 00104 00105 void ShiftOut::writeBitAtPos(unsigned char pin, bool state){ 00106 if(hasChanged){ 00107 clearStateArray(); 00108 hasChanged = false; 00109 } 00110 if(pin < 8*REGISTER_CNT){ 00111 stateArr[pin] = state; 00112 } 00113 writeArray(stateArr); 00114 } 00115 00116 void ShiftOut::writeArray(char arr[8*REGISTER_CNT]){ 00117 for(int i = (8*REGISTER_CNT)-1; i >= 0; i--) { 00118 writeBit(arr[i]); 00119 } 00120 00121 } 00122 00123 void ShiftOut::clearStateArray(){ 00124 for(int i = 0; i < 8*REGISTER_CNT; i++){ 00125 stateArr[i] = 0; 00126 } 00127 }
Generated on Tue Jul 12 2022 17:26:08 by
1.7.2