Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ILI9341 for Msp430
#ifndef ILI9341_H_
#define ILI9341_H_
#include <msp430.h>
#define RES BIT0 // P1.0
#define SCLK BIT5 // P1.5
#define CS BIT4 // P1.4
#define DC BIT6 // P1.6
#define SDIN BIT7 // P1.7
// rotated
#define MAX_W 240
#define MAX_H 320
#define AQUA 0x07FF
#define BLACK 0x0000
#define BLUE 0x001f
#define CYAN 0x07ff
#define DARKCYAN 0x03EF
#define DARKGREEN 0x03E0
#define DARKGREY 0x7BEF
#define FUCHSIA 0xF81F
#define GRAY1 0x8410
#define GRAY2 0x4208
#define GRAY3 0x2104
#define GREEN 0x07e0
#define LIGHTGREEN 0xAFE5
#define LIGHTGREY 0xC618
#define LIME 0x07E0
#define MAGENTA 0xF81F
#define MAROON 0x7800
#define NAVY 0x000F
#define OLIVE 0x8400
#define ORANGE 0xFD20
#define PURPLE 0x780F
#define RED 0xf800
#define SILVER 0xC618
#define TEAL 0x0410
#define WHITE 0xffff
#define YELLOW 0xffe0
void sendCmd(uchar number) {
P1OUT &= ~DC;
while (IFG2&UCB0TXIFG)
;
UCB0TXBUF = number;
}
void sendParameter(uchar number) {
P1OUT |= DC;
while (IFG2&UCB0TXIFG)
;
UCB0TXBUF = number;
}
void select()
{
P1OUT &= ~CS;
}
void unselect()
{
P1OUT |= CS;
}
void set_cursor(int x, int y) {
if (x < 0 || x >= MAX_W)
return;
if (y < 0 || y >= MAX_H)
return;
sendCmd(0x2A);
sendParameter( x >> 8);
sendParameter( x & 0xFF);
sendCmd(0x2C);
sendCmd(0x2B);
sendParameter( y >> 8);
sendParameter( y & 0xFF);
sendCmd(0x2C);
}
void draw_pixel(uint color) {
sendParameter( color >> 8);
sendParameter( color & 0xFF);
}
void fill_rect(int x, int y, int w, int h, uint color) {
if (y >= MAX_H || x >= MAX_W || y + h >= MAX_H || x + w >= MAX_W)
return;
while (h-- >= 0) {
set_cursor(x, y++);
int i;
for (i = w; i >= 0; i--) {
draw_pixel(color);
}
}
}
void fill_screen(uint color)
{
unsigned long tot = MAX_W;
tot *= MAX_H;
tot++;
sendCmd(0x2A);
sendParameter( 0);
sendParameter( 0);
sendParameter( (MAX_H-1) >> 8);
sendParameter( (MAX_H-1) & 0xFF);
sendCmd(0x2C);
sendCmd(0x2B);
sendParameter( 0);
sendParameter( 0);
sendParameter( (MAX_W-1) >> 8);
sendParameter( (MAX_W-1) & 0xFF);
sendCmd(0x2C);
while ( tot > 0)
{
draw_pixel(color);
tot--;
}
}
void SPI_init() {
// start spi
P1DIR |= RES + DC + CS;
P1OUT &= ~RES + DC + CS;
P1SEL |= SDIN + SCLK;
P1SEL2 |= SDIN + SCLK;
UCB0CTL1 |= UCSWRST;
// SPI Master, 8bit, MSB first, synchronous mode
UCB0CTL0 |= UCMST + UCSYNC + UCCKPH + UCMSB;
UCB0CTL1 |= UCSSEL_2;
UCB0CTL1 &= ~UCSWRST;
IE2 |= UCB0TXIE;
IFG2 &= ~UCB0TXIFG;
_EINT();
}
void TFT_init() {
P1OUT |= RES;
wait_ms(10);
P1OUT &= ~RES;
wait_ms(10);
P1OUT |= RES;
wait_ms(100);
sendCmd(0xCB); // POWER CONTROL A
sendParameter(0x39); // 1. (default)
sendParameter(0x2C); // 2. (default)
sendParameter(0x00); // 3. (default)
sendParameter(0x34); // 4. (Vcore Controll: 1.55V, default)
sendParameter(0x02); // 5. (DDVDH: 5.8V, default)
sendCmd(0xCF); // POWER CONTROL B
sendParameter(0x00); // 1. (default)
sendParameter(0xC1); // 2. (PC&EQ operation for power saving enabled), 0x81 def.
sendParameter(0x30); // 3. (default)
sendCmd(0xE8); // DRIVER TIMING CONTROL A
sendParameter(0x85); // 1. (gate driver non-overlap timing control), 0x84 def.
sendParameter(0x00); // 2. (EQ timing control), 0x11 def.
sendParameter(0x78); // 3. (pre-charge timing control), 0x7A def.
sendCmd(0xEA); // DRIVER TIMING CONTROL A
sendParameter(0x00); // 1. (gate driver timing control), 0x66 def.
sendParameter(0x00); // 2. (default)
sendCmd(0xED); // POWER ON SEQUENCE CONTROL
sendParameter(0x64); // 1. (soft start control), 0x55 def.
sendParameter(0x03); // 2. (power on sequence control), 0x01 def.
sendParameter(0x12); // 3. (power on sequence control), 0x23 def.
sendParameter(0x81); // 4. (DDVDH enhance mode(only for 8 external capacitors)), enabled, 0x01 def.
sendCmd(0xF7); // PUMP RATION CONTROL
sendParameter(0x20); // 1. (ratio control)
sendCmd(0xC0); // POWER CONTROL 1
sendParameter(0x23); // 1. (set the GVDD level, 4.6V), 0x21 def.
sendCmd(0xC1); // POWER CONTROL 2
sendParameter(0x10); // 1. (sets the factor used in the step-up circuits) default
sendCmd(0xC5); // VCOM CONTROL 1
sendParameter(0x3e); // 1. (VCOMH voltage = 5.85V), 0x31 def.
sendParameter(0x28); // 2. (VCOML voltage = -1.50V), 0x3C def.
sendCmd(0xC7); // VCOM CONTROL 2
sendParameter(0x86); // 1. (VCOM offset voltage), 0xC0 def.
sendCmd(0x36); // MEMORY ACCESS CONTROL / Orientation
sendParameter(0xE8); // 1. ( ), 0x00 def.
sendCmd(0x3A); // COLMOD: PIXEL FORMAT SET
sendParameter(0x55); // 1. (sets the pixel format for the RGB image data used by the interface, 16bits/pixel)
sendCmd(0xB1); // FRAME RATE CONTROL (IN NORMAL MODE / FULL COLORS)
sendParameter(0x00); // 1. (division ratio for internal clocks when Normal mode), default
sendParameter(0x18); // 2. (frame frequency, 79Hz), 0x1B def. (70Hz)
sendCmd(0xB6); // DISPLAY FUNCTION CONTROL
sendParameter(0x08); // 1. (), 0x0A def.
sendParameter(0x82); // 2. (default)
sendParameter(0x27); // 3. (default)
sendCmd(0xF2); // 3GAMMA CONTROL
sendParameter(0x00); // 1. (disabled), 0x02 def.
sendCmd(0x26); // GAMMA SET
sendParameter(0x01); // 1. (default)
sendCmd(0xE0); // POSITIVE GAMMA CORRECTION
sendParameter(0x0F); // 1.
sendParameter(0x31); // 2.
sendParameter(0x2B); // 3.
sendParameter(0x0C); // 4.
sendParameter(0x0E); // 5.
sendParameter(0x08); // 6.
sendParameter(0x4E); // 7.
sendParameter(0xF1); // 8.
sendParameter(0x37); // 9.
sendParameter(0x07); // 10.
sendParameter(0x10); // 11.
sendParameter(0x03); // 12.
sendParameter(0x0E); // 13.
sendParameter(0x09); // 14.
sendParameter(0x00); // 15.
sendCmd(0xE1); // NEGATIVE GAMMA CORRECTION
sendParameter(0x00); // 1.
sendParameter(0x0E); // 2.
sendParameter(0x14); // 3.
sendParameter(0x03); // 4.
sendParameter(0x11); // 5.
sendParameter(0x07); // 6.
sendParameter(0x31); // 7.
sendParameter(0xC1); // 8.
sendParameter(0x48); // 9.
sendParameter(0x08); // 10.
sendParameter(0x0F); // 11.
sendParameter(0x0C); // 12.
sendParameter(0x31); // 13.
sendParameter(0x36); // 14.
sendParameter(0x0F); // 15.
sendCmd(0x11);
wait_ms(120);
sendCmd(0x2C);
sendCmd(0x29);
sendCmd(0x2C);
}
#pragma INTERRUPT (USCI)
#pragma vector=USCIAB0TX_VECTOR
void USCI(void)
{
IFG2&=~UCB0TXIFG;
}
#endif /* ILI9341_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment