Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

PIC16F877A LCD CODE

#include<pic.h>

#include<pic1687x.h>

__CONFIG(HS & WDTDIS &    PWRTDIS & LVPDIS &     DUNPROT & WRTDIS &    DEBUGDIS);

static bit Rd        @((unsigned)&PORTE*8+0);    //lcdRs

static bit Wr        @((unsigned)&PORTE*8+1);    //lcdRw

static bit lcdEn    @((unsigned)&PORTC*8+0);

static bit lcdLed    @((unsigned)&PORTB*8+5);   

static bit ppiEn    @((unsigned)&PORTE*8+2);

static bit A0        @((unsigned)&PORTA*8+4);

static bit A1        @((unsigned)&PORTA*8+5);

static bit    ALED0    @((unsigned)&PORTB*8+1);

static bit    ALED1    @((unsigned)&PORTB*8+2);

static bit    ALED2    @((unsigned)&PORTB*8+3);

static bit    ALED3    @((unsigned)&PORTB*8+4);

unsigned char sec, min, hour, day, month, year;

//=================================LCD======================================

//LCD Procedure

void lcdWrCmd(unsigned char ch);

void lcdInit(void);

void lcdWrDat(unsigned char ch);

void lcdWrInt(unsigned int val);

void lcdGoHome (void);        // Move cursor go to home;

void lcdGoLeft (void);      // Move cursor left

void lcdGoRight (void);     // Move cursor right

void lcdGoPos (unsigned char line, unsigned char pos_of_line);

void lcdDownLine(void);

void lcdClr(void);

void lcdWrStr(unsigned char s[]);

//=====================================i2c=================================

void init();

void delay(unsigned int d);

void tmr1Init(void);

//8255 Procedure

void ppiInit(void);

void ppiWrite(unsigned char data, unsigned char ch);

unsigned char ppiRead(unsigned char ch);

unsigned char bank1 buffer, mask, START = 0;

unsigned int n;

unsigned char segTable[11] = {0xEB,0x28,0xB3,0xBA,0x78,0xDA,0xDB,0xA8,0xFB,0xFA,0x10};

/*

* * * * * * *

*     (7)    *

*           *

*(6)     (5)*

*           *

*    (4)    *

* * * * * * *

*             *

*           *

*(0)     (3)*

*           *

*    (1)    *

* * * * * * *  *(2)

*/

//============MAIN=================================

void main()

{   

    unsigned char lcdVal;

    unsigned char m[] = "Tan phat Automation";

    unsigned char n[] = "Segment Led Tester";

    buffer = 0x01;

    lcdInit();

    tmr1Init();

    ALED0 = 0;

    lcdLed = 1;

    lcdWrStr(m);

    delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);

    lcdGoPos(1,0);    lcdWrStr(n);

    delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);

    GIE = 1;

    while(1)

    {

        while(lcdVal==buffer){}

        lcdClr();

        lcdVal = buffer;

        lcdWrInt(lcdVal);

    }

}

static void interrupt isr(void)            // Here be interrupt function - the name is

{

    if(TMR1IF)

    {

        if(lcdEn==1)    lcdEn = 0;   

        if(n<150)    n++;

        else

        {

            n = 0;

            if(buffer==0)    buffer = 0x01;

            else            buffer <<= 1;

        }

        if(START==0)

        {

            ppiWrite(0x81,'W'); //PA, PB, Upper Nible of PC are Outputs, Lower Nible of PC are Inputs

            START = 1;

        }

        TMR1L = 0x3C;

        TMR1H = 0xF6;

        TMR1IF = 0;

        if(ALED3==0)

        {

            ALED3 = 1;

            ppiWrite(buffer,'B');

            ALED0 = 0;

        }

        else

        {

            if(ALED0==0)

            {

                ALED0 = 1;

                ppiWrite(buffer,'B');

                ALED1 = 0;

            }

            else

            {

                if(ALED1==0)

                {

                    ALED1 = 1;

                    ppiWrite(buffer,'B');

                    ALED2 = 0;

                }

                else

                {

                    ALED2 = 1;   

                    ppiWrite(buffer,'B');

                    ALED3 = 0;

                }

            }

        }

    }

}

//=================================KHAOI TAO HE THONG=======================

void init()

{

    ADCON1 = 0x07;   

    TRISA = 0; //config ports are output

    TRISE = 0;

    TRISC = 0;  

    TRISB = 0;

    TRISD = 0; 

    ALED0 = ALED1 = ALED2 = ALED3 = 1;

}

void tmr1Init(void)

{

    T1CON = 0b00110000;        //Fin = 11.059.200/(4*8) = 345600Hz

    TMR1L = 0x48;

    TMR1H = 0xF4;

    TMR1IE = 1;   

    PEIE = 1;

    TMR1ON = 1;

}

void ppiInit(void)

{

    ppiWrite(0x81,'W'); //PA, PB, Upper Nible of PC are Outputs, Lower Nible of PC are Inputs

}

void ppiWrite(unsigned char data, char ch)

{

    TRISD = 0;

    Rd = 1;   

    lcdEn = 0;

    ppiEn = 0;

    switch(ch)

    {

    case 'A': //PortA   

        A0 = 0;

        A1 = 0;

        break;

    case 'B': //PortB

        A0 = 1;

        A1 = 0;

        break;

    case 'C': //PortC

        A0 = 0;

        A1 = 1;

        break;

    case 'W': //CW

        A0 = 1;

        A1 = 1;

    }

    PORTD = data;

    Wr = 0;

    Wr = 1;

    ppiEn = 1;

    TRISD = 0xff;

}

unsigned char ppiRead(char ch)

{

    unsigned char data;  

    TRISD = 0xff;

    Wr = 1; 

    lcdEn = 0;

    ppiEn = 0;

    switch(ch)

    {

    case 'A': //PortA   

        A0 = 0;

        A1 = 0;

        break;

    case 'B': //PortB

        A0 = 1;

        A1 = 0;

        break;

    case 'C': //PortC

        A0 = 0;

        A1 = 1;

        break;

    case 'W': //CW

        A0 = 1;

    A1 = 1;

    }

    Rd = 0;

    Rd = 1;

    data = PORTD;

    ppiEn = 1;

    return data;

}

//========================================LCD===============================

void delay(unsigned int d)

{

    while(d--){};

}

//==============================================

void    delayns(unsigned int n)

{

    unsigned int i;

    for(i=0;i<n;i++);

}

//===============================================

void lcdWrCmd(unsigned char ch)

{

    TRISD = 0;

    ppiEn = 1;

    delay(180);

    Rd = 0;    

     Wr = 0;

    PORTD = ch;

     lcdEn = 1;

    lcdEn=0;

    TRISD = 0xff;

}

//================================================

void lcdWrDat(unsigned char ch)

{   

    TRISD = 0;

    ppiEn = 1;

    delay(180);

    Rd = 1;    

     Wr = 0;

    PORTD = ch;

     lcdEn = 1;

    lcdEn=0;

    TRISD = 0xff;

}

//-------------------------------------------

void lcdWrStr(unsigned char s[])

{

    unsigned char i = 0;

    while(s[i]!='\0')    lcdWrDat(s[i++]);

}

//------------------------------------------

void lcdInit(void)

{     

    TRISD = 0;

    ppiEn = 1;

    delayns(0xFFFF);

    Rd = 0;     // RS = 0;  Instruction

    Wr = 0;        // RW = 0;  Write to

    lcdEn = 1;        // E = 0;   Enable   

    PORTD = 0x38;

    lcdEn    = 1;

    delayns(100);                               

    lcdEn    = 0;

    delayns(100);                             

    lcdWrCmd(0x0C);   // Display ON, Cursor off, no blink

    delayns(10);

    lcdWrCmd(0x01);   // Clear Display

    delayns(10);

    lcdWrCmd(0x80);   // Set Cursor to top left

    delayns(100); 

    TRISD = 0xff;

}

//-------------------------------------------------------------

void lcdWrInt(unsigned int val)

{

    unsigned char i, j, num[5];

    i = 0;

    while(val > 9)

    {

        num[i] = val%10 + 0x30;

        val = val/10;

        i++;

    }

    num[i] = val + 0x30;

    for(j = 0; j <= i; j++)

    {

        lcdWrDat(num[i - j]);

    }

}

//-------------------------------------------------------------

void lcdGoHome (void)    // Move cursor go to home;

{

    lcdWrCmd (0x02);    //  Cursor go home        

}

//-------------------------------------------------------------

void lcdGoLeft (void)    // Move cursor left

{

    lcdWrCmd (0x10);    //  Cursor go left           

}

//-------------------------------------------------------------

void lcdGoRight (void)   // Move cursor right

{

    lcdWrCmd (0x14);    //  Cursor go home           

}

//-------------------------------------------------------------

void lcdGoPos (unsigned char line, unsigned char pos_of_line)// Move cursor to posistion of a line

{

    unsigned char DDRAMAddr;

    if (line==0) DDRAMAddr = pos_of_line;

    else DDRAMAddr = 0x40+pos_of_line;

    lcdWrCmd (0x80+DDRAMAddr);               

}

//-------------------------------------------------------------

void lcdDownLine(void)

{

    lcdWrCmd(0xC0);

}

//-------------------------------------------------------------

void lcdClr(void)

{

    lcdWrCmd (0x01);    //  Cursor go home 

    lcdGoHome();      

}

//==============================================================

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: