Created
September 19, 2017 18:45
-
-
Save vishnumaiea/10eeb888b97832c435b052474e75ea10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-----------------------------------------------------------// | |
// A driver library for ILI9481, 3.2", 320 x 480 display. | |
// Author : Vishnu M Aiea | |
// Website : www.vishnumaiea.in | |
//-----------------------------------------------------------// | |
#include <driverlib.h> | |
#define CS_PIN GPIO_PORT_P10, GPIO_PIN2 //P10.2 | |
#define RST_PIN GPIO_PORT_P10, GPIO_PIN3 //P10.3 | |
#define DC_PIN GPIO_PORT_P10, GPIO_PIN0 //P10.0 | |
#define WR_PIN GPIO_PORT_P10, GPIO_PIN1 //P10.1 | |
#define PD0 GPIO_PORT_P9, GPIO_PIN0 //parallel data pins | |
#define PD1 GPIO_PORT_P9, GPIO_PIN1 | |
#define PD2 GPIO_PORT_P9, GPIO_PIN2 | |
#define PD3 GPIO_PORT_P9, GPIO_PIN3 | |
#define PD4 GPIO_PORT_P9, GPIO_PIN4 | |
#define PD5 GPIO_PORT_P9, GPIO_PIN5 | |
#define PD6 GPIO_PORT_P9, GPIO_PIN6 | |
#define PD7 GPIO_PORT_P9, GPIO_PIN7 | |
#define PD8 GPIO_PORT_P7, GPIO_PIN0 | |
#define PD9 GPIO_PORT_P7, GPIO_PIN1 | |
#define PD10 GPIO_PORT_P7, GPIO_PIN2 | |
#define PD11 GPIO_PORT_P7, GPIO_PIN3 | |
#define PD12 GPIO_PORT_P7, GPIO_PIN4 | |
#define PD13 GPIO_PORT_P7, GPIO_PIN5 | |
#define PD14 GPIO_PORT_P7, GPIO_PIN6 | |
#define PD15 GPIO_PORT_P7, GPIO_PIN7 | |
static const int Width = 320; | |
static const int Height = 480; | |
int _width = Width; | |
int _height = Height; | |
void writeData (uint8_t); | |
void writeCommand (uint8_t); | |
void initializeDisplay(void); | |
void startDisplay(void); | |
void writeData16 (uint16_t); | |
void writeData8 (uint8_t); | |
void displayOn(void); | |
void displayOff(void); | |
void setAddrWindow(int, int, int, int); | |
void setPixel(int, int, uint16_t); | |
void fillScreen(uint16_t); | |
void fillRectangle(int, int, int, int, uint16_t); | |
void drawHorizontalLine(int, int, int, uint16_t); | |
void drawVerticalLine(int, int, int, uint16_t); | |
void setRotation(int); | |
void invertDisplay(bool); | |
void openWindow(int, int, int, int); | |
void windowData(uint16_t); | |
void windowData(uint16_t*, int); | |
//=============================================================// | |
void setup() { | |
P10REN = 0; //disable pulls on all pins of P9 | |
P10DIR = 0xFF; //set all pins as outputs of P9 | |
P9REN = 0; //disable pulls on all pins of P9 | |
P9DIR = 0xFF; //set all pins as outputs of P9 | |
P8REN = 0; //disable pulls | |
P8DIR = 0xFF; //set as outputs | |
P7REN = 0; //disable pulls | |
P7DIR = 0xFF; //set as outputs | |
initializeDisplay(); | |
setRotation(1); | |
delay(10); | |
//setAddrWindow(0, 0, 320, 480); | |
//fillScreen(1); | |
//delay(10); | |
//fillScreen(1); | |
} | |
//=============================================================// | |
int color = 0; | |
void loop() { | |
//fillRectangle(0, 0, 480, 320, 6500); | |
//fillScreen(6500); | |
//fillScreen(200); | |
//fillScreen(15000); | |
//fillScreen(3000); | |
//fillScreen(28000); | |
//drawHorizontalLine(0, 200, 480, 10); | |
if(color >= 65535) color = 0; | |
//displayOff(); | |
setAddrWindow(0, 0, 479, 319); | |
for(int i=0; i<640; i++) { | |
for(int j=0; j<480; j++) { | |
writeData16(color); | |
} | |
} | |
color++; | |
//displayOn(); | |
} | |
//=============================================================// | |
void writeData (uint8_t inputData) { | |
P10OUT &= 0xFB; //1111 1011 - P10.2 - CS low | |
P10OUT |= 0x8; //0000 1000 - P10.3 - RST high | |
P10OUT |= 0x1; //0000 0001 - P10.0 - DC high | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
P9OUT = inputData; //assert the data | |
P7OUT = 0x0; | |
P10OUT &= 0xFD; //1111 1101 - P10.1 - WR low | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
} | |
//=============================================================// | |
void writeCommand (uint8_t inputCommand) { | |
P10OUT &= 0xFB; //1111 1011 - P10.2 - CS | |
P10OUT |= 0x8; //0000 1000 - P10.3 - RST | |
P10OUT &= 0xFE; //1111 1110 - P10.0 - DC | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR | |
P9OUT = inputCommand; //assert the command | |
P7OUT = 0x0; | |
P10OUT &= 0xFD; //1111 1101 - P10.1 - WR low | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
P10OUT |= 0x1; //0000 0001 - P10.0 - DC high | |
} | |
//=============================================================// | |
void initializeDisplay() { | |
P10OUT |= 0x1; //0000 0001 - P10.0 - DC high | |
P10OUT |= 0x4; //0000 0100 - P10.2 - CS high | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
P10OUT |= 0x8; //0000 1000 - P10.3 - RST high | |
delay(100); | |
P10OUT |= 0x8; //0000 1000 - P10.3 - RST high | |
delay(100); | |
P10OUT |= 0x8; //0000 1000 - P10.3 - RST high | |
delay(100); | |
P10OUT &= 0xFB; //1111 1011 - P10.2 - CS low | |
startDisplay(); | |
} | |
//=============================================================// | |
void startDisplay() { | |
writeCommand(0x11); //exit sleep mode | |
delay(80); | |
writeCommand(0xD0); //power setting | |
writeData8(0x07); // | |
writeData8(0x42); | |
writeData8(0x18); | |
writeCommand(0xD1); //VCOM control | |
writeData8(0x00); | |
writeData8(0x07); | |
writeData8(0x10); | |
writeCommand(0xD2); //power setting for normal mode | |
writeData8(0x01); | |
writeData8(0x02); //Fosc setting | |
writeCommand(0xC0); //panel driving setting - 5 parameters | |
writeData8(0x10); //REV=1, SM=0, GS=0 - grayscale inversion enabled : will invert colors | |
//writeData8(0x00); //REV=0, SM=0, GS=0 - no color inversion | |
writeData8(0x3B); //NL[5] - max lines | |
writeData8(0x00); //SCN - scanning start poisition | |
writeData8(0x02); //NDL (non-display area o/p level), PTS[3] | |
//writeData8(0x11); //PTG=1 (interval scan), ISC[3]=0001 (3 frames) | |
//writeData8(0x1F); //PTG=1 (interval scan), ISC[3]=1111 (31 frames) | |
writeData8(0x01); //PTG=0 (normal scan), ISC[3]=0002 (3 frames) | |
writeCommand(0xC5); //frame rate and inversion control - 1 parameter | |
//writeData8(0x03); //72FPS (default) - this had backlight flickering | |
writeData8(0x00); //125 FPS (max) - this has no flickering | |
writeCommand(0xB3); //frame memeory access and interface setting | |
writeData8(0x0); //extra data is ignored | |
writeData8(0x0); //all defaults | |
writeData8(0x0); | |
writeData8(0x0); | |
writeCommand(0xC1); //display timing for normal mode | |
writeData8(0x10); //BC=0 (frame inversion waveform) | |
writeData8(0x10); //line period is 16 clocks | |
writeData8(0x22); //front and back porch lines = 2 | |
writeCommand(0xC8); //gamma setting | |
writeData8(0x00); | |
writeData8(0x32); | |
writeData8(0x36); | |
writeData8(0x45); | |
writeData8(0x06); | |
writeData8(0x16); | |
writeData8(0x37); | |
writeData8(0x75); | |
writeData8(0x77); | |
writeData8(0x54); | |
writeData8(0x0C); | |
writeData8(0x00); | |
writeCommand(0x36); //set address mode - 1 parameter | |
writeData8(0x0A); //BGR, horizontal flip | |
writeCommand(0x13); //eneter normal mode - no param | |
writeCommand(0x3A); //set pixel format | |
writeData8(0x55); //16-bit per pixel | |
//writeData(0x66); //18-bit per pixel | |
setAddrWindow(0, 0, _width, _height); | |
//writeCommand(0x2A); //set column address | |
//writeData8(0x00); | |
//writeData8(0x00); | |
//writeData8(0x01); | |
//writeData8(0x3F); | |
//writeCommand(0x2B); //set page address | |
//writeData8(0x00); | |
//writeData8(0x00); | |
//writeData8(0x01); | |
//writeData8(0xDF); | |
delay(120); | |
writeCommand(0x29); //set display on | |
} | |
//=============================================================// | |
//writes 16 bit data to the 8-bit bus | |
void writeData16 (uint16_t inputData) { | |
P10OUT |= 0x1; //0000 0001 - P10.0 - DC high | |
//P9OUT = inputData & 0xFF; //writes the low byte (small endian) | |
P7OUT = (uint8_t) ((unsigned int)(inputData >> 8) & 0x00FF); //write the high byte (big endian) | |
P9OUT = (uint8_t) inputData & 0x00FF; //writes the low byte | |
P10OUT &= 0xFD; //1111 1101 - P10.1 - WR low | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
//P9OUT = inputData >> 8; //write the high byte | |
//P9OUT = inputData & 0xFF; //writes the low byte | |
} | |
//=============================================================// | |
void writeData8 (uint8_t inputData) { | |
P10OUT |= 0x1; //0000 0001 - P10.0 - DC high | |
P7OUT = 0x0; | |
P9OUT = inputData; //write 8-bit paralell data | |
P10OUT &= 0xFD; //1111 1101 - P10.1 - WR low | |
P10OUT |= 0x2; //0000 0010 - P10.1 - WR high | |
} | |
//=============================================================// | |
void displayOn() { | |
writeCommand(0x29); //set display on | |
} | |
//=============================================================// | |
void displayOff() { | |
writeCommand(0x28); //set display off | |
} | |
//=============================================================// | |
void setAddrWindow(int x0, int y0, int x1, int y1) { | |
uint16_t temp; | |
writeCommand(0x2A); //set col address - 4 param | |
temp = (unsigned int) x0 >> 8; | |
writeData8(temp); //start col address : high byte | |
writeData8(x0 & 0x00FF); //low byte | |
temp = (unsigned int) x1 >> 8; | |
writeData8(temp); //end col address : high byte | |
writeData8(x1 & 0x00FF); //low byte | |
//writeData16(x0); | |
//writeData16(x1); | |
writeCommand(0x2B); //set page address - 4 param | |
temp = (unsigned int) y0 >> 8; | |
writeData8(temp); //start page addree : high byte | |
writeData8(y0 & 0x00FF); //low byte | |
temp = (unsigned int) y1 >> 8; | |
writeData8(temp); //end page address : high byte | |
writeData8(y1 & 0x00FF); //low byte | |
//writeData16(y0); | |
//writeData16(y1); | |
writeCommand(0x2C); //write memory start | |
//writeCommand(0x3C); //write memory continue | |
/* | |
writeCommand(0x2A); //set col address - 4 param | |
if(x0 > 255) writeData8(0x01); //start col address : high byte | |
else writeData8(0x0); | |
writeData8(x0 & 0xFF); //low byte | |
if(x1 > 255) writeData8(0x01); //start col address : high byte | |
else writeData8(0x0); //end col address : high byte | |
writeData8(x1 & 0xFF); //low byte | |
writeCommand(0x2B); //set page address - 4 param | |
if(y0 > 255) writeData8(0x01); //start col address : high byte | |
else writeData8(0x0); //start page addree : high byte | |
writeData8(y0 & 0xFF); //low byte | |
if(y1 > 255) writeData8(0x01); //start col address : high byte | |
else writeData8(0x0); //end page address : high byte | |
writeData8(y1 & 0xFF); //low byte | |
writeCommand(0x2C); //write memory start | |
*/ | |
} | |
//=============================================================// | |
void setPixel(int x, int y, uint16_t color) { | |
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) | |
return; | |
setAddrWindow(x,y,x,y); | |
writeData16(color); | |
} | |
//=============================================================// | |
void fillScreen(uint16_t color) { | |
fillRectangle(0, 0, _width, _height, color); | |
} | |
//=============================================================// | |
void fillRectangle(int x, int y, int w, int h, uint16_t color) { //fillRectangle(0, 0, 480, 320, 6500); | |
if((x >= _width) || (y >= _height)) | |
return; | |
if((x + w - 1) >= _width) | |
w = _width - x; | |
if((y + h - 1) >= _height) | |
h = _height - y; | |
setAddrWindow(x, y, x+w-1, y+h-1); | |
for(y=h*2; y>0; y--) { | |
for(x=w; x>0; x--) { | |
writeData16(color); | |
} | |
} | |
} | |
//=============================================================// | |
void drawHorizontalLine(int x, int y, int w, uint16_t color) { | |
// Rudimentary clipping | |
if((x >= _width) || (y >= _height)) | |
return; | |
if((x+w-1) >= _width) | |
w = _width-x; | |
setAddrWindow(x, y, x+w-1, y); | |
while (w--) { | |
writeData16(color); | |
} | |
} | |
//=============================================================// | |
void drawVerticalLine(int x, int y, int h, uint16_t color) { | |
if((x >= _width) || (y >= _height)) | |
return; | |
if((y+h-1) >= _height) | |
h = _height-y; | |
setAddrWindow(x, y, x, y+h-1); | |
while (h--) { | |
writeData16(color); | |
} | |
} | |
//=============================================================// | |
void setRotation(int m) { | |
writeCommand (0x36); //set address mode | |
int rotation = m % 4; // can't be higher than 3 | |
switch (rotation) { | |
case 0: | |
writeData8(0x0A); | |
_width = Width; | |
_height = Height; | |
break; | |
case 1: | |
writeData8(0x28); | |
_width = Height; | |
_height = Width; | |
break; | |
case 2: | |
writeData8(0x09); | |
_width = Width; | |
_height = Height; | |
break; | |
case 3: | |
writeData8(0x2B); | |
_width = Height; | |
_height = Width; | |
break; | |
} | |
} | |
//=============================================================// | |
void invertDisplay(boolean i) { | |
writeCommand(i ? 0x21 : 0x20); | |
} | |
//=============================================================// | |
void openWindow(int x0, int y0, int x1, int y1) { | |
setAddrWindow(x0, y0, x0 + x1 - 1, y0 + y1 - 1); | |
} | |
//=============================================================// | |
void windowData(uint16_t c) { | |
writeData16(c); | |
} | |
//=============================================================// | |
void windowData(uint16_t *c, int len) { | |
for (uint32_t i = 0; i < len; i++) { | |
writeData16(c[i]); | |
} | |
} | |
//=============================================================// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment