📄 lcd_drv.c
字号:
//----------------------------------------------------------------------------
// Filename : lcd_drv.c
//----------------------------------------------------------------------------
//
// Copyright (c) 2008,东莞太平计算机科技有限公司
// All rights reserved.
// www.pacific-gold.com.cn
//
// 历史版本:
//
// 版本: V1.0
// 作者: 罗先能
// 日期: 2007-12-21
// 描述: 建立第一版本
//
//
// 描述:
// 1. LCD的驱动程序,
// 2. ST7541驱动IC.
//
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------
// system headers
#include <stdio.h>
#include <stdarg.h>
#include "71x_lib.h"
// internal headers
#include "LCD_drv.h"
// extern headers
//----------------------------------------------------------------------------
// Local Constant Definitions
//----------------------------------------------------------------------------
#define LCD_CMD_PORT (*(vu8*)0x66000000) // lcd command address
#define LCD_DAT_PORT (*(vu8*)0x66000001) // lcd datas address
#define IAP_EXT_BASE ((vu8*)0x40000000) // IAP base address
#define ASC_BASE ((vu8*)0x40004000) // ASCII base address
#define HZK_BASE ((vu8*)0x40005000) // HZK base address
// lcd buffer size of bytes
#define LCD_BUFFER_SIZE 2048
//----------------------------------------------------------------------------
// Local Variables Definitions
//----------------------------------------------------------------------------
private u8 buffer[ LCD_BUFFER_SIZE ]; // data buffer
//----------------------------------------------------------------------------
// Global Variables Definitions
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Local Function Definitions
//----------------------------------------------------------------------------
/**************************************************************
* Function: delay(ms)
* Description: wait *ms
*
* Input: None
* Return: None
*
**************************************************************/
private void delay(int ms)
{
int i,j;
for(j=0; j<ms; j++)
{ for(i = 0; i < 0x180*25; i++);
}
}
/**************************************************************
* Function: lcd_goto(...)
* Description: jump to the address of (line,col)
*
* Input: line: the Number of line
* col : the Number of colume
* Return: None
*
**************************************************************/
private void lcd_goto(u32 line, u32 col)
{
#define SET_PAGE_ADDRESS 0xb0
#define SET_COLUME_ADDRESS_MSB 0x10
#define SET_COLUME_ADDRESS_LSB 0x00
u32 i;
LCD_CMD_PORT = (u8)(SET_PAGE_ADDRESS | (line & 0x0f));
LCD_CMD_PORT = (u8)(SET_COLUME_ADDRESS_MSB | ((col>>4) & 0x07));
LCD_CMD_PORT = (u8)(SET_COLUME_ADDRESS_LSB | ((col>>0) & 0x0f));
for(i = 0; i < 0x180; i++);
}
/**************************************************************
* Function: lcd_clear(...)
* Description: clear lcd's map
*
* Input: None
* Return: None
*
**************************************************************/
private void lcd_clear(void)
{
u16 i;
for( i=0; i<LCD_BUFFER_SIZE; i++)
{ buffer[i] = 0; }
}
/**************************************************************
* Function: lcd_reverse(...)
* Description: reverse datas of buffer
*
* Input: line [ --> ]: start line
* col [ --> ]: start colume
* size [ --> ]: size of data
*
* Return: TRUE: OK
* FALSE: error
**************************************************************/
private bool lcd_reverse( u32 offset,u32 size)
{
u16 i;
u8 *p;
if(size == 0 ) return FALSE;
if( (offset+size) > LCD_BUFFER_SIZE ) return FALSE;
p = &buffer[offset];
for(i=0; i<size; i++)
{ (*p) = ~(*p);
p++;
}
return TRUE;
}
/**************************************************************
* Function:
* Description:
*
* Input:
* Return:
*
**************************************************************/
private void lcd_update(void)
{
u16 i;
lcd_goto(0,0);
for( i=0; i<LCD_BUFFER_SIZE; i++)
{ LCD_DAT_PORT = buffer[i];
LCD_DAT_PORT = buffer[i];
}
}
/**************************************************************
* Function:
* Description:
*
* Input:
* Return:
*
**************************************************************/
/*
private void lcd_printf(const char *format, ...)
{
static char buf[256];
va_list args;
va_start(args, format);
vsprintf(buf, format, args);
va_end(args);
lcd_puts(buf);
}
*/
/**************************************************************
* Function: init_driver(...)
* Description: initialize LCD driver of ST7541
*
* Input: None
* Return: None
*
**************************************************************/
private void init_driver(void)
{ int i;
// GPIO_Config(GPIO2, 0x000F, GPIO_AF_PP);
EMI_Config(3, EMI_ENABLE | EMI_WAITSTATE(15) | EMI_SIZE_8);
// reset LCD (P1.12)
GPIO_Config(GPIO1, 0x1000, GPIO_OUT_PP);
GPIO_BitWrite(GPIO0, 12, 0);
delay(30);
GPIO_BitWrite(GPIO0, 12, 1);
delay(30);
// clear VRAM
for(i=0; i<2048; i++)
{ LCD_DAT_PORT = 0;
LCD_DAT_PORT = 0;
}
// set mode
LCD_CMD_PORT = 0x38;
LCD_CMD_PORT = 0x00;
delay(1);
// ICON disable
LCD_CMD_PORT = 0xa2;
delay(1);
// set duty=128
LCD_CMD_PORT = 0x48;
LCD_CMD_PORT = 128;
delay(1);
// adc = 0
LCD_CMD_PORT = 0xa1;
delay(1);
// shl = 0
LCD_CMD_PORT = 0xc0;
delay(1);
// set line = 0
LCD_CMD_PORT = 0x40;
LCD_CMD_PORT = 0x00;
// set com0 = 0
LCD_CMD_PORT = 0x44;
LCD_CMD_PORT = 0x00;
// oscillator on
LCD_CMD_PORT = 0xab;
// set set-up: 6*
LCD_CMD_PORT = 0x66;
delay(100);
// set 1+Rb/Ra
LCD_CMD_PORT = 0x27; // 20 - 27
delay(1);
// set contrast controll set EV
LCD_CMD_PORT = 0x81;
LCD_CMD_PORT = 0x30;
// set bias = 1/12
LCD_CMD_PORT = 0x57;
// set mode
LCD_CMD_PORT = 0x38;
LCD_CMD_PORT = 0x00;
// vc, vr, vf = 1
LCD_CMD_PORT = 0x2b;
delay(10);
// set FRC an PWM mode
LCD_CMD_PORT = 0x97;
// set white mode
LCD_CMD_PORT = 0x88;
LCD_CMD_PORT = 0x00;
LCD_CMD_PORT = 0x89;
LCD_CMD_PORT = 0x00;
// set light gray mode
LCD_CMD_PORT = 0x8a;
LCD_CMD_PORT = 0xaa;
LCD_CMD_PORT = 0x8b;
LCD_CMD_PORT = 0xaa;
// set dark gray mode
LCD_CMD_PORT = 0x8c;
LCD_CMD_PORT = 0xee;
LCD_CMD_PORT = 0x8d;
LCD_CMD_PORT = 0xee;
// set black mode
LCD_CMD_PORT = 0x8e;
LCD_CMD_PORT = 0xff;
LCD_CMD_PORT = 0x8f;
LCD_CMD_PORT = 0xff;
// select normal/reverse display=normal
LCD_CMD_PORT = 0xa6;
// set entire display on/off=off
LCD_CMD_PORT = 0xa4;
// display on
LCD_CMD_PORT = 0xaf;
delay(100);
}
//----------------------------------------------------------------------------
// Global Function Definitions
//----------------------------------------------------------------------------
/**************************************************************
* Function: lcd_init(...)
* Description:
* initialize LCD && ports
* Input: no
* Return: no
*
**************************************************************/
public void lcd_init(void)
{
init_driver();
lcd_clear();
}
/**************************************************************
* Function: lcd_open(...)
* Description: open lcd
*
* Input: no
* Return: no
*
**************************************************************/
public void lcd_open(void)
{
init_driver();
lcd_update();
}
/**************************************************************
* Function: lcd_close(...)
* Description: close lcd
*
* Input: None
* Return: None
*
**************************************************************/
public void lcd_close(void)
{
// set RESET == 0
GPIO_BitWrite(GPIO0, 12, 0);
}
/**************************************************************
* Function: lcd_read(...)
* Description: read data from LCD buffer
*
* Input: line [--> ]: the number of line
* col [--> ]: the number of colume
* size [<-->]: the size of data
* --> : the size of wanting to get data
*
* Output: buffer[<--]: the buffer of getting data
* Return: TRUE: ok
* FALSE: error
**************************************************************/
public bool lcd_read( u32 line, u32 col, u32 size, u8* buf)
{
u8 *p;
u16 i;
if( size == 0 ) return FALSE;
p = &buffer[line*128+size];
for(i=0; i<size; i++)
{
if( p>&buffer[LCD_BUFFER_SIZE-1] ) return FALSE;
buf[i] = *p;
p++;
}
return TRUE;
}
/**************************************************************
* Function: lcd_write(...)
* Description: write the data of buffer to LCD
*
* Input: line [--> ]: the number of line
* col [--> ]: the number of colume
* size [--> ]: the size of data
* buffer [--> ]: the point of data
*
* Return:
*
**************************************************************/
public bool lcd_write(u32 line, u32 col, u32 size, const u8* buf)
{
u8 *p;
u16 i;
if( size == 0 ) return FALSE;
p = &buffer[line*128+col];
for(i=0; i<size; i++)
{
if( p>&buffer[LCD_BUFFER_SIZE-1] ) return FALSE;
*p = buf[i];
p++;
}
return TRUE;
}
/**************************************************************
* Function: lcd_ctrl(...)
* Description: controll the lcd
*
* Input: command [--> ]: controll command
* arg1 [--> ]: parameter1
* arg2 [--> ]: parameter2
*
* Parameter: (command, arg1, arg2)
*
* REVERSE, offset, size
* GOTO, line, colume
* UPDATE, NULL, NULL
* CLEAR, NULL, NULL
*
* Return: TRUE: OK
* FALSE: ERROR
**************************************************************/
public bool lcd_ctrl( LCD_CMD command, u32 arg1, u32 arg2)
{
switch( command )
{
case CLEAR: /* clear buffer */
{
lcd_clear();
}
break;
case GOTO: /* goto(line, colume) */
{
lcd_goto( arg1,arg2);
break;
}
case REVERSE: /* lcd_revease(offset,size) */
{
if( !lcd_reverse(arg1,arg2) )
return FALSE;
break;
}
case UPDATE: /* Update; buffers --> LCD's VRAM */
{
lcd_update();
break;
}
default:
{ return FALSE; }
}
return TRUE;
}
/**************************************************************
* Function: lcd_test(...)
* Description: test lcd
*
* Input: None
* Return: true : lcd is ok
* false: lcd is error
**************************************************************/
public bool lcd_test(void)
{ u8 ch;
ch = LCD_CMD_PORT;
if( ch == 0x42 )
return TRUE;
else
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -