collab/PineapplePad
git clone https://git.julia.blue/collab/PineapplePad
eaf9b07
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.cpp 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 25// March 5, 2018 26// Augmented 7/17/2014 to have a simple graphics facility 27// Tested with LaunchPadDLL.dll simulator 9/2/2014 28// Modified for use of CS (simultaneous with other SPI slaves) 3/28/2016 29 30/* This example accompanies the book 31"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers", 32ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2017 33 34Copyright 2018 by Jonathan W. Valvano, valvano@mail.utexas.edu 35You may use, edit, run or distribute this file 36as long as the above copyright notice remains 37THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED 38OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF 39MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. 40VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, 41OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 42For more information about my classes, my research, and my books, see 43http://users.ece.utexas.edu/~valvano/ 44*/ 45 46// hardware connections 47// **********ST7735 TFT and SDC******************* 48// ST7735 49// Backlight (pin 10) connected to +3.3 V 50// MISO (pin 9) unconnected 51// SCK (pin 8) connected to PA2 (SSI0Clk) 52// MOSI (pin 7) connected to PA5 (SSI0Tx) 53// TFT_CS (pin 6) connected to PA3 (SSI0Fss) 54// CARD_CS (pin 5) unconnected 55// Data/Command (pin 4) connected to PA6 (GPIO), high for data, low for command 56// RESET (pin 3) connected to PA7 (GPIO) 57// VCC (pin 2) connected to +3.3 V 58// Gnd (pin 1) connected to ground 59 60// **********wide.hk ST7735R with ADXL345 accelerometer ******************* 61// Silkscreen Label (SDC side up; LCD side down) - Connection 62// VCC - +3.3 V 63// GND - Ground 64// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 65// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 66// DC - PA6 TFT data/command 67// RES - PA7 TFT reset 68// CS - PA3 TFT_CS, active low to enable TFT 69// *CS - (NC) SDC_CS, active low to enable SDC 70// MISO - (NC) MISO SPI data from SDC to microcontroller 71// SDA � (NC) I2C data for ADXL345 accelerometer 72// SCL � (NC) I2C clock for ADXL345 accelerometer 73// SDO � (NC) I2C alternate address for ADXL345 accelerometer 74// Backlight + - Light, backlight connected to +3.3 V 75 76// **********wide.hk ST7735R with ADXL335 accelerometer ******************* 77// Silkscreen Label (SDC side up; LCD side down) - Connection 78// VCC - +3.3 V 79// GND - Ground 80// !SCL - PA2 Sclk SPI clock from microcontroller to TFT or SDC 81// !SDA - PA5 MOSI SPI data from microcontroller to TFT or SDC 82// DC - PA6 TFT data/command 83// RES - PA7 TFT reset 84// CS - PA3 TFT_CS, active low to enable TFT 85// *CS - (NC) SDC_CS, active low to enable SDC 86// MISO - (NC) MISO SPI data from SDC to microcontroller 87// X� (NC) analog input X-axis from ADXL335 accelerometer 88// Y� (NC) analog input Y-axis from ADXL335 accelerometer 89// Z� (NC) analog input Z-axis from ADXL335 accelerometer 90// Backlight + - Light, backlight connected to +3.3 V 91 92// **********HiLetgo ST7735 TFT and SDC (SDC not tested)******************* 93// ST7735 94// LED- (pin 16) TFT, connected to ground 95// LED+ (pin 15) TFT, connected to +3.3 V 96// SD_CS (pin 14) SDC, chip select 97// MOSI (pin 13) SDC, MOSI 98// MISO (pin 12) SDC, MISO 99// SCK (pin 11) SDC, serial clock 100// CS (pin 10) TFT, PA3 (SSI0Fss) 101// SCL (pin 9) TFT, SCK PA2 (SSI0Clk) 102// SDA (pin 8) TFT, MOSI PA5 (SSI0Tx) 103// A0 (pin 7) TFT, Data/Command PA6 (GPIO), high for data, low for command 104// RESET (pin 6) TFT, to PA7 (GPIO) 105// NC (pins 3,4,5) not connected 106// VCC (pin 2) connected to +3.3 V 107// GND (pin 1) connected to ground 108 109//#include <stdio.h> 110#include <stdint.h> 111//#include <stdio.h> 112#include "../inc/ST7735.h" 113#include "../inc/tm4c123gh6pm.h" 114 115#define SDC_CS_PB0 0 116#define SDC_CS_PD7 1 117 118// 16 rows (0 to 15) and 21 characters (0 to 20) 119// Requires (11 + size*size*6*8) bytes of transmission for each character 120uint32_t StX = 0 ;// position along the horizonal axis 0 to 20 121uint32_t StY = 0 ;// position along the vertical axis 0 to 15 122uint16_t StTextColor = ST7735_YELLOW ; 123 124#define ST7735_NOP 0x00 125#define ST7735_SWRESET 0x01 126#define ST7735_RDDID 0x04 127#define ST7735_RDDST 0x09 128 129#define ST7735_SLPIN 0x10 130#define ST7735_SLPOUT 0x11 131#define ST7735_PTLON 0x12 132#define ST7735_NORON 0x13 133 134#define ST7735_INVOFF 0x20 135#define ST7735_INVON 0x21 136#define ST7735_DISPOFF 0x28 137#define ST7735_DISPON 0x29 138#define ST7735_CASET 0x2A 139#define ST7735_RASET 0x2B 140#define ST7735_RAMWR 0x2C 141#define ST7735_RAMRD 0x2E 142 143#define ST7735_PTLAR 0x30 144#define ST7735_COLMOD 0x3A 145#define ST7735_MADCTL 0x36 146 147#define ST7735_FRMCTR1 0xB1 148#define ST7735_FRMCTR2 0xB2 149#define ST7735_FRMCTR3 0xB3 150#define ST7735_INVCTR 0xB4 151#define ST7735_DISSET5 0xB6 152 153#define ST7735_PWCTR1 0xC0 154#define ST7735_PWCTR2 0xC1 155#define ST7735_PWCTR3 0xC2 156#define ST7735_PWCTR4 0xC3 157#define ST7735_PWCTR5 0xC4 158#define ST7735_VMCTR1 0xC5 159 160#define ST7735_RDID1 0xDA 161#define ST7735_RDID2 0xDB 162#define ST7735_RDID3 0xDC 163#define ST7735_RDID4 0xDD 164 165#define ST7735_PWCTR6 0xFC 166 167#define ST7735_GMCTRP1 0xE0 168#define ST7735_GMCTRN1 0xE1 169 170#define TFT_CS (*((volatile uint32_t *)0x40004020)) 171#define TFT_CS_LOW 0 // CS normally controlled by hardware 172#define TFT_CS_HIGH 0x08 173 174#if SDC_CS_PD7 175// PD7 output used for SDC CS 176#define SDC_CS (*((volatile uint32_t *)0x40007200)) 177#define SDC_CS_LOW 0 // CS controlled by software 178#define SDC_CS_HIGH 0x80 179void SDC_CS_Init (void ){ 180SYSCTL_RCGCGPIO_R |=0x08 ;// activate clock for Port D 181while ((SYSCTL_PRGPIO_R & 0x08 )== 0 ){};// allow time for clock to stabilize 182GPIO_PORTD_LOCK_R = 0x4C4F434B ;// unlock GPIO Port D 183GPIO_PORTD_CR_R = 0xFF ;// allow changes to PD7-0 184// only PD7 needs to be unlocked, other bits can't be locked 185GPIO_PORTD_DIR_R |=0x80 ;// make PD7 out 186GPIO_PORTD_AFSEL_R &= ~0x80 ;// disable alt funct on PD7 187GPIO_PORTD_DR4R_R |=0x80 ;// 4mA drive on outputs 188GPIO_PORTD_PUR_R |=0x80 ;// enable weak pullup on PD7 189GPIO_PORTD_DEN_R |=0x80 ;// enable digital I/O on PD7 190// configure PD7 as GPIO 191GPIO_PORTD_PCTL_R = (GPIO_PORTD_PCTL_R & 0x0FFFFFFF )+ 0x00000000 ; 192GPIO_PORTD_AMSEL_R &= ~0x80 ;// disable analog functionality on PD7 193SDC_CS = SDC_CS_HIGH ; 194} 195#endif 196// PB0 output used for SDC CS 197#if SDC_CS_PB0 198#define SDC_CS (*((volatile uint32_t *)0x40005004)) 199#define SDC_CS_LOW 0 // CS controlled by software 200#define SDC_CS_HIGH 0x01 201void SDC_CS_Init (void ){ 202SYSCTL_RCGCGPIO_R |=0x02 ;// activate clock for Port B 203while ((SYSCTL_PRGPIO_R & 0x02 )== 0 ){};// allow time for clock to stabilize 204GPIO_PORTB_DIR_R |=0x01 ;// make PB0 out 205GPIO_PORTB_AFSEL_R &= ~0x01 ;// disable alt funct on PB0 206GPIO_PORTB_DR4R_R |=0x01 ;// 4mA drive on outputs 207GPIO_PORTB_PUR_R |=0x01 ;// enable weak pullup on PB0 208GPIO_PORTB_DEN_R |=0x01 ;// enable digital I/O on PB0 209// configure PB0 as GPIO 210GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R & 0xFFFFFFF0 )+ 0x00000000 ; 211GPIO_PORTB_AMSEL_R &= ~0x01 ;// disable analog functionality on PB0 212SDC_CS = SDC_CS_HIGH ; 213} 214#endif 215 216#define DC (*((volatile uint32_t *)0x40004100)) 217#define DC_COMMAND 0 218#define DC_DATA 0x40 219#define RESET (*((volatile uint32_t *)0x40004200)) 220#define RESET_LOW 0 221#define RESET_HIGH 0x80 222 223#define SSI_CR0_SCR_M 0x0000FF00 // SSI Serial Clock Rate 224#define SSI_CR0_SPH 0x00000080 // SSI Serial Clock Phase 225#define SSI_CR0_SPO 0x00000040 // SSI Serial Clock Polarity 226#define SSI_CR0_FRF_M 0x00000030 // SSI Frame Format Select 227#define SSI_CR0_FRF_MOTO 0x00000000 // Freescale SPI Frame Format 228#define SSI_CR0_DSS_M 0x0000000F // SSI Data Size Select 229#define SSI_CR0_DSS_8 0x00000007 // 8-bit data 230#define SSI_CR1_MS 0x00000004 // SSI Master/Slave Select 231#define SSI_CR1_SSE 0x00000002 // SSI Synchronous Serial Port 232// Enable 233#define SSI_SR_BSY 0x00000010 // SSI Busy Bit 234#define SSI_SR_TNF 0x00000002 // SSI Transmit FIFO Not Full 235#define SSI_CPSR_CPSDVSR_M 0x000000FF // SSI Clock Prescale Divisor 236#define SSI_CC_CS_M 0x0000000F // SSI Baud Clock Source 237#define SSI_CC_CS_SYSPLL 0x00000000 // Either the system clock (if the 238// PLL bypass is in effect) or the 239// PLL output (default) 240#define SYSCTL_RCGC1_SSI0 0x00000010 // SSI0 Clock Gating Control 241#define SYSCTL_RCGC2_GPIOA 0x00000001 // port A Clock Gating Control 242#define ST7735_TFTWIDTH 128 243#define ST7735_TFTHEIGHT 160 244 245#define ST7735_NOP 0x00 246#define ST7735_SWRESET 0x01 247#define ST7735_RDDID 0x04 248#define ST7735_RDDST 0x09 249 250#define ST7735_SLPIN 0x10 251#define ST7735_SLPOUT 0x11 252#define ST7735_PTLON 0x12 253#define ST7735_NORON 0x13 254 255#define ST7735_INVOFF 0x20 256#define ST7735_INVON 0x21 257#define ST7735_DISPOFF 0x28 258#define ST7735_DISPON 0x29 259#define ST7735_CASET 0x2A 260#define ST7735_RASET 0x2B 261#define ST7735_RAMWR 0x2C 262#define ST7735_RAMRD 0x2E 263 264#define ST7735_PTLAR 0x30 265#define ST7735_COLMOD 0x3A 266#define ST7735_MADCTL 0x36 267 268#define ST7735_FRMCTR1 0xB1 269#define ST7735_FRMCTR2 0xB2 270#define ST7735_FRMCTR3 0xB3 271#define ST7735_INVCTR 0xB4 272#define ST7735_DISSET5 0xB6 273 274#define ST7735_PWCTR1 0xC0 275#define ST7735_PWCTR2 0xC1 276#define ST7735_PWCTR3 0xC2 277#define ST7735_PWCTR4 0xC3 278#define ST7735_PWCTR5 0xC4 279#define ST7735_VMCTR1 0xC5 280 281#define ST7735_RDID1 0xDA 282#define ST7735_RDID2 0xDB 283#define ST7735_RDID3 0xDC 284#define ST7735_RDID4 0xDD 285 286#define ST7735_PWCTR6 0xFC 287 288#define ST7735_GMCTRP1 0xE0 289#define ST7735_GMCTRN1 0xE1 290 291// standard ascii 5x7 font 292// originally from glcdfont.c from Adafruit project 293static const uint8_t Font []= { 2940x00 ,0x00 ,0x00 ,0x00 ,0x00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0x11 ,0x12 ,0x7C ,// A 3600x7F ,0x49 ,0x49 ,0x49 ,0x36 ,// B 3610x3E ,0x41 ,0x41 ,0x41 ,0x22 ,// C 3620x7F ,0x41 ,0x41 ,0x41 ,0x3E ,// D 3630x7F ,0x49 ,0x49 ,0x49 ,0x41 ,// E 3640x7F ,0x09 ,0x09 ,0x09 ,0x01 ,// F 3650x3E ,0x41 ,0x41 ,0x51 ,0x73 ,// G 3660x7F ,0x08 ,0x08 ,0x08 ,0x7F ,// H 3670x00 ,0x41 ,0x7F ,0x41 ,0x00 ,// I 3680x20 ,0x40 ,0x41 ,0x3F ,0x01 ,// J 3690x7F ,0x08 ,0x14 ,0x22 ,0x41 ,// K 3700x7F ,0x40 ,0x40 ,0x40 ,0x40 ,// L 3710x7F ,0x02 ,0x1C ,0x02 ,0x7F ,// M 3720x7F ,0x04 ,0x08 ,0x10 ,0x7F ,// N 3730x3E ,0x41 ,0x41 ,0x41 ,0x3E ,// O 3740x7F ,0x09 ,0x09 ,0x09 ,0x06 ,// P 3750x3E ,0x41 ,0x51 ,0x21 ,0x5E ,// Q 3760x7F ,0x09 ,0x19 ,0x29 ,0x46 ,// R 3770x26 ,0x49 ,0x49 ,0x49 ,0x32 ,// S 3780x03 ,0x01 ,0x7F ,0x01 ,0x03 ,// T 3790x3F ,0x40 ,0x40 ,0x40 ,0x3F ,// U 3800x1F ,0x20 ,0x40 ,0x20 ,0x1F ,// V 3810x3F ,0x40 ,0x38 ,0x40 ,0x3F ,// W 3820x63 ,0x14 ,0x08 ,0x14 ,0x63 ,// X 3830x03 ,0x04 ,0x78 ,0x04 ,0x03 ,// Y 3840x61 ,0x59 ,0x49 ,0x4D ,0x43 ,// Z 3850x00 ,0x7F ,0x41 ,0x41 ,0x41 , 3860x02 ,0x04 ,0x08 ,0x10 ,0x20 , 3870x00 ,0x41 ,0x41 ,0x41 ,0x7F , 3880x04 ,0x02 ,0x01 ,0x02 ,0x04 , 3890x40 ,0x40 ,0x40 ,0x40 ,0x40 , 3900x00 ,0x03 ,0x07 ,0x08 ,0x00 , 3910x20 ,0x54 ,0x54 ,0x78 ,0x40 ,// a 3920x7F ,0x28 ,0x44 ,0x44 ,0x38 ,// b 3930x38 ,0x44 ,0x44 ,0x44 ,0x28 ,// c 3940x38 ,0x44 ,0x44 ,0x28 ,0x7F ,// d 3950x38 ,0x54 ,0x54 ,0x54 ,0x18 ,// e 3960x00 ,0x08 ,0x7E ,0x09 ,0x02 ,// f 3970x18 ,0xA4 ,0xA4 ,0x9C ,0x78 ,// g 3980x7F ,0x08 ,0x04 ,0x04 ,0x78 ,// h 3990x00 ,0x44 ,0x7D ,0x40 ,0x00 ,// i 4000x20 ,0x40 ,0x40 ,0x3D ,0x00 ,// j 4010x7F ,0x10 ,0x28 ,0x44 ,0x00 ,// k 4020x00 ,0x41 ,0x7F ,0x40 ,0x00 ,// l 4030x7C ,0x04 ,0x78 ,0x04 ,0x78 ,// m 4040x7C ,0x08 ,0x04 ,0x04 ,0x78 ,// n 4050x38 ,0x44 ,0x44 ,0x44 ,0x38 ,// o 4060xFC ,0x18 ,0x24 ,0x24 ,0x18 ,// p 4070x18 ,0x24 ,0x24 ,0x18 ,0xFC ,// q 4080x7C ,0x08 ,0x04 ,0x04 ,0x08 ,// r 4090x48 ,0x54 ,0x54 ,0x54 ,0x24 ,// s 4100x04 ,0x04 ,0x3F ,0x44 ,0x24 ,// t 4110x3C ,0x40 ,0x40 ,0x20 ,0x7C ,// u 4120x1C ,0x20 ,0x40 ,0x20 ,0x1C ,// v 4130x3C ,0x40 ,0x30 ,0x40 ,0x3C ,// w 4140x44 ,0x28 ,0x10 ,0x28 ,0x44 ,// x 4150x4C ,0x90 ,0x90 ,0x90 ,0x7C ,// y 4160x44 ,0x64 ,0x54 ,0x4C ,0x44 ,// z 4170x00 ,0x08 ,0x36 ,0x41 ,0x00 , 4180x00 ,0x00 ,0x77 ,0x00 ,0x00 , 4190x00 ,0x41 ,0x36 ,0x08 ,0x00 , 4200x02 ,0x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2A ,0x2A ,0x3E ,0x14 , 5190x7E ,0x02 ,0x02 ,0x06 ,0x06 , 5200x02 ,0x7E ,0x02 ,0x7E ,0x02 , 5210x63 ,0x55 ,0x49 ,0x41 ,0x63 , 5220x38 ,0x44 ,0x44 ,0x3C ,0x04 , 5230x40 ,0x7E ,0x20 ,0x1E ,0x20 , 5240x06 ,0x02 ,0x7E ,0x02 ,0x02 , 5250x99 ,0xA5 ,0xE7 ,0xA5 ,0x99 , 5260x1C ,0x2A ,0x49 ,0x2A ,0x1C , 5270x4C ,0x72 ,0x01 ,0x72 ,0x4C , 5280x30 ,0x4A ,0x4D ,0x4D ,0x30 , 5290x30 ,0x48 ,0x78 ,0x48 ,0x30 , 5300xBC ,0x62 ,0x5A ,0x46 ,0x3D , 5310x3E ,0x49 ,0x49 ,0x49 ,0x00 , 5320x7E ,0x01 ,0x01 ,0x01 ,0x7E , 5330x2A ,0x2A ,0x2A ,0x2A ,0x2A , 5340x44 ,0x44 ,0x5F ,0x44 ,0x44 , 5350x40 ,0x51 ,0x4A ,0x44 ,0x40 , 5360x40 ,0x44 ,0x4A ,0x51 ,0x40 , 5370x00 ,0x00 ,0xFF ,0x01 ,0x03 , 5380xE0 ,0x80 ,0xFF ,0x00 ,0x00 , 5390x08 ,0x08 ,0x6B ,0x6B ,0x08 , 5400x36 ,0x12 ,0x36 ,0x24 ,0x36 , 5410x06 ,0x0F ,0x09 ,0x0F ,0x06 , 5420x00 ,0x00 ,0x18 ,0x18 ,0x00 , 5430x00 ,0x00 ,0x10 ,0x10 ,0x00 , 5440x30 ,0x40 ,0xFF ,0x01 ,0x01 , 5450x00 ,0x1F ,0x01 ,0x01 ,0x1E , 5460x00 ,0x19 ,0x1D ,0x17 ,0x12 , 5470x00 ,0x3C ,0x3C ,0x3C ,0x3C , 5480x00 ,0x00 ,0x00 ,0x00 ,0x00 , 549}; 550 551 552static uint8_t ColStart ,RowStart ;// some displays need this changed 553static uint8_t Rotation ;// 0 to 3 554static enum initRFlags TabColor ; 555static int16_t _width = ST7735_TFTWIDTH ;// this could probably be a constant, except it is used in Adafruit_GFX and depends on image rotation 556static int16_t _height = ST7735_TFTHEIGHT ; 557 558 559// The Data/Command pin must be valid when the eighth bit is 560// sent. The SSI module has hardware input and output FIFOs 561// that are 8 locations deep; however, they are not used in 562// this implementation. Each function first stalls while 563// waiting for any pending SSI0 transfers to complete. Once 564// the SSI0 module is idle, it then prepares the Chip Select 565// pins for the SD card and LCD and the Data/Command pin. 566// Next it starts transmitting the data or command. Finally 567// once the hardware is idle again, it sets all chip select 568// pins high as required by the serial protocol. This is a 569// significant change from previous implementations of this 570// function. It is less efficient without the FIFOs, but it 571// should ensure that the Chip Select and Data/Command pin 572// statuses all match the byte that is actually being 573// transmitted. 574// NOTE: These functions will crash or stall indefinitely if 575// the SSI0 module is not initialized and enabled. 576void static writecommand (unsigned char c ) { 577volatile uint32_t response ; 578// wait until SSI0 not busy/transmit FIFO empty 579while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 580SDC_CS = SDC_CS_HIGH ; 581TFT_CS = TFT_CS_LOW ; 582DC = DC_COMMAND ; 583SSI0_DR_R = c ;// data out 584while ((SSI0_SR_R & SSI_SR_RNE )== 0 ){};// wait until response 585TFT_CS = TFT_CS_HIGH ; 586response = SSI0_DR_R ;// acknowledge response 587} 588 589 590void static writedata (unsigned char c ) { 591volatile uint32_t response ; 592// wait until SSI0 not busy/transmit FIFO empty 593while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 594SDC_CS = SDC_CS_HIGH ; 595TFT_CS = TFT_CS_LOW ; 596DC = DC_DATA ; 597SSI0_DR_R = c ;// data out 598while ((SSI0_SR_R & SSI_SR_RNE )== 0 ){};// wait until response 599TFT_CS = TFT_CS_HIGH ; 600response = SSI0_DR_R ;// acknowledge response 601} 602 603void static deselect (void ) { 604// wait until SSI0 not busy/transmit FIFO empty 605while ((SSI0_SR_R & SSI_SR_BSY )== SSI_SR_BSY ){}; 606TFT_CS = TFT_CS_HIGH ; 607} 608 609// Subroutine to wait 1 msec 610// Inputs: None 611// Outputs: None 612// Notes: ... 613void Delay1ms (uint32_t n ){uint32_t volatile time ; 614while (n ){ 615time = 72724 * 2 /91 ;// 1msec, tuned at 80 MHz 616while (time ){ 617time -- ; 618 } 619n -- ; 620 } 621} 622 623// Rather than a bazillion writecommand() and writedata() calls, screen 624// initialization commands and arguments are organized in these tables 625// stored in ROM. The table may look bulky, but that's mostly the 626// formatting -- storage-wise this is hundreds of bytes more compact 627// than the equivalent code. Companion function follows. 628#define DELAY 0x80 629static const uint8_t 630Bcmd []= {// Initialization commands for 7735B screens 63118 ,// 18 commands in list: 632ST7735_SWRESET ,DELAY ,// 1: Software reset, no args, w/delay 63350 ,// 50 ms delay 634ST7735_SLPOUT ,DELAY ,// 2: Out of sleep mode, no args, w/delay 635255 ,// 255 = 500 ms delay 636ST7735_COLMOD ,1 + DELAY ,// 3: Set color mode, 1 arg + delay: 6370x05 ,// 16-bit color 63810 ,// 10 ms delay 639ST7735_FRMCTR1 ,3 + DELAY ,// 4: Frame rate control, 3 args + delay: 6400x00 ,// fastest refresh 6410x06 ,// 6 lines front porch 6420x03 ,// 3 lines back porch 64310 ,// 10 ms delay 644ST7735_MADCTL ,1 ,// 5: Memory access ctrl (directions), 1 arg: 6450x08 ,// Row addr/col addr, bottom to top refresh 646ST7735_DISSET5 ,2 ,// 6: Display settings #5, 2 args, no delay: 6470x15 ,// 1 clk cycle nonoverlap, 2 cycle gate 648// rise, 3 cycle osc equalize 6490x02 ,// Fix on VTL 650ST7735_INVCTR ,1 ,// 7: Display inversion control, 1 arg: 6510x0 ,// Line inversion 652ST7735_PWCTR1 ,2 + DELAY ,// 8: Power control, 2 args + delay: 6530x02 ,// GVDD = 4.7V 6540x70 ,// 1.0uA 65510 ,// 10 ms delay 656ST7735_PWCTR2 ,1 ,// 9: Power control, 1 arg, no delay: 6570x05 ,// VGH = 14.7V, VGL = -7.35V 658ST7735_PWCTR3 ,2 ,// 10: Power control, 2 args, no delay: 6590x01 ,// Opamp current small 6600x02 ,// Boost frequency 661ST7735_VMCTR1 ,2 + DELAY ,// 11: Power control, 2 args + delay: 6620x3C ,// VCOMH = 4V 6630x38 ,// VCOML = -1.1V 66410 ,// 10 ms delay 665ST7735_PWCTR6 ,2 ,// 12: Power control, 2 args, no delay: 6660x11 ,0x15 , 667ST7735_GMCTRP1 ,16 ,// 13: Magical unicorn dust, 16 args, no delay: 6680x09 ,0x16 ,0x09 ,0x20 ,// (seriously though, not sure what 6690x21 ,0x1B ,0x13 ,0x19 ,// these config values represent) 6700x17 ,0x15 ,0x1E ,0x2B , 6710x04 ,0x05 ,0x02 ,0x0E , 672ST7735_GMCTRN1 ,16 + DELAY ,// 14: Sparkles and rainbows, 16 args + delay: 6730x0B ,0x14 ,0x08 ,0x1E ,// (ditto) 6740x22 ,0x1D ,0x18 ,0x1E , 6750x1B ,0x1A ,0x24 ,0x2B , 6760x06 ,0x06 ,0x02 ,0x0F , 67710 ,// 10 ms delay 678ST7735_CASET ,4 ,// 15: Column addr set, 4 args, no delay: 6790x00 ,0x02 ,// XSTART = 2 6800x00 ,0x81 ,// XEND = 129 681ST7735_RASET ,4 ,// 16: Row addr set, 4 args, no delay: 6820x00 ,0x02 ,// XSTART = 1 6830x00 ,0x81 ,// XEND = 160 684ST7735_NORON ,DELAY ,// 17: Normal display on, no args, w/delay 68510 ,// 10 ms delay 686ST7735_DISPON ,DELAY ,// 18: Main screen turn on, no args, w/delay 687255 };// 255 = 500 ms delay 688static const uint8_t 689Rcmd1 []= {// Init for 7735R, part 1 (red or green tab) 69015 ,// 15 commands in list: 691ST7735_SWRESET ,DELAY ,// 1: Software reset, 0 args, w/delay 692150 ,// 150 ms delay 693ST7735_SLPOUT ,DELAY ,// 2: Out of sleep mode, 0 args, w/delay 694255 ,// 500 ms delay 695ST7735_FRMCTR1 ,3 ,// 3: Frame rate ctrl - normal mode, 3 args: 6960x01 ,0x2C ,0x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 697ST7735_FRMCTR2 ,3 ,// 4: Frame rate control - idle mode, 3 args: 6980x01 ,0x2C ,0x2D ,// Rate = fosc/(1x2+40) * (LINE+2C+2D) 699ST7735_FRMCTR3 ,6 ,// 5: Frame rate ctrl - partial mode, 6 args: 7000x01 ,0x2C ,0x2D ,// Dot inversion mode 7010x01 ,0x2C ,0x2D ,// Line inversion mode 702ST7735_INVCTR ,1 ,// 6: Display inversion ctrl, 1 arg, no delay: 7030x07 ,// No inversion 704ST7735_PWCTR1 ,3 ,// 7: Power control, 3 args, no delay: 7050xA2 , 7060x02 ,// -4.6V 7070x84 ,// AUTO mode 708ST7735_PWCTR2 ,1 ,// 8: Power control, 1 arg, no delay: 7090xC5 ,// VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD 710ST7735_PWCTR3 ,2 ,// 9: Power control, 2 args, no delay: 7110x0A ,// Opamp current small 7120x00 ,// Boost frequency 713ST7735_PWCTR4 ,2 ,// 10: Power control, 2 args, no delay: 7140x8A ,// BCLK/2, Opamp current small & Medium low 7150x2A , 716ST7735_PWCTR5 ,2 ,// 11: Power control, 2 args, no delay: 7170x8A ,0xEE , 718ST7735_VMCTR1 ,1 ,// 12: Power control, 1 arg, no delay: 7190x0E , 720ST7735_INVOFF ,0 ,// 13: Don't invert display, no args, no delay 721ST7735_MADCTL ,1 ,// 14: Memory access control (directions), 1 arg: 7220xC8 ,// row addr/col addr, bottom to top refresh 723ST7735_COLMOD ,1 ,// 15: set color mode, 1 arg, no delay: 7240x05 };// 16-bit color 725static const uint8_t 726Rcmd2green []= {// Init for 7735R, part 2 (green tab only) 7272 ,// 2 commands in list: 728ST7735_CASET ,4 ,// 1: Column addr set, 4 args, no delay: 7290x00 ,0x02 ,// XSTART = 0 7300x00 ,0x7F + 0x02 ,// XEND = 127 731ST7735_RASET ,4 ,// 2: Row addr set, 4 args, no delay: 7320x00 ,0x01 ,// XSTART = 0 7330x00 ,0x9F + 0x01 };// XEND = 159 734static const uint8_t 735Rcmd2red []= {// Init for 7735R, part 2 (red tab only) 7362 ,// 2 commands in list: 737ST7735_CASET ,4 ,// 1: Column addr set, 4 args, no delay: 7380x00 ,0x00 ,// XSTART = 0 7390x00 ,0x7F ,// XEND = 127 740ST7735_RASET ,4 ,// 2: Row addr set, 4 args, no delay: 7410x00 ,0x00 ,// XSTART = 0 7420x00 ,0x9F };// XEND = 159 743static const uint8_t 744Rcmd3 []= {// Init for 7735R, part 3 (red or green tab) 7454 ,// 4 commands in list: 746ST7735_GMCTRP1 ,16 ,// 1: Magical unicorn dust, 16 args, no delay: 7470x02 ,0x1c ,0x07 ,0x12 , 7480x37 ,0x32 ,0x29 ,0x2d , 7490x29 ,0x25 ,0x2B ,0x39 , 7500x00 ,0x01 ,0x03 ,0x10 , 751ST7735_GMCTRN1 ,16 ,// 2: Sparkles and rainbows, 16 args, no delay: 7520x03 ,0x1d ,0x07 ,0x06 , 7530x2E ,0x2C ,0x29 ,0x2D , 7540x2E ,0x2E ,0x37 ,0x3F , 7550x00 ,0x00 ,0x02 ,0x10 , 756ST7735_NORON ,DELAY ,// 3: Normal display on, no args, w/delay 75710 ,// 10 ms delay 758ST7735_DISPON ,DELAY ,// 4: Main screen turn on, no args w/delay 759100 };// 100 ms delay 760 761 762// Companion code to the above tables. Reads and issues 763// a series of LCD commands stored in ROM byte array. 764void static commandList (const uint8_t * addr ) { 765 766uint8_t numCommands ,numArgs ; 767uint16_t ms ; 768 769numCommands = * (addr ++ );// Number of commands to follow 770while (numCommands -- ) {// For each command... 771writecommand (* (addr ++ ));// Read, issue command 772numArgs = * (addr ++ );// Number of args to follow 773ms = numArgs & DELAY ;// If hibit set, delay follows args 774numArgs &= ~DELAY ;// Mask out delay bit 775while (numArgs -- ) {// For each argument... 776writedata (* (addr ++ ));// Read, issue argument 777 } 778 779if (ms ) { 780ms = * (addr ++ );// Read post-command delay time (ms) 781if (ms == 255 )ms = 500 ;// If 255, delay for 500 ms 782Delay1ms (ms ); 783 } 784 } 785} 786 787 788// Initialization code common to both 'B' and 'R' type displays 789void static commonInit (const uint8_t * cmdList ) { 790ColStart = RowStart = 0 ;// May be overridden in init func 791// activate clock for Port A 792SYSCTL_RCGCGPIO_R |=SYSCTL_RCGCGPIO_R0 ; 793// allow time for clock to stabilize 794while ((SYSCTL_PRGPIO_R & SYSCTL_PRGPIO_R0 )== 0 ){}; 795// activate clock for SSI0 796SYSCTL_RCGCSSI_R |=SYSCTL_RCGCSSI_R0 ; 797// allow time for clock to stabilize 798while ((SYSCTL_PRSSI_R & SYSCTL_PRSSI_R0 )== 0 ){}; 799 800// toggle RST low to reset; CS low so it'll listen to us 801// SSI0Fss is used as GPIO 802GPIO_PORTA_DIR_R |=0xC8 ;// make PA3,6,7 out 803GPIO_PORTA_AFSEL_R &= ~0xC8 ;// disable alt funct on PA3,6,7 804GPIO_PORTA_DEN_R |=0xC8 ;// enable digital I/O on PA3,6,7 805// configure PA3,6,7 as GPIO 806GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0x00FF0FFF )+ 0x00000000 ; 807GPIO_PORTA_AMSEL_R &= ~0xC8 ;// disable analog functionality on PA3,6,7 808TFT_CS = TFT_CS_LOW ; 809RESET = RESET_HIGH ; 810Delay1ms (500 ); 811RESET = RESET_LOW ; 812Delay1ms (500 ); 813RESET = RESET_HIGH ; 814Delay1ms (500 ); 815 816// initialize SSI0 817GPIO_PORTA_AFSEL_R |=0x34 ;// enable alt funct on PA2,4,5 818GPIO_PORTA_PUR_R |=0x3C ;// enable weak pullup on PA2,3,4,5 819GPIO_PORTA_DEN_R |=0x34 ;// enable digital I/O on PA2,4,5 820// configure PA2,4,5 as SSI 821GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0xFF00F0FF )+ 0x00220200 ; 822GPIO_PORTA_AMSEL_R &= ~0x34 ;// disable analog functionality on PA2,4,5 823SDC_CS_Init ();// SDC CS on PB0 or PD7 824SDC_CS = SDC_CS_HIGH ; 825SSI0_CR1_R &= ~SSI_CR1_SSE ;// disable SSI 826SSI0_CR1_R &= ~SSI_CR1_MS ;// master mode 827// configure for clock from source PIOSC for baud clock source 828SSI0_CC_R = (SSI0_CC_R & ~SSI_CC_CS_M )+ SSI_CC_CS_PIOSC ; 829// clock divider for 8 MHz SSIClk (16 MHz PIOSC/2) 830// PIOSC/(CPSDVSR*(1+SCR)) 831// 16/(2*(1+0)) = 8 MHz 832SSI0_CPSR_R = (SSI0_CPSR_R & ~SSI_CPSR_CPSDVSR_M )+ 2 ;// must be even number 833SSI0_CR0_R &= ~(SSI_CR0_SCR_M |// SCR = 0 (8 Mbps data rate) 834SSI_CR0_SPH |// SPH = 0 835SSI_CR0_SPO );// SPO = 0 836// FRF = Freescale format 837SSI0_CR0_R = (SSI0_CR0_R & ~SSI_CR0_FRF_M )+ SSI_CR0_FRF_MOTO ; 838// DSS = 8-bit data 839SSI0_CR0_R = (SSI0_CR0_R & ~SSI_CR0_DSS_M )+ SSI_CR0_DSS_8 ; 840SSI0_CR1_R |=SSI_CR1_SSE ;// enable SSI 841 842if (cmdList )commandList (cmdList ); 843} 844 845 846//------------ST7735_InitB------------ 847// Initialization for ST7735B screens. 848// Input: none 849// Output: none 850void ST7735_InitB (void ) { 851commonInit (Bcmd ); 852ST7735_SetCursor (0 ,0 ); 853StTextColor = ST7735_YELLOW ; 854ST7735_FillScreen (0 );// set screen to black 855} 856 857 858//------------ST7735_InitR------------ 859// Initialization for ST7735R screens (green or red tabs). 860// Input: option one of the enumerated options depending on tabs 861// Output: none 862void ST7735_InitR (enum initRFlags option ) { 863commonInit (Rcmd1 ); 864if (option == INITR_GREENTAB ) { 865commandList (Rcmd2green ); 866ColStart = 2 ; 867RowStart = 1 ; 868 }else { 869// colstart, rowstart left at default '0' values 870commandList (Rcmd2red ); 871 } 872commandList (Rcmd3 ); 873 874// if black, change MADCTL color filter 875if (option == INITR_BLACKTAB ) { 876writecommand (ST7735_MADCTL ); 877writedata (0xC0 ); 878 } 879TabColor = option ; 880ST7735_SetCursor (0 ,0 ); 881StTextColor = ST7735_YELLOW ; 882ST7735_FillScreen (0 );// set screen to black 883} 884 885 886// Set the region of the screen RAM to be modified 887// Pixel colors are sent left to right, top to bottom 888// (same as Font table is encoded; different from regular bitmap) 889// Requires 11 bytes of transmission 890void static setAddrWindow (uint8_t x0 ,uint8_t y0 ,uint8_t x1 ,uint8_t y1 ) { 891 892writecommand (ST7735_CASET );// Column addr set 893writedata (0x00 ); 894writedata (x0 + ColStart );// XSTART 895writedata (0x00 ); 896writedata (x1 + ColStart );// XEND 897 898writecommand (ST7735_RASET );// Row addr set 899writedata (0x00 ); 900writedata (y0 + RowStart );// YSTART 901writedata (0x00 ); 902writedata (y1 + RowStart );// YEND 903 904writecommand (ST7735_RAMWR );// write to RAM 905} 906 907 908// Send two bytes of data, most significant byte first 909// Requires 2 bytes of transmission 910void static pushColor (uint16_t color ) { 911writedata ((uint8_t )(color >>8 )); 912writedata ((uint8_t )color ); 913} 914 915 916//------------ST7735_DrawPixel------------ 917// Color the pixel at the given coordinates with the given color. 918// Requires 13 bytes of transmission 919// Input: x horizontal position of the pixel, columns from the left edge 920// must be less than 128 921// 0 is on the left, 126 is near the right 922// y vertical position of the pixel, rows from the top edge 923// must be less than 160 924// 159 is near the wires, 0 is the side opposite the wires 925// color 16-bit color, which can be produced by ST7735_Color565() 926// Output: none 927void ST7735_DrawPixel (int16_t x ,int16_t y ,uint16_t color ) { 928 929if ((x < 0 )|| (x >=_width )|| (y < 0 )|| (y >=_height ))return ; 930 931// setAddrWindow(x,y,x+1,y+1); // original code, bug??? 932setAddrWindow (x ,y ,x ,y ); 933 934pushColor (color ); 935 936deselect (); 937} 938 939 940//------------ST7735_DrawFastVLine------------ 941// Draw a vertical line at the given coordinates with the given height and color. 942// A vertical line is parallel to the longer side of the rectangular display 943// Requires (11 + 2*h) bytes of transmission (assuming image fully on screen) 944// Input: x horizontal position of the start of the line, columns from the left edge 945// y vertical position of the start of the line, rows from the top edge 946// h vertical height of the line 947// color 16-bit color, which can be produced by ST7735_Color565() 948// Output: none 949void ST7735_DrawFastVLine (int16_t x ,int16_t y ,int16_t h ,uint16_t color ) { 950uint8_t hi = color >>8 ,lo = color ; 951 952// Rudimentary clipping 953if ((x >=_width )|| (y >=_height ))return ; 954if ((y + h - 1 ) >=_height )h = _height - y ; 955setAddrWindow (x ,y ,x ,y + h - 1 ); 956 957while (h -- ) { 958writedata (hi ); 959writedata (lo ); 960 } 961 962deselect (); 963} 964 965 966//------------ST7735_DrawFastHLine------------ 967// Draw a horizontal line at the given coordinates with the given width and color. 968// A horizontal line is parallel to the shorter side of the rectangular display 969// Requires (11 + 2*w) bytes of transmission (assuming image fully on screen) 970// Input: x horizontal position of the start of the line, columns from the left edge 971// y vertical position of the start of the line, rows from the top edge 972// w horizontal width of the line 973// color 16-bit color, which can be produced by ST7735_Color565() 974// Output: none 975void ST7735_DrawFastHLine (int16_t x ,int16_t y ,int16_t w ,uint16_t color ) { 976uint8_t hi = color >>8 ,lo = color ; 977 978// Rudimentary clipping 979if ((x >=_width )|| (y >=_height ))return ; 980if ((x + w - 1 ) >=_width )w = _width - x ; 981setAddrWindow (x ,y ,x + w - 1 ,y ); 982 983while (w -- ) { 984writedata (hi ); 985writedata (lo ); 986 } 987 988deselect (); 989} 990 991 992//------------ST7735_FillScreen------------ 993// Fill the screen with the given color. 994// Requires 40,971 bytes of transmission 995// Input: color 16-bit color, which can be produced by ST7735_Color565() 996// Output: none 997void ST7735_FillScreen (uint16_t color ) { 998ST7735_FillRect (0 ,0 ,_width ,_height ,color );// original 999// screen is actually 129 by 161 pixels, x 0 to 128, y goes from 0 to 160 1000} 1001 1002 1003//------------ST7735_FillRect------------ 1004// Draw a filled rectangle at the given coordinates with the given width, height, and color. 1005// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 1006// Input: x horizontal position of the top left corner of the rectangle, columns from the left edge 1007// y vertical position of the top left corner of the rectangle, rows from the top edge 1008// w horizontal width of the rectangle 1009// h vertical height of the rectangle 1010// color 16-bit color, which can be produced by ST7735_Color565() 1011// Output: none 1012void ST7735_FillRect (int16_t x ,int16_t y ,int16_t w ,int16_t h ,uint16_t color ) { 1013uint8_t hi = color >>8 ,lo = color ; 1014 1015// rudimentary clipping (drawChar w/big text requires this) 1016if ((x >=_width )|| (y >=_height ))return ; 1017if ((x + w - 1 ) >=_width )w = _width - x ; 1018if ((y + h - 1 ) >=_height )h = _height - y ; 1019 1020setAddrWindow (x ,y ,x + w - 1 ,y + h - 1 ); 1021 1022for (y = h ;y > 0 ;y -- ) { 1023for (x = w ;x > 0 ;x -- ) { 1024writedata (hi ); 1025writedata (lo ); 1026 } 1027 } 1028 1029deselect (); 1030} 1031 1032//------------ST7735_DrawSmallCircle------------ 1033// Draw a small circle (diameter of 6 pixels) 1034// rectangle at the given coordinates with the given color. 1035// Requires (11*6+24*2)=114 bytes of transmission (assuming image on screen) 1036// Input: x horizontal position of the top left corner of the circle, columns from the left edge 1037// y vertical position of the top left corner of the circle, rows from the top edge 1038// color 16-bit color, which can be produced by ST7735_Color565() 1039// Output: none 1040int16_t const smallCircle [6 ][3 ]= { 1041 {2 ,3 ,2 }, 1042 {1 ,4 ,4 }, 1043 {0 ,5 ,6 }, 1044 {0 ,5 ,6 }, 1045 {1 ,4 ,4 }, 1046 {2 ,3 ,2 }}; 1047void ST7735_DrawSmallCircle (int16_t x ,int16_t y ,uint16_t color ) { 1048uint32_t i ,w ; 1049uint8_t hi = color >>8 ,lo = color ; 1050// rudimentary clipping 1051if ((x > _width - 5 )|| (y > _height - 5 ))return ;// doesn't fit 1052for (i = 0 ;i < 6 ;i ++ ){ 1053setAddrWindow (x + smallCircle [i ][0 ],y + i ,x + smallCircle [i ][1 ],y + i ); 1054w = smallCircle [i ][2 ]; 1055while (w -- ) { 1056writedata (hi ); 1057writedata (lo ); 1058 } 1059 } 1060deselect (); 1061} 1062//------------ST7735_DrawCircle------------ 1063// Draw a small circle (diameter of 10 pixels) 1064// rectangle at the given coordinates with the given color. 1065// Requires (11*10+68*2)=178 bytes of transmission (assuming image on screen) 1066// Input: x horizontal position of the top left corner of the circle, columns from the left edge 1067// y vertical position of the top left corner of the circle, rows from the top edge 1068// color 16-bit color, which can be produced by ST7735_Color565() 1069// Output: none 1070int16_t const circle [10 ][3 ]= { 1071 {4 ,5 ,2 }, 1072 {2 ,7 ,6 }, 1073 {1 ,8 ,8 }, 1074 {1 ,8 ,8 }, 1075 {0 ,9 ,10 }, 1076 {0 ,9 ,10 }, 1077 {1 ,8 ,8 }, 1078 {1 ,8 ,8 }, 1079 {2 ,7 ,6 }, 1080 {4 ,5 ,2 }}; 1081void ST7735_DrawCircle (int16_t x ,int16_t y ,uint16_t color ) { 1082uint32_t i ,w ; 1083uint8_t hi = color >>8 ,lo = color ; 1084// rudimentary clipping 1085if ((x > _width - 9 )|| (y > _height - 9 ))return ;// doesn't fit 1086for (i = 0 ;i < 10 ;i ++ ){ 1087setAddrWindow (x + circle [i ][0 ],y + i ,x + circle [i ][1 ],y + i ); 1088w = circle [i ][2 ]; 1089while (w -- ) { 1090writedata (hi ); 1091writedata (lo ); 1092 } 1093 } 1094deselect (); 1095} 1096 1097//------------ST7735_Color565------------ 1098// Pass 8-bit (each) R,G,B and get back 16-bit packed color. 1099// Input: r red value 1100// g green value 1101// b blue value 1102// Output: 16-bit color 1103uint16_t ST7735_Color565 (uint8_t r ,uint8_t g ,uint8_t b ) { 1104return ((b & 0xF8 ) <<8 ) | ((g & 0xFC ) <<3 ) | (r >>3 ); 1105} 1106 1107 1108//------------ST7735_SwapColor------------ 1109// Swaps the red and blue values of the given 16-bit packed color; 1110// green is unchanged. 1111// Input: x 16-bit color in format B, G, R 1112// Output: 16-bit color in format R, G, B 1113uint16_t ST7735_SwapColor (uint16_t x ) { 1114return (x <<11 ) | (x & 0x07E0 ) | (x >>11 ); 1115} 1116 1117 1118//------------ST7735_DrawBitmap------------ 1119// Displays a 16-bit color BMP image. A bitmap file that is created 1120// by a PC image processing program has a header and may be padded 1121// with dummy columns so the data have four byte alignment. This 1122// function assumes that all of that has been stripped out, and the 1123// array image[] has one 16-bit halfword for each pixel to be 1124// displayed on the screen (encoded in reverse order, which is 1125// standard for bitmap files). An array can be created in this 1126// format from a 24-bit-per-pixel .bmp file using the associated 1127// converter program. 1128// (x,y) is the screen location of the lower left corner of BMP image 1129// Requires (11 + 2*w*h) bytes of transmission (assuming image fully on screen) 1130// Input: x horizontal position of the bottom left corner of the image, columns from the left edge 1131// y vertical position of the bottom left corner of the image, rows from the top edge 1132// image pointer to a 16-bit color BMP image 1133// w number of pixels wide 1134// h number of pixels tall 1135// Output: none 1136// Must be less than or equal to 128 pixels wide by 160 pixels high 1137void ST7735_DrawBitmap (int16_t x ,int16_t y ,const uint16_t * image ,int16_t w ,int16_t h ){ 1138int16_t skipC = 0 ;// non-zero if columns need to be skipped due to clipping 1139int16_t originalWidth = w ;// save this value; even if not all columns fit on the screen, the image is still this width in ROM 1140int i = w * (h - 1 ); 1141 1142if ((x >=_width )|| ((y - h + 1 ) >=_height )|| ((x + w ) <=0 )|| (y < 0 )){ 1143return ;// image is totally off the screen, do nothing 1144 } 1145if ((w > _width )|| (h > _height )){// image is too wide for the screen, do nothing 1146//***This isn't necessarily a fatal error, but it makes the 1147//following logic much more complicated, since you can have 1148//an image that exceeds multiple boundaries and needs to be 1149//clipped on more than one side. 1150return ; 1151 } 1152if ((x + w - 1 ) >=_width ){// image exceeds right of screen 1153skipC = (x + w )- _width ;// skip cut off columns 1154w = _width - x ; 1155 } 1156if ((y - h + 1 )< 0 ){// image exceeds top of screen 1157i = i - (h - y - 1 )* originalWidth ;// skip the last cut off rows 1158h = y + 1 ; 1159 } 1160if (x < 0 ){// image exceeds left of screen 1161w = w + x ; 1162skipC = -1 * x ;// skip cut off columns 1163i = i - x ;// skip the first cut off columns 1164x = 0 ; 1165 } 1166if (y >=_height ){// image exceeds bottom of screen 1167h = h - (y - _height + 1 ); 1168y = _height - 1 ; 1169 } 1170 1171setAddrWindow (x ,y - h + 1 ,x + w - 1 ,y ); 1172 1173for (y = 0 ;y < h ;y = y + 1 ){ 1174for (x = 0 ;x < w ;x = x + 1 ){ 1175// send the top 8 bits 1176writedata ((uint8_t )(image [i ] >>8 )); 1177// send the bottom 8 bits 1178writedata ((uint8_t )image [i ]); 1179i = i + 1 ;// go to the next pixel 1180 } 1181i = i + skipC ; 1182i = i - 2 * originalWidth ; 1183 } 1184 1185deselect (); 1186} 1187 1188 1189//------------ST7735_DrawCharS------------ 1190// Simple character draw function. This is the same function from 1191// Adafruit_GFX.c but adapted for this processor. However, each call 1192// to ST7735_DrawPixel() calls setAddrWindow(), which needs to send 1193// many extra data and commands. If the background color is the same 1194// as the text color, no background will be printed, and text can be 1195// drawn right over existing images without covering them with a box. 1196// Requires (11 + 2*size*size)*6*8 (image fully on screen; textcolor != bgColor) 1197// Input: x horizontal position of the top left corner of the character, columns from the left edge 1198// y vertical position of the top left corner of the character, rows from the top edge 1199// c character to be printed 1200// textColor 16-bit color of the character 1201// bgColor 16-bit color of the background 1202// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 1203// Output: none 1204void ST7735_DrawCharS (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ){ 1205uint8_t line ;// vertical column of pixels of character in font 1206int32_t i ,j ; 1207if ((x >=_width )|| // Clip right 1208 (y >=_height )|| // Clip bottom 1209 ((x + 6 * size - 1 )< 0 )|| // Clip left 1210 ((y + 8 * size - 1 )< 0 ))// Clip top 1211return ; 1212 1213for (i = 0 ;i < 6 ;i ++ ) { 1214if (i == 5 ) 1215line = 0x0 ; 1216else 1217line = Font [(c * 5 )+ i ]; 1218for (j = 0 ;j < 8 ;j ++ ) { 1219if (line & 0x1 ) { 1220if (size == 1 )// default size 1221ST7735_DrawPixel (x + i ,y + j ,textColor ); 1222else {// big size 1223ST7735_FillRect (x + (i * size ),y + (j * size ),size ,size ,textColor ); 1224 } 1225 }else if (bgColor != textColor ) { 1226if (size == 1 )// default size 1227ST7735_DrawPixel (x + i ,y + j ,bgColor ); 1228else {// big size 1229ST7735_FillRect (x + i * size ,y + j * size ,size ,size ,bgColor ); 1230 } 1231 } 1232line >>=1 ; 1233 } 1234 } 1235} 1236 1237 1238//------------ST7735_DrawChar------------ 1239// Advanced character draw function. This is similar to the function 1240// from Adafruit_GFX.c but adapted for this processor. However, this 1241// function only uses one call to setAddrWindow(), which allows it to 1242// run at least twice as fast. 1243// Requires (11 + size*size*6*8) bytes of transmission (assuming image fully on screen) 1244// Input: x horizontal position of the top left corner of the character, columns from the left edge 1245// y vertical position of the top left corner of the character, rows from the top edge 1246// c character to be printed 1247// textColor 16-bit color of the character 1248// bgColor 16-bit color of the background 1249// size number of pixels per character pixel (e.g. size==2 prints each pixel of font as 2x2 square) 1250// Output: none 1251void ST7735_DrawChar (int16_t x ,int16_t y ,char c ,int16_t textColor ,int16_t bgColor ,uint8_t size ){ 1252uint8_t line ;// horizontal row of pixels of character 1253int32_t col ,row ,i ,j ;// loop indices 1254if (((x + 6 * size - 1 ) >=_width )|| // Clip right 1255 ((y + 8 * size - 1 ) >=_height )|| // Clip bottom 1256 ((x + 6 * size - 1 )< 0 )|| // Clip left 1257 ((y + 8 * size - 1 )< 0 )){// Clip top 1258return ; 1259 } 1260 1261setAddrWindow (x ,y ,x + 6 * size - 1 ,y + 8 * size - 1 ); 1262 1263line = 0x01 ;// print the top row first 1264// print the rows, starting at the top 1265for (row = 0 ;row < 8 ;row = row + 1 ){ 1266for (i = 0 ;i < size ;i = i + 1 ){ 1267// print the columns, starting on the left 1268for (col = 0 ;col < 5 ;col = col + 1 ){ 1269if (Font [(c * 5 )+ col ]& line ){ 1270// bit is set in Font, print pixel(s) in text color 1271for (j = 0 ;j < size ;j = j + 1 ){ 1272pushColor (textColor ); 1273 } 1274 }else { 1275// bit is cleared in Font, print pixel(s) in background color 1276for (j = 0 ;j < size ;j = j + 1 ){ 1277pushColor (bgColor ); 1278 } 1279 } 1280 } 1281// print blank column(s) to the right of character 1282for (j = 0 ;j < size ;j = j + 1 ){ 1283pushColor (bgColor ); 1284 } 1285 } 1286line = line <<1 ;// move up to the next row 1287 } 1288 1289deselect (); 1290} 1291//------------ST7735_DrawString------------ 1292// String draw function. 1293// 16 rows (0 to 15) and 21 characters (0 to 20) 1294// Requires (11 + size*size*6*8) bytes of transmission for each character 1295// Input: x columns from the left edge (0 to 20) 1296// y rows from the top edge (0 to 15) 1297// pt pointer to a null terminated string to be printed 1298// textColor 16-bit color of the characters 1299// bgColor is Black and size is 1 1300// Output: number of characters printed 1301uint32_t ST7735_DrawString (uint16_t x ,uint16_t y ,char * pt ,int16_t textColor ){ 1302uint32_t count = 0 ; 1303if (y > 15 )return 0 ; 1304while (* pt ){ 1305ST7735_DrawCharS (x * 6 ,y * 10 ,* pt ,textColor ,ST7735_BLACK ,1 ); 1306pt ++ ; 1307x = x + 1 ; 1308if (x > 20 )return count ;// number of characters printed 1309count ++ ; 1310 } 1311return count ;// number of characters printed 1312} 1313 1314//-----------------------fillmessage----------------------- 1315// Output a 32-bit number in unsigned decimal format 1316// Input: 32-bit number to be transferred 1317// Output: none 1318// Variable format 1-10 digits with no space before or after 1319char Message [12 ]; 1320uint32_t Messageindex ; 1321 1322void fillmessage (uint32_t n ){ 1323// This function uses recursion to convert decimal number 1324// of unspecified length as an ASCII string 1325if (n >=10 ){ 1326fillmessage (n /10 ); 1327n = n %10 ; 1328 } 1329Message [Messageindex ]= (n + '0' );/* n is between 0 and 9 */ 1330if (Messageindex < 11 )Messageindex ++ ; 1331} 1332//********ST7735_SetCursor***************** 1333// Move the cursor to the desired X- and Y-position. The 1334// next character will be printed here. X=0 is the leftmost 1335// column. Y=0 is the top row. 1336// inputs: newX new X-position of the cursor (0<=newX<=20) 1337// newY new Y-position of the cursor (0<=newY<=15) 1338// outputs: none 1339void ST7735_SetCursor (uint32_t newX ,uint32_t newY ){ 1340if ((newX > 20 )|| (newY > 15 )){// bad input 1341return ;// do nothing 1342 } 1343StX = newX ; 1344StY = newY ; 1345} 1346//-----------------------ST7735_OutUDec----------------------- 1347// Output a 32-bit number in unsigned decimal format 1348// Position determined by ST7735_SetCursor command 1349// Color set by ST7735_SetTextColor 1350// Input: 32-bit number to be transferred 1351// Output: none 1352// Variable format 1-10 digits with no space before or after 1353void ST7735_OutUDec (uint32_t n ){ 1354Messageindex = 0 ; 1355fillmessage (n ); 1356Message [Messageindex ]= 0 ;// terminate 1357ST7735_DrawString (StX ,StY ,Message ,StTextColor ); 1358StX = StX + Messageindex ; 1359if (StX > 20 ){ 1360StX = 20 ; 1361ST7735_DrawCharS (StX * 6 ,StY * 10 ,'*' ,ST7735_RED ,ST7735_BLACK ,1 ); 1362 } 1363} 1364 1365 1366 1367 1368 1369#define MADCTL_MY 0x80 1370#define MADCTL_MX 0x40 1371#define MADCTL_MV 0x20 1372#define MADCTL_ML 0x10 1373#define MADCTL_RGB 0x00 1374#define MADCTL_BGR 0x08 1375#define MADCTL_MH 0x04 1376 1377//------------ST7735_SetRotation------------ 1378// Change the image rotation. 1379// Requires 2 bytes of transmission 1380// Input: m new rotation value (0 to 3) 1381// Output: none 1382void ST7735_SetRotation (uint8_t m ) { 1383 1384writecommand (ST7735_MADCTL ); 1385Rotation = m %4 ;// can't be higher than 3 1386switch (Rotation ) { 1387case 0 : 1388if (TabColor == INITR_BLACKTAB ) { 1389writedata (MADCTL_MX |MADCTL_MY |MADCTL_RGB ); 1390 }else { 1391writedata (MADCTL_MX |MADCTL_MY |MADCTL_BGR ); 1392 } 1393_width = ST7735_TFTWIDTH ; 1394_height = ST7735_TFTHEIGHT ; 1395break ; 1396case 1 : 1397if (TabColor == INITR_BLACKTAB ) { 1398writedata (MADCTL_MY |MADCTL_MV |MADCTL_RGB ); 1399 }else { 1400writedata (MADCTL_MY |MADCTL_MV |MADCTL_BGR ); 1401 } 1402_width = ST7735_TFTHEIGHT ; 1403_height = ST7735_TFTWIDTH ; 1404break ; 1405case 2 : 1406if (TabColor == INITR_BLACKTAB ) { 1407writedata (MADCTL_RGB ); 1408 }else { 1409writedata (MADCTL_BGR ); 1410 } 1411_width = ST7735_TFTWIDTH ; 1412_height = ST7735_TFTHEIGHT ; 1413break ; 1414case 3 : 1415if (TabColor == INITR_BLACKTAB ) { 1416writedata (MADCTL_MX |MADCTL_MV |MADCTL_RGB ); 1417 }else { 1418writedata (MADCTL_MX |MADCTL_MV |MADCTL_BGR ); 1419 } 1420_width = ST7735_TFTHEIGHT ; 1421_height = ST7735_TFTWIDTH ; 1422break ; 1423 } 1424 1425deselect (); 1426} 1427 1428 1429//------------ST7735_InvertDisplay------------ 1430// Send the command to invert all of the colors. 1431// Requires 1 byte of transmission 1432// Input: i 0 to disable inversion; non-zero to enable inversion 1433// Output: none 1434void ST7735_InvertDisplay (int i ) { 1435if (i ){ 1436writecommand (ST7735_INVON ); 1437 }else { 1438writecommand (ST7735_INVOFF ); 1439 } 1440deselect (); 1441} 1442// graphics routines 1443// y coordinates 0 to 31 used for labels and messages 1444// y coordinates 32 to 159 128 pixels high 1445// x coordinates 0 to 127 128 pixels wide 1446 1447int32_t Ymax ,Ymin ,X ;// X goes from 0 to 127 1448int32_t Yrange ;//YrangeDiv2; 1449 1450// *************** ST7735_PlotClear ******************** 1451// Clear the graphics buffer, set X coordinate to 0 1452// This routine clears the display 1453// Inputs: ymin and ymax are range of the plot 1454// Outputs: none 1455void ST7735_PlotClear (int32_t ymin ,int32_t ymax ){ 1456ST7735_FillRect (0 ,32 ,128 ,128 ,ST7735_Color565 (228 ,228 ,228 ));// light grey 1457if (ymax > ymin ){ 1458Ymax = ymax ; 1459Ymin = ymin ; 1460Yrange = ymax - ymin ; 1461 }else { 1462Ymax = ymin ; 1463Ymin = ymax ; 1464Yrange = ymax - ymin ; 1465 } 1466//YrangeDiv2 = Yrange/2; 1467X = 0 ; 1468} 1469 1470// *************** ST7735_PlotPoint ******************** 1471// Used in the voltage versus time plot, plot one point at y 1472// It does output to display 1473// Inputs: y is the y coordinate of the point plotted 1474// Outputs: none 1475void ST7735_PlotPoint (int32_t y ){int32_t j ; 1476if (y < Ymin )y = Ymin ; 1477if (y > Ymax )y = Ymax ; 1478// X goes from 0 to 127 1479// j goes from 159 to 32 1480// y=Ymax maps to j=32 1481// y=Ymin maps to j=159 1482j = 32 + (127 * (Ymax - y ))/Yrange ; 1483if (j < 32 )j = 32 ; 1484if (j > 159 )j = 159 ; 1485ST7735_DrawPixel (X ,j ,ST7735_BLUE ); 1486ST7735_DrawPixel (X + 1 ,j ,ST7735_BLUE ); 1487ST7735_DrawPixel (X ,j + 1 ,ST7735_BLUE ); 1488ST7735_DrawPixel (X + 1 ,j + 1 ,ST7735_BLUE ); 1489} 1490// *************** ST7735_PlotLine ******************** 1491// Used in the voltage versus time plot, plot line to new point 1492// It does output to display 1493// Inputs: y is the y coordinate of the point plotted 1494// Outputs: none 1495int32_t lastj = 0 ; 1496void ST7735_PlotLine (int32_t y ){int32_t i ,j ; 1497if (y < Ymin )y = Ymin ; 1498if (y > Ymax )y = Ymax ; 1499// X goes from 0 to 127 1500// j goes from 159 to 32 1501// y=Ymax maps to j=32 1502// y=Ymin maps to j=159 1503j = 32 + (127 * (Ymax - y ))/Yrange ; 1504if (j < 32 )j = 32 ; 1505if (j > 159 )j = 159 ; 1506if (lastj < 32 )lastj = j ; 1507if (lastj > 159 )lastj = j ; 1508if (lastj < j ){ 1509for (i = lastj + 1 ;i <=j ;i ++ ){ 1510ST7735_DrawPixel (X ,i ,ST7735_BLUE ) ; 1511ST7735_DrawPixel (X + 1 ,i ,ST7735_BLUE ) ; 1512 } 1513 }else if (lastj > j ){ 1514for (i = j ;i < lastj ;i ++ ){ 1515ST7735_DrawPixel (X ,i ,ST7735_BLUE ) ; 1516ST7735_DrawPixel (X + 1 ,i ,ST7735_BLUE ) ; 1517 } 1518 }else { 1519ST7735_DrawPixel (X ,j ,ST7735_BLUE ) ; 1520ST7735_DrawPixel (X + 1 ,j ,ST7735_BLUE ) ; 1521 } 1522lastj = j ; 1523} 1524 1525// *************** ST7735_PlotPoints ******************** 1526// Used in the voltage versus time plot, plot two points at y1, y2 1527// It does output to display 1528// Inputs: y1 is the y coordinate of the first point plotted 1529// y2 is the y coordinate of the second point plotted 1530// Outputs: none 1531void ST7735_PlotPoints (int32_t y1 ,int32_t y2 ){int32_t j ; 1532if (y1 < Ymin )y1 = Ymin ; 1533if (y1 > Ymax )y1 = Ymax ; 1534// X goes from 0 to 127 1535// j goes from 159 to 32 1536// y=Ymax maps to j=32 1537// y=Ymin maps to j=159 1538j = 32 + (127 * (Ymax - y1 ))/Yrange ; 1539if (j < 32 )j = 32 ; 1540if (j > 159 )j = 159 ; 1541ST7735_DrawPixel (X ,j ,ST7735_BLUE ); 1542if (y2 < Ymin )y2 = Ymin ; 1543if (y2 > Ymax )y2 = Ymax ; 1544j = 32 + (127 * (Ymax - y2 ))/Yrange ; 1545if (j < 32 )j = 32 ; 1546if (j > 159 )j = 159 ; 1547ST7735_DrawPixel (X ,j ,ST7735_BLACK ); 1548} 1549// *************** ST7735_PlotBar ******************** 1550// Used in the voltage versus time bar, plot one bar at y 1551// It does not output to display until RIT128x96x4ShowPlot called 1552// Inputs: y is the y coordinate of the bar plotted 1553// Outputs: none 1554void ST7735_PlotBar (int32_t y ){ 1555int32_t j ; 1556if (y < Ymin )y = Ymin ; 1557if (y > Ymax )y = Ymax ; 1558// X goes from 0 to 127 1559// j goes from 159 to 32 1560// y=Ymax maps to j=32 1561// y=Ymin maps to j=159 1562j = 32 + (127 * (Ymax - y ))/Yrange ; 1563ST7735_DrawFastVLine (X ,j ,159 - j ,ST7735_BLACK ); 1564 1565} 1566 1567// full scaled defined as 3V 1568// Input is 0 to 511, 0 => 159 and 511 => 32 1569uint8_t const dBfs [512 ]= { 1570159 ,159 ,145 ,137 ,131 ,126 ,123 ,119 ,117 ,114 ,112 ,110 ,108 ,107 ,105 ,104 ,103 ,101 , 1571100 ,99 ,98 ,97 ,96 ,95 ,94 ,93 ,93 ,92 ,91 ,90 ,90 ,89 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,44 ,44 ,44 ,44 ,44 ,44 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 ,43 , 158443 ,43 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,42 ,41 ,41 ,41 ,41 ,41 , 158541 ,41 ,41 ,41 ,41 ,41 ,41 ,41 ,41 ,41 ,41 ,40 ,40 ,40 ,40 ,40 ,40 ,40 ,40 ,40 ,40 ,40 , 158640 ,40 ,40 ,40 ,40 ,40 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 ,39 , 158739 ,39 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,38 ,37 , 158837 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,37 ,36 ,36 ,36 ,36 , 158936 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,36 ,35 ,35 ,35 ,35 ,35 , 159035 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,35 ,34 ,34 ,34 ,34 ,34 ,34 , 159134 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,34 ,33 ,33 ,33 ,33 ,33 , 159233 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,33 ,32 ,32 ,32 , 159332 ,32 ,32 ,32 ,32 ,32 ,32 ,32 ,32 ,32 1594}; 1595 1596// *************** ST7735_PlotdBfs ******************** 1597// Used in the amplitude versus frequency plot, plot bar point at y 1598// 0 to 0.625V scaled on a log plot from min to max 1599// It does output to display 1600// Inputs: y is the y ADC value of the bar plotted 1601// Outputs: none 1602void ST7735_PlotdBfs (int32_t y ){ 1603int32_t j ; 1604y = y /2 ;// 0 to 2047 1605if (y < 0 )y = 0 ; 1606if (y > 511 )y = 511 ; 1607// X goes from 0 to 127 1608// j goes from 159 to 32 1609// y=511 maps to j=32 1610// y=0 maps to j=159 1611j = dBfs [y ]; 1612ST7735_DrawFastVLine (X ,j ,159 - j ,ST7735_BLACK ); 1613 1614} 1615 1616// *************** ST7735_PlotNext ******************** 1617// Used in all the plots to step the X coordinate one pixel 1618// X steps from 0 to 127, then back to 0 again 1619// It does not output to display 1620// Inputs: none 1621// Outputs: none 1622void ST7735_PlotNext (void ){ 1623if (X == 127 ){ 1624X = 0 ; 1625 }else { 1626X ++ ; 1627 } 1628} 1629 1630// *************** ST7735_PlotNextErase ******************** 1631// Used in all the plots to step the X coordinate one pixel 1632// X steps from 0 to 127, then back to 0 again 1633// It clears the vertical space into which the next pixel will be drawn 1634// Inputs: none 1635// Outputs: none 1636void ST7735_PlotNextErase (void ){ 1637if (X == 127 ){ 1638X = 0 ; 1639 }else { 1640X ++ ; 1641 } 1642ST7735_DrawFastVLine (X ,32 ,128 ,ST7735_Color565 (228 ,228 ,228 )); 1643} 1644 1645// Used in all the plots to write buffer to LCD 1646// Example 1 Voltage versus time 1647// ST7735_PlotClear(0,4095); // range from 0 to 4095 1648// ST7735_PlotPoint(data); ST7735_PlotNext(); // called 128 times 1649 1650// Example 2a Voltage versus time (N data points/pixel, time scale) 1651// ST7735_PlotClear(0,4095); // range from 0 to 4095 1652// { for(j=0;j<N;j++){ 1653// ST7735_PlotPoint(data[i++]); // called N times 1654// } 1655// ST7735_PlotNext(); 1656// } // called 128 times 1657 1658// Example 2b Voltage versus time (N data points/pixel, time scale) 1659// ST7735_PlotClear(0,4095); // range from 0 to 4095 1660// { for(j=0;j<N;j++){ 1661// ST7735_PlotLine(data[i++]); // called N times 1662// } 1663// ST7735_PlotNext(); 1664// } // called 128 times 1665 1666// Example 3 Voltage versus frequency (512 points) 1667// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 1668// ST7735_PlotClear(0,1023); // clip large magnitudes 1669// { 1670// ST7735_PlotBar(mag[i++]); // called 4 times 1671// ST7735_PlotBar(mag[i++]); 1672// ST7735_PlotBar(mag[i++]); 1673// ST7735_PlotBar(mag[i++]); 1674// ST7735_PlotNext(); 1675// } // called 128 times 1676 1677// Example 4 Voltage versus frequency (512 points), dB scale 1678// perform FFT to get 512 magnitudes, mag[i] (0 to 4095) 1679// ST7735_PlotClear(0,511); // parameters ignored 1680// { 1681// ST7735_PlotdBfs(mag[i++]); // called 4 times 1682// ST7735_PlotdBfs(mag[i++]); 1683// ST7735_PlotdBfs(mag[i++]); 1684// ST7735_PlotdBfs(mag[i++]); 1685// ST7735_PlotNext(); 1686// } // called 128 times 1687 1688// *************** ST7735_OutChar ******************** 1689// Output one character to the LCD 1690// Position determined by ST7735_SetCursor command 1691// Color set by ST7735_SetTextColor 1692// Inputs: 8-bit ASCII character 1693// Outputs: none 1694void ST7735_OutChar (char ch ){ 1695if ((ch == 10 )|| (ch == 13 )|| (ch == 27 )){ 1696StY ++ ;StX = 0 ; 1697if (StY > 15 ){ 1698StY = 0 ; 1699 } 1700ST7735_DrawString (0 ,StY ," " ,StTextColor ); 1701return ; 1702 } 1703ST7735_DrawCharS (StX * 6 ,StY * 10 ,ch ,ST7735_YELLOW ,ST7735_BLACK ,1 ); 1704StX ++ ; 1705if (StX > 20 ){ 1706StX = 20 ; 1707ST7735_DrawCharS (StX * 6 ,StY * 10 ,'*' ,ST7735_RED ,ST7735_BLACK ,1 ); 1708 } 1709return ; 1710} 1711//********ST7735_OutString***************** 1712// Print a string of characters to the ST7735 LCD. 1713// Position determined by ST7735_SetCursor command 1714// Color set by ST7735_SetTextColor 1715// The string will not automatically wrap. 1716// inputs: ptr pointer to NULL-terminated ASCII string 1717// outputs: none 1718void ST7735_OutString (char * ptr ){ 1719while (* ptr ){ 1720ST7735_OutChar (* ptr ); 1721ptr = ptr + 1 ; 1722 } 1723} 1724// ************** ST7735_SetTextColor ************************ 1725// Sets the color in which the characters will be printed 1726// Background color is fixed at black 1727// Input: 16-bit packed color 1728// Output: none 1729// ******************************************************** 1730void ST7735_SetTextColor (uint16_t color ){ 1731StTextColor = color ; 1732} 1733 1734// *************** Output_Init ******************** 1735// Standard device driver initialization function for printf 1736// Initialize ST7735 LCD 1737// Inputs: none 1738// Outputs: none 1739void Output_Init (void ){ 1740ST7735_InitR (INITR_REDTAB ); 1741ST7735_FillScreen (0 );// set screen to black 1742} 1743 1744// Clear display 1745void Output_Clear (void ){// Clears the display 1746ST7735_FillScreen (0 );// set screen to black 1747} 1748// Turn off display (low power) 1749void Output_Off (void ){// Turns off the display 1750Output_Clear ();// not implemented 1751} 1752// Turn on display 1753void Output_On (void ){// Turns on the display 1754Output_Init ();// reinitialize 1755} 1756// set the color for future output 1757// Background color is fixed at black 1758// Input: 16-bit packed color 1759// Output: none 1760void Output_Color (uint32_t newColor ){// Set color of future output 1761ST7735_SetTextColor (newColor ); 1762}