📄 portlcd.c
字号:
/*****************************************************************************
* portlcd.c: 4-bit port LCD C file for NXP LPC23xx/34xx Family
* Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2006.07.12 ver 1.00 Prelimnary version, first Release
*
*****************************************************************************/
#include "LPC23xx.H" /* LPC23xx/24xx definitions */
#include "type.h"
#include "irq.h"
#include "portlcd.h"
#define USE_FIO 0
#if USE_FIO
#define IO1DIR FIO1DIR
#define IO1SET FIO1SET
#define IO1CLR FIO1CLR
#define IO1PIN FIO1PIN
#else
#define IO1DIR IODIR1
#define IO1SET IOSET1
#define IO1CLR IOCLR1
#define IO1PIN IOPIN1
#endif
/* Please note, on old MCB2300 board, the LCD_E bit is p1.30, on the new board
it's p1.31, please check the schematic carefully, and change LCD_CTRL and LCD_E
accordingly if you have a different board. */
/* LCD IO definitions */
#define LCD_E 0x80000000 /* Enable control pin */
#define LCD_RW 0x20000000 /* Read/Write control pin */
#define LCD_RS 0x10000000 /* Data/Instruction control */
#define LCD_CTRL 0xB0000000 /* Control lines mask */
#define LCD_DATA 0x0F000000 /* Data lines mask */
/* Local variables */
static DWORD lcd_ptr;
/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
static const BYTE 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 */
void delay( DWORD cnt );
void lcd_write( DWORD c );
void lcd_write_4bit( DWORD c );
DWORD lcd_read_stat( void );
void lcd_write_cmd( DWORD c );
void lcd_write_data( DWORD d );
void lcd_wait_busy( void );
/******************************************************************************
** Function name: delay
**
** Descriptions:
**
** parameters: delay counter
** Returned value: None
**
******************************************************************************/
void delay (DWORD cnt)
{
/* Delay in while loop cycles. */
while (cnt--);
}
/******************************************************************************
** Function name: lcd_write_4bit
**
** Descriptions:
**
** parameters: four bits to write
** Returned value: None
**
******************************************************************************/
void lcd_write_4bit( DWORD c )
{
/* Write a 4-bit command to LCD controller. */
IO1DIR |= LCD_DATA | LCD_CTRL;
IO1CLR = LCD_RW | LCD_DATA;
IO1SET = (c & 0xF) << 24;
IO1SET = LCD_E;
delay (10);
IO1CLR = LCD_E;
delay (10);
return;
}
/******************************************************************************
** Function name: lcd_write
**
** Descriptions:
**
** parameters: word to write
** Returned value: None
**
******************************************************************************/
void lcd_write( DWORD c )
{
/* Write data/command to LCD controller. */
lcd_write_4bit (c >> 4);
lcd_write_4bit (c);
return;
}
/******************************************************************************
** Function name: lcd_read_stat
**
** Descriptions:
**
** parameters: None
** Returned value: status
**
******************************************************************************/
DWORD lcd_read_stat( void )
{
/* Read status of LCD controller (ST7066) */
DWORD stat;
IO1DIR &= ~LCD_DATA;
IO1CLR = LCD_RS;
IO1SET = LCD_RW;
delay (10);
IO1SET = LCD_E;
delay (10);
stat = (IO1PIN >> 20) & 0xF0;
IO1CLR = LCD_E;
delay (10);
IO1SET = LCD_E;
delay (10);
stat |= (IO1PIN >> 24) & 0xF;
IO1CLR = LCD_E;
return (stat);
}
/******************************************************************************
** Function name: lcd_wait_busy
**
** Descriptions:
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void lcd_wait_busy( void )
{
/* Wait until LCD controller (ST7066) is busy. */
DWORD stat;
do
{
stat = lcd_read_stat();
} while (stat & 0x80); /* Wait for busy flag */
return;
}
/******************************************************************************
** Function name: lcd_write_cmd
**
** Descriptions:
**
** parameters: command word
** Returned value: None
**
******************************************************************************/
void lcd_write_cmd( DWORD c )
{
/* Write command to LCD controller. */
lcd_wait_busy();
IO1CLR = LCD_RS;
lcd_write(c);
return;
}
/******************************************************************************
** Function name: lcd_write_data
**
** Descriptions:
**
** parameters: data word
** Returned value: None
**
******************************************************************************/
void lcd_write_data( DWORD d )
{
/* Write data to LCD controller. */
lcd_wait_busy();
IO1SET = LCD_RS;
lcd_write(d);
return;
}
/******************************************************************************
** Function name: LCD_init
**
** Descriptions:
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void LCD_init( void )
{
/* Initialize the ST7066 LCD controller to 4-bit mode. */
PINSEL3 = 0x00000000;
#if USE_FIO
SCS |= 0x00000001; /* set GPIOx to use Fast I/O */
#endif
IO1DIR |= LCD_CTRL | LCD_DATA;
IO1CLR = LCD_RW | LCD_RS | LCD_DATA;
lcd_write_4bit(0x3); /* Select 4-bit interface */
delay (100000);
lcd_write_4bit(0x3);
delay (10000);
lcd_write_4bit(0x3);
lcd_write_4bit(0x2);
lcd_write_cmd(0x28); /* 2 lines, 5x8 character matrix */
lcd_write_cmd(0x0e); /* Display ctrl:Disp/Curs/Blnk=ON */
lcd_write_cmd(0x06); /* Entry mode: Move right, no shift */
LCD_load( (BYTE *)&UserFont, sizeof (UserFont) );
LCD_cls();
return;
}
/******************************************************************************
** Function name: LCD_load
**
** Descriptions:
**
** parameters: pointer to the buffer and counter
** Returned value: None
**
******************************************************************************/
void LCD_load( BYTE *fp, DWORD cnt )
{
/* Load user-specific characters into CGRAM */
DWORD i;
lcd_write_cmd( 0x40 ); /* Set CGRAM address counter to 0 */
for (i = 0; i < cnt; i++, fp++)
{
lcd_write_data( *fp );
}
return;
}
/******************************************************************************
** Function name: LCD_gotoxy
**
** Descriptions:
**
** parameters: pixel X and Y
** Returned value: None
**
******************************************************************************/
void LCD_gotoxy( DWORD x, DWORD y )
{
/* Set cursor position on LCD display. Left corner: 1,1, right: 16,2 */
DWORD c;
c = --x;
if (--y)
{
c |= 0x40;
}
lcd_write_cmd (c | 0x80);
lcd_ptr = y*16 + x;
return;
}
/******************************************************************************
** Function name: LCD_cls
**
** Descriptions:
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void LCD_cls( void )
{
/* Clear LCD display, move cursor to home position. */
lcd_write_cmd (0x01);
LCD_gotoxy (1,1);
return;
}
/******************************************************************************
** Function name: LCD_cur_off
**
** Descriptions:
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void LCD_cur_off( void )
{
/* Switch off LCD cursor. */
lcd_write_cmd(0x0c);
return;
}
/******************************************************************************
** Function name: LCD_on
**
** Descriptions:
**
** parameters: None
** Returned value: None
**
******************************************************************************/
void LCD_on( void )
{
/* Switch on LCD and enable cursor. */
lcd_write_cmd (0x0e);
return;
}
/******************************************************************************
** Function name: LCD_putc
**
** Descriptions:
**
** parameters: byte character
** Returned value: None
**
******************************************************************************/
void LCD_putc( BYTE c )
{
/* Print a character to LCD at current cursor position. */
if (lcd_ptr == 16)
{
lcd_write_cmd (0xc0);
}
lcd_write_data(c);
lcd_ptr++;
return;
}
/******************************************************************************
** Function name: LCD_puts
**
** Descriptions:
**
** parameters: pointer to the buffer
** Returned value: None
**
******************************************************************************/
void LCD_puts ( BYTE *sp )
{
/* Print a string to LCD display. */
while (*sp)
{
LCD_putc (*sp++);
}
return;
}
/******************************************************************************
** Function name: LCD_bargraph
**
** Descriptions:
**
** parameters: value and size
** Returned value: None
**
******************************************************************************/
void LCD_bargraph( DWORD val, DWORD size )
{
/* Print a bargraph to LCD display. */
/* - val: value 0..100 % */
/* - size: size of bargraph 1..16 */
DWORD i;
val = val * size / 20; /* Display matrix 5 x 8 pixels */
for (i = 0; i < size; i++)
{
if (val > 5)
{
LCD_putc(5);
val -= 5;
}
else
{
LCD_putc(val);
break;
}
}
return;
}
/*****************************************************************************
** End Of File
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -