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.h
00001 /* 00002 * Copyright 2015 Benjamin R. Moeklegaard 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef SHIFT_H 00018 #define SHIFT_H 00019 00020 //Constant for managing n-numbers of registers, Function supported (writeByte, writeBit, writeBitAtPos) 00021 #define REGISTER_CNT 1 00022 /** 00023 * This is a library for easy interfacing with the SN74HC595N 8-bit Shift Register. 00024 * The library includes functions for writing bits, bytes and animation array to the register. 00025 * The Functions are mainly based for writting 8-bits or one byte and can be moddified to work 00026 * with multiple shift registers. 00027 *@code 00028 * #include "mbed.h" 00029 * #include "ShiftOut.h" 00030 * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); 00031 * 00032 * int main(){ 00033 * while(1){ 00034 * reg.writeBitAtPos(3, 1); 00035 * wait(2); 00036 * reg.writeByte(0x30); 00037 * wait(2); 00038 * } 00039 * }@endcode 00040 */ 00041 00042 class ShiftOut 00043 { 00044 public: 00045 00046 /** Create a ShiftOut interface 00047 * 00048 * @param ser Serial data line 00049 * @param srclk Data register clock 00050 * @param rclk Output register clock 00051 * @param oe Output enable (Active Low) 00052 * @param reset Reset line for data register (Active Low) 00053 * Writing Byte Example: 00054 * @code 00055 * #include "mbed.h" 00056 * #include "ShiftOut.h" 00057 * 00058 * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); 00059 * int main() 00060 * { 00061 * reg.writeByte(0x00); //Writes each bit to the SN74HC595N 00062 * while(1) { 00063 * wait_ms(300); 00064 * } 00065 * } 00066 * @endcode 00067 */ 00068 00069 00070 ShiftOut(PinName ser, PinName srclk, PinName rclk, 00071 PinName oe, PinName reset); 00072 00073 /** 00074 * Writes a byte to the shift register 00075 * @param byte 0xXX or numbers from 0-255 00076 */ 00077 void writeByte(unsigned char); 00078 00079 00080 00081 00082 /** 00083 * Writes a bit to the first output on the shift register 00084 * @param bit 0 or 1 00085 */ 00086 00087 void writeBit(unsigned char); 00088 00089 /** 00090 * Writes bits from an 2D array, with configurable delay 00091 * @param int array, int lines, int delay_ms 00092 * writes a 2D array with n-lines with a configurable delay in ms 00093 * @code 00094 * #include "mbed.h" 00095 * #include "ShiftOut.h" 00096 * ShiftOut reg(PA_8, PA_9, PC_7, PB_6, D1); 00097 * 00098 * int main(){ 00099 * int strobe[][8]= {{1,0,0,0,0,0,0,0}, 00100 * {0,1,0,0,0,0,0,0}, 00101 * {0,0,1,0,0,0,0,0}, 00102 * {0,0,0,1,0,0,0,0}, 00103 * {0,0,0,0,1,0,0,0}, 00104 * {0,0,0,0,0,1,0,0}, 00105 * {0,0,0,0,0,0,1,0}, 00106 * {0,0,0,0,0,0,0,1}}; 00107 * while(1){ 00108 * reg.animate(strobe, 8, 200); 00109 * } 00110 * } 00111 * @endcode 00112 */ 00113 00114 void animate(int[][8], int, int); 00115 00116 /** 00117 * Demonstrates two animation examples by using the animate function 00118 */ 00119 00120 void animationExample(void); 00121 00122 /** 00123 * Writes the desired state to the output on the shift register 00124 * @param char output, state 00125 */ 00126 00127 void writeBitAtPos(unsigned char, bool); 00128 00129 /** 00130 * Writes the corresponding array item to the output on the shift register 00131 * @param char array writes to the output from a state array 00132 */ 00133 00134 void writeArray(char[8]); 00135 00136 protected: 00137 void updateRegister(void); 00138 void updateOutput(void); 00139 void clearStateArray(void); 00140 DigitalOut DSERIAL, LATCH, RCLK, SRCLK, RESET; 00141 00142 }; 00143 00144 #endif
Generated on Wed Jul 13 2022 14:06:48 by
