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

📄 portlcd.c

📁 深圳优龙科技LPC2468开发板
💻 C
字号:
/*****************************************************************************
 *   portlcd.c:  4-bit port LCD C file for NXP LPC34xx Family
 *   Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.12  ver 1.00    Prelimnary version, first Release
 *
*****************************************************************************/
#include "LPC24xx.H"                        /* LPC24xx definitions */
#include "type.h"
#include "irq.h"
#include "portlcd.h"

#define USE_FIO		0

#if USE_FIO
#define IO0DIR    FIO0DIR
#define IO0SET    FIO0SET
#define IO0CLR    FIO0CLR
#define IO0PIN    FIO0PIN
#else
#define IO0DIR    IODIR0
#define IO0SET    IOSET0
#define IO0CLR    IOCLR0
#define IO0PIN    IOPIN0
#endif

/* LCD IO definitions */
#define LCD_E     0x04000000            /* Enable control pin                */
#define LCD_RW    0x02000000            /* Read/Write control pin            */
#define LCD_RS    0x01000000            /* Data/Instruction control          */
#define LCD_CTRL  0x07000000            /* Control lines mask                */
#define LCD_DATA  0x001E0000            /* 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. */
   IO0DIR |= LCD_DATA | LCD_CTRL;
   IO0CLR  = LCD_RW   | LCD_DATA;
   IO0SET  = (c & 0xF) << 17;
   IO0SET  = LCD_E;
   delay (10);
   IO0CLR  = 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;

   IO0DIR &= ~LCD_DATA;
   IO0CLR  = LCD_RS;
   IO0SET  = LCD_RW;
   delay (10);
   IO0SET  = LCD_E;
   delay (10);
   stat    = (IO0PIN >> 13) & 0xF0;
   IO0CLR  = LCD_E;
   delay (10);
   IO0SET  = LCD_E;
   delay (10);
   stat   |= (IO0PIN >> 17) & 0xF;
   IO0CLR  = 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();
   IO0CLR = 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();
   IO0SET = 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. */
#if USE_FIO
	SCS |= 0x00000001;	/* set GPIOx to use Fast I/O */
#endif
   IO0DIR |= LCD_CTRL | LCD_DATA;
   IO0CLR  = 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 + -