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

📄 lcdd.txt

📁 LCD驱动显示模块实例源码
💻 TXT
📖 第 1 页 / 共 3 页
字号:
/** 
 * @file  hal_lcd.c
 * 
 * Copyright 2008 Texas Instruments, Inc.
***************************************************************************/

#include  <msp430x54xA.h>
#include "hal_MSP-EXP430F5438.h"
#include "hal_lcd_fonts.h"

unsigned char LcdInitMacro[]={
            0x74,0x00,0x00,0x76,0x00,0x01,  // R00 start oscillation//开始振动
            0x74,0x00,0x01,0x76,0x00,0x0D,  // R01 driver output control//驱动输出控制
            0x74,0x00,0x02,0x76,0x00,0x4C,  // R02 LCD - driving waveform control//LCD波形控制
            0x74,0x00,0x03,0x76,0x12,0x14,  // R03 Power control//电源控制
            0x74,0x00,0x04,0x76,0x04,0x66,  // R04 Contrast control//对比度控制
            0x74,0x00,0x05,0x76,0x00,0x10,  // R05 Entry mode//进入模式
            0x74,0x00,0x06,0x76,0x00,0x00,  // R06 RAM data write mask
            0x74,0x00,0x07,0x76,0x00,0x15,  // R07 Display control//显示控制
            0x74,0x00,0x08,0x76,0x00,0x03,  // R08 Cursor Control//光标控制
            0x74,0x00,0x09,0x76,0x00,0x00,  // R09 RAM data write mask//内存写数据
            0x74,0x00,0x0A,0x76,0x00,0x15,  // R0A 
            0x74,0x00,0x0B,0x76,0x00,0x03,  // R0B Horizontal Cursor Position//水平光标位置
            0x74,0x00,0x0C,0x76,0x00,0x03,  // R0C Vertical Cursor Position//垂直光标位置
            0x74,0x00,0x0D,0x76,0x00,0x00,  // R0D 
            0x74,0x00,0x0E,0x76,0x00,0x15,  // R0E 
            0x74,0x00,0x0F,0x76,0x00,0x03,  // R0F 
            0x74,0x00,0x10,0x76,0x00,0x15,  // R0E 
            0x74,0x00,0x11,0x76,0x00,0x03,  // R0F 
};
//读区域地址数组
unsigned char Read_Block_Address_Macro[]= {0x74,0x00,0x12,0x77,0x00,0x00};
//画区域值数组
unsigned char Draw_Block_Value_Macro[]={0x74,0x00,0x12,0x76,0xFF,0xFF};
//画区域地址数组
unsigned char Draw_Block_Address_Macro[]={0x74,0x00,0x11,0x76,0x00,0x00};

unsigned int  LcdAddress = 0, LcdTableAddress = 0;
unsigned char contrast   = 0x66;//对比度
unsigned char backlight  = 8;背光
int LCD_MEM[110*17];

/**********************************************************************//**
 * @brief  Sends 3+3 bytes of data to the LCD using the format specified
 *         by the LCD Guide.
 * 
 * @param  Data[] Data array for transmission 
 * 
 * @return none
 *************************************************************************/
void halLcdSendCommand(unsigned char Data[]) 
{
  unsigned char i;

  LCD_CS_RST_OUT &= ~LCD_CS_PIN;            //CS = 0 --> Start Transfer
  for ( i = 0; i < 6; i++ )
  {
    while (!(UCB2IFG & UCTXIFG));           // Wait for TXIFG    
    UCB2TXBUF = Data[i];                    // Load data 
 
    while (UCB2STAT & UCBUSY);       

    if (i == 2)                             //Pull CS up after 3 bytes
    {
      LCD_CS_RST_OUT |= LCD_CS_PIN;         //CS = 1 --> Stop Transfer
      LCD_CS_RST_OUT &= ~LCD_CS_PIN;        //CS = 0 --> Start Transfer	 
    }
  }
  LCD_CS_RST_OUT |= LCD_CS_PIN;             //CS = 1 --> Stop Transfer       
}

/**********************************************************************//**
 * @brief  Initializes the USCI module, LCD device for communication. 
 *           
 * - Sets up the SPI2C Communication Module
 * - Performs Hitachi LCD Initialization Procedure
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdInit(void) //初始化LCD屏
{
  volatile unsigned int i=0;

  LCD_CS_RST_OUT |= LCD_CS_PIN | LCD_RESET_PIN ;
  LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN ;     
  
  LCD_COMM_SEL |= LCD_BACKLIGHT_PIN;
  
  LCD_CS_RST_OUT &= ~LCD_RESET_PIN;         // Reset LCD
  __delay_cycles(0x47FF);                   //Reset Pulse
  LCD_CS_RST_OUT |= LCD_RESET_PIN;        
    
  // UCLK,MOSI setup, SOMI cleared
  P9SEL |= BIT1 + BIT3;
  P9SEL &= ~BIT2;
  P9DIR |= BIT1 + BIT3;
  P9DIR &= ~BIT2;
  
  /* Initialize USCI state machine */ 
  UCB2CTL0 |= UCMST+UCSYNC+UCCKPL+UCMSB;    // 3-pin, 8-bit SPI master
  UCB2CTL1 |= UCSSEL_2;                     // SMCLK
  UCB2BR0 = 3; 
  UCB2BR1 = 0;
  UCB2CTL1 &= ~UCSWRST;                            
  UCB2IFG &= ~UCRXIFG;
   
  // LCD Initialization Routine Using Predefined Macros
  while (i < 8*6)
  {    
    halLcdSendCommand(&LcdInitMacro[i]);
    i += 6;
  }  
  halLcdActive();  
}

/**********************************************************************//**
 * @brief  Shuts down the LCD display and disables the USCI communication. 
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdShutDown(void)//关闭LCD
{
  halLcdStandby();  

  LCD_CS_RST_DIR |= LCD_CS_PIN | LCD_RESET_PIN ;
  LCD_CS_RST_OUT &= ~(LCD_CS_PIN | LCD_RESET_PIN );  
  LCD_CS_RST_OUT &= ~LCD_RESET_PIN;                                         
  
  P9SEL &= ~(BIT1 + BIT3 + BIT2);
  P9DIR |= BIT1 + BIT3 + BIT2;
  P9OUT &= ~(BIT1 + BIT3 + BIT2);  
  
  UCB2CTL0 = UCSWRST; 
}  

/**********************************************************************//**
 * @brief  Initializes the LCD backlight PWM signal. 
 * 
 * @param  none
 * 
 * @return none
 * 
 *************************************************************************/
void halLcdBackLightInit(void)
{
  LCD_COMM_DIR |= LCD_BACKLIGHT_PIN;
  LCD_COMM_OUT |= LCD_BACKLIGHT_PIN;     
  
  LCD_COMM_SEL |= LCD_BACKLIGHT_PIN;
  TA0CCTL3 = OUTMOD_7;
  TA0CCR3 = TA0CCR0 >> 1 ;
  backlight = 8;
  
  TA0CCR0 = 400;
  TA0CTL = TASSEL_2+MC_1;   
}

/**********************************************************************//**
 * @brief  Get function for the backlight PWM's duty cycle. 
 * 
 * @param  none 
 * 
 * @return backlight One of the the 17 possible settings - valued 0 to 16. 
 *
 *************************************************************************/
unsigned int halLcdGetBackLight(void)
{  
  return backlight;
}

/**********************************************************************//**
 * @brief  Set function for the backlight PWM's duty cycle 
 * 
 * @param  BackLightLevel The target backlight duty cycle - valued 0 to 16.
 * 
 * @return none
 *************************************************************************/
void halLcdSetBackLight(unsigned char BackLightLevel)
{ 
  unsigned int dutyCycle = 0, i, dummy; 
  
  if (BackLightLevel > 0)
  {
    TA0CCTL3 = OUTMOD_7;
    dummy = (TA0CCR0 >> 4);
    
    for (i = 0; i < BackLightLevel; i++) 
      dutyCycle += dummy;
      
    TA0CCR3 = dutyCycle;
    
    // If the backlight was previously turned off, turn it on. 
    if (!backlight)                         
      TA0CTL |= MC0;  
  }
  else
  {   	
    TA0CCTL3 = 0;
    TA0CTL &= ~MC0;
  }  
  backlight = BackLightLevel;
}

/**********************************************************************//**
 * @brief  Turns off the backlight. 
 * 
 * Clears the respective GPIO and timer settings.
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdShutDownBackLight(void)
{
  LCD_COMM_DIR |= LCD_BACKLIGHT_PIN;
  LCD_COMM_OUT &= ~(LCD_BACKLIGHT_PIN);  
  LCD_COMM_SEL &= ~LCD_BACKLIGHT_PIN;
  
  TA0CCTL3 = 0;
  TA0CTL = 0;  
  
  backlight = 0;  
}

/**********************************************************************//**
 * @brief  Set function for the contrast level of the LCD. 
 * 
 * @param  ContrastLevel The target contrast level 
 * 
 * @return none 
 *************************************************************************/
void halLcdSetContrast(unsigned char ContrastLevel)
{
  if (ContrastLevel > 127) ContrastLevel = 127;
  if (ContrastLevel < 70) ContrastLevel = 70;
  LcdInitMacro[ 0x04 * 6 + 5 ] = ContrastLevel;
  halLcdSendCommand(&LcdInitMacro[ 0x04 * 6 ]);
} 

/**********************************************************************//**
 * @brief  Get function for the contrast level of the LCD. 
 * 
 * @param  none
 * 
 * @return ContrastLevel The LCD constrast level
 *************************************************************************/
unsigned char halLcdGetContrast(void)
{
  return LcdInitMacro[ 0x04 * 6 + 5 ] ;
}

/**********************************************************************//**
 * @brief  Turns the LCD cursor on at the current text position.
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdCursor(void)
{
  LcdInitMacro[  8 * 6 + 5 ] ^= BIT2;
  halLcdSendCommand(&LcdInitMacro[ 8 * 6 ]);
  
  LcdInitMacro[ 0x0B * 6 + 5 ] = ((LcdAddress & 0x1F) << 3) ;
  LcdInitMacro[ 0x0B * 6 + 4 ] = ( (LcdAddress & 0x1F) << 3 ) + 3;
  LcdInitMacro[ 0x0C * 6 + 5 ] = (LcdAddress >> 5);
  LcdInitMacro[ 0x0C * 6 + 4 ] = (LcdAddress >> 5) + 7;
  halLcdSendCommand(&LcdInitMacro[ 0x0B * 6 ]);
  halLcdSendCommand(&LcdInitMacro[ 0x0C * 6 ]);
  
  halLcdSetAddress(LcdAddress);
}

/**********************************************************************//**
 * @brief  Turns off the LCD cursor.
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdCursorOff(void)
{
  LcdInitMacro[  8 * 6 + 5 ] &= ~BIT2;
  halLcdSendCommand(&LcdInitMacro[ 8 * 6 ]);
}

/**********************************************************************//**
 * @brief  Inverts the grayscale values of the LCD display (Black <> white).  
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdReverse(void)
{
  LcdInitMacro[  7 * 6 + 5 ] ^= BIT1;
  halLcdSendCommand(&LcdInitMacro[ 7 * 6 ]);
}

/**********************************************************************//**
 * @brief  Sets the LCD in standby mode to reduce power consumption.
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdStandby(void)
{
  LcdInitMacro[ 3 * 6 + 5 ] &= (~BIT3) & (~BIT2);
  LcdInitMacro[ 3 * 6 + 5 ] |= BIT0;
  halLcdSendCommand(&LcdInitMacro[ 3 * 6 ]);
}

/**********************************************************************//**
 * @brief  Puts the LCD into active mode.
 * 
 * @param  none
 * 
 * @return none
 *************************************************************************/
void halLcdActive(void)
{
  halLcdSendCommand(LcdInitMacro);
  LcdInitMacro[ 3 * 6 + 5 ] |= BIT3 ;
  LcdInitMacro[ 3 * 6 + 5 ] &= ~BIT0;
  halLcdSendCommand(&LcdInitMacro[ 3 * 6 ]);
}

/**********************************************************************//**
 * @brief  Sets the pointer location in the LCD. 
 *         
 * - LcdAddress      = Address  					 
 * - LcdTableAddress = Correct Address Row + Column 
 *                   = (Address / 0x20)* 17 + Column  

⌨️ 快捷键说明

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