?? lcd.c
字號:
// FYD12864 測試程序(串口)
//***************************************************************************
//連線表: CPU=msp430 SystemClock=8Mhz
// Reset=RC in Board
//P4.4 CS模擦組片選
//P4.5 SID 串行數據輸入端
//P4.6 SCK 串行同步時鐘,上升沿有效
//P4.7 PSB串口方式接低電平
//***************************************************************************
#include <msp430x16x.h>
#include "lcd.h"
unsigned char AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行漢字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行漢字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行漢字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行漢字位置
};
//全局的AC,記錄光標的位置
char GlobeRow;
char GlobeCol;
//控制信號的430底層描述
/*******SCK 串行同步時鐘設置*******************************/
void SCK(unsigned char sck)
{
SCK_OUT;
if (sck==1)
SCK_H;
if (sck==0)
SCK_L;
}
/********************CS模擦組片選,高電平有效***********************************/
void CS(unsigned char cs)
{
CS_OUT ;
if (cs==1)
CS_H ;
if (cs==0)
CS_L;
/*********SID串行數據輸出********************************/
void OUTSID(unsigned char out)
{
SID_OUT;
if (out==0)
SID_L;
if (out==1)
SID_H ;
}
/***********SID串行數據輸入*******************************/
unsigned char INSID()
{
unsigned char temp;
SID_IN; // 設置SID串行數據輸入方向
temp=SID; // 把輸入的數據移到第0位,傳給temp
return temp;
}
//串口發送一個字節
void LCD_SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
unsigned char CY;
SCK(0);//SCK = 0;
CY=((Dbyte&0x80)>>7);
Dbyte=Dbyte<<1; //左移一位
OUTSID(CY);//SID = CY; //移出的位給SID
SCK(1);//SCK = 1;
SCK(0);//SCK = 0;
}
}
//串口接收一個字節
//僅在讀取數據的時候用到
//而讀出的數據是一次只能讀出4bit的
unsigned char LCD_ReceiveByte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
SCK(0);//SCK = 0;
SCK(1);//SCK = 1;
SCK(0);//SCK = 0;
if(INSID()) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
SCK(0);//SCK = 0;
SCK(1);//SCK = 1;
SCK(0);//SCK = 0;
if(INSID()) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
void LCD_CheckBusy( void )
{
do LCD_SendByte(0xfc); //11111,RW(1),RS(0),0
while(0x80&LCD_ReceiveByte()); //BF(.7)=1 Busy
}
void LCD_WriteCommand( unsigned char Cbyte )
{
CS(1);//CS = 1;
LCD_CheckBusy();
LCD_SendByte(0xf8); //1111,1,RW(0),RS(0),0
LCD_SendByte(0xf0&Cbyte); //高四位
LCD_SendByte(0xf0&Cbyte<<4);//低四位(先執行<<)
CS(0);// CS = 0;
}
void LCD_WriteData( unsigned char Dbyte )
{
CS(1);// CS = 1;
LCD_CheckBusy();
LCD_SendByte(0xfa); //11111,RW(0),RS(1),0
LCD_SendByte(0xf0&Dbyte); //高四位
LCD_SendByte(0xf0&Dbyte<<4);//低四位(先執行<<)
CS(0);//CS = 0;
}
unsigned char LCD_ReadData( void )
{
LCD_CheckBusy();
LCD_SendByte(0xfe); //11111,RW(1),RS(1),0
return LCD_ReceiveByte();
}
void LCD_Delay(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0) //for 12M
{ usn = 2;
while(usn!=0)
{
us=0xf5;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
//文本區清RAM函數
void LCD_LcmClearTXT( void )
{
unsigned char i;
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
LCD_WriteCommand(0x80); //AC歸起始位
for(i=0;i<64;i++)
LCD_WriteData(0x20);
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
LCD_WriteCommand(0x80); //AC歸起始位
}
//圖形區和文本區顯示在兩個不同的RAM區
//圖形區清RAM函數
void LCD_LcmClearBMP( void )
{
unsigned char i,j;
LCD_WriteCommand(0x34); //8Bit擴充指令集,即使是36H也要寫兩次
LCD_WriteCommand(0x36); //繪圖ON,基本指令集里面36H不能開繪圖
for(i=0;i<32;i++) //12864實際為256x32
{
LCD_WriteCommand(0x80|i); //行位置
LCD_WriteCommand(0x80); //列位置
for(j=0;j<32;j++) //256/8=32 byte
LCD_WriteData(0);
}
}
void LCD_setAC(unsigned char row,unsigned char col) //設置光標的位置
{
GlobeRow=row; GlobeCol=col;
}
//顯示一個字符串,row=-1和col=-1,接著上次顯示完的地方開始顯示,滿四行清屏
void LCD_PutStr( int row, int col,unsigned char *puts)
{
unsigned char temp1=0xff;
unsigned char temp2=1;
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
if ((row==-1)||(col==-1))//若row=-1和col=-1,接著上次顯示完的地方開始顯示
{
row=GlobeRow;
col=GlobeCol;
}
LCD_WriteCommand(AC_TABLE[8*row+col]); //起始位置
while(*puts != '\0') //判斷字符串是否顯示完畢
{
if(col==8) //判斷換行
{ //若不判斷,則自動從第一行到第三行
col=0;
row++;
}
if(row==4)
{
row=0;
LCD_LcmClearTXT();
/*LCD_PutStr(0,0," \0");
LCD_PutStr(1,0," \0");
LCD_PutStr(2,0," \0"); */
} //一屏顯示完,回到屏左上角
if(temp1) { //如果是第一個位置輸入
LCD_WriteCommand(AC_TABLE[8*row+col]);//寫入待顯示字符地址
if((*puts)>=0xA1) temp2=1;else temp2=0; //temp2=1為輸入的是中文
}else if((temp2==0)&&(*puts)>=0xA1) { //如果是第二個位置輸入而且是中文字符的第一個字節,則寫入一空格
LCD_WriteData(0x20);
col++;
temp1=~temp1;
continue;
}
LCD_WriteData(*puts);
puts++;
temp1=~temp1;
if(temp1) col++;
}
if(temp1=~temp1) col++;
GlobeRow=row; GlobeCol=col; //記錄當前位置
}
//顯示一個字符串,row=-1和col=-1,接著上次顯示完的地方開始顯示,滿三行清屏
void LCD_PutStr3( int row, int col,unsigned char *puts)
{
unsigned char temp1=0xff;
unsigned char temp2=1;
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
if ((row==-1)||(col==-1))//若row=-1和col=-1,接著上次顯示完的地方開始顯示
{
row=GlobeRow;
col=GlobeCol;
}
LCD_WriteCommand(AC_TABLE[8*row+col]); //起始位置
while(*puts != '\0') //判斷字符串是否顯示完畢
{
if(col==8) //判斷換行
{ //若不判斷,則自動從第一行到第三行
col=0;
row++;
}
if(row==3)
{
row=0;
LCD_PutStr(0,0," \0");
LCD_PutStr(1,0," \0");
LCD_PutStr(2,0," \0");
} //一屏顯示完,回到屏左上角
if(temp1) { //如果是第一個位置輸入
LCD_WriteCommand(AC_TABLE[8*row+col]);//寫入待顯示字符地址
if((*puts)>=0xA1) temp2=1;else temp2=0; //temp2=1為輸入的是中文
}else if((temp2==0)&&(*puts)>=0xA1) { //如果是第二個位置輸入而且是中文字符的第一個字節,則寫入一空格
LCD_WriteData(0x20);
col++;
temp1=~temp1;
continue;
}
LCD_WriteData(*puts); //一個漢字要寫兩次
puts++;
temp1=~temp1;
if(temp1) col++;
}
if(temp1=~temp1) col++;
GlobeRow=row; GlobeCol=col; //記錄當前位置
}
//顯示一個字節的數據,row=-1和col=-1,接著上次顯示完的地方開始顯示,滿4行翻屏
void LCD_PutChar ( int row, int col,unsigned char put)
{
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
if ((row==-1)||(col==-1))//若row=-1和col=-1,接著上次顯示完的地方開始顯示
{
row=GlobeRow;
col=GlobeCol;
}
if(col==8) //判斷換行
{ //若不判斷,則自動從第一行到第三行
col=0;
row++;
}
if(row==4) {row=0;LCD_LcmClearTXT();} //一屏顯示完,回到屏左上角,并清屏
LCD_WriteCommand(AC_TABLE[8*row+col]);
LCD_WriteData(put);
GlobeRow=row; GlobeCol=col+1; //記錄當前位置 ****
}
//顯示一個字節的數據,row=-1和col=-1,接著上次顯示完的地方開始顯示,滿3行翻屏
void LCD_PutChar3 ( int row, int col,unsigned char put)
{
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
if ((row==-1)||(col==-1))//若row=-1和col=-1,接著上次顯示完的地方開始顯示
{
row=GlobeRow;
col=GlobeCol;
}
if(col==8) //判斷換行
{ //若不判斷,則自動從第一行到第三行
col=0;
row++;
}
if(row==3) //一屏顯示完,回到屏左上角,清空前三行
{
row=0;
LCD_PutStr(0,0," \0");
LCD_PutStr(1,0," \0");
LCD_PutStr(2,0," \0");
}
LCD_WriteCommand(AC_TABLE[8*row+col]);
LCD_WriteData(put);
GlobeRow=row; GlobeCol=col+1;//記錄當前位置
}
void LCD_PutBMP(unsigned char *puts)
{
unsigned int x=0;
unsigned char i,j;
LCD_WriteCommand(0x34); //8Bit擴充指令集,即使是36H也要寫兩次
LCD_WriteCommand(0x36); //繪圖ON,基本指令集里面36H不能開繪圖
for(i=0;i<32;i++) //128x64實際為256x32
{
LCD_WriteCommand(0x80|i); //行位置
LCD_WriteCommand(0x80); //列位置
for(j=0;j<32;j++) //256/8=32 byte
{ //列位置每行自動增加
LCD_WriteData(puts[x]);
x++;
}
}
}
//電子測試用點陣顯示
void LCD_DisplayDots(unsigned char DotByte)
{
unsigned char i,j;
LCD_WriteCommand(0x34); //8Bit擴充指令集,即使是36H也要寫兩次
LCD_WriteCommand(0x36); //繪圖ON,基本指令集里面36H不能開繪圖
for(i=0;i<32;i++) //12864實際為256x32
{
LCD_WriteCommand(0x80|i); //行位置
LCD_WriteCommand(0x80); //列位置
for(j=0;j<32;j++) //256/8=32 byte
{ //列位置每行自動增加
LCD_WriteData(DotByte);
}
DotByte=~DotByte;
}
}
//顯示一個INT型的數據,row=-1和col=-1,接著上次顯示完的地方開始顯示,滿4行翻屏
void LCD_PutNum ( int row, int col,unsigned int num)
{
unsigned char numbuff[]={" "};
char i,j;
numbuff[5]='\0';
for (i=5;i>0;i--)
{
numbuff[i-1]=num-num/10*10+0x30;
num=num/10;
}
j=0;
while(numbuff[j]==0X30)
j++;
for (i=0;i<(5-j)+1;i++)
numbuff[i]=numbuff[i+j];
for (;i<6;i++)
numbuff[i]='\0';
LCD_PutStr(row ,col,numbuff);
}
/***************************************************************
顯示一個無符號 長整型 數據
*****************************************************************/
void LCD_PutNum_l ( int row, int col,unsigned long num)
{
unsigned char numbuff[]={" "};
char i,j;
numbuff[7]='\0';
for (i=7;i>0;i--)
{
numbuff[i-1]=num-num/10*10+0x30;
num=num/10;
}
j=0;
while(numbuff[j]==0X30)
j++;
for (i=0;i<(7-j)+1;i++)
numbuff[i]=numbuff[i+j];
for (;i<8;i++)
numbuff[i]='\0';
LCD_PutStr(row ,col,numbuff);
}
/********************************************************************************************
初始化液晶
**********************************************************************************************/
void LCD_LcmInit( void )
{
PSB_OUT; //設置PSB為低電平
PSB_L;
LCD_WriteCommand(0x30); //8BitMCU,基本指令集合
LCD_WriteCommand(0x03); //AC歸0,不改變DDRAM內容
LCD_WriteCommand(0x0C); //顯示ON,游標OFF,游標位反白OFF
LCD_WriteCommand(0x01); //清屏,AC歸0
LCD_WriteCommand(0x06); //寫入時,游標右移動
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -