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

📄 op_lcd.c

📁 用msp430 对128×64的LCD的完全操作
💻 C
字号:
/*
 * edited by dingnan,  DUT
 *2003.09.16
 *Op_LCD is used to operate the 12864 LCD;
 */

/* 
 * D/I       :p1.5
 * R/W      :p1.6
 * E        :p1.7
 * DB0~DB7  :p2.0~p2.7
 * CS1      :p3.0
 * CS2      :p3.1
 * RST      :be linked to high;
 * VEE      :be offered by it self;
 */
 
#include <msp430x14x.h>
#include "Op_LCD.h"
#include "Main.h"
#include "Init.h"
//#include "character.h"

void delay(unsigned int con)
{
  unsigned int temp;
  for(temp = 0 ;temp < con  ; temp++)
  {
  }
}

//check the BF ; 1:busy;  0:idle;
//lr: lr=0 left; lr=1 right;
//used inside;
unsigned char read_BF(unsigned char lr) 
{
  unsigned char condition;
  if(lr==0)
  {
    P3OUT |=  0x01;   //set cs1=1;
    P3OUT &=  0xFD;   //set cs2=0;
  }
  else
  {
    P3OUT |=  0x02;   //set cs2=1;
    P3OUT &=  0xFE;   //set cs1=0;
  }
  
  //delay(10);
  P1OUT |=  0x80;     //E = 1;
  P1OUT &=  0x7F;     //E = 0;
  
  delay(30);
  
  P1OUT &=  0xBF;     //R/W = 0;
  P1OUT |=  0x40;     //R/W = 1;
    
  P1OUT &=  0xDF;     //D/I =0;
  delay(30);
  
  P1OUT |=  0x80;     //E = 1;
  //delay(10);
    
  P2DIR = 0x00;   // 0000 0000  change p2 to input
  delay(10);
  condition  =  P2IN;
  
  if((condition & 0x80) == 0x00)    //check BF
  {
    P2DIR = 0xFF;   // 1111 1111  rechange p2 to output
    return 0;  
  }
  /*
  {
    if((condition & 0x20) == 0x00)    //check DISP ON/OFF
    {
      if((condition & 0x10) == 0x00)  //check RET;
       {
          
          return 0;
       }
      else 
        return 1;  
      
    }
    else
      return 1;
  }
  */
  else
  {
    P2DIR = 0xFF;   // 1111 1111  rechange p2 to output
    return 1;
  }
}


//cs1 = 1; cs2= 0;
//used inside;
void commond_left(unsigned char commond)
{
  unsigned char flag = 1;
  P3OUT |=  0x01;   //set cs1=1;
  P3OUT &=  0xFD;   //set cs2=0;
  
  while(flag)
  {
    flag   =  read_BF(0);
  }
  //open the LCD  (0 0 0011 1111);
  flag = 1;
  P1OUT |=  0x80;     //E = 1;
  P1OUT &=  0x7F;     //E = 0;
  
  delay(30);
  
  //P1OUT |=  0x40;     //R/W = 1;
  P1OUT &=  0xBF;     //R/W = 0;
  
  P1OUT &=  0xDF;     //D/I =0;
  delay(10);
  
  P1OUT |=  0x80;     //E = 1;
  

  P2OUT  =  commond;
  //delay(10);
  P1OUT &=  0x7F;     //E = 0;
  delay(30);
  
}
//cs1 = 0; cs2= 1;
//used inside;
void commond_right(unsigned char commond)
{
  unsigned char flag =1;
  P3OUT |=  0x02;   //set cs2=1;
  P3OUT &=  0xFE;   //set cs1=0;
  
   while(flag)
  {
    flag   =  read_BF(1);
  }
 
  flag = 1;
  
  P1OUT |=  0x80;     //E = 1;
  P1OUT &=  0x7F;     //E = 0;
  
  delay(30);
  
 // P1OUT |=  0x40;     //R/W = 1;
  P1OUT &=  0xBF;     //R/W = 0;
  
  P1OUT &=  0xDF;     //D/I =0;
  delay(10);
  
  P1OUT |=  0x80;     //E = 1;

  P2OUT  =  commond;
  //delay(10);
  P1OUT &=  0x7F;     //E = 0;
  delay(30);
  
}

//disp the data on the left_disp;
//used inside;
void data_left(unsigned char data)
{
  unsigned char flag = 1;
  P3OUT |=  0x01;   //set cs1=1;
  P3OUT &=  0xFD;   //set cs2=0;
  
  while(flag)
  {
    flag   =  read_BF(0);
  }
  flag = 1;
  
  P1OUT |=  0x80;     //E = 1;
  P1OUT &=  0x7F;     //E = 0;
  
  delay(30);
  
  P1OUT |=  0x40;     //R/W = 1;
  P1OUT &=  0xBF;     //R/W = 0;
  
  P1OUT |=  0x20;     //D/I =1;
  delay(30);
  
  P1OUT |=  0x80;     //E = 1;
  P2OUT  =  data;
  P1OUT &=  0x7F;     //E = 0;
  delay(30);
  
}

//disp the data on the right_disp
//used inside;
void data_right(unsigned char data)
{
  unsigned char flag = 1;
  P3OUT |=  0x02;   //set cs1=1;
  P3OUT &=  0xFE;   //set cs2=0;
  
  while(flag)
  {
    flag   =  read_BF(1);
  }
  flag = 1;
  
  P1OUT |=  0x80;     //E = 1;
  P1OUT &=  0x7F;     //E = 0;
  
  delay(30);
  
  P1OUT |=  0x40;     //R/W = 1;
  P1OUT &=  0xBF;     //R/W = 0;
  
  P1OUT |=  0x20;     //D/I =1;
  delay(30);
  
  P1OUT |=  0x80;     //E = 1;
  P2OUT  =  data;
  P1OUT &=  0x7F;     //E = 0;
  delay(30);
  
}

//test , disp black_screen;

void fill(void)
{
  unsigned char  pos_x, pos_y,i;
  for(pos_x=0; pos_x<8; pos_x++)
  {
    commond_left(pos_x+0xB8);
    commond_right(pos_x+0xB8);
    commond_left(0x40);
    commond_right(0x40);
    for(i=0;i<64;i++)
      {
       data_left(0xff);
       data_right(0xff);
      } 
  }
}
  


void cls(void)
{
  unsigned char  pos_x, pos_y,i;
  for(pos_x=0; pos_x<8; pos_x++)
  {
    commond_left(pos_x+0xB8);
    commond_right(pos_x+0xB8);
    commond_left(0x40);
    commond_right(0x40);
    for(i=0;i<64;i++)
      {
       data_left(0x00);
       data_right(0x00);
      } 
  }
}
//pos_x:(0~7) 相当于y轴;
//pos_y:(0~125)相当于x轴;(0~63 cs1=1    64~125 cs2=1)
void set_cursor(unsigned char pos_x,unsigned char pos_y)
{
  if(pos_y < 64)
  {
    commond_left(pos_x+0xb8);
    commond_left(pos_y|0x40);
  }
  else
  {
    commond_right(pos_x+0xb8);
    commond_right(pos_y-64+0x40);
  }
}

//disp_num : be used to disp the 8*8 charator;
void disp_num(unsigned char pos_x,unsigned char pos_y,unsigned char *data)
{
  unsigned char i,temp;
  //flag = 1; //用于标志已经更改过地址;
  temp = pos_y;
  set_cursor(pos_x,pos_y);
  for(i=0;i<8;i++)
  {
    if(temp <64)
    {
      set_cursor(pos_x,temp);
      data_left(data[i]);
    }
    else
    {
      if(temp == 64)
      {
          set_cursor(pos_x,64);
          data_right(data[i]);
      }
      else
      {
        if(temp == 128)
        {
          set_cursor(pos_x+1,0);
          data_left(data[i]);
          temp = 0;
        }
        else
         {
          set_cursor(pos_x,temp);
           data_right(data[i]);
          }
      }
    }
    temp++;
      
  }

}

//pos_x: 相当于纵坐标;
//pos_y: 相当于横坐标;
//此程序段用于显示16*16的字符;
void disp_character(unsigned char pos_x,unsigned char pos_y,unsigned char *data)
{
  char i,temp;
  temp  = pos_y;
  set_cursor(pos_x,pos_y);
  for(i=0;i<16;i++)
  {
    if(temp <64)
    {
      data_left(data[i]);
    }
    else
    {
      if(temp == 64)
      {
          set_cursor(pos_x,64);
          data_right(data[i]);
      }
      else
      {
        if(temp == 128)
        {
          set_cursor(pos_x+2,0);
          data_left(data[i]);
          pos_y = 0;
        }
        else
          data_right(data[i]);
      }
    }
    temp++;
  }
  
  set_cursor(pos_x+1,pos_y);
  temp = pos_y;
  for(i=0;i<16;i++)
  {
    if(temp <64)
    {
      data_left(data[16+i]);
    }
    else
    {
      if(temp == 64)
      {
          set_cursor(pos_x+1,64);
          data_right(data[16+i]);
      }
      else
      {
        if(temp == 128)
        {
          set_cursor(pos_x+3,0);
          data_left(data[16+i]);
          pos_y = 0;
        }
        else
          data_right(data[16+i]);
      }
    }
    temp++;
  }
  
}

void disp_logo1(unsigned char *data1)
{
  char pos_x,pos_y,temp;
  pos_x = 0;
  pos_y = 0;
  temp = 0;
  set_cursor(pos_x,pos_y);
  for(pos_x=0;pos_x<8;pos_x++)
  {
    set_cursor(pos_x,0);
    for(pos_y=0;pos_y<128;pos_y++)
    {
      if(pos_y<64)
      {
        set_cursor(pos_x,pos_y);
        data_left(data1[128*pos_x+pos_y]);
      }
      else 
      {
               
         set_cursor(pos_x,pos_y);
         data_right(data1[128*pos_x+pos_y]);
         
        
      }
     
    }
  }
  //disp_num(pos_x,pos_y,(unsigned char *)num[0]);
}

void init_LCD(void)
{
//  unsigned char flag = 1;
  // initialize the ports;
  P1DIR = 0xE0;   // 1110 0000  
  P2DIR = 0xFF;   // 1111 1111
  P3DIR = 0x03;   // 0000 0011
  
  delay(10000);
  commond_left(0x3F); //open the LCD  (0 0 0011 1111);
  commond_right(0x3F);
  commond_left(0xC0); //(0 0 1100 1000)set the 0th line to be the first line;(0~63)
  commond_right(0xC0);
     
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -