|
c语言,学长写的,很是崇拜啊!mplab的c语言!
============================================
#include <p18f452.h>
#include <delays.h>
#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 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
unsigned char Temp_CMD ;
unsigned char Str_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[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() ;
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 );
}
|
|