/*
===============================================================================
Name : main.c
Author :
Version :
Copyright : Copyright (C)
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
#include <NXP/crp.h>
// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
#include "lpc17xx_spi.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_gpio.h"
/************************** PRIVATE DEFINITONS **********************/
// PORT number that /CS pin assigned on
#define CS_PORT_NUM 0
// PIN number that /CS pin assigned on
#define CS_PIN_NUM 16
#define RST_PORT_NUM 0
#define RST_PIN_NUM 2
// SPI Configuration structure variable
SPI_CFG_Type SPI_ConfigStruct;
// Prototypes
int initDisp(uint8_t retries);
void CS_Init(void);
void lcd_clear(unsigned int color);
void lcd_draw(unsigned int color);
void lcd_drawstop(void);
void lcd_drawstart(void);
void lcd_cmd(unsigned int reg, unsigned int param);
void lcd_data(unsigned int c);
void lcd_reset(void);
void delay_ms(int n);
// Defines
#define LCD_WIDTH (320)
#define LCD_HEIGHT (240)
#define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
#define LCD_ID (0)
#define LCD_DATA ((0x72)|(LCD_ID<<2))
#define LCD_REGISTER ((0x70)|(LCD_ID<<2))
int main(void)
{
// Init Display
initDisp(0);
delay_ms(1000);
lcd_reset();
delay_ms(1000);
unsigned int color = 0;
while(1)
{
lcd_clear(color);
color += 100;
if (color > 65535)
{
color = 0;
}
delay_ms(100);
}
return 0;
}
/*********************************************************************//**
* @brief Initialize CS pin as GPIO function to drive /CS pin
* due to definition of CS_PORT_NUM and CS_PORT_NUM
* @param None
* @return None
***********************************************************************/
void CS_Init(void)
{
GPIO_SetDir(CS_PORT_NUM, (1<<CS_PIN_NUM), 1);
GPIO_SetValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
}
void RST_Init(void)
{
GPIO_SetDir(RST_PORT_NUM, (1<<RST_PIN_NUM), 1);
GPIO_SetValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
}
/*********************************************************************//**
* @brief Drive CS output pin to low/high level to select slave device
* via /CS pin state
* @param[in] state State of CS output pin that will be driven:
* - 0: Drive CS pin to low level
* - 1: Drive CS pin to high level
* @return None
***********************************************************************/
void CS_Force(int32_t state)
{
if (state){
GPIO_SetValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
}else{
GPIO_ClearValue(CS_PORT_NUM, (1<<CS_PIN_NUM));
}
}
void RST_Force(int32_t state)
{
if (state){
GPIO_SetValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
}else{
GPIO_ClearValue(RST_PORT_NUM, (1<<RST_PIN_NUM));
}
}
int initDisp(uint8_t retries)
{
PINSEL_CFG_Type PinCfg;
SPI_DATA_SETUP_Type xferConfig;
/*
* Initialize SPI pin connect
* P0.15 - SCK;
* P0.16 - SSEL - used as GPIO
* P0.17 - MISO
* P0.18 - MOSI
*/
PinCfg.Funcnum = 3;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 15;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 17;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 18;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 16;
PinCfg.Funcnum = 0;
PINSEL_ConfigPin(&PinCfg);
// initialize SPI configuration structure to default
SPI_ConfigStructInit(&SPI_ConfigStruct);
// Initialize SPI peripheral with parameter given in structure above
SPI_Init(LPC_SPI, &SPI_ConfigStruct);
// Initialize /CS pin to GPIO function
CS_Init();
// Initialize /RST pin to GPIO function
RST_Init();
return 0;
}
void lcd_clear(unsigned int color)
{
unsigned int i;
lcd_area(0, 0, (LCD_WIDTH-1), (LCD_HEIGHT-1));
lcd_drawstart();
for(i=(LCD_WIDTH*LCD_HEIGHT/8); i!=0; i--)
{
lcd_draw(color); //1
lcd_draw(color); //2
lcd_draw(color); //3
lcd_draw(color); //4
lcd_draw(color); //5
lcd_draw(color); //6
lcd_draw(color); //7
lcd_draw(color); //8
}
lcd_drawstop();
return;
}
void lcd_draw(unsigned int color)
{
SPI_SendData(LPC_SPI, color>>8);
SPI_SendData(LPC_SPI, color);
return;
}
void lcd_drawstop(void)
{
CS_Force(1);
return;
}
void lcd_drawstart(void)
{
CS_Force(0);
SPI_SendData(LPC_SPI, LCD_REGISTER);
SPI_SendData(LPC_SPI, 0x22);
CS_Force(1);
CS_Force(0);
SPI_SendData(LPC_SPI, LCD_DATA);
return;
}
void lcd_cmd(unsigned int reg, unsigned int param)
{
CS_Force(0);
SPI_SendData(LPC_SPI, LCD_REGISTER);
SPI_SendData(LPC_SPI, reg);
CS_Force(1);
CS_Force(0);
SPI_SendData(LPC_SPI, LCD_DATA);
SPI_SendData(LPC_SPI, param);
CS_Force(1);
}
void lcd_data(unsigned int c)
{
CS_Force(0);
SPI_SendData(LPC_SPI, LCD_DATA);
SPI_SendData(LPC_SPI, c >> 8);
SPI_SendData(LPC_SPI, c & 0xFF);
CS_Force(1);
}
void lcd_area(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
{
lcd_cmd(0x03, (x0>>0)); //set x0
lcd_cmd(0x02, (x0>>8)); //set x0
lcd_cmd(0x05, (x1>>0)); //set x1
lcd_cmd(0x04, (x1>>8)); //set x1
lcd_cmd(0x07, (y0>>0)); //set y0
lcd_cmd(0x06, (y0>>8)); //set y0
lcd_cmd(0x09, (y1>>0)); //set y1
lcd_cmd(0x08, (y1>>8)); //set y1
}
void lcd_reset(void)
{
//reset
CS_Force(1);
RST_Force(0);
delay_ms(50);
RST_Force(1);
delay_ms(50);
//driving ability
lcd_cmd(0xEA, 0x0000);
lcd_cmd(0xEB, 0x0020);
lcd_cmd(0xEC, 0x000C);
lcd_cmd(0xED, 0x00C4);
lcd_cmd(0xE8, 0x0040);
lcd_cmd(0xE9, 0x0038);
lcd_cmd(0xF1, 0x0001);
lcd_cmd(0xF2, 0x0010);
lcd_cmd(0x27, 0x00A3);
//power voltage
lcd_cmd(0x1B, 0x001B);
lcd_cmd(0x1A, 0x0001);
lcd_cmd(0x24, 0x002F);
lcd_cmd(0x25, 0x0057);
//VCOM offset
lcd_cmd(0x23, 0x008D); //for flicker adjust
//power on
lcd_cmd(0x18, 0x0036);
lcd_cmd(0x19, 0x0001); //start osc
lcd_cmd(0x01, 0x0000); //wakeup
lcd_cmd(0x1F, 0x0088);
delay_ms(5);
lcd_cmd(0x1F, 0x0080);
delay_ms(5);
lcd_cmd(0x1F, 0x0090);
delay_ms(5);
lcd_cmd(0x1F, 0x00D0);
delay_ms(5);
//color selection
lcd_cmd(0x17, 0x0005); //0x0005=65k, 0x0006=262k
//panel characteristic
lcd_cmd(0x36, 0x0000);
//display on
lcd_cmd(0x28, 0x0038);
delay_ms(40);
lcd_cmd(0x28, 0x003C);
//display options
#ifdef LCD_MIRROR
lcd_cmd(0x16, 0x0068); //MY=0 MX=1 MV=1 ML=0 BGR=1
#else
lcd_cmd(0x16, 0x00A8); //MY=1 MX=0 MV=1 ML=0 BGR=1
#endif
lcd_area(0, 0, (LCD_WIDTH-1), (LCD_HEIGHT-1));
return;
}
void delay_ms(int n)
{
volatile int d;
for (d=0; d<n*3000; d++){}
}
|