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

📄 hd4478_8b.c

📁 LCD开发例子!PIC环境下的
💻 C
字号:
/**********************************************************************/
/*                                                                    */
/* File name: hd4478_8b.c                                             */
/*                                                                    */
/* Since:     2004/11/11                                              */
/*                                                                    */
/* Version:   PICos18 v2.00 - LCD driver v1.1                         */
/*            Copyright (C) 2003, 2004 Pragmatec.                     */
/*                                                                    */
/* Author:    Designed by Pragmatec S.A.R.L.        www.pragmatec.net */
/*            MONTAGNE Xavier [XM]      xavier.montagne@pragmatec.net */
/*            ROZIER Bertrand [RZR]     bertrand.rozier@pragmatec.net */				
/*                                                                    */
/* Purpose:   HITACHI LCD driver in 8 bits mode                       */
/*                                                                    */
/* Distribution: This file is part of PICos18.                        */
/*            PICos18 is free software; you can redistribute it       */
/*            and/or modify it under the terms of the GNU General     */
/*            Public License as published by the Free Software        */
/*            Foundation; either version 2, or (at your option)       */
/*            any later version.                                      */
/*                                                                    */
/*            PICos18 is distributed in the hope that it will be      */
/*            useful, but WITHOUT ANY WARRANTY; without even the      */
/*            implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/*            PARTICULAR PURPOSE.  See the GNU General Public         */
/*            License for more details.                               */
/*                                                                    */
/*            You should have received a copy of the GNU General      */
/*            Public License along with gpsim; see the file           */
/*            COPYING.txt. If not, write to the Free Software         */
/*            Foundation, 59 Temple Place - Suite 330,                */
/*            Boston, MA 02111-1307, USA.                             */
/*                                                                    */
/*          > A special exception to the GPL can be applied should    */
/*            you wish to distribute a combined work that includes    */
/*            PICos18, without being obliged to provide the source    */
/*            code for any proprietary components.                    */
/*                                                                    */
/* History:                                                           */
/* 2004/11/11 [RZR] Create the original file                          */
/*                                                                    */
/**********************************************************************/

#include "define.h"
#include "hd4478.h"


/**********************************************************************
 * ----------------------- LCD USER SETTINGS --------------------------
 **********************************************************************/
#define COLUMN            8              /* Character number per line */
#define LINE              2              /*            Number of line */

#define LCD_ALARM_ID      1              /*     Alarm ID in tascdesc.c*/

#define DATALATCH   LATD                 /*                 DATA port */
#define TRISDATA    TRISD  
#define PORTDATA    PORTD             
#define RS_PIN      LATCbits.LATC0       /*               CMD or DATA */
#define TRIS_RS     TRISCbits.TRISC0
#define E_PIN       LATCbits.LATC2       /*           Enable transfer */
#define TRIS_E      TRISCbits.TRISC2
#define RW_PIN      LATCbits.LATC1       /*             READ or WRITE */
#define TRIS_RW     TRISCbits.TRISC1


/**********************************************************************
 * --------------- LCD macros and function prototypes -----------------
 **********************************************************************/
#define LCDINST  0
#define LCDDATA  1
#define Lcd_write_instruction(inst)  Lcd_write(LCDINST, inst)
#define Lcd_write_data(data)         Lcd_write(LCDDATA, data)
#define Lcd_clear()                  Lcd_write(LCDINST, 0x01)
#define Lcd_home()                   Lcd_write(LCDINST, 0x02)
#define Lcd_blink_ON()               Lcd_write(LCDINST, 0x0D)
#define Lcd_blink_OFF()              Lcd_write(LCDINST, 0x0C)
#define Lcd_left_shift()             Lcd_write(LCDINST, 0x04)
#define Lcd_right_shift()            Lcd_write(LCDINST, 0x06 )
#define Lcd_position(line, pos) \
        line ? Lcd_write(LCDINST,0xC0+pos):Lcd_write(LCDINST, 0x80+pos)
 
void Init_LCD(void);
void Lcd_Strobe(void);
void Lcd_write_4bits(unsigned char c);
void Lcd_write_8bits(unsigned char c);
void Lcd_write(unsigned char type, unsigned char value);
void Delay_LCD_ms(unsigned int delay);
void Lcd_refresh(void);
void WaitState(void);


/**********************************************************************
 * ---------------------- LCD global variables ------------------------
 **********************************************************************/
EventMaskType LCDevent;
unsigned char LCDchar[LINE][COLUMN];


/**********************************************************************
 * ----------------------------- LCD TASK -----------------------------
 **********************************************************************/
TASK(HD4478_DRV)
{
  ADCON1 = 0x8E;
  ADCON0 = 0x00;
  Init_LCD();
  
  while(1)
  {
    WaitEvent(LCD_EVENT);
    ClearEvent(LCD_EVENT);
    Lcd_refresh();
  }
}

/**********************************************************************
 * Print at screen what is on the LCDchar buffer (both lines).
 *
 * @param  void
 * @return void 
 **********************************************************************/
void Lcd_refresh(void)
{
  unsigned char i, j;
  
  /* GO HOME LCD command */
  Lcd_write_instruction(0x02);
  for (i = 0; i < LINE; i++)
  {
    Lcd_position(i, 0);
    for (j = 0; j < COLUMN; j++)
      Lcd_write_data(LCDchar[i][j]);
  }
}

/**********************************************************************
 * Write a command or data on the LCD bus.
 *
 * @param  new_type  IN  0 for CMD, 1 for DATA
 * @param  value     IN  value of the data placed on the bus
 * @return void 
 **********************************************************************/
void Lcd_write(unsigned char type, unsigned char value)
{
  WaitState();
  RW_PIN = 0;
  RS_PIN = type;
  Nop();
  Lcd_write_8bits(value);
}

/**********************************************************************
 * Write the data on the bus (8 bits mode only).
 *
 * @param  c         IN  data written 
 * @return void 
 **********************************************************************/
void Lcd_write_8bits(unsigned char c)
{
  DATALATCH = c;
  Lcd_Strobe();
  Nop(); Nop();
  Nop(); Nop();
}


/**********************************************************************
 * Enable the data present on the bus.
 *
 * @param  void
 * @return void 
 **********************************************************************/
void Lcd_Strobe(void)
{
  E_PIN = 1;
  Nop();
  E_PIN = 0;
}

/**********************************************************************
 * Generic routine to create a delay of many milliseconds.
 *
 * @param  delay     IN  time to wait in ms
 * @return void 
 **********************************************************************/
void Delay_LCD_ms(unsigned int delay)
{
  SetRelAlarm(LCD_ALARM_ID, delay, 0);
  WaitEvent(ALARM_EVENT);
  GetEvent(HD4478_DRV_ID, &LCDevent);
  if (LCDevent & ALARM_EVENT)
    ClearEvent(ALARM_EVENT);
  return;
}

/**********************************************************************
 * Init phase of the LCD.
 * Do not modify this sequence.
 *
 * @param  void
 * @return void 
 **********************************************************************/
void Init_LCD(void)
{
  TRISDATA  = 0x00;
  DATALATCH = 0x00;

  TRIS_E    = 0;
  TRIS_RS   = 0;
  TRIS_RW   = 0;
  RS_PIN    = 0;
  E_PIN     = 0;
  RW_PIN    = 0;
  DATALATCH = 0;

  // Delay for 25ms to allow for LCD Power on reset
  Delay_LCD_ms(25);

  /* Mode 8 bit, 2 lines , font5x7 */
  Lcd_write_instruction(0x38);

  /* DISPLAY off, ENTRY mode, BLINK off, no SHIFT */
  Lcd_write_instruction(0x08);
  Lcd_write_instruction(0x06);
  Lcd_write_instruction(0x0C);
  Lcd_write_instruction(0x80);
  Lcd_clear(); 
}

/**********************************************************************
 * Copy a string of characters into the LCDchar buffer.
 *
 * @param  s         IN  string to copy into the buffer
 * @param  positionX IN  column selection
 * @param  positionY IN  line selection
 * @return void 
 **********************************************************************/
void LcdPrintString(const rom char *s,
                      unsigned char positionX,
                      unsigned char positionY)
{
  while (*s)
  {
    LCDchar[positionY][positionX] = *s++;
    positionX++;
  }
  SetEvent(HD4478_DRV_ID, LCD_EVENT);
}

/**********************************************************************
 * Erase the local buffer LCDchar.
 *
 * @param  void
 * @return void 
 **********************************************************************/
void LcdClear(void)
{
  unsigned char i, j;

  for (i = 0; i < LINE; i++)
    for (j = 0; j < COLUMN; j++)
      LCDchar[i][j] = ' ';
  SetEvent(HD4478_DRV_ID, LCD_EVENT);
}

/**********************************************************************
 * Convert a data (8 bits only) into a string of 2 characters.
 *
 * @param  value     IN  Value converted in string of characters
 * @param  positionX IN  column selection
 * @param  positionY IN  line selection
 * @return void 
 **********************************************************************/
void LcdPrintData(unsigned char value,
                     unsigned char positionX,
                     unsigned char positionY)
{
  LCDchar[positionY][positionX] = 0x30 + (value / 10);
  LCDchar[positionY][positionX+1] = 0x30 + (value % 10);
  SetEvent(HD4478_DRV_ID, LCD_EVENT);
}

/**********************************************************************
 * Wait until LCD is not busy
 *
 * @param  void
 * @return void 
 **********************************************************************/
void WaitState(void)
{
  unsigned char val;
  RW_PIN = 1;
  TRISDATA  = 0xFF;
  Nop();
  do
  {
    RS_PIN = 0;
    Nop(); Nop();
    E_PIN  = 1;
    Nop(); Nop();
    val = (PORTDATA & 0x80);
    E_PIN  = 0;
  } while(val);
  RW_PIN = 0;
  TRISDATA  = 0x00;
  Nop();
}

/* End of File : hd4478_8b.c */

⌨️ 快捷键说明

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