// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#define _XTAL_FREQ 4000000
__bit busy;
#define RS RA5
#define RW RA4
#define E RA3
#define PSB RA2
#define nRST RA1
void init(void);
void BusyCheck(void);
void WriteCmd(unsigned char cmd);
void WriteData(unsigned char dat);
void SetPos(unsigned char x, unsigned char y);
void Dis_Str(unsigned char *s);
void init_LCD(void);
void main(void)
{
unsigned char sec = 0;
init();
init_LCD();
SetPos(0,1);
Dis_Str("第一行,第二格");
SetPos(2,2);
Dis_Str("time:");
while(1)
{
SetPos(2,6);
__delay_ms(1000);
sec++;
if(sec > 60)
sec = 0;
WriteData(sec / 10 + '0');
WriteData(sec % 10 + '0');
}
}
void init(void)
{
PORTA = 0x02;
TRISA = 0x00;
ADCON1 = 0x06;
PORTD = 0x00;
TRISD = 0x00;
}
void BusyCheck(void)
{
busy = 1;
unsigned char temp;
TRISD = 0xFF; //数据口设为输入
RS = 0;
RW = 1;
while(busy)
{
E = 1;
temp = PORTD; //读取DB7
E = 0;
if((temp & 0x80) == 0)
{
TRISD = 0x00;
busy = 0;
break; //照理说这里的brake语句可以跳出循环,但是不行,必须用busy变量的方法,原因不明。
}
}
}
void WriteCmd(unsigned char cmd)
{
BusyCheck();
RS = 0;
RW = 0;
E = 1;
PORTD = cmd;
E = 0;
}
void WriteData(unsigned char dat)
{
BusyCheck();
RS = 1;
RW = 0;
E = 1;
PORTD = dat;
E = 0;
}
void SetPos(unsigned char x, unsigned char y)
{
unsigned char pos;
switch(x)
{
case 0:x = 0x80;break;
case 1:x = 0x90;break;
case 2:x = 0x88;break;
case 3:x = 0x98;break;
default:break;
}
pos = x + y;
WriteCmd(pos);
}
void Dis_Str(unsigned char *s)
{
while(*s)
WriteData(*s++);
}
void init_LCD(void)
{
nRST = 1;
PSB = 1;
WriteCmd(0x30);
WriteCmd(0x0C);
WriteCmd(0x01);
__delay_ms(1000);
}
|