新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > MSP430控制手機液晶屏

MSP430控制手機液晶屏

作者: 時間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
液晶屏為2.4寸TFT彩屏,

#include <msp430xG43x.h>
#define uchar unsigned char
#define uint unsigned int

本文引用地址:http://m.butianyuan.cn/article/201611/316369.htm


#include "qqp.H"
#include "ascii hex(8x16).h"
#include "GB2312.h"
uint color[]={0xf800,0x07e0,0x001f,0xffe0,0x0000,0xffff,0x07ff,0xf81f};

void delay_us(uint t);
void delay_us(uint t);

#define LCD_CONTROL_PORT P5OUT
#define LCD_CONTROL_DIR_PORT P5DIR
#define LCD_RST 0x01
#define LCD_RD 0x02
#define LCD_RS 0x04
#define LCD_CS 0x08
#define LCD_WR 0x10

void LCD_WR_REG(uchar index);
void LCD_WR_DATA8(uchar pd);
void LCD_WR_CMD(uchar index,uint val);
unsigned int LCD_RD_REG16(uchar index);

void LCD_WR_Data(uint val);
void LCD_Init();
void LCD_test();
void LCD_QQ();
void LCD_clear(uint p);

void DisplayChar(uchar casc,uchar postion_x,uchar postion_y);
void DisplayGB2312(uchar gb,uchar postion_x,uchar postion_y);
void DisplayString(uchar *s,uchar x,uchar y);//英文字符串顯示.

void delay_us(uint t)
{
t+=170;
while(--t);
}
void delay_ms(uint t)
{
uchar i,j;
while(--t)
{
j=5;
while(--j)
while(--i);
}
}

void SET_LCD_CONTROL_PORT()
{
LCD_CONTROL_DIR_PORT&=0X1F;
}

voidSET_LCD_RST_HIGH(){LCD_CONTROL_PORT|=LCD_RST; };
voidSET_LCD_RST_LOW() {LCD_CONTROL_PORT&=~LCD_RST;};

voidSET_LCD_RD_HIGH(){LCD_CONTROL_PORT|=LCD_RST;};
voidSET_LCD_RD_LOW() {LCD_CONTROL_PORT&=~LCD_RD;};

void SET_LCD_RS_HIGH(){LCD_CONTROL_PORT|=LCD_RS; };
void SET_LCD_RS_LOW() {LCD_CONTROL_PORT&=~LCD_RS;};

void SET_LCD_CS_HIGH(){LCD_CONTROL_PORT|=LCD_CS; };
void SET_LCD_CS_LOW() {LCD_CONTROL_PORT&=~LCD_CS;};

voidSET_LCD_WR_HIGH(){LCD_CONTROL_PORT|=LCD_WR; };
voidSET_LCD_WR_LOW() {LCD_CONTROL_PORT&=~LCD_WR;};

void SET_LCD_DATA(unsigned char lcd_data)
{
P4DIR&=0XFF;
P4OUT|=lcd_data;
};
unsigned int GET_LCD_DATA()
{
unsigned int lcd_data1;
P4DIR|=0X00;
lcd_data1=P4IN;
return(lcd_data1);
};

void LCD_WR_REG(uchar index)
{
SET_LCD_RS_LOW();
SET_LCD_CS_LOW ( );
//DATA = index;
SET_LCD_DATA(index);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();
}

void LCD_WR_DATA8(uchar pd)
{
SET_LCD_RS_HIGH();
SET_LCD_CS_LOW();
//DATA = pd;
SET_LCD_DATA(pd);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();
}

void LCD_WR_CMD(uchar index,uint val)
{
SET_LCD_RS_LOW();
SET_LCD_CS_LOW();
//DATA = index;
SET_LCD_DATA(index);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();

SET_LCD_RS_HIGH();
SET_LCD_CS_LOW();
//DATA = (uchar)(val>>8);
SET_LCD_DATA(val>>8);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();
SET_LCD_RS_HIGH();
SET_LCD_CS_LOW();
//DATA = (uchar)val;
SET_LCD_DATA(val);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();
}

unsigned int LCD_RD_REG16(uchar index)
{
unsigned int pd,ppd;

SET_LCD_RS_LOW();
SET_LCD_CS_LOW();
_nop_();
_nop_();

SET_LCD_DATA(index);
_nop_();
_nop_();

SET_LCD_WR_LOW();
_nop_();
_nop_();

SET_LCD_WR_HIGH();
_nop_();
_nop_();

SET_LCD_RS_HIGH();
_nop_();
_nop_();

//DATA = 0xFF;
SET_LCD_DATA(0xff);
_nop_();
_nop_();

SET_LCD_RD_LOW();
_nop_();
_nop_();
SET_LCD_RD_HIGH();
pd=GET_LCD_DATA();
//pd = DATA;

pd = pd<<8;

_nop_();
_nop_();
//DATA = 0xFF;
SET_LCD_DATA(0xff);
SET_LCD_RD_LOW();
_nop_();
_nop_();
SET_LCD_RD_HIGH();
ppd=GET_LCD_DATA();
pd = pd + ppd;
SET_LCD_CS_HIGH();
SET_LCD_RS_LOW();
return pd;
}

void LCD_WR_Data(uint val)
{
//SET_LCD_RS_HIGH();
//SET_LCD_CS_LOW();
//DATA = (uchar)(val>>8);
SET_LCD_DATA(val>>8);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
//SET_LCD_RS_HIGH();
//DATA = (uchar)val;
SET_LCD_DATA((uchar)val);
SET_LCD_WR_LOW();
SET_LCD_WR_HIGH();
//SET_LCD_CS_HIGH();
//SET_LCD_RS_LOW();
}

void LCD_Init()
{
SET_LCD_CONTROL_PORT();
//LCD_RST = 0;
SET_LCD_RST_LOW();
delay_ms(200);
//LCD_RST = 1;
SET_LCD_RST_HIGH();
delay_ms(200);

//initializing funciton 1
delay_us(10);
LCD_WR_CMD(0xA1,0x00);
LCD_WR_CMD(0xA0,0x00);
delay_us(10);
LCD_WR_CMD(0x70,0xC8);//Start osc

delay_ms(20);

//Power Setting
LCD_WR_CMD(0x72,0xA3);
LCD_WR_CMD(0x73,0x04);
LCD_WR_CMD(0x75,0x45);
LCD_WR_CMD(0x76,0x23);
LCD_WR_CMD(0x77,0x08);
LCD_WR_CMD(0x78,0x08);
LCD_WR_CMD(0x79,0x00);
LCD_WR_CMD(0x7F,0xF0);
LCD_WR_CMD(0x71,0x81);

delay_ms(2);

LCD_WR_CMD(0x0D,0x23);
//delay_us(10);
LCD_WR_CMD(0x11,0x00);
LCD_WR_CMD(0x12,0x00);
LCD_WR_CMD(0x21,0x37);
//delay_us(10);
LCD_WR_CMD(0x22,0x02);
//delay_us(10);
LCD_WR_CMD(0x23,0x24);
LCD_WR_CMD(0x24,0x13);
LCD_WR_CMD(0x25,0x0A);
LCD_WR_CMD(0x26,0x82);
LCD_WR_CMD(0x27,0x01);
LCD_WR_CMD(0x1E,0x25);
LCD_WR_CMD(0x1F,0x59);

//GAMMA Setting
LCD_WR_CMD(0x30,0x0777);
LCD_WR_CMD(0x31,0x0444);
LCD_WR_CMD(0x32,0x0555);
LCD_WR_CMD(0x33,0x0444);
LCD_WR_CMD(0x34,0x0333);
LCD_WR_CMD(0x35,0x0333);
LCD_WR_CMD(0x36,0x0333);
LCD_WR_CMD(0x37,0x0333);
LCD_WR_CMD(0x38,0x0444);
LCD_WR_CMD(0x39,0x0555);
LCD_WR_CMD(0x3A,0x0666);
LCD_WR_CMD(0x3B,0x0666);
LCD_WR_CMD(0x3C,0x0777);
LCD_WR_CMD(0x3D,0x0777);
LCD_WR_CMD(0x3E,0x0777);
LCD_WR_CMD(0x3F,0x0777);
LCD_WR_CMD(0x40,0x0777);
LCD_WR_CMD(0x41,0x0777);
LCD_WR_CMD(0x42,0x0777);
LCD_WR_CMD(0x43,0x0777);
LCD_WR_CMD(0x44,0x0777);
LCD_WR_CMD(0x45,0x0777);
LCD_WR_CMD(0x46,0x0777);
LCD_WR_CMD(0x47,0x0777);
LCD_WR_CMD(0x48,0x0777);
LCD_WR_CMD(0x49,0x0777);
LCD_WR_CMD(0x4A,0x0777);
LCD_WR_CMD(0x4B,0x0777);
LCD_WR_CMD(0x4C,0x0777);
LCD_WR_CMD(0x4D,0x0666);
LCD_WR_CMD(0x4E,0x0666);
LCD_WR_CMD(0x4F,0x0666);

LCD_WR_CMD(0x85,0x74);//在手機和MP4播視頻時很重要.

LCD_WR_CMD(0x00,0x04);
LCD_WR_CMD(0x01,0x07);

//LCD display area setting
LCD_WR_CMD(0x02,0x00);
LCD_WR_CMD(0x03,0x0000);
LCD_WR_CMD(0x04,0xEF);
LCD_WR_CMD(0x05,0x013F);
LCD_WR_CMD(0x06,0x00);
LCD_WR_CMD(0x07,0x0000);
LCD_WR_CMD(0x08,0xEF);
LCD_WR_CMD(0x09,0x013F);

LCD_WR_CMD(0x0A,0x00);
LCD_WR_CMD(0x0B,0x00);
LCD_WR_CMD(0x0C,0x00);
LCD_WR_CMD(0x14,0x00);
LCD_WR_CMD(0x15,0x00);
LCD_WR_CMD(0x16,0x00);
LCD_WR_CMD(0x17,0x01FF);
LCD_WR_CMD(0x18,0x01FF);
LCD_WR_CMD(0x13,0x00);
LCD_WR_CMD(0x19,0x01FF);
LCD_WR_CMD(0x1B,0x01FF);
LCD_WR_CMD(0x1C,0x01FF);
LCD_WR_CMD(0x1A,0x01FF);
LCD_WR_CMD(0x1D,0x0E);
LCD_WR_CMD(0x10,0x06);//開顯示.
LCD_WR_REG(0x0E);
delay_us(10);
}

void LCD_test()
{
uint temp,num,i;
uchar n;

LCD_WR_CMD(0x02,0x00);
LCD_WR_CMD(0x03,0x0000);
LCD_WR_CMD(0x04,0xEF);
LCD_WR_CMD(0x05,0x013F);

LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
for(n=0;n<8;n++)
{
temp=color[n];
for(num=40*240;num>0;num--)
LCD_WR_Data(temp);
}
for(n=0;n<8;n++)
{
LCD_WR_CMD(0x02,0x00);
LCD_WR_CMD(0x03,0x0000);
LCD_WR_CMD(0x04,0xEF);
LCD_WR_CMD(0x05,0x013F);

LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
temp=color[n];
for(i=0;i<240;i++)
for(num=0;num<320;num++)
LCD_WR_Data(temp);
}
SET_LCD_CS_HIGH();
}
void LCD_QQ()
{
uint temp,num;
const unsigned int *p;
uchar n,c,g;
//顯示小QQ圖標(biāo)
for(g=0;g<8;g++)
{
for(c=0;c<6;c++)
{
LCD_WR_CMD(0x02,c*40);
LCD_WR_CMD(0x03,g*40);
LCD_WR_CMD(0x04,c*40+39);
LCD_WR_CMD(0x05,g*40+39);
LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
p = (uint*)gImage_qqp;
for(num=0;num<1600;num++)//
{
LCD_WR_Data(*p++);
}
//delay_ms(100);
}
}
SET_LCD_CS_HIGH();
}

void LCD_clear(uint p)
{
uint i,j;

LCD_WR_CMD(0x02,0x00);
LCD_WR_CMD(0x03,0x0000);
LCD_WR_CMD(0x04,0xEF);
LCD_WR_CMD(0x05,0x013F);

LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
for(i=0;i<320;i++)
{
for(j=0;j<240;j++)
{
LCD_WR_Data(p);
}
}
SET_LCD_CS_HIGH();
}


//8X16點陣的字符顯示函數(shù)
//參數(shù)說明:
// casc:字符對應(yīng)字模中的位置
// x:顯示位置第幾列,每行是240/8 ==30列
// y:顯示位置第幾行,整屏是320/16==20行

void DisplayChar(uchar casc,uchar postion_x,uchar postion_y)
{
uchar cTemp;
uchar i,j,b;
const uchar *p;

LCD_WR_CMD(0x02,postion_x*8); //x start point
LCD_WR_CMD(0x03,postion_y*16); //y start point
LCD_WR_CMD(0x04,postion_x*8+7);//x end point
LCD_WR_CMD(0x05,postion_y*16+15);//y end point
LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
p=ascii;
p+=casc*16;
for(j=0;j<16;j++)
{
b=*(p+j);
for(i=0;i<8;i++)
{
if(b&0x80)
{
LCD_WR_Data(0xffff);
}
else
{
LCD_WR_Data(0x0000);
}
b=b<<1;

}
}
SET_LCD_CS_HIGH();
}

//16X16點陣的漢字顯示函數(shù)
//參數(shù)說明:
// gb:漢字對應(yīng)字模中的位置
// x:顯示位置第幾列
// y:顯示位置第幾行
void DisplayGB2312(uchar gb,uchar postion_x,uchar postion_y)
{
uchar cTemp;
uchar i,j,b;
const uchar *p;

LCD_WR_CMD(0x02,postion_x*16); //x start point
LCD_WR_CMD(0x03,postion_y*16); //y start point
LCD_WR_CMD(0x04,postion_x*16+15);//x end point
LCD_WR_CMD(0x05,postion_y*16+15);//y end point
LCD_WR_REG(0x0E);
SET_LCD_CS_LOW();
SET_LCD_RS_HIGH();
p=(char*)GB2132;
p+=gb*32;
for(j=0;j<32;j++)
{
b=*(p+j);
for(i=0;i<8;i++)
{
if(b&0x80)
{
LCD_WR_Data(0xffff);
}
else
{
LCD_WR_Data(0x0000);
}
b=b<<1;
}
}
SET_LCD_CS_HIGH();
}

void DisplayString(uchar *s,uchar x,uchar y)//英文字符串顯示.
{
while (*s)
{
DisplayChar(*s,x,y);
if(++x>=30)
{
x=0;
if(++y>=20)
{
y=0;
}
}
s++;
}
}


void main()
{
uchar x=0;
uchar y=0;
uchar i;

LCD_Init();
LCD_clear(0);


// delay_ms(1000);
//LCD_test();
// delay_ms(2000);
//LCD_clear(0);
// LCD_QQ();
// delay_ms(2000);

/*delay_ms(2000);
LCD_clear(0);
DisplayString("LTM024D250 testing!",0,0);
delay_ms(500);

LCD_clear(0);
for(i=2;i<8;i++)
{
DisplayGB2312(i-2,i*2,0);
}
DisplayString("2009",4,0);
DisplayString( "06",10,0);
DisplayString( "15",14,0);
DisplayString( "14",18,0);
DisplayString( "30",22,0);
DisplayString( "22",26,0);
}
*/


for(i=0;i<128;i++)
{
DisplayChar(i,x,y);
if(++x>=30)
{
x=0;
if(++y>=20)
{
y=0;
}
}
}
while(1) ;

}



關(guān)鍵詞: MSP430控制手機液晶

評論


技術(shù)專區(qū)

關(guān)閉