⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.c

📁 小型实时可控操作系统 单片机为msp430
💻 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 + -