📄 lcd_hd44780.c
字号:
/******************************************************************************
* *
* (c) Freescale Inc. 2005 All rights reserved *
* *
*******************************************************************************
* *
* File Name : setup.c *
* *
* Description : HD44780 driven LCD display service functions *
* *
* Compiler : CW HCS12X 4.1 *
* *
* $Version : 1.0.9.0$ *
* *
*******************************************************************************
******************************************************************************/
/*****************************************************************************/
#include "9S12XDP512.h"
#include "LCD_HD44780.H"
#include "string.h"
#include "stddef.h"
/*****************************************************************************/
/*****************************************************************************/
/* INTERNAL DEFINITIONS FOR DISPLAY CONTROL */
/*****************************************************************************/
#define LCD_DATA 1
#define LCD_CTRL 0
#define LCD_BUSY 0
#define LCD_READ 1
#define LCD_WRITE 0
#define LCD_TRUE 1
#define LCD_FALSE 0
/*****************************************************************************/
/* AUXILIARY DEFINITIONS FOR DISPLAY CONTROL */
/*****************************************************************************/
/*---------------------------------------------------------------------------*/
/* 10ms delay constant - used in the function void LcdDelay6ms(void) */
#define LCD_LOOP10MS ((LCD_FBUS*1000000L/400)+1)
/* IIC delay constant - used in the function void LcdDelayIIC(void) */
#define LCD_LOOPIIC ((LCD_FBUS*1000000L/40000000)+1)
/* 1us delay constant - used in the function void LcdDelay1us(void) */
#define LCD_LOOP1US ((LCD_FBUS*1000000L/1000000L)+1)
/* set port as in/out - used in the function LcdSetPortDirection(LCD_INPORT) */
#define LCD_INPORT 0
#define LCD_OUTPORT 1
/* determine interface width */
#define LCD_SETDATA8 0x00
#define LCD_SETDATA4 0x04
/* determine number of display driver memory lines */
#define LCD_SETLINES1 0x02
#define LCD_SETLINES2 0x00
/* set data width constants */
#if LCD_DATAWIDTH==8
#define LCD_DATAWIDTH_8
#define LCD__DATAWIDTH LCD_SETDATA8
#else
#if LCD_DATAWIDTH==4
#define LCD_DATAWIDTH_4
#define LCD__DATAWIDTH LCD_SETDATA4
#else
#define LCD_DATAWIDTH_2
#define LCD__DATAWIDTH LCD_SETDATA4
#endif
#endif
/* set memory lines constant */
#if LCD_MLINES==1
#define LCD_SETLINES 0x02
#else
#define LCD_SETLINES 0x00
#endif
/*****************************************************************************/
/* define type : structure defining behaviour */
/*****************************************************************************/
typedef struct
{
unsigned char line; /* cursor line */
unsigned char col ; /* cursor column */
unsigned char address; /* cursor address */
}
S_LcdPosition;
typedef struct
{
S_LcdPosition cursor; /* cursor position structure */
unsigned char movingSetup ; /* display moving setup */
unsigned char onOffControl; /* display on/off control */
unsigned char interface ; /* display interface */
/* 0 0 0 0 0 1 I/D S */
/* 0 0 0 0 1 D C B */
/* 0 0 1 DL N F . . */
}
S_LCD;
/*****************************************************************************/
/* error messages and Variables used with freemaster */
/*****************************************************************************/
const char messages[][29]={
{"Running... [00]"},
{"FAULT on CAN bus [Xx]"},
{"Short CAN-HI to GND [02]"},
{"Short CAN_HI to Vbatt [03]"},
{"Short CAN_HI to Vpwr [04]"},
{"CAN_LO to CAN_HI or GND [05]"},
{"Short CAN_LO to Vbatt [06]"},
{"Short CAN_LO to Vpwr [07]"},
{"CAN_LO to CAN_HI or GND [08]"},
{"Short CAN_RX to CAN_TX [09]"},
{"FAULT on LIN bus [Xx]"},
{"Short LIN to GND [11]"},
{"Short LIN to Vpr (12V) [12]"},
{"FAULT on MC33879 (COSS) [Xx]"},
{"open or short load [14]"},
{"open or short load [15]"},
{"FAULT: on MC33874(Puma) [Xx]"},
{"over current high [17]"},
{"over current low [18]"},
{"open load [19]"},
{"over voltage detection [20]"},
{"under voltage [21]"},
{"FAULT on MC33580(Jaguar)[Xx]"},
{"loss of ground [23]"},
{"loss of Vpr (12V) [24]"},
{"open load [25]"},
{"over voltage detection [26]"},
{"under voltage detection [27]"},
{"SYSTEM MESSAGE: [Xx]"},
{"FAULT: NOT ENOUGH MEMORY[63]"}, /*28*/ //63
{"NO FAULT [64]"}, /*29*/ //64
{"CAN communication is OK [65]"}, /*30*/ //65
{"LIN communication is OK [74]"}, /*31*/ //74
{"Fault on MC33879 Removed[77]"}, /*32*/ //77
{"No Fault on MC33874 [80]"}, /*33*/ //80
{"No Fault on MC33580 [86]"}, /*34*/ //86
{"No Fault on MC33879 [78]"}, /*48*/ //78
{"No Fault on MC33742 [93]"},
{"No Fault on MC33972 [92]"},
{"FAULT on CAN REMOVED: [Xx]"}, /*35*/
{"Short CAN-HI to GND [66]"}, /*36*/ //66
{"Short CAN_HI to Vbatt [67]"}, /*37*/ //67
{"Short CAN_HI to Vpwr [68]"}, /*38*/ //68
{"CAN_LO to CAN_HI or GND [69]"}, /*39*/ //69
{"Short CAN_LO to Vbatt [70]"}, /*40*/ //70
{"Short CAN_LO to Vpr [71]"}, /*41*/ //71
{"CAN_LO to CAN_HI or GND [72]"}, /*42*/ //72
{"Short CAN_RX to CAN_TX [73]"}, /*43*/ //73
{"FAULT on LIN REMOVED: [Xx]"}, /*44*/
{"Short LIN to GND [75]"}, /*45*/ //75
{"Short LIN to Vpr [76]"}, /*46*/ //76
{"FAULT on MC33879 REMOVED[Xx]"}, /*47*/
{"open or short load [79]"}, /*49*/ //79
{"FAULT on MC33874 REMOVED[Xx]"}, /*50*/
{"Overcurrent high detect [81]"}, /*51*/ //81
{"Overcurrent low detect [82]"}, /*52*/ //82
{"Open load detection [83]"}, /*53*/ //83
{"Over voltage detection [84]"}, /*54*/ //84
{"Under voltage detection [85]"}, /*55*/ //85
{"FAULT on MC33580 REMOVED[Xx]"}, /*56*/
{"Loss of ground detection[87]"}, /*57*/ //87
{"Loss of Vpwr detection [88]"}, /*58*/ //88
{"Open load detection [89]"}, /*59*/ //89
{"Over voltage detection [90]"}, /*60*/ //90
{"Under voltage detection [91]"}, /*61*/ //91
{"FAULT ON MC33742 (SBC) [Xx]"},
{"Over temperature [29]"},
{"Under voltage detection [30]"},
{"FAULT ON MC33972 (MSDI) [28]"} ,
};
unsigned char systemStatus=0xFF;
/*****************************************************************************/
/* STATIC FUNCTION DEFINITIONS */
/*****************************************************************************/
static void LcdDelay6ms (void);
static void LcdDelayIIC (void);
static void LcdDelay1us (void);
static void LcdSetDataLength (tUC length);
static void LcdSetDisplayLines (tUC lines);
static void LcdClock (void);
static void LcdPutNumberOnPortRST (tUC data, tUC rs, tUC rw);
static void LcdPutNumberOnPort (tUC data, tUC rs, tUC rw);
static tUC LcdReadBusyFlag (void);
static void LcdSetPortDirection (tUC dir);
static void LcdCommand (tUC Command);
#ifdef IIC_LCD_TRANSMIT
static void LcdPutNumberOnIIC(tUC data);
#endif
#ifndef LCD_NO_READ_FUNCTION
static tUC LcdReadDataPort (tUC what);
static tUC LcdGetPortDirection (void);
static tUC LcdGetAddressDDRAM (void);
static void LcdGetLineCol (void);
#else
#define LcdReadDataPort() 0
#define LcdGetPortDirection() 0
#define LcdGetAddressDDRAM() 0
#define LcdGetLineCol() 0
#endif
static S_LCD Lcd;
/******************************************************************************
* Module : static void LcdDelay10ms(void)
* Description : 10 ms software delay loop
* delay produced by this loop is :
* t = busperiod * DBNE = bus period * LCD_LOOP10MS * 3
* Global Data : none
* Static Global Data: none
* Returns : none
* Arguments : none
* Special Issues : none
******************************************************************************/
static void LcdDelay10ms(void)
{
asm
{
LDD #LCD_LOOP10MS ; [2]
loop1: nop ; [1]
DBNE D, loop1 ; [3]
};
}
/******************************************************************************
* Module : static void LcdDelayIIC(void)
* Description : 150 us software delay loop
* delay produced by this loop is :
* t = busperiod * DBNE = bus period * LCD_LOOP10MS * 3
* Global Data : none
* Static Global Data: none
* Returns : none
* Arguments : none
* Special Issues : none
******************************************************************************/
static void LcdDelayIIC(void)
{
asm
{
LDD #LCD_LOOPIIC ; [2]
loop1: nop ; [1]
DBNE D, loop1 ; [3]
};
}
/******************************************************************************
* Module : static void LcdDelay1us(void)
* Description : 1 us software delay loop
* delay produced by this loop is :
* t = busperiod * DBNE = bus period * LCD_LOOP1US * 3
* Static Global Data: none
* Returns : none
* Arguments : none
* Special Issues : none
******************************************************************************/
static void LcdDelay1us(void)
{
asm
{
LDD #LCD_LOOP1US ; [2]
loop2: DBNE D, loop2 ; [3]
};
}
/******************************************************************************
* Module : void LcdClear(void)
* Description : clear all display and return the cursor to the home
* Global Data : none
* Static Global Data: Lcd.cursor.address, Lcd.cursor.line, Lcd.cursor.col
* Returns : none
* Arguments : none
* Special Issues : none
******************************************************************************/
void LcdClear(void)
{
LcdCommand(0x01);
#ifdef LCD_NO_READ_FUNCTION
LcdDelay10ms();
LcdDelay10ms();
LcdDelay10ms();
#else
while(LcdReadBusyFlag());
#endif
LcdGetLineCol();
}
/******************************************************************************
* Module : void LcdReturnHome(void)
* Description : return cursor to the home position
* and return shifted display to original position.
* DDRAM contents remains unchanged
* Global Data : none
* Static Global Data: none
* Returns : none
* Arguments : none
* Special Issues : none
******************************************************************************/
void LcdReturnHome(void)
{
LcdCommand(0x02);
#ifdef LCD_NO_READ_FUNCTION
LcdDelay10ms();
#else
while(LcdReadBusyFlag());
#endif
LcdGetLineCol();
}
/******************************************************************************
* Module : void LcdSetCursorDirection(tUC direction)
* Description : set cursor moving direction to the right or left
* Global Data : none
* Static Global Data: Lcd.movingSetup
* Returns : none
* Arguments : tUC direction = {LCD_CURSORRIGHT, LCD_CURSORLEFT}
* Special Issues : none
******************************************************************************/
void LcdSetCursorDirection(tUC direction)
{
switch(direction&0x80)
{
case LCD_CURSORRIGHT : Lcd.movingSetup&=0x05; /* 0000 0x0x */
Lcd.movingSetup|=0x06; /* 0000 011x */
break;
case LCD_CURSORLEFT : Lcd.movingSetup&=0x05; /* 0000 0x0x */
Lcd.movingSetup|=0x04; /* 0000 010x */
break;
default : return;
}
LcdCommand(Lcd.movingSetup);
}
/******************************************************************************
* Module : void LcdSetDisplayShiftEnable(tUC onoff)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -