#include"msp430g2553.h"
//此程序用于STC12C5A60S2系列单片机上运行,运行时选用12MHz晶体
//与3310LCD的接口关系如下:
//VCC---(Pin1)
//GND---(Pin6)
//Vout---(Pin7)
#define set(x) P2OUT|=x
#define clr(x) P2OUT&=~x
#define SCK BIT0 //P2.0----SCK(Pin2串行时钟)
#define SDIN BIT1 //P2.1----SDIN(Pin3串行数据)
#define DC BIT2 //P2.2----D/C(Pin4数据/命令模式)
#define SCE BIT3 //P2.3----SCE(Pin5使能)
#define RES BIT4 //P2.4----RES(Pin8复位)
#define COMMAND 0x00 //命令
#define DATA 0x01 //数据
/*显示图像,84*48像素*/
/*const unsigned char DISP_CODE[6][84]=
unsigned char DISP_string0[]={"I Love You DOVE "};
unsigned char DISP_string1[]={"01234567891011AB"};
unsigned char DISP_string2[]={"A B C D a b c d "};
/*-----------------------------------------------------------------------
LCD_write_byte : 使用SPI接口写数据到LCD
输入参数:data :写入的数据;
com :写数据/命令选择;
-----------------------------------------------------------------------*/
void LCD_write_byte(unsigned char dat, unsigned char com)
{
unsigned char i;
if (com == 0) //命令模式
clr(DC);
else
set(DC); //数据模式
clr(SCE); //使能
_delay_cycles(10);
for(i=0;i<8;i++) //按照SPI协议发送数据
{
clr(SCK);
if(dat&0x80)
set(SDIN);
else
clr(SDIN);
dat=dat<<1;
set(SCK);
}
set(SCE); //失能
}
/*-----------------------------------------------------------------------
LCD_set_XY : 设置LCD坐标函数
输入参数: X :0-83
Y :0-5
-----------------------------------------------------------------------*/
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x80 | X, COMMAND); // row
LCD_write_byte(0x40 | Y, COMMAND); // column
}
/*------------------------------------------------------------------------
*------------------------------刷屏函数------------------------------------
--------------------------------------------------------------------------*/
/*void DISP_Memory(void)
{
unsigned char i,j;
for(i=0;i<6;i++)
{
LCD_set_XY(0,i);
for(j=0;j<84;j++)
{
LCD_write_byte(DISP_CODE[j],DATA);
}
}
}*/
/*-----------------------------------------------------------------------
LCD_XY : 设置LCD坐标函数,精确到具体的位
输入参数: X :0-83
Y :0-47
这里的坐标都是以(0,0)为起始点!
次屏幕分辨率为84*48,共6*84个字节,这里分为6*84个块。
先确定块坐标,再确定块内坐标。
-----------------------------------------------------------------------*/
//void LCD_XY(unsigned char X, unsigned char Y)
//{
// unsigned char DATA_CODE;
// unsigned char DY;/*定义调用LCD_set_XY(X,Y)函数时需要用到的Y值*/
// unsigned char n;/*定义Y轴偏移量*/
// DY=Y/8;/*确定DY的值*/
// n=Y%8;/*确定偏移量*/
// if(n==0)DATA_CODE=BIT0;
// if(n==1)DATA_CODE=BIT1;
// if(n==2)DATA_CODE=BIT2;
// if(n==3)DATA_CODE=BIT3;
// if(n==4)DATA_CODE=BIT4;
// if(n==5)DATA_CODE=BIT5;
// if(n==6)DATA_CODE=BIT6;
// if(n==7)DATA_CODE=BIT7;
// LCD_set_XY(X,DY);/*确定所在块坐标*/
// LCD_write_byte(DATA_CODE,DATA);/*确定块内坐标,打点显示*/
//DISP_CODE[X][DY] |= DATA_CODE;
//DISP_Memory();/*刷屏*/
//}
/*-----------------------------------------------------------------------
LCD_write_char : 显示英文字符
输入参数:c :显示的字符;16*16字模
X,Y :显示位置
把屏幕分成5行3列,共可以显示15个字母
-----------------------------------------------------------------------*/
/*void LCD_write_char_16_16(unsigned char c,unsigned char X,unsigned char Y)
{
unsigned char line;
c=c-'A';
//显示字符上半部分
LCD_set_XY(16*X,2*Y);
for(line=0; line<16; line++)
LCD_write_byte(DISP_16_16[c][line], DATA);
//显示字符下半部分
LCD_set_XY(16*X,2*Y+1);
for(line=0; line<16; line++)
LCD_write_byte(DISP_16_16[c][line+16], DATA);
}*/
/*-----------------------------------------------------------------------
LCD_write_char : 显示英文字符
输入参数:c :显示的字符;8*5字模
-----------------------------------------------------------------------*/
void LCD_write_char_8_5(unsigned char c,unsigned char X,unsigned char Y)
{
unsigned char line;
if((c<='Z')&&(c>='A'))c=c-'A';
if((c<='z')&&(c>='a'))c=c-'a'+26;
if((c<='9')&&(c>='0'))c=c-'0'+52;
if(c==' ')c=62;
/*显示字符上半部分*/
LCD_set_XY(5*X,Y);
for(line=0; line<5; line++)
LCD_write_byte(DISP_8_5[c][line], DATA);
}
/*-----------------------------------------------------------------------
LCD_write_char : 显示英文字符串
输入参数:*c :显示的字符串指针
X,Y :显示位置
把屏幕分成5行3列,共可以显示15个字母
-----------------------------------------------------------------------*/
void LCD_write_string(unsigned char *c,unsigned char X,unsigned char Y)
{
while(*c!='\0')
{
//LCD_write_char_16_16(*c,X,Y);
LCD_write_char_8_5(*c,X,Y);
*c++;
X++;
}
}
/*-----------------------------------------------------------------------
LCD_clear : LCD清屏函数
-----------------------------------------------------------------------*/
void LCD_clear(void)
{
unsigned int i,j;
for(i=0;i<6;i++)
{
LCD_set_XY(0,i); //设置XY坐标
for(j=0;j<84;j++) //显示内容
{
LCD_write_byte(0,DATA);
}
}
}
/*-----------------------------------------------------------------------
* --------------------------初始化函数----------------------------------*
-----------------------------------------------------------------------*/
void LCD_INIT(void)
{
P2DIR|=SCK+SDIN+DC+SCE+RES; //设置P2.0-P2.4为输出
clr(RES); // 产生一个让LCD复位的低电平脉冲
_delay_cycles(10);
set(RES);
clr(SCE); // 关闭LCD
_delay_cycles(10);
set(SCE); // 使能LCD
_delay_cycles(10);
LCD_write_byte(0x21, COMMAND); // 使用扩展命令设置LCD模式
LCD_write_byte(0xc8, COMMAND); // 设置偏置电压
LCD_write_byte(0x06, COMMAND); // 温度校正
LCD_write_byte(0x13, COMMAND); // 1:48
LCD_write_byte(0x20, COMMAND); // 芯片活动,水平寻址,基本指令
LCD_clear(); // 清屏
LCD_write_byte(0x0c, COMMAND); // 设定显示模式,正常显示
clr(SCE); // 关闭LCD
}
/*------------------------------------------------------------------------
* ------------------------主函数-----------------------------------------
------------------------------------------------------------------------*/
void main(void)
{
//int n,i;
WDTCTL = WDTPW + WDTHOLD; // 杀狗
BCSCTL2 = CALBC1_16MHZ; // 设置系统频率
DCOCTL = CALDCO_16MHZ;
BCSCTL2 |= SELM_0 + DIVM_0;
LCD_INIT(); //LCD初始化
LCD_write_byte(0x20,COMMAND); //LCD基本指令设置
/*for(n=0;n<6;n++)
{
LCD_set_XY(0,n); //设置XY坐标
for(i=0;i<84;i++) //显示内容
{
LCD_write_byte(0xF0,DATA);
}
}
_delay_cycles(500000);
LCD_clear();*/
/*DISP_Memory();*/
//LCD_write_char_8_5('I',2,1);
LCD_write_string(DISP_string0,0,0);
LCD_write_string(DISP_string1,0,1);
LCD_write_string(DISP_string2,0,2);
LCD_write_string(DISP_string0,0,3);
LCD_write_string(DISP_string1,0,4);
LCD_write_string(DISP_string2,0,5);
while(1);
}
文章评论(0条评论)
登录后参与讨论