#include <p18f452.h>
#include <delays.h>
#include "P18F_LCD.h"
//
// Defines for I/O ports that provide LCD data & control
// PORTD[0:3]-->DB[4:7]: Higher order 4 lines data bus with bidirectional
// : DB7 can be used as a BUSY flag
// PORTA,2 --> [E] : LCD operation start signal control
// PORTD,5 --> [RW]: LCD Read/Write control
// PORTD,4 --> [RS]: LCD Register Select control
// : "0" for Instrunction register (Write), Busy Flag (Read)
// : "1" for data register (Read/Write)
//
#define CPU_SPEED 16 // CPU speed is 16 Mhz !!
#define LCD_RS PORTDbits.RD4 // The definition of control pins
#define LCD_RW PORTDbits.RD5
#define LCD_E PORTAbits.RA2
#define LCD_DATA LATD // PORTD[0:3] as LCD DB[4:7]
//#define LCD_CTRL LATA
#define DIR_LCD_DATA TRISD
// LCD Module commands
#define DISP_2Line_8Bit 0b00111000
#define DISP_2Line_4Bit 0b00101000
#define DISP_ON 0x00C // Display on
#define DISP_ON_C 0x00E // Display on, Cursor on
#define DISP_ON_B 0x00F // Display on, Cursor on, Blink cursor
#define DISP_OFF 0x008 // Display off
#define CLR_DISP 0x001 // Clear the Display
#define ENTRY_INC 0x006 //
#define ENTRY_INC_S 0x007 //
#define ENTRY_DEC 0x004 //
#define ENTRY_DEC_S 0x005 //
#define DD_RAM_ADDR 0x080 // Least Significant 7-bit are for address
#define DD_RAM_UL 0x080 // Upper Left coner of the Display
#pragma udata
unsigned char Temp_CMD ;
unsigned char Str_Temp ;
unsigned char Out_Mask ;
#pragma code
void OpenLCD(void)
{
ADCON1=(ADCON1 & 0xF0)|0b00001110; // Set AN0 for analog input
LCD_E=0;
LCD_DATA = 0x00; // LCD DB[4:7] & RS & R/W --> Low
DIR_LCD_DATA = 0x00; // LCD DB[4:7} & RS & R/W are output function
TRISAbits.TRISA2=0; // Set E pin as output
LCD_DATA = 0b00000011 ;
LCD_CMD_W_Timing() ;
LCD_L_Delay() ;
LCD_DATA = 0b00000011 ;
LCD_CMD_W_Timing() ;
LCD_L_Delay() ;
LCD_DATA = 0b00000011 ;
LCD_CMD_W_Timing() ;
LCD_L_Delay() ;
LCD_DATA = 0b00000010 ;
LCD_CMD_W_Timing() ;
LCD_L_Delay() ;
WriteCmdLCD(DISP_2Line_4Bit) ;
LCD_S_Delay() ;
WriteCmdLCD(DISP_ON) ;
LCD_S_Delay() ;
WriteCmdLCD(ENTRY_INC) ;
LCD_S_Delay() ;
WriteCmdLCD(CLR_DISP) ;
LCD_L_Delay() ;
}
//*********************************************
// _ ______________________________
// RS _>--<______________________________
// _____
// RW \_____________________________
// __________________
// E ____________/ \___
// _____________ ______
// DB _____________>--------------<______
//***********************************************
// Write Command to LCD module
//
void WriteCmdLCD( unsigned char LCD_CMD)
{
Temp_CMD = (LCD_CMD & 0xF0)>>4 ; // Send high nibble to LCD bus
LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;
LCD_CMD_W_Timing () ;
Temp_CMD = LCD_CMD & 0x0F ; // Send low nibble to LCD bus
LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;
LCD_CMD_W_Timing () ;
LCD_S_Delay() ; // Delay 100uS for execution
}
//***********************************************
// Write Data to LCD module
//
void WriteDataLCD( unsigned char LCD_CMD)
{
Temp_CMD = (LCD_CMD & 0xF0)>>4 ; // Send high nibble to LCD bus
LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;
LCD_DAT_W_Timing () ;
Temp_CMD = LCD_CMD & 0x0F ; // Send low nibble to LCD bus
LCD_DATA= (LCD_DATA & 0xF0)|Temp_CMD ;
LCD_DAT_W_Timing () ;
LCD_S_Delay() ; // Delay 100uS for execution
}
void putcLCD(unsigned char LCD_Char)
{
WriteDataLCD(LCD_Char) ;
}
void LCD_CMD_W_Timing( void )
{
LCD_RS = 0 ; // Set for Command Input
Nop();
// LCD_RW = 0 ;
Nop();
LCD_E = 1 ;
Nop();
Nop();
LCD_E = 0 ;
}
void LCD_DAT_W_Timing( void )
{
LCD_RS = 1 ; // Set for Data Input
Nop();
// LCD_RW = 0 ;
Nop();
LCD_E = 1 ;
Nop();
Nop();
LCD_E = 0 ;
}
//***********************************************
// Set Cursor position on LCD module
// CurY = Line (0 or 1)
// CurX = Position ( 0 to 15)
//
void LCD_Set_Cursor(unsigned char CurY, unsigned char CurX)
{
WriteCmdLCD( 0x80 + CurY * 0x40 + CurX) ;
LCD_S_Delay() ;
}
void SetDDRamAddr (unsigned char DDRamAddr)
{
WriteCmdLCD(DDRamAddr);
LCD_S_Delay();
}
//***********************************************
// Put a ROM string to LCD Module
//
void putrsLCD( const rom char *Str )
{
while (1)
{
Str_Temp = *Str ;
if (Str_Temp != 0x00 )
{
WriteDataLCD(Str_Temp) ;
Str ++ ;
}
else
return ;
}
}
//***********************************************
// Put a RAM string to LCD Module
//
void putsLCD( char *Str)
{
while (1)
{
Str_Temp = *Str ;
if (Str_Temp != 0x00 )
{
WriteDataLCD(Str_Temp) ;
Str ++ ;
}
else
return ;
}
}
void puthexLCD(unsigned char HEX_Val)
{
unsigned char Temp_HEX ;
Temp_HEX = (HEX_Val >> 4) & 0x0f ;
if ( Temp_HEX > 9 )Temp_HEX += 0x37 ;
else Temp_HEX += 0x30 ;
WriteDataLCD(Temp_HEX) ;
Temp_HEX = HEX_Val & 0x0f ;
if ( Temp_HEX > 9 )Temp_HEX += 0x37 ;
else Temp_HEX += 0x30 ;
WriteDataLCD(Temp_HEX) ;
}
// *********************************************************************************
// Delay for atleast 10 ms
// *********************************************************************************
void LCD_L_Delay(void)
{
Delay10KTCYx(CPU_SPEED / 2) ;
}
// *********************************************************************************
// Delay for 100 us
// *********************************************************************************
void LCD_S_Delay(void)
{
Delay100TCYx(CPU_SPEED) ;
}
simple_S_watch.rar_watch
版权申诉
86 浏览量
2022-09-23
16:13:43
上传
评论
收藏 71KB RAR 举报
APei
- 粉丝: 64
- 资源: 1万+