#include <p18f4520.h>
#include "lcd.h"
#define LCD_RS PORTAbits.RA3
#define LCD_RW PORTAbits.RA2
#define LCD_EN PORTAbits.RA1
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
void __delay_us(unsigned int time)
{
while(--time)
{;}
}
void __delay_ms(unsigned int time1)
{
unsigned int i=0,j=2000;
i=j*time1;
while(--i)
{;}
}
void delay(void)
{
unsigned int n,m;
for(n=0;n<=6000;n++)
{m++;}
if(m==6000)
{m=0;}
}
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position
*/
void lcd_goto(unsigned char y,unsigned char x)
{
LCD_RS = 0;
y&=0x01;
x&=0x0f;//限制x不能大于15,y不能大于1
if(y)x|=0x40;//第二行地址为0x40+0x80;
x|=0x80;
lcd_write(x);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
ADCON1 = 0x06; // Disable analog pins on PORTA
init_value = 0x3;
TRISA=0x10;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
|