本帖最后由 haorenyigea 于 2014-6-10 14:56 编辑
#include "pic.h"
__CONFIG(FOSC_INTOSC&CLKOUTEN_OFF&MCLRE_ON&WDTE_OFF);//&PWRTE_ON&BOREN_ON
#define _XTAL_FREQ 4000000L //内部4MHz
#define uint unsigned int
#define uchar unsigned char
#define RS RB0
#define RW RB1
#define EN RB2
#define BLK_CON RB4
uchar table[]="LCD1602 check ok";
uchar table1[]="study up";
//*******************************************************************//
// 系统初始化
//*******************************************************************//
void System_Init(void)
{
OSCCON = 0b01101000; //内部4MHz
OSCTUNE= 0b00000000; //出厂校准
TRISA = 0b00000000; //RA0-RA3输出、RA5输入
ANSELA = 0b00000000; //RA5 模拟输入
TRISB = 0b00000000; //RB0-RB3触摸按键
ANSELB = 0b00000000; //4个触摸按键
WPUB = 0b00000000; //无上拉
IOCBP = 0b00000000; //关电平变化中断
IOCBN = 0b00000000;
TRISD = 0b00000000; //PORTD输出
ANSELD = 0b00000000; //数字 I/O
TRISC = 0b10011000; //串口 I2C
TRISE = 0b00000000; //PORTE输出
ANSELE = 0b00000000; //数字 I/O
WPUE = 0b00000000; //无上拉
}
//*******************************************************************//
// 主程序部分
//*******************************************************************//
void delay(uint n)
{ uint i;
uchar j;
for(i=0;i<n;i++)
{asm("CLRWDT");
for(j=0;j<100;j++);
}
}
void lcd_wcom(uchar com)
{
RS=0;
RW=0;
PORTD=com;
delay(5);
EN=1;
EN=0;
}
void lcd_wdat(uchar dat)
{
RS=1;
RW=0;
PORTD=dat;
delay(5);
EN=1;
EN=0;
}
bit lcd_check_busy()
{
static bit busy_flag;
RS=0;
RW=1;
EN=1;
asm("nop");
asm("nop");
asm("nop");
if(PORTD&0X80)
busy_flag=1;
else
busy_flag=0;
EN=0;
return busy_flag;
}
void lcd_init()
{
lcd_wcom(0x38);
delay(5);//稳定
// lcd_wcom(0x01);//清屏
lcd_wcom(0x0c); //开显示,不显示光标
lcd_wcom(0x06); //写数据后地址自动后移
lcd_wcom(0x01); //显示指针起始位置
}
void LCD_Write_Char(uchar x,uchar y,uchar s)
{
if(x==1)
lcd_wcom(0x80+y);
else
lcd_wcom(0xc0+y);
lcd_wdat(s);
}
void LCD_Write_String(uchar x,uchar y,uchar *s)
{
if(x==1)
lcd_wcom(0x80+y);
else
lcd_wcom(0xc0+y);
while(*s)
{
lcd_wdat(*s);
s++;
}
}
void main (void)
{
System_Init();
lcd_init();
uchar n,m=0;
lcd_wcom(0x80);
for(m=0;m<16;m++)
{
lcd_wdat(table[m]);
delay(200);
}
lcd_wcom(0x80+0x44);
for(n=0;n<8;n++)
{
lcd_wdat(table1[n]);
delay(200);
}
while(1);
|