📄 lcd_lpc23xx.c
字号:
/*
********************************************************************
*
* File: LCD_LPC23xx.h
*
* System: ARM7TDMI-S 32 Bit
*
* Compiler: GCC 4.0.0.0
*
* Date: 2006-08-25
*
* Author: Wenz
*
* Rights: Hitex Development Tools GmbH
* Greschbachstr. 12
* D-76229 Karlsruhe
*
********************************************************************
* Description
*
* driver for LCD module (A162) 2x16 on board MCB2300
*
********************************************************************
*
* History:
*
* Initial revision
*
*
********************************************************************
*/
#include "lpc23xx.h" /* LPC23xx definitions */
#include "LCD_LPC23xx.h"
/* LCD definitions */
#define LCD_E 0x40000000 /* Enable control pin */
#define LCD_RW 0x20000000 /* Read/Write control pin */
#define LCD_RS 0x10000000 /* Data/Instruction control */
#define LCD_CTRL 0x70000000 /* Control lines mask */
#define LCD_DATA 0x0F000000 /* Data lines mask */
/* Local variables */
static uint32_t lcd_ptr;
/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
static const uint8_t UserFont[8][8] =
{
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
{ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
{ 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
{ 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
{ 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
};
/* Local Function Prototypes */
static void prvDelay (uint32_t cnt);
static void prvLCDWrite (uint32_t c);
static void prvLCDWrite4Bit (uint32_t c);
static uint32_t prvLCDReadStat(void);
static void prvLCDWriteCommand (uint32_t c);
static void prvLCDWriteData (uint32_t d);
static void prvLCDWaitForBusy (void);
/****************************************************************************
* Name: prvDelay
*
* Description: a cycle delay function
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvDelay(uint32_t aCount)
{
while (aCount--);
}
/****************************************************************************
* Name: prvLCDWrite
*
* Description: Write a 4-bit command to LCD controller.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvLCDWrite4Bit(uint32_t c)
{
IODIR1 |= LCD_DATA | LCD_CTRL;
IOCLR1 = LCD_RW | LCD_DATA;
IOSET1 = (c & 0xF) << 24;
IOSET1 = LCD_E;
prvDelay (10);
IOCLR1 = LCD_E;
prvDelay (10);
}
/****************************************************************************
* Name: prvLCDWrite
*
* Description: Write data/command to LCD controller.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvLCDWrite (uint32_t c)
{
prvLCDWrite4Bit(c >> 4);
prvLCDWrite4Bit(c);
}
/****************************************************************************
* Name: prvLCDReadStatus
*
* Description: Read status of LCD controller
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static uint32_t prvLCDReadStat (void)
{
uint32_t stat;
IODIR1 &= ~LCD_DATA;
IOCLR1 = LCD_RS;
IOSET1 = LCD_RW;
prvDelay (10);
IOSET1 = LCD_E;
prvDelay (10);
stat = (IOPIN1 >> 20) & 0xF0;
IOCLR1 = LCD_E;
prvDelay (10);
IOSET1 = LCD_E;
prvDelay (10);
stat |= (IOPIN1 >> 24) & 0xF;
IOCLR1 = LCD_E;
return (stat);
}
/****************************************************************************
* Name: prvLCDWaitForBusy
*
* Description: Wait until LCD controller is busy.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvLCDWaitForBusy (void)
{
uint32_t stat;
do
{
stat = prvLCDReadStat();
}
while (stat & 0x80); /* Wait for busy flag */
}
/****************************************************************************
* Name: prvLCDWriteCommand
*
* Description: Write command to LCD controller.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvLCDWriteCommand (uint32_t c)
{
prvLCDWaitForBusy ();
IOCLR1 = LCD_RS;
prvLCDWrite(c);
}
/****************************************************************************
* Name: prvLCDWriteData
*
* Description: Write data to LCD controller.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
static void prvLCDWriteData (uint32_t data)
{
prvLCDWaitForBusy ();
IOSET1 = LCD_RS;
prvLCDWrite(data);
}
/****************************************************************************
* Name: vLCDInit
*
* Description: Initialize the LCD controller to 4-bit mode.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDInit (void)
{
IODIR1 |= LCD_CTRL | LCD_DATA;
IOCLR1 = LCD_RW | LCD_RS | LCD_DATA;
prvLCDWrite4Bit(0x3); /* Select 4-bit interface */
prvDelay (100000);
prvLCDWrite4Bit (0x3);
prvDelay (10000);
prvLCDWrite4Bit (0x3);
prvLCDWrite4Bit (0x2);
prvLCDWriteCommand (0x28); /* 2 lines, 5x8 character matrix */
prvLCDWriteCommand (0x0e); /* Display ctrl:Disp/Curs/Blnk=ON */
prvLCDWriteCommand (0x06); /* Entry mode: Move right, no shift */
vLCDLoad((uint8_t*)&UserFont, sizeof (UserFont));
vLCDCls();
}
/****************************************************************************
* Name: vLCDLoad
*
* Description: Load user-specific characters into CGRAM
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDLoad (uint8_t *fp, uint32_t cnt)
{
uint32_t i;
prvLCDWriteCommand (0x40); /* Set CGRAM address counter to 0 */
for (i = 0; i < cnt; i++, fp++) {
prvLCDWriteData (*fp);
}
}
/****************************************************************************
* Name: vLCDGoto
*
* Description: Set cursor position on LCD display. Left corner: 1,1, right: 16,2
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDGoto (uint32_t x, uint32_t y)
{
uint32_t c;
c = --x;
if (--y) {
c |= 0x40;
}
prvLCDWriteCommand (c | 0x80);
lcd_ptr = y*16 + x;
}
/****************************************************************************
* Name: vLCDCls
*
* Description: Clear LCD display, move cursor to home position.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDCls (void)
{
prvLCDWriteCommand (0x01);
vLCDGoto(1,1);
}
/****************************************************************************
* Name: vLCDCursorOff
*
* Description: Switch off LCD cursor.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDCursorOff(void)
{
prvLCDWriteCommand (0x0c);
}
/****************************************************************************
* Name: vLCDsSwitchOn
*
* Description: Switch on LCD and enable cursor.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDsSwitchOn(void)
{
prvLCDWriteCommand (0x0e);
}
/****************************************************************************
* Name: vLCDPutc
*
* Description: Print a character to LCD at current cursor position.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDPutc (int8_t c)
{
if (lcd_ptr == 16) {
prvLCDWriteCommand (0xc0);
}
prvLCDWriteData (c);
lcd_ptr++;
}
/****************************************************************************
* Name: vLCDPuts
*
* Description: Print a string to LCD at current cursor position.
*
* Parameters: none
*
* Return value: none
****************************************************************************
*/
void vLCDPuts (int8_t *sp)
{
while (*sp)
{
vLCDPutc (*sp++);
}
}
/****************************************************************************
* Name: vLCDPuts
*
* Description: Print a bargraph to LCD display.
*
* Parameters: - val: value 0..100 %
* - size: size of bargraph 1..16
*
* Return value: none
****************************************************************************
*/
void vLCDBargraph (uint32_t val, uint32_t size)
{
uint32_t i;
val = val * size / 20; /* Display matrix 5 x 8 pixels */
for (i = 0; i < size; i++)
{
if (val > 5)
{
vLCDPutc (5);
val -= 5;
}
else
{
vLCDPutc (val);
break;
}
}
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -