+/EE445L
embedded systems
git clone https://git.julia.blue/+/EE445L
c12dcba
master
1/*************************************************** 2This is a library for the Adafruit 1.8" SPI display. 3This library works with the Adafruit 1.8" TFT Breakout w/SD card 4----> http://www.adafruit.com/products/358 5as well as Adafruit raw 1.8" TFT display 6----> http://www.adafruit.com/products/618 7 8Check out the links above for our tutorials and wiring diagrams 9These displays use SPI to communicate, 4 or 5 pins are required to 10interface (RST is optional) 11Adafruit invests time and resources providing this open source code, 12please support Adafruit and open-source hardware by purchasing 13products from Adafruit! 14 15Written by Limor Fried/Ladyada for Adafruit Industries. 16MIT license, all text above must be included in any redistribution 17****************************************************/ 18 19// ST7735.h 20// Runs on LM4F120/TM4C123 21// Low level drivers for the ST7735 160x128 LCD based off of 22// the file described above. 23// 16-bit color, 128 wide by 160 high LCD 24// Daniel Valvano, March 30, 2015 25// Augmented 7/17/2014 to have a simple graphics facility 26// Tested with LaunchPadDLL.dll simulator 9/2/2014 27 28/* This example accompanies the book 29"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", 30ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014 31 32Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu 33You may use, edit, run or distribute this file 34as long as the above copyright notice remains 35THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 36OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 37MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 38VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 39OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 40For more information about my classes, my research, and my books, see 41http://users.ece.utexas.edu/~valvano/ 42*/ 43 44// hardware connections 45// **********ST7735 TFT and SDC******************* 46// ST7735 47// Backlight (pin 10) connected to +3.3 V 48// MISO (pin 9) unconnected 49// SCK (pin 8) connected to PA2 (SSI0Clk) 50// MOSI (pin 7) connected to PA5 (SSI0Tx) 51// TFT_CS (pin 6) connected to PA3 (SSI0Fss) 52// CARD_CS (pin 5) unconnected 53// Data/Command (pin 4) connected to PA6 (GPIO), high for data, low for command 54// RESET (pin 3) connected to PA7 (GPIO) 55// VCC (pin 2) connected to +3.3 V 56// Gnd (pin 1) connected to ground 57 58// **********wide.hk ST7735R with ADXL345 accelerometer ******************* 59// Silkscreen Label (SDC side up; LCD side down) - Connection 60// VCC - +3.3 V 61// GND - Ground 62// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 63// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 64// DC - PA6 TFT data/command 65// RES - PA7 TFT reset 66// CS - PA3 TFT_CS, active low to enable TFT 67// *CS - (NC) SDC_CS, active low to enable SDC 68// MISO - (NC) MISO SPI data from SDC to microcontroller 69// SDA � (NC) I2C data for ADXL345 accelerometer 70// SCL � (NC) I2C clock for ADXL345 accelerometer 71// SDO � (NC) I2C alternate address for ADXL345 accelerometer 72// Backlight + - Light, backlight connected to +3.3 V 73 74// **********wide.hk ST7735R with ADXL335 accelerometer ******************* 75// Silkscreen Label (SDC side up; LCD side down) - Connection 76// VCC - +3.3 V 77// GND - Ground 78// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 79// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 80// DC - PA6 TFT data/command 81// RES - PA7 TFT reset 82// CS - PA3 TFT_CS, active low to enable TFT 83// *CS - (NC) SDC_CS, active low to enable SDC 84// MISO - (NC) MISO SPI data from SDC to microcontroller 85// X� (NC) analog input X-axis from ADXL335 accelerometer 86// Y� (NC) analog input Y-axis from ADXL335 accelerometer 87// Z� (NC) analog input Z-axis from ADXL335 accelerometer 88// Backlight + - Light, backlight connected to +3.3 V 89 90 91// **********HiLetgo ST7735 TFT and SDC (SDC not tested)******************* 92// ST7735 93// LED- (pin 16) TFT, connected to ground 94// LED+ (pin 15) TFT, connected to +3.3 V 95// SD_CS (pin 14) SDC, chip select 96// MOSI (pin 13) SDC, MOSI 97// MISO (pin 12) SDC, MISO 98// SCK (pin 11) SDC, serial clock 99// CS (pin 10) TFT, PA3 (SSI0Fss) 100// SCL (pin 9) TFT, SCK PA2 (SSI0Clk) 101// SDA (pin 8) TFT, MOSI PA5 (SSI0Tx) 102// A0 (pin 7) TFT, Data/Command PA6 (GPIO), high for data, low for command 103// RESET (pin 6) TFT, to PA7 (GPIO) 104// NC (pins 3,4,5) not connected 105// VCC (pin 2) connected to +3.3 V 106// GND (pin 1) connected to ground 107 108#ifndef _ST7735H_ 109#define _ST7735H_ 110#include <stdint.h> 111// some flags for ST7735_InitR() 112enum initRFlags { 113none , 114INITR_GREENTAB , 115INITR_REDTAB , 116INITR_BLACKTAB 117}; 118 119#define ST7735_TFTWIDTH 128 120#define ST7735_TFTHEIGHT 160 121 122 123// Color definitions 124#define ST7735_BLACK 0x0000 125#define ST7735_BLUE 0xF800 126#define ST7735_RED 0x001F 127#define ST7735_GREEN 0x07E0 128#define ST7735_CYAN 0xFFE0 129#define ST7735_MAGENTA 0xF81F 130#define ST7735_YELLOW 0x07FF 131#define ST7735_WHITE 0xFFFF 132 133//------------ST7735_InitB------------ 134// Initialization for ST7735B screens. 135// Input: none 136// Output: none 137void ST7735_InitB (void ); 138 139 140//------------ST7735_InitR------------ 141// Initialization for ST7735R screens (green or red tabs). 142// Input: option one of the enumerated options depending on tabs 143// Output: none 144void ST7735_InitR (enum initRFlags option ); 145 146 147//------------ST7735_DrawPixel------------ 148// Color the pixel at the given coordinates with the given color. 149// Requires 13 bytes of transmission 150// Input: x horizontal position of the pixel, columns from the left edge 151// must be less than 128 152// 0 is on the left, 126 is near the right 153// y vertical position of the pixel, rows from the top edge 154// must be less than 160 155// 159 is near the wires, 0 is the side opposite the wires 156// color 16-bit color, which can be produced by ST7735_Color565() 157// Output: none 158void ST7735_DrawPixel (int16_t x ,int16_t y ,uint16_t color ); 159 160//------------ST7735_DrawFastVLine------------ 161// Draw a vertical line at the given coordinates with the given height and color. 162// A vertical line is parallel to the longer side of the rectangular display 163// Requires (11 + 2*h) bytes of transmission (assuming image fully on screen) 164// Input: x horizontal position of the start of the line, columns from the left edge 165// y vertical position of the start of the line, rows from the top edge 166// h vertical height of the line 167// color 16-bit color, which can be produced by ST7735_Color565() 168// Output: none 169void ST7735_DrawFastVLine (int16_t x ,int16_t y ,int16_t h ,uint16_t color ); 170 171 172//------------ST7735_DrawFastHLine------------ 173// Draw a horizontal line at the given coordinates with the given width and color. 174// A horizontal line is parallel to the shorter side of the rectangular display 175// Requires (11 + 2*w) bytes of transmission (assuming image fully on screen) 176// Input: x horizontal position of the start of the line, columns from the left edge 177// y vertical position of the start of the line, rows from the top edge 178// w horizontal width of the line 179// color 16-bit color, which can be produced by ST7735_Color565() 180// Output: none 181void ST7735_DrawFastHLine (int16_t x ,int16_t y ,int16_t w ,uint16_t color ); 182 183 184//------------ST7735_FillScreen------------ 185// Fill the screen with the given color. 186// Requires 40,971 bytes of transmission 187// Input: color 16-bit color, which can be produced by ST7735_Color565() 188// Output: none 189void ST7735_FillScreen (uint16_t color ); 190 191 192//------------ST7735_FillRect------------ 193// Draw a filled rectangle at the given coordinates with the given width, height, and color. 194// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 195// Input: x horizontal position of the top left corner of the rectangle, columns from the left edge 196// y vertical position of the top left corner of the rectangle, rows from the top edge 197// w horizontal width of the rectangle 198// h vertical height of the rectangle 199// color 16-bit color, which can be produced by ST7735_Color565() 200// Output: none 201void ST7735_FillRect (int16_t x ,int16_t y ,int16_t w ,int16_t h ,uint16_t color ); 202 203//------------ST7735_DrawSmallCircle------------ 204// Draw a small circle (diameter of 6 pixels) 205// rectangle at the given coordinates with the given color. 206// Requires (11*6+24*2)=114 bytes of transmission (assuming image fully on screen) 207// Input: x horizontal position of the top left corner of the circle, columns from the left edge 208// y vertical position of the top left corner of the circle, rows from the top edge 209// color 16-bit color, which can be produced by ST7735_Color565() 210// Output: none 211void ST7735_DrawSmallCircle (int16_t x ,int16_t y ,uint16_t color ); 212 213//------------ST7735_DrawCircle------------ 214// Draw a small circle (diameter of 10 pixels) 215// rectangle at the given coordinates with the given color. 216// Requires (11*10+68*2)=178 bytes of transmission (assuming image on screen) 217// Input: x horizontal position of the top left corner of the circle, columns from the left edge 218// y vertical position of the top left corner of the circle, rows from the top edge 219// color 16-bit color, which can be produced by ST7735_Color565() 220// Output: none 221void ST7735_DrawCircle (int16_t x ,int16_t y ,uint16_t color ); 222//------------ST7735_Color565------------ 223// Pass 8-bit (each) R,G,B and get back 16-bit packed color. 224// Input: r red value 225// g green value 226// b blue value 227// Output: 16-bit color 228uint16_t ST7735_Color565 (uint8_t r ,uint8_t g ,uint8_t b ); 229 230//------------ST7735_SwapColor------------ 231// Swaps the red and blue values of the given 16-bit packed color; 232// green is unchanged. 233// Input: x 16-bit color in format B, G, R 234// Output: 16-bit color in format R, G, B 235uint16_t ST7735_SwapColor (uint16_t x ) ; 236 237 238//------------ST7735_DrawBitmap------------ 239// Displays a 16-bit color BMP image. A bitmap file that is created 240// by a PC image processing program has a header and may be padded 241// with dummy columns so the data have four byte alignment. This 242// function assumes that all of that has been stripped out, and the 243// array image[] has one 16-bit halfword for each pixel to be 244// displayed on the screen (encoded in reverse order, which is 245// standard for bitmap files). An array can be created in this 246// format from a 24-bit-per-pixel .bmp file using the associated 247// converter program. 248// (x,y) is the screen location of the lower left corner of BMP image 249// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 250// Input: x horizontal position of the bottom left corner of the image, columns from the left edge 251// y vertical position of the bottom left corner of the image, rows from the top edge 252// image pointer to a 16-bit color BMP image 253// w number of pixels wide 254// h number of pixels tall 255// Output: none 256// Must be less than or equal to 128 pixels wide by 160 pixels high 257void ST7735_DrawBitmap (int16_t x ,int16_t y ,const uint16_t * image ,int16_t w ,int16_t h ); 258 259//------------ST7735_DrawCharS------------ 260// Simple character draw function. This is the same function from 261// Adafruit_GFX.c but adapted for this processor. However, each call 262// to ST7735_DrawPixel() calls setAddrWindow(), which needs to send 263// many extra data and commands. If the background color is the same 264// as the text color, no background will be printed, and text can be 265// drawn right over existing images without covering them with a box. 266// Requires (11 + 2*size*size)*6*8 (image fully on screen; textcolor != bgColor) 267// Input: x horizontal position of the top left corner of the character, columns from the left edge 268// y vertical position of the top left corner of the character, rows from the top edge 269// c character to be printed 270// textColor 16-bit color of the character 271// bgColor 16-bit color of the background 272// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 273// Output: none 274void ST7735_DrawCharS (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ); 275 276//------------ST7735_DrawChar------------ 277// Advanced character draw function. This is similar to the function 278// from Adafruit_GFX.c but adapted for this processor. However, this 279// function only uses one call to setAddrWindow(), which allows it to 280// run at least twice as fast. 281// Requires (11 + size*size*6*8) bytes of transmission (assuming image fully on screen) 282// Input: x horizontal position of the top left corner of the character, columns from the left edge 283// y vertical position of the top left corner of the character, rows from the top edge 284// c character to be printed 285// textColor 16-bit color of the character 286// bgColor 16-bit color of the background 287// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 288// Output: none 289void ST7735_DrawChar (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ); 290 291//------------ST7735_DrawString------------ 292// String draw function. 293// 16 rows (0 to 15) and 21 characters (0 to 20) 294// Requires (11 + size*size*6*8) bytes of transmission for each character 295// Input: x columns from the left edge (0 to 20) 296// y rows from the top edge (0 to 15) 297// pt pointer to a null terminated string to be printed 298// textColor 16-bit color of the characters 299// bgColor is Black and size is 1 300// Output: number of characters printed 301uint32_t ST7735_DrawString (uint16_t x ,uint16_t y ,char * pt ,int16_t textColor ); 302 303 304 305//********ST7735_SetCursor***************** 306// Move the cursor to the desired X- and Y-position. The 307// next character will be printed here. X=0 is the leftmost 308// column. Y=0 is the top row. 309// inputs: newX new X-position of the cursor (0<=newX<=20) 310// newY new Y-position of the cursor (0<=newY<=15) 311// outputs: none 312void ST7735_SetCursor (uint32_t newX ,uint32_t newY ); 313 314//-----------------------ST7735_OutUDec----------------------- 315// Output a 32-bit number in unsigned decimal format 316// Position determined by ST7735_SetCursor command 317// Color set by ST7735_SetTextColor 318// Input: 32-bit number to be transferred 319// Output: none 320// Variable format 1-10 digits with no space before or after 321void ST7735_OutUDec (uint32_t n ); 322 323 324//------------ST7735_SetRotation------------ 325// Change the image rotation. 326// Requires 2 bytes of transmission 327// Input: m new rotation value (0 to 3) 328// Output: none 329void ST7735_SetRotation (uint8_t m ) ; 330 331 332//------------ST7735_InvertDisplay------------ 333// Send the command to invert all of the colors. 334// Requires 1 byte of transmission 335// Input: i 0 to disable inversion; non-zero to enable inversion 336// Output: none 337void ST7735_InvertDisplay (int i ) ; 338 339// graphics routines 340// y coordinates 0 to 31 used for labels and messages 341// y coordinates 32 to 159 128 pixels high 342// x coordinates 0 to 127 128 pixels wide 343 344// *************** ST7735_PlotClear ******************** 345// Clear the graphics buffer, set X coordinate to 0 346// This routine clears the display 347// Inputs: ymin and ymax are range of the plot 348// Outputs: none 349void ST7735_PlotClear (int32_t ymin ,int32_t ymax ); 350 351// *************** ST7735_PlotPoint ******************** 352// Used in the voltage versus time plot, plot one point at y 353// It does output to display 354// Inputs: y is the y coordinate of the point plotted 355// Outputs: none 356void ST7735_PlotPoint (int32_t y ); 357 358// *************** ST7735_PlotLine ******************** 359// Used in the voltage versus time plot, plot line to new point 360// It does output to display 361// Inputs: y is the y coordinate of the point plotted 362// Outputs: none 363void ST7735_PlotLine (int32_t y ); 364 365// *************** ST7735_PlotPoints ******************** 366// Used in the voltage versus time plot, plot two points at y1, y2 367// It does output to display 368// Inputs: y1 is the y coordinate of the first point plotted 369// y2 is the y coordinate of the second point plotted 370// Outputs: none 371void ST7735_PlotPoints (int32_t y1 ,int32_t y2 ); 372 373// *************** ST7735_PlotBar ******************** 374// Used in the voltage versus time bar, plot one bar at y 375// It does not output to display until RIT128x96x4ShowPlot called 376// Inputs: y is the y coordinate of the bar plotted 377// Outputs: none 378void ST7735_PlotBar (int32_t y ); 379 380// *************** ST7735_PlotdBfs ******************** 381// Used in the amplitude versus frequency plot, plot bar point at y 382// 0 to 0.625V scaled on a log plot from min to max 383// It does output to display 384// Inputs: y is the y ADC value of the bar plotted 385// Outputs: none 386void ST7735_PlotdBfs (int32_t y ); 387 388// *************** ST7735_PlotNext ******************** 389// Used in all the plots to step the X coordinate one pixel 390// X steps from 0 to 127, then back to 0 again 391// It does not output to display 392// Inputs: none 393// Outputs: none 394void ST7735_PlotNext (void ); 395 396// *************** ST7735_PlotNextErase ******************** 397// Used in all the plots to step the X coordinate one pixel 398// X steps from 0 to 127, then back to 0 again 399// It clears the vertical space into which the next pixel will be drawn 400// Inputs: none 401// Outputs: none 402void ST7735_PlotNextErase (void ); 403 404// Used in all the plots to write buffer to LCD 405// Example 1 Voltage versus time 406// ST7735_PlotClear(0,4095); // range from 0 to 4095 407// ST7735_PlotPoint(data); ST7735_PlotNext(); // called 128 times 408 409// Example 2a Voltage versus time (N data points/pixel, time scale) 410// ST7735_PlotClear(0,4095); // range from 0 to 4095 411// { for(j=0;j<N;j++){ 412// ST7735_PlotPoint(data[i++]); // called N times 413// } 414// ST7735_PlotNext(); 415// } // called 128 times 416 417// Example 2b Voltage versus time (N data points/pixel, time scale) 418// ST7735_PlotClear(0,4095); // range from 0 to 4095 419// { for(j=0;j<N;j++){ 420// ST7735_PlotLine(data[i++]); // called N times 421// } 422// ST7735_PlotNext(); 423// } // called 128 times 424 425// Example 3 Voltage versus frequency (512 points) 426// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 427// ST7735_PlotClear(0,1023); // clip large magnitudes 428// { 429// ST7735_PlotBar(mag[i++]); // called 4 times 430// ST7735_PlotBar(mag[i++]); 431// ST7735_PlotBar(mag[i++]); 432// ST7735_PlotBar(mag[i++]); 433// ST7735_PlotNext(); 434// } // called 128 times 435 436// Example 4 Voltage versus frequency (512 points), dB scale 437// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 438// ST7735_PlotClear(0,511); // parameters ignored 439// { 440// ST7735_PlotdBfs(mag[i++]); // called 4 times 441// ST7735_PlotdBfs(mag[i++]); 442// ST7735_PlotdBfs(mag[i++]); 443// ST7735_PlotdBfs(mag[i++]); 444// ST7735_PlotNext(); 445// } // called 128 times 446 447// *************** ST7735_OutChar ******************** 448// Output one character to the LCD 449// Position determined by ST7735_SetCursor command 450// Color set by ST7735_SetTextColor 451// Inputs: 8-bit ASCII character 452// Outputs: none 453void ST7735_OutChar (char ch ); 454 455//********ST7735_OutString***************** 456// Print a string of characters to the ST7735 LCD. 457// Position determined by ST7735_SetCursor command 458// Color set by ST7735_SetTextColor 459// The string will not automatically wrap. 460// inputs: ptr pointer to NULL-terminated ASCII string 461// outputs: none 462void ST7735_OutString (char * ptr ); 463 464// ************** ST7735_SetTextColor ************************ 465// Sets the color in which the characters will be printed 466// Background color is fixed at black 467// Input: 16-bit packed color 468// Output: none 469// ******************************************************** 470void ST7735_SetTextColor (uint16_t color ); 471 472// *************** Output_Init ******************** 473// Standard device driver initialization function for printf 474// Initialize ST7735 LCD 475// Inputs: none 476// Outputs: none 477void Output_Init (void ); 478 479// Clear display 480void Output_Clear (void ); 481 482// Turn off display (low power) 483void Output_Off (void ); 484 485// Turn on display 486void Output_On (void ); 487 488// set the color for future output 489// Background color is fixed at black 490// Input: 16-bit packed color 491// Output: none 492void Output_Color (uint32_t newColor ); 493 494#endif