【问题标题】:Digital Clock C code / PIC, Protues数字时钟 C 代码/PIC、Proteus
【发布时间】:2015-05-08 08:47:25
【问题描述】:

这学期我正在学习微控制器课程,我的任务是使用 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);
    }
}

【问题讨论】:

  • “出了点问题”!怎么了?会发生什么,不会发生什么?它编译吗?它运行吗?是否显示时间?时间错了吗?会不会着火??

标签: microcontroller digital lcd pic18


【解决方案1】:

使用 7 段显示器显示数字时钟比使用 LCD 显示更好。

【讨论】:

    【解决方案2】:

    在您的程序中使用此代码并检查... 对于 lcddata(mscounter);

               use *thous(mscounter);*
    

    在代码中声明并检查 lcdcmd(0x08);改为使用 lcdcmd(0x80);

             void thous(unsigned int count)
                       {
                lcddata((count/1000)+0x30);
                 lcddata(((count/100)%10)+0x30);
                    lcddata(((count%100)/10)+0x30);
                    lcddata((count%10)+0x30);
    
               }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      相关资源
      最近更新 更多