这学期我要学习微控制器课程,我有一个任务是使用 PIC18 制作一个数字时钟并将其显示在 LCD 上。我的代码在 C 中,我很想模拟。
我写了一个代码,但如果有人能帮我找出我的错误,那就错了..谢谢
#include <P18F4580.h>
#define ldata PORTD
#define rs PORTBbits.RB0
#define rw PORTBbits.RB1
#define en PORTBbits.RB2
void msdelay(unsigned int itime)
{ unsigned int i,j;
for (i=0; i<itime; i++)
for (j=0; j<135; j++);
}
void lcdcmd(unsigned char value)
{ ldata = value;
rs = 0;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void lcddata(unsigned char value)
{ ldata = value;
rs = 1;
rw = 0;
en = 1;
msdelay(1);
en = 0;
}
void main()
{
TRISD = 0;
TRISB = 0;
en = 0;
int msCounter =0;
int secCounter =0;
int minCounter =0;
int hrCounter =0;
msdelay(15);
lcdcmd(0x01); //Clear display
msdelay(15);
lcdcmd(0x02); //Home cursor
msdelay(15);
lcdcmd(0x06); //Left to right, still
msdelay(250);
lcdcmd(0x0E); //display cursor
msdelay(250);
lcdcmd(0x3C); //5x10 2 lines
msdelay(15);
lcdcmd(0x86);
while(1)
{
msdelay(15);
lcdcmd(0x08);
lcddata(secCounter);
msdelay(15);
msCounter++;
if (msCounter==1000)
{secCounter++; msCounter=0; }
if (secCounter==60)
{minCounter++; secCounter=0; }
if (minCounter==60)
{hrCounter++; minCounter=0; }
if (hrCounter==24)
{hrCounter=0; }
msdelay(15);
lcddata(hrCounter);
msdelay(15);
lcddata(':');
msdelay(15);
lcddata(minCounter);
msdelay(15);
lcddata(':');
msdelay(15);
lcddata(secCounter);
}
}