0

I'm taking a Microcontroller course this semester, and I have an assignment to make a digital clock using PIC18 and display it on LCD. My code is in C and I'm protues to simulate.

I wrote a code but something is wrong if anyone can help me figure out my mistake.. Thank you

    #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);
    }
}
  • *"something is wrong"*! What is wrong? What happens, what does not happen? Does it compile? Does it run? Does it display a time? Is the time wrong? Does it catch fire?? – Weather Vane May 08 '15 at 19:11

2 Answers2

0

Its better to display digital clock using 7-segment display than in LCD.

0

use this code in your program and check... for lcddata(mscounter);

           use *thous(mscounter);*

declare it in the code and check lcdcmd(0x08); instead use 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);

           }
Bk Francis
  • 13
  • 7