J.W. BRUCE / TTU_CSC1300

Dependents:   CSC1300_EduBaseV2_Lab0 mbed_blinky EduBaseV2_Lab0 mbed_blinky ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TTU_CSC1300.h Source File

TTU_CSC1300.h

00001 // macros, defines, globals,and prototypes for the TTU_F031 library
00002 
00003 #ifndef TTU_CSC1300_H
00004 #define TTU_CSC1300_H
00005 
00006 #include "mbed.h"
00007 
00008 /*************************************************************************
00009 **
00010 ** Uncomment the following line IF (AND ONLY IF) you have removed SB18
00011 **   from the backside of the Nucleo-F031K6 to un-short pins PB_7 and
00012 **   PA_5.
00013 **
00014 *************************************************************************/
00015 // #define      USE_LED3
00016 
00017 
00018 /*************************************************************************
00019  * 
00020  * DEFINES and MACROS
00021  * 
00022  **************************************************************************/
00023 #define     FALSE           0
00024 #define     TRUE            (~FALSE)
00025 
00026 // some useful bitmasks
00027 #define     BIT0            0x00000001
00028 #define     BIT1            0x00000002
00029 #define     BIT2            0x00000004
00030 #define     BIT3            0x00000008
00031 #define     BIT4            0x00000010
00032 #define     BIT5            0x00000020
00033 #define     BIT6            0x00000040
00034 #define     BIT7            0x00000080
00035 #define     BIT8            0x00000100
00036 #define     BIT9            0x00000200
00037 #define     BIT10           0x00000400
00038 #define     BIT11           0x00000800
00039 #define     BIT12           0x00001000
00040 #define     BIT13           0x00002000
00041 #define     BIT14           0x00004000
00042 #define     BIT15           0x00008000
00043 #define     BIT16           0x00010000
00044 #define     BIT17           0x00020000
00045 #define     BIT18           0x00040000
00046 #define     BIT19           0x00080000
00047 #define     BIT20           0x00100000
00048 #define     BIT21           0x00200000
00049 #define     BIT22           0x00400000
00050 #define     BIT23           0x00800000
00051 #define     BIT24           0x01000000
00052 #define     BIT25           0x02000000
00053 #define     BIT26           0x04000000
00054 #define     BIT27           0x08000000
00055 #define     BIT28           0x10000000
00056 #define     BIT29           0x20000000
00057 #define     BIT30           0x40000000
00058 #define     BIT31           0x80000000
00059 
00060 // bitmask for individual LED segments
00061 #define     SEG_A           0xFE
00062 #define     SEG_B           0xFD
00063 #define     SEG_C           0xFB
00064 #define     SEG_D           0xF7
00065 #define     SEG_E           0xEF
00066 #define     SEG_F           0xDF
00067 #define     SEG_G           0xBF
00068 #define     SEG_DP          0x7F
00069 
00070 // Some useful seven-segment patterns for display HEX digits
00071 //     NOTE: User can easily create additional ones for other letters/symbols
00072 #define PATTERN_BLANK       0xFF
00073 #define PATTERN_0           (SEG_A & SEG_B & SEG_C & SEG_D & SEG_E & SEG_F)
00074 #define PATTERN_1           (SEG_B & SEG_C)
00075 #define PATTERN_2           (SEG_A & SEG_B & SEG_D & SEG_E & SEG_G)
00076 #define PATTERN_3           (SEG_A & SEG_B & SEG_C & SEG_D & SEG_G)
00077 #define PATTERN_4           (SEG_B & SEG_C & SEG_F & SEG_G)
00078 #define PATTERN_5           (SEG_A & SEG_C & SEG_D & SEG_F & SEG_G)
00079 #define PATTERN_6           (SEG_A & SEG_C & SEG_D & SEG_E & SEG_F & SEG_G)
00080 #define PATTERN_7           (SEG_A & SEG_B & SEG_C)
00081 #define PATTERN_8           (SEG_A & SEG_B & SEG_C & SEG_D & SEG_E & SEG_F & SEG_G)
00082 #define PATTERN_9           (SEG_A & SEG_B & SEG_C & SEG_D & SEG_F & SEG_G)
00083 #define PATTERN_A           (SEG_A & SEG_B & SEG_C & SEG_E & SEG_F & SEG_G)
00084 #define PATTERN_B           (SEG_C & SEG_D & SEG_E & SEG_F & SEG_G)
00085 #define PATTERN_C           (SEG_A & SEG_D & SEG_E & SEG_F)
00086 #define PATTERN_D           (SEG_B & SEG_C & SEG_D & SEG_E & SEG_G)
00087 #define PATTERN_E           (SEG_A & SEG_D & SEG_E & SEG_F & SEG_G)
00088 #define PATTERN_F           (SEG_A & SEG_E & SEG_F & SEG_G)
00089 
00090 /*************************************************************************
00091  * 
00092  * HW to peripheral pin mappings  -- based on F031K6 using adapter
00093  *    inserted into Edubase-V2 target                          (July 2020)
00094  * 
00095 **************************************************************************/
00096 #define     SCL_PIN         PA_9
00097 #define     SDA_PIN         PA_10
00098 #define     LED0_PIN        PA_12
00099 #define     LED1_PIN        PB_0
00100 #define     LED3_PIN        PB_7
00101 #define     PWM1_PIN        PB_1
00102 #define     LED2_PIN        PF_0
00103 #define     CS_SD_PIN       PF_1
00104 #define     SPK_PIN         PA_8
00105 #define     CS_LCD_PIN      PA_11
00106 #define     MOSI_PIN        PB_5
00107 #define     MISO_PIN        PB_4
00108 #define     SCLK_PIN        PB_3
00109 #define     LGT_SENS_PIN    PA_0
00110 #define     POT_PIN         PA_1
00111 #define     TEMP_SENS_PIN   PA_3
00112 #define     PWM2_PIN        PA_4
00113 #define     SW4_PIN         PA_5
00114 #define     SW5_PIN         PA_6
00115 #define     CS_7SEG_PIN     PA_7
00116 #define     TX_PIN          PA_12
00117 #define     RX_PIN          PA_15          
00118 
00119 /*************************************************************************
00120  * 
00121  * Other useful defines
00122  * 
00123  **************************************************************************/
00124 #define     I2C_SPEED_STD       100000
00125 #define     I2C_SPEED_FAST      400000
00126 #define     I2C_SPEED_FASTPLUS  1000000
00127 
00128 #define     SPI_SPEED_100KBPS   100000
00129 #define     SPI_SPEED_500KBPS   500000
00130 #define     SPI_SPEED_1MBPS     1000000
00131 
00132 /*************************************************************************
00133  *
00134  * USEFUL MACROS
00135  *
00136  *************************************************************************/
00137 
00138 /*************************************************************************
00139  *
00140  * G L O B A L     V A R I A B L E S
00141  *
00142  *************************************************************************/
00143 extern SPI         spi;
00144 //extern I2C         i2c;
00145 extern Serial      pc;
00146 
00147 // create the LEDs
00148 extern DigitalOut      led0;
00149 extern DigitalOut      led1;
00150 extern DigitalOut      led2;
00151 /* only uncomment the following line if your Nucleo32 F031K6
00152 ** has removed the solder jumper SB18 on the underside
00153 */
00154 #ifdef  USE_LED3
00155 extern DigitalOut      led3;
00156 #endif
00157 
00158 // create the speaker
00159 extern DigitalOut      speaker;
00160 
00161 //create the chip-selects
00162 extern DigitalOut      cs_sd;
00163 extern DigitalOut      cs_lcd;
00164 extern DigitalOut      cs_7seg;
00165 
00166 // create the switches
00167 extern DigitalIn       sw4;
00168 extern DigitalIn       sw5;
00169 
00170 // create the analog sensors
00171 extern AnalogIn        lightSensor;
00172 extern AnalogIn        pot;
00173 extern AnalogIn        tempSensor;
00174  
00175 /*************************************************************************
00176  *
00177  * P R O T O T Y P E S
00178  *
00179  *************************************************************************/
00180 void init_spi(int i_spiBaud);
00181 //void init_i2c(int i_i2cBaud);
00182 void init_serial(int i_serialBaud);
00183 void init_leds(void);
00184 void init_all(void);
00185 
00186 /* TEXTLCD
00187  * 
00188  * A TextLCD library targeted the F031K6 + Edubase-V2 combination
00189  * implmements a 16x2 HD44780 LCD module using a 4-bit inteface
00190  * connected via a 74HC595 using the chip's SPI
00191  *
00192  * This work is based upon the TextLCD mbed library by Simon Ford
00193  * found at https://os.mbed.com/users/simon/code/TextLCD/
00194  *
00195  * Copyright (c) 2007-2010, sford, http://mbed.org
00196  * Copyright (c) 2020, jwbruce   (jwbruce@tntech.edu)
00197  *
00198  * Permission is hereby granted, free of charge, to any person obtaining a copy
00199  * of this software and associated documentation files (the "Software"), to deal
00200  * in the Software without restriction, including without limitation the rights
00201  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00202  * copies of the Software, and to permit persons to whom the Software is
00203  * furnished to do so, subject to the following conditions:
00204  *
00205  * The above copyright notice and this permission notice shall be included in
00206  * all copies or substantial portions of the Software.
00207  *
00208  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00209  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00210  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00211  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00212  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00213  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00214  * THE SOFTWARE.
00215  */
00216  
00217 /**  A TextLCD interface for driving a HD44780 LCD character
00218  *   module connected via a 74HC595 using the F031K6 SPI peripheral.
00219  *   Specifically, the Nucleo32 F031K6 boards plugged into the Edubase-V2
00220  *   via the adapter used at Tennessee Tech.
00221  *
00222  * Supports only the 16x2 display on the EduBase-V2 board
00223  *
00224  * @code
00225  * #include "mbed.h"
00226  * #include "TTU_CSC1300.h"
00227  * 
00228  * TextLCD lcd(TextLCD::LCD_CURSOR_ON_BLINKING_ON);     // select your cursor style
00229  * 
00230  * int main() {
00231  *     lcd.printf("Hello World!\n");
00232  * }
00233  * @endcode
00234  */
00235 class TextLCD : public Stream {
00236 public:
00237 
00238     typedef enum {
00239         LCD_CURSOR_OFF_BLINKING_OFF = 0,
00240         LCD_CURSOR_OFF_BLINKING_ON = 0x01,        
00241         LCD_CURSOR_ON_BLINKING_OFF = 0x02,
00242         LCD_CURSOR_ON_BLINKING_ON = 0x03,
00243         LCD_CURSOR_DEFAULT = 0x03
00244     } LCDCursor;
00245   
00246     /** Constructor to create a TextLCD interface
00247      *
00248      * @param cur   Desired behavior of LCD cursor
00249      *
00250      * @see setCursor()
00251      * @see TextLCD::LCD_CURSOR_OFF_BLINKING_OFF    No cursor and no blinking block
00252      * @see TextLCD::LCD_CURSOR_OFF_BLINKING_ON     No cursor and but blinking block
00253      * @see TextLCD::LCD_CURSOR_ON_BLINKING_OFF     Underline cursor and no blinking block
00254      * @see TextLCD::LCD_CURSOR_ON_BLINKING_ON      Both underlinecursor and blinking block
00255      */ 
00256     TextLCD(LCDCursor cur);      // default constructor
00257  
00258 #if DOXYGEN_ONLY
00259     /** Write a character to the LCD at the current cursor location
00260      *
00261      * @param c The character to write to the display
00262      *
00263      * @see setLocation()
00264      */
00265     int putc(int c);
00266  
00267     /** Write a formated string to the LCD at the curren cursor location
00268      *
00269      * @param format A printf-style format string, followed by the
00270      *               variables to use in formating the string.
00271      *
00272      * @note    A string is denoted whthin double-quotes.
00273      */
00274     int printf(const char* format, ...);
00275 #endif
00276  
00277     /** Locate cursor to a screen row and column
00278      *
00279      * @param row     The vertical position from the top, indexed from 0
00280      * @param column  The horizontal position from the left, indexed from 0     
00281      */
00282     void setLocation(int column, int row);
00283  
00284     /** Clear the screen and locate to 0,0 */
00285     void cls();
00286  
00287     /** Right a single character to a specific location
00288      *
00289      * @param row     The vertical position from the top, indexed from 0
00290      * @param column  The horizontal position from the left, indexed from 0     
00291      * @param c       The character to display on-screen
00292      *
00293      * @note    A character is denoted within single-quotes (apostrophes)
00294      */
00295     void setCharacter(int row, int column, int c);
00296 
00297     /** Change the LCD cursor mode
00298      *
00299      * @param c     A LCDCursor type denoted the cursor and blink
00300      *
00301      * @see TextLCD::LCD_CURSOR_OFF_BLINKING_OFF
00302      * @see TextLCD::LCD_CURSOR_OFF_BLINKING_ON
00303      * @see TextLCD::LCD_CURSOR_ON_BLINKING_OFF
00304      * @see TextLCD::LCD_CURSOR_ON_BLINKING_ON
00305      */
00306     void setCursor(LCDCursor c);
00307  
00308 protected:
00309  
00310     // Stream implementation functions
00311     virtual int _putc(int value);
00312     virtual int _getc();
00313  
00314     int address(int column, int row);
00315     void writeNibble(char incoming, unsigned char rs);
00316     void writeCommand(int command);
00317     void writeData(int data);
00318  
00319     int _column;
00320     int _row;
00321     int _maxColumns;
00322     int _maxRows;
00323     int _cursorState;
00324     
00325 };
00326  
00327  
00328 /* F031K6 + Edubase-V2 7-segment LED Library
00329  * for the 4 seven-segment LEDs inteface connected via two 74HC595s using SPI
00330  *
00331  * Copyright (c) 2020, jwbruce   (jwbruce@tntech.edu)
00332  *
00333  * Permission is hereby granted, free of charge, to any person obtaining a copy
00334  * of this software and associated documentation files (the "Software"), to deal
00335  * in the Software without restriction, including without limitation the rights
00336  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00337  * copies of the Software, and to permit persons to whom the Software is
00338  * furnished to do so, subject to the following conditions:
00339  *
00340  * The above copyright notice and this permission notice shall be included in
00341  * all copies or substantial portions of the Software.
00342  *
00343  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00344  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00345  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00346  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00347  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00348  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00349  * THE SOFTWARE.
00350  */
00351  
00352 /**  A 7-segment LED interface for driving 4 seven-segment LEDs connected
00353  *   via two cascaded 74HC595s using the STM32F031K6 SPI peripheral.
00354  *   Specifically, the Nucleo32 F031K6 boards plugged into the Edubase-V2
00355  *   via the adapter used at Tennessee Tech.
00356  *
00357  *   Supports only the four seven-segment displays (DISP1-DISP4) on
00358  *   the EduBase-V2 board
00359  *
00360  * @code
00361  * #include "mbed.h"
00362  * #include "TTU_CSC1300.h"
00363  * 
00364  * SSLED     seg7(4, ~0x00);    // blank DISP4 on Edubase-V2 board
00365  * 
00366  * int main() {
00367  *     while(1) {
00368  *         seg7.setDisplay( 1, ~0x3F);      // display "0"
00369  *         wait(1.0);
00370  *         seg7.setDisplay( 2, ~0x06);      // display "1"
00371  *         wait(1.0);
00372  *         seg7.setDisplay( 3, ~0x5B);      // display "2"
00373  *         wait(1.0);
00374  *         seg7.setDisplay( 4, ~0x4F);      // display "3"
00375  *         wait(1.0);
00376  *     } // end while()
00377  * } // end main()
00378  * @endcode
00379  */
00380 class SSLED {
00381 public:
00382     /** Constructor to create the 4-digit seven-segment display interface
00383      *
00384      * @param display   integer (1-4) selected desired 7-segment display DISP1-DISP4
00385      * @param pattern   byte containing bitmask of to display (zeroed bits illuminate)
00386      *
00387      * @see     setDisplay()
00388      */ 
00389     SSLED(int display, int pattern);      
00390  
00391     /** Write a pattern to a chosen display
00392      *
00393      * @param display   integer (1-4) selected desired 7-segment display DISP1-DISP4
00394      * @param pattern   byte containing bitmask of to display (zeroed bits illuminate)
00395      *
00396      * @note    MSb in pattern is decimal point segment, then gfedcba (with segment
00397      *          a as LSb). Zeroes in the pattern bitmask are illuminated.
00398      *          For example, pattern ~0x66 will illuminate the number "4".
00399      *
00400      * @see     https://en.wikipedia.org/wiki/Seven-segment_display
00401      */ 
00402     void setDisplay(int display, int pattern); // change patterns to blank and write the displays
00403 };
00404  
00405  
00406 
00407 #endif