超规范:Pic18f452 4位数据1602lcd驱动程序
c语言,学长写的,很是崇拜啊!mplab的c语言!============================================
#include <p18f452.h>
#include <delays.h>
#define CPU_SPEED16 // 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_DATALATD // PORTD as LCD DB
#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_C0x00E// Display on, Cursor on
#define DISP_ON_B0x00F// Display on, Cursor on, Blink cursor
#define DISP_OFF0x008// Display off
#define CLR_DISP0x001// Clear the Display
#define ENTRY_INC0x006//
#define ENTRY_INC_S0x007//
#define ENTRY_DEC0x004//
#define ENTRY_DEC_S0x005//
#define DD_RAM_ADDR0x080// Least Significant 7-bit are for address
#define DD_RAM_UL0x080// Upper Left coner of the Display
unsigned charTemp_CMD ;
unsigned charStr_Temp ;
unsigned char Out_Mask ;
void LCD_Set_Cursor(unsigned char CurY, unsigned char CurX);
void LCD_L_Delay(void) ;
void LCD_S_Delay(void) ;
void WriteCmdLCD(unsigned char LCD_CMD);
void WriteDataLCD( unsigned char LCD_CMD) ;
void LCD_CMD_W_Timing( void ) ;
void LCD_DAT_W_Timing( void ) ;
void main (void);
void main (void){
//对LCD进行初始化**********************************************************************
char flag=1;
char c1='I';
char c2=' ';
char c3='a';
char c4='m';
char c5=' ';
char c6='s';
char c7='!';
TRISBbits.TRISB0=1;
LCD_E=0;
LCD_DATA = 0x00; // LCD DB & 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() ;
LCD_Set_Cursor(0,5);
//**********************************************
WriteDataLCD(c1);
WriteDataLCD(c2);
WriteDataLCD(c3);
WriteDataLCD(c4);
WriteDataLCD(c5);
WriteDataLCD(c6);
WriteDataLCD(c7);
Nop();
while(1)
{
if(PORTBbits.RB0==1)
{
flag=1;
}
else
{
if(flag==1)
{
WriteCmdLCD(DISP_ON) ;
LCD_S_Delay() ;
WriteCmdLCD(ENTRY_INC) ;
LCD_S_Delay() ;
WriteCmdLCD(CLR_DISP);
LCD_L_Delay() ;
LCD_Set_Cursor(1,5);
WriteDataLCD(c1);
WriteDataLCD(c2);
WriteDataLCD(c3);
WriteDataLCD(c4);
WriteDataLCD(c5);
WriteDataLCD(c6);
WriteDataLCD(c7);
}
flag=flag+1;
}
}
}
//*********************************************
// _ ______________________________
// 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 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() ;
}
// *********************************************************************************
// Delay for atleast 10 ms
// *********************************************************************************
void LCD_L_Delay(void)
{
Delay10KTCYx( 8);
}
// *********************************************************************************
// Delay for 100 us
// *********************************************************************************
void LCD_S_Delay(void)
{
Delay100TCYx( 16 );
}
页:
[1]