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

📄 drv_glcd.c

📁 nxp lpc177x/8x lcd keil&iar 例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************
 *
*    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2008
 *
 *    File name   : drv_glcd.c
 *    Description : Graphical LCD driver
 *
 *    History :
 *    1. Date        : 6, March 2008
 *       Author      : Stanimir Bonev
 *       Description : Create
 *
 *
 *    $Revision: 24636 $
 *
 *    @Modify: NXP MCU Application Team - NguyenCao
 *    @Date: 04. March. 2011
 **************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "board.h"
#include "drv_glcd.h"
#include "lpc177x_8x_clkpwr.h"
#include "lpc177x_8x_pinsel.h"
#include "sdram_k4s561632j.h"
#include "Cursor.h"

//#define MHZ           

#define C_GLCD_CLK_PER_LINE     (C_GLCD_H_SIZE + C_GLCD_H_PULSE + C_GLCD_H_FRONT_PORCH + C_GLCD_H_BACK_PORCH)
#define C_GLCD_LINES_PER_FRAME  (C_GLCD_V_SIZE + C_GLCD_V_PULSE + C_GLCD_V_FRONT_PORCH + C_GLCD_V_BACK_PORCH)
#define C_GLCD_PIX_CLK          (6.4*1000000l)


#define LCD_VRAM_BASE_ADDR 	((uint32_t)SDRAM_BASE_ADDR + 0x00100000)
#define LCD_CURSOR_BASE_ADDR 	((uint32_t)0x20088800)

static pFontType_t pCurrFont = NULL;
static LdcPixel_t TextColour;
static LdcPixel_t TextBackgndColour;

static uint32_t TextX_Pos = 0;
static uint32_t TextY_Pos = 0;

static uint32_t XL_Win = 0;
static uint32_t YU_Win = 0;
static uint32_t XR_Win = C_GLCD_H_SIZE-1;
static uint32_t YD_Win = C_GLCD_V_SIZE-1;

static uint32_t TabSize = TEXT_DEF_TAB_SIZE;

static uint32_t WindY_Size, WindX_Size;
static uint32_t CurrY_Size, CurrX_Size;
static uint32_t *pWind;
static uint32_t *pPix;

/*************************************************************************
 * Function Name: GLCD_Cursor_Cnfg
 * Parameters:
 *
 * Return: none
 *
 * Description: Configure the cursor
 *
 *************************************************************************/
void GLCD_Cursor_Cfg(int Cfg)
{
  LPC_LCD->CRSR_CFG = Cfg;
}
/*************************************************************************
 * Function Name: GLCD_Cursor_En
 * Parameters: cursor - Cursor Number
 *
 * Return: none
 *
 * Description: Enable Cursor
 *
 *************************************************************************/
void GLCD_Cursor_En(int cursor)
{
  LPC_LCD->CRSR_CTRL |= (cursor<<4);
  LPC_LCD->CRSR_CTRL |= 1;
}

/*************************************************************************
 * Function Name: GLCD_Cursor_Dis
 * Parameters: None
 *
 * Return: none
 *
 * Description: Disable Cursor
 *
 *************************************************************************/
void GLCD_Cursor_Dis(int cursor)
{
  LPC_LCD->CRSR_CTRL &= (1<<0);
}

/*************************************************************************
 * Function Name: GLCD_Move_Cursor
 * Parameters: x - cursor x position
 *             y - cursor y position
 *
 * Return: none
 *
 * Description: Moves cursor on position (x,y). Negativ values are posible.
 *
 *************************************************************************/
void GLCD_Move_Cursor(int x, int y)
{
  LPC_LCD->CRSR_CLIP = 0;
  LPC_LCD->CRSR_XY = 0;
  if(0 <= x)
  {//no clipping
    LPC_LCD->CRSR_XY |= (x & 0x3FF);
  }
  else
  {//clip x
    LPC_LCD->CRSR_CLIP |= -x;
  }

  if(0 <= y)
  {//no clipping

    LPC_LCD->CRSR_XY |= (y << 16);
  }
  else
  {//clip y
    LPC_LCD->CRSR_CLIP |= (-y << 8);
  }
}

/*************************************************************************
 * Function Name: GLCD_Copy_Cursor
 * Parameters: pCursor - pointer to cursor conts image
 *             cursor - cursor Number (0,1,2 or 3)
 *                      for 64x64(size 256) pix cursor always use 0
 *             size - cursor size in words
 * Return: none
 *
 * Description: Copy Cursor from const image to LCD RAM image
 *
 *************************************************************************/
void GLCD_Copy_Cursor (const uint32_t *pCursor, int cursor, int size)
{
   	uint32_t i ;
   	uint32_t * pDst = (uint32_t *)LCD_CURSOR_BASE_ADDR;
   
   	pDst += cursor*64;

   	for(i = 0; i < size ; i++) 
//	   *pDst++ = *pCursor++; 
	{
		*pDst = *pCursor;
		pDst++;
		pCursor++;
	}
}
/*************************************************************************
 * Function Name: GLCD_Init
 * Parameters: const uint32_t *pPain, const uint32_t * pPallete
 *
 * Return: none
 *
 * Description: GLCD controller init
 *
 *************************************************************************/
void GLCD_Init (const uint32_t *pPain, const uint32_t * pPallete)
{
	uint32_t i;
	uint32_t *pDst = (uint32_t *)LCD_VRAM_BASE_ADDR;
	// Assign pins
	LPC_IOCON->P0_4 = 0x27;
	LPC_IOCON->P0_5 = 0x27;
	LPC_IOCON->P0_6 = 0x27;
	LPC_IOCON->P0_7 = 0x27;
	LPC_IOCON->P0_8 = 0x27;
	LPC_IOCON->P0_9 = 0x27;
	LPC_IOCON->P1_20 = 0x27;
	LPC_IOCON->P1_21 = 0x27;
	LPC_IOCON->P1_22 = 0x27;
	LPC_IOCON->P1_23 = 0x27;
	LPC_IOCON->P1_24 = 0x27;
	LPC_IOCON->P1_25 = 0x27;
	LPC_IOCON->P1_26 = 0x27;
	LPC_IOCON->P1_27 = 0x27;
	LPC_IOCON->P1_28 = 0x27;
	LPC_IOCON->P1_29 = 0x27;

	LPC_IOCON->P2_1 = 0x20;
	LPC_IOCON->P2_2 = 0x27;
	LPC_IOCON->P2_3 = 0x27;
	LPC_IOCON->P2_4 = 0x27;
	LPC_IOCON->P2_5 = 0x27;
	LPC_IOCON->P2_6 = 0x27;
	LPC_IOCON->P2_7 = 0x27;
	LPC_IOCON->P2_8 = 0x27;
	LPC_IOCON->P2_9 = 0x27;

	LPC_IOCON->P2_12 = 0x27;
	LPC_IOCON->P2_13 = 0x27;
	LPC_IOCON->P4_28 = 0x27;
	LPC_IOCON->P4_29 = 0x27;
	
	/*Back light enable*/
	LPC_GPIO2->DIR = (1<<1);
	LPC_GPIO2->SET= (1<<1);

	//Turn on LCD clock
	CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCLCD, ENABLE);
	
	// Disable cursor
	LPC_LCD->CRSR_CTRL &=~(1<<0);
	
	// disable GLCD controller	
	LPC_LCD->CTRL = 0;
	// 24 bpp
	LPC_LCD->CTRL &= ~(0x07 <<1);
	LPC_LCD->CTRL |=(5<<1);
	
	// TFT panel
	LPC_LCD->CTRL |= (1<<5);
	// single panel
	LPC_LCD->CTRL &= ~(1<<7);
	// notmal output
	LPC_LCD->CTRL &= ~(1<<8);
	// little endian byte order
	LPC_LCD->CTRL &= ~(1<<9);
	// little endian pix order
	LPC_LCD->CTRL &= ~(1<<10);
	// disable power
	LPC_LCD->CTRL &= ~(1<<11);
	// init pixel clock
	LPC_SC->LCD_CFG = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER) / ((uint32_t)C_GLCD_PIX_CLK);
	// bypass inrenal clk divider
	LPC_LCD->POL |=(1<<26);
	// clock source for the LCD block is HCLK
	LPC_LCD->POL &= ~(1<<5);
	// LCDFP pin is active LOW and inactive HIGH
	LPC_LCD->POL |= (1<<11);
	// LCDLP pin is active LOW and inactive HIGH
	LPC_LCD->POL |= (1<<12);
	// data is driven out into the LCD on the falling edge
	LPC_LCD->POL |= (1<<13);
	// active high
	LPC_LCD->POL &= ~(1<<14);
	LPC_LCD->POL &= ~(0x3FF <<16);
	LPC_LCD->POL |= (C_GLCD_H_SIZE-1)<<16;
	
	// init Horizontal Timing
	LPC_LCD->TIMH = 0; //reset TIMH before set value
	LPC_LCD->TIMH |= (C_GLCD_H_BACK_PORCH - 1)<<24;
	LPC_LCD->TIMH |= (C_GLCD_H_FRONT_PORCH - 1)<<16;
	LPC_LCD->TIMH |= (C_GLCD_H_PULSE - 1)<<8;
	LPC_LCD->TIMH |= ((C_GLCD_H_SIZE/16) - 1)<<2;
	
	// init Vertical Timing
	LPC_LCD->TIMV = 0;  //reset TIMV value before setting
	LPC_LCD->TIMV |= (C_GLCD_V_BACK_PORCH)<<24;
	LPC_LCD->TIMV |= (C_GLCD_V_FRONT_PORCH)<<16;
	LPC_LCD->TIMV |= (C_GLCD_V_PULSE - 1)<<10;
	LPC_LCD->TIMV |= C_GLCD_V_SIZE - 1;
	// Frame Base Address doubleword aligned
	LPC_LCD->UPBASE = LCD_VRAM_BASE_ADDR & ~7UL ;
	LPC_LCD->LPBASE = LCD_VRAM_BASE_ADDR & ~7UL ;
        // init colour pallet

	if(NULL != pPallete)
	{
		GLCD_SetPallet(pPallete);
	}
	
	if (NULL == pPain)
	{
		// clear display memory
		for( i = 0; (C_GLCD_H_SIZE * C_GLCD_V_SIZE) > i; i++)
		{
	  		*pDst++ = 0;
		}
	}
	else
	{
		// set display memory
		for(i = 0; (C_GLCD_H_SIZE * C_GLCD_V_SIZE) > i; i++)
		{
	  		*pDst++ = *pPain++;
		}
	}

  	for(i = C_GLCD_ENA_DIS_DLY; i; i--);
}

/*************************************************************************
 * Function Name: GLCD_SetPallet
 * Parameters: const uint32_t * pPallete
 *
 * Return: none
 *
 * Description: GLCD init colour pallete
 *
 *************************************************************************/
void GLCD_SetPallet (const uint32_t * pPallete)
{
	uint32_t i;
	uint32_t * pDst = (uint32_t *)LPC_LCD->PAL;
	assert(pPallete);
	for (i = 0; i < 128; i++)
	{
	*pDst++ = *pPallete++;
	}
}

/*************************************************************************
 * Function Name: GLCD_Ctrl
 * Parameters: Bool bEna
 *
 * Return: none
 *
 * Description: GLCD enable disabe sequence
 *
 *************************************************************************/
void GLCD_Ctrl (Bool bEna)
{
	volatile uint32_t i;
  if (bEna)
  {
//    LCD_CTRL_bit.LcdEn = 1;
    LPC_LCD->CTRL |= (1<<0);
    for(i = C_GLCD_PWR_ENA_DIS_DLY; i; i--);
//    LCD_CTRL_bit.LcdPwr= 1;   // enable power
    LPC_LCD->CTRL |= (1<<11);
  }
  else
  {
//    LCD_CTRL_bit.LcdPwr= 0;   // disable power
    LPC_LCD->CTRL &= ~(1<<11);
    for(i = C_GLCD_PWR_ENA_DIS_DLY; i; i--);
//    LCD_CTRL_bit.LcdEn = 0;
    LPC_LCD->CTRL &= ~(1<<0);

⌨️ 快捷键说明

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