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

📄 display.c

📁 基于MMA7260QR2的跌落检查程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************************
* This is the source file for Display Driver.
*
*
* (c) Copyright 2008, Freescale, Inc.  All rights reserved.
*
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale Semiconductor.
************************************************************************************/

#include "../../PLM/Interface/EmbeddedTypes.h"
#include "../../PLM/LibInterface/GPIO_Interface.h"
#include "../../SMAC/Drivers/Interface/Delay.h"
#include "Display.h"
#define gLcdBackLightEnabled_d 1
#if (gLCDSupported_d)



/************************************************************************************
*************************************************************************************
* Private macros
*************************************************************************************
************************************************************************************/

/* pin defines */

/* D0  = SPI_SS    (GPIO  4) , PORT LO */
/* D1  = SPI_MISO  (GPIO  5) , PORT LO */
/* D2  = SPI_MOSI  (GPIO  6) , PORT LO */
/* D3  = SPI_SCK   (GPIO  7) , PORT LO */
/* D4  = ADC2      (GPIO  32), PORT HI */
/* D5  = ADC3      (GPIO  33), PORT HI */
/* D6  = ADC4      (GPIO  34), PORT HI */
/* D7  = ADC5      (GPIO  35), PORT HI */
#define LCD_D0  gGpioPin4_c
#define LCD_D1  gGpioPin5_c
#define LCD_D2  gGpioPin6_c
#define LCD_D3  gGpioPin7_c
#define LCD_D4  gGpioPin32_c
#define LCD_D5  gGpioPin33_c
#define LCD_D6  gGpioPin34_c
#define LCD_D7  gGpioPin35_c

#define LCD_DATA_LO_MASK  0x000000F0
#define LCD_DATA_LO_PORT  gGpioPort0_c
#define LCD_DATA_LO_WRITE(x) (x)<<4
#define LCD_DATA_LO_READ(x)  (x)>>4

#define LCD_DATA_HI_MASK  0x0000000F
#define LCD_DATA_HI_PORT  gGpioPort1_c
#define LCD_DATA_HI_WRITE(x) (x)>>4
#define LCD_DATA_HI_READ(x)  (x)<<4

/* A0     = UART2_RTS (GPIO  21)   inverted, 0-data, 1-command */
/* RD     = TMR0      (GPIO  8)    double inverted, active high */
/* WR     = TMR1      (GPIO  9)    double inverted, active high */
/* CS     = UART2_CTS (GPIO  20)   double inverted, active high */
/* BL_PWM = TMR2      (GPIO  10)   1 - light, 0 - dark or PWM variable duty cycle */
/* RST    = RX_ON     (GPIO  45)   active low */
/* RST    = UART2_RX  (GPIO  19)   active low */
#define LCD_A0          gGpioPin21_c
#define LCD_RD          gGpioPin8_c
#define LCD_WR          gGpioPin9_c
#define LCD_CS          gGpioPin20_c
#define LCD_BL_PWM      gGpioPin10_c
#define LCD_RSTN        gGpioPin45_c
#define LCD_RSTN_debug  gGpioPin19_c


/* pin states */
#define LCD_A0_COMMAND  gGpioPinStateHigh_c
#define LCD_A0_DATA     gGpioPinStateLow_c
#define LCD_CS_ACTIVE   gGpioPinStateHigh_c
#define LCD_CS_INACTIVE gGpioPinStateLow_c
#define LCD_WR_ACTIVE   gGpioPinStateHigh_c
#define LCD_WR_INACTIVE gGpioPinStateLow_c
#define LCD_RD_ACTIVE   gGpioPinStateHigh_c
#define LCD_RD_INACTIVE gGpioPinStateLow_c
#define LCD_BL_ON       gGpioPinStateHigh_c
#define LCD_BL_OFF      gGpioPinStateLow_c
#define LCD_RSTN_IDLE   gGpioPinStateHigh_c
#define LCD_RSTN_RESET  gGpioPinStateLow_c

#define ON                      1
#define OFF                     0

#define mFontOffset_c          32

/* number of columns to be cleared in LCD_Task */
#define mMaxDeleteColumns_c    32
#define mMaxLineNumber_c        8
#define mMaxQueueEntries_c     10
#define mEmptyQueue_c          0xFF

/* number of characters to be displayed in LCD_Task */
#define mNumberOfCharToWrite_c  19

/************************************************************************************
*************************************************************************************
* Private type definitions
*************************************************************************************
************************************************************************************/
/* #pragma pack(1) */

typedef struct displayParams_tag{
  uint8_t currentXCoord;
  uint8_t currentYCoord;
  uint8_t currentLine;
  /* Bitmask: 
  *       line i is cleared, bit i from writtenLines is 0
  *       line i is written, bit i from writtenLines is 1 
  */
  uint8_t writtenLines;
  /* Bitmask: 
  *       line i will not be written, bit i from linesToWrite is 0
  *       line i will be written, bit i from linesToWrite is 1 
  */
  uint8_t linesToWrite;  
  uint8_t linesWidthWritten[mMaxLineNumber_c];
  uint8_t currentFontType;
  uint8_t currentStringIndex;
  uint8_t currentString[mMaxLineNumber_c][gMAX_LCD_CHARS_c];
}displayParams_t;

typedef struct lineParams_tag{
  uint8_t xCoord;
  uint8_t yCoord;
  uint8_t page;  
}lineParams_t;

typedef struct pageParams_tag{
  uint8_t xStartCoord;
  uint8_t xEndCoord;
  uint8_t yCoord;
  uint8_t line;
}pageParams_t;

typedef enum operation_tag{
  gLCD_WriteOp_c = 0,
  gLCD_ClearDisplayOp_c,
  gLCD_ClearLineOp_c,
  gLCD_MaxOp_c = 0xFF
}operation_t;

typedef struct opLine_tag{
  operation_t opQueue;
  uint8_t lineQueue;  
}opLine_t;

typedef struct queue_tag{
  uint8_t head;
  uint8_t tail;
  opLine_t opLine[mMaxQueueEntries_c];
}queue_t;

/* #pragma pack() */

/************************************************************************************
*************************************************************************************
* Private prototypes
*************************************************************************************
************************************************************************************/

void LCD_SetBacklight(uint8_t brightnessValue);
void LCD_ClearPage(uint8_t startXCoord, uint8_t endXCoord, uint8_t yCoord);
uint8_t LCD_ClearLine(uint8_t line);
static void SendData(uint8_t data);
static void SetHiZ(void);
static void SetOutput(void);
static void InitDisplayWrite(uint8_t xStartCoord, uint8_t yStartCoord);
static uint8_t ReadData(void);
static uint8_t GetData(void);
static uint8_t GetStrlen(const uint8_t *pString);


/************************************************************************************
*************************************************************************************
* Private memory declarations
*************************************************************************************
************************************************************************************/

static displayParams_t mDisplayParams = {
  /* currentXCoord */
  0x00,
  /* currentYCoord */
  0x00,
  /* currentLine */
  0x00,
  /* writtenLines */
  0xFF,
  /* linesToWrite */
  0x00,
  /* linesWidthWritten */
  0x00,
  /* currentFontType */
  0x00,
  /* currentStringIndex */
  0x00,  
  /* currentString */
  0x00
};

static lineParams_t mLineParams[mMaxLineNumber_c] = {
  {0, 0, 0},
  {0, 8, 1},
  {0, 16, 2},
  {0, 24, 3},
  {0, 32, 4},
  {0, 40, 5},
  {0, 48, 6},
  {0, 56, 7},
};  


/* after initialization, all LCD lines must be erased */
static bool_t mIsLcdInitialized = FALSE;

/* error indicator, used for error reporting */
static volatile lcdError_t mErrorIndicator = gLCD_NoError_c;

/******************************************************************************
*******************************************************************************
* Public memory definitions
*******************************************************************************
******************************************************************************/

const uint8_t gaHexValue[] = "0123456789ABCDEF";

const font_t aLCDFont[gLCDNumFonts_c] = 
{
  /* character font with fixed width */ 
  {gLCDNormalIndex,  gLCDNormalFont,  1},
  /* character font with variable width */ 
  {gLCDVerdanaIndex, gLCDVerdanaFont, 2}   
};

/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/

/******************************************************************************
* This function initializes the display
*
* Interface assumptions:
*
*
* Return value:
* None
*
******************************************************************************/
void LCD_Init(void)
{

    Gpio_SetPinFunction(LCD_WR, gGpioNormalMode_c);
    Gpio_SetPinFunction(LCD_CS, gGpioNormalMode_c);
    Gpio_SetPinFunction(LCD_RD, gGpioNormalMode_c);
    Gpio_SetPinFunction(LCD_A0, gGpioNormalMode_c);
    Gpio_SetPinFunction(LCD_RSTN, gGpioNormalMode_c);
    Gpio_SetPinFunction(LCD_RSTN_debug, gGpioNormalMode_c);

    Gpio_SetPinData(LCD_WR, LCD_WR_INACTIVE);
    Gpio_SetPinData(LCD_CS, LCD_CS_INACTIVE);
    Gpio_SetPinData(LCD_RD, LCD_RD_INACTIVE);
    Gpio_SetPinData(LCD_A0, LCD_A0_COMMAND);
    Gpio_SetPinData(LCD_RSTN, LCD_RSTN_IDLE);
    Gpio_SetPinData(LCD_RSTN_debug, LCD_RSTN_IDLE);

    /* define the LCD pins as output pins */
    (void)Gpio_SetPinReadSource(LCD_CS,     gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_A0,     gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_RD,     gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_WR,     gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_BL_PWM, gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_RSTN,   gGpioPinReadReg_c);
    (void)Gpio_SetPinReadSource(LCD_RSTN_debug,   gGpioPinReadReg_c);

    (void)Gpio_SetPinDir(LCD_CS,     gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_A0,     gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_RD,     gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_WR,     gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_BL_PWM, gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_RSTN,   gGpioDirOut_c);
    (void)Gpio_SetPinDir(LCD_RSTN_debug,   gGpioDirOut_c);

    /* data pins */
#if gLCDGpioPinMode_c
    (void)Gpio_SetPinFunction(LCD_D0,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D1,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D2,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D3,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D4,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D5,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D6,      gGpioNormalMode_c);
    (void)Gpio_SetPinFunction(LCD_D7,      gGpioNormalMode_c);

    /* define data pins as inputs */
    (void)Gpio_EnPinPullup(LCD_D0,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D1,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D2,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D3,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D4,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D5,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D6,      TRUE);
    (void)Gpio_EnPinPullup(LCD_D7,      TRUE);

    (void)Gpio_SelectPinPullup(LCD_D0,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D1,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D2,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D3,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D4,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D5,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D6,      gGpioPinPullup_c);
    (void)Gpio_SelectPinPullup(LCD_D7,      gGpioPinPullup_c);

    (void)Gpio_EnPinPuKeeper(LCD_D0,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D1,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D2,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D3,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D4,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D5,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D6,      TRUE);
    (void)Gpio_EnPinPuKeeper(LCD_D7,      TRUE);

    (void)Gpio_SetPinReadSource(LCD_D0,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D1,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D2,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D3,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D4,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D5,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D6,      gGpioPinReadPad_c);
    (void)Gpio_SetPinReadSource(LCD_D7,      gGpioPinReadPad_c);

    (void)Gpio_SetPinDir(LCD_D0,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D1,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D2,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D3,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D4,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D5,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D6,      gGpioDirIn_c);
    (void)Gpio_SetPinDir(LCD_D7,      gGpioDirIn_c);
#else /* gLCDGpioPinMode_c */
    Gpio_SetPortFunction(LCD_DATA_LO_PORT, gGpioNormalMode_c, LCD_DATA_LO_MASK);
    Gpio_SetPortFunction(LCD_DATA_HI_PORT, gGpioNormalMode_c, LCD_DATA_HI_MASK);

    Gpio_WrPortSetting(LCD_DATA_LO_PORT, gGpioPullUpEnAttr_c, LCD_DATA_LO_MASK, LCD_DATA_LO_MASK);
    Gpio_WrPortSetting(LCD_DATA_HI_PORT, gGpioPullUpEnAttr_c, LCD_DATA_HI_MASK, LCD_DATA_HI_MASK);

    Gpio_WrPortSetting(LCD_DATA_LO_PORT, gGpioPullUpSelAttr_c, LCD_DATA_LO_MASK, LCD_DATA_LO_MASK);
    Gpio_WrPortSetting(LCD_DATA_HI_PORT, gGpioPullUpSelAttr_c, LCD_DATA_HI_MASK, LCD_DATA_HI_MASK);

    Gpio_WrPortSetting(LCD_DATA_LO_PORT, gGpioPullUpKeepAttr_c, LCD_DATA_LO_MASK, LCD_DATA_LO_MASK);
    Gpio_WrPortSetting(LCD_DATA_HI_PORT, gGpioPullUpKeepAttr_c, LCD_DATA_HI_MASK, LCD_DATA_HI_MASK);
    
    Gpio_WrPortSetting(LCD_DATA_LO_PORT, gGpioInputDataSelAttr_c, ~LCD_DATA_LO_MASK, LCD_DATA_LO_MASK);
    Gpio_WrPortSetting(LCD_DATA_HI_PORT, gGpioInputDataSelAttr_c, ~LCD_DATA_HI_MASK, LCD_DATA_HI_MASK);
    
    Gpio_SetPortDir(LCD_DATA_LO_PORT, ~LCD_DATA_LO_MASK, LCD_DATA_LO_MASK);
    Gpio_SetPortDir(LCD_DATA_HI_PORT, ~LCD_DATA_HI_MASK, LCD_DATA_HI_MASK);
#endif /* gLCDGpioPinMode_c */


  Gpio_SetPinData(LCD_RSTN, LCD_RSTN_RESET);
  Gpio_SetPinData(LCD_RSTN_debug, LCD_RSTN_RESET);
  DelayUs(10);
  Gpio_SetPinData(LCD_RSTN, LCD_RSTN_IDLE);
  Gpio_SetPinData(LCD_RSTN_debug, LCD_RSTN_IDLE);
  DelayMs(1);

    /* Turn ON the VDD-VSS power keeping the RES pin = LOW for 0.5us */
    /* When power is stabilized, relese the RES pin (RES pin = HIGH) */
    
    /* Default initialized state: 
     - Display off
	   - Normal display (COM0 --> COM63)
	   - ADC select: normal (RAM column address n <-> SEGn)
	   - Power control register: Booster circuit OFF, Voltage regulator circuit OFF, Voltage follower circuit OFF
	   - LCD power supply bias rate = 1/9
	   - All-indicator lamps-on OFF
	   - Power saving clear
	   - V5 voltage regulator internal resistors Ra and Rb separation
	   - Output conditions of SEG and COM terminals SEG:V2/V3; COM: V1/V4
	   - Read modify write OFF
	   - Static indicator OFF
	   - Display start line set to first line
	   - Column address set to Address 0
	   - Page address set to Page 0
	   - Common output status normal
	   - V5 voltage regulator internal resistor ratio set mode clear
	   - Electronic volume register set mode clear
	   - Test mode clear 
    */

    /* reset display */
    LCD_WriteCommand(gLCDResetCommand_c);
    DelayUs(10);

    /* set LCD bias to 1/9 */
    LCD_WriteCommand(gLCD19BiasCommand_c);

    /* set ADC selection to normal: RAM column address n <-> SEGn */
    /* LCD_WriteCommand(gLCDADCNormalCommand_c); */
    LCD_WriteCommand(gLCDADCReverseCommand_c);

    /* set scan direction of the COM to normal: COM0 --> COM63 */
    LCD_WriteCommand(gLCDComNormalCommand_c);

    /* set the Rb/Ra ratio for V5 to medium (D2D1D0 = 0b100) */
    LCD_WriteCommand( gLCDDefaultRatioCommand_c | (gLCDRatioValue_c & 0x07) );

    /* enable the electronic volume register set command */
    /* this command must always be followed by an electronic volume register set command */
    LCD_WriteCommand(gLCDElectronicVolumeModeSetCommand_c);
    
    LCD_WriteCommand(gLCDElectronicVolumeInitCommand_c);


    /* disable booster circuit, disable voltage regulator circuit, disable voltage follower circuit */
    LCD_WriteCommand(0x2F);
    DelayUs(10);
    /** turn ON */
    LCD_WriteCommand(gLCDDisplayOnCommand_c);
    DelayUs(10);
    
    /* turn on the backlight */
    LCD_SetBacklight(gLCDDefaultBrightnessValue_c);
    
    /* set default font */
    (void)LCD_SetFont(gLCD_NormalFont_c);

    mIsLcdInitialized = FALSE;
}

/******************************************************************************
* This function clears the display. In case an error occured during add to queue
* operation, the clear display operation will not execute.
*
* Interface assumptions:
*
*
* Return value:
* None
*
******************************************************************************/
void LCD_ClearDisplay(void)
{

  static uint8_t linesToClear = 0x00;
  uint8_t line, deleteWriteLines = 0xFF;
  
  /* clear the error indicator */
  mErrorIndicator = gLCD_NoError_c;
  

⌨️ 快捷键说明

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