KEYPAD C

#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 portInit();

void delay(unsigned int d);

unsigned char colScan(unsigned char channel);

unsigned char keyFilter(unsigned char num, unsigned char channel);

unsigned char keyScan(void);

unsigned char freeHand(void);

/////////////////////////////////////////////////////////////

void tmr1Init(void);

//8255 Procedure

void ppiInit(void);

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

unsigned char ppiRead(unsigned char ch);

unsigned char buffer, mask, START = 0;

unsigned int i, 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    m[] = "Tan phat Automation";

    unsigned char     n[] = "Test KeyPad";

    unsigned char keybuff;

    buffer = 0x01;

    portInit();     

    lcdInit();

    tmr1Init();

    ALED0 = 0;

    lcdLed = 1;

    ppiWrite(0xAA,'B');

    lcdWrStr(m);

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

    lcdGoPos(1,0);

    lcdWrStr(n);

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

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

    GIE = 0;

    ppiWrite(0x0F,'C');

    A0 = 1;    A1 = 1;

    while(1)

    {

        keybuff = keyScan();

        while(keybuff==255)    keybuff = keyScan();       

        lcdClr();    lcdWrInt(keybuff);

        while(freeHand()==0){}

    }

}

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

{

    if(TMR1IF)

    {

        TMR1L = 0x3C;

        TMR1H = 0xF6;

        TMR1IF = 0;

    }

}

unsigned char colScan(unsigned char channel)

{

    unsigned char buffer,buff,i = 255;

    switch(channel)

    {

        case 0: ppiWrite(0xE0,'C');    break;

        case 1: ppiWrite(0xD0,'C'); break;

        case 2: ppiWrite(0xB0,'C'); break;

        case 3: ppiWrite(0x70,'C');

    }

    buffer = ppiRead('C')&0x0F;

    if(buffer!=0x0F)

    {

        while(i--){}

        buff = ppiRead('C')&0x0F;

        if(buffer!=buff) buffer = 0xEE;

    }

    else    buffer = 0xFF;

    return buffer;

}

unsigned char keyFilter(unsigned char num, unsigned char channel)

{

    unsigned char buffer;

    switch(num)

    {

        case 14: buffer = 1; break;   

        case 13: buffer = 2; break;

        case 11: buffer = 3; break;

        case 7:     buffer = 13; break;

        default: buffer = 0xEE;

    }

    if(buffer!=0xEE)

    {

        buffer = buffer+channel*3;

        if(buffer==11)     buffer = 0;

    }

    return buffer;

}

unsigned char keyScan(void)

{

    unsigned char buffer, buff, i, n;

    i = 0;

    while(i<4)

    {

        buffer = colScan(i);

        if(buffer!=0xff)

        {

            n = i;

            i = 5;

        }

        else    i++;

    }

    if(i==5)

    {

        i = n+1;

        while(i<4)

        {

            buff = colScan(i);

            if(buff!=0xff)

            {

                i = 5;

                buffer = 0xee;

            }

            else    i++;

        }

     }

    if((buffer!=0xff)&&(buffer!=0xee)) buffer = keyFilter(buffer,n);

    return buffer;

}

unsigned char freeHand(void)

{

    ppiWrite(0x00,'C');

    buffer = ppiRead('C')&0x0F;

    if(buffer==0x0F)    return 1;

    else                return 0;

}   

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

void portInit()

{

    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: AzTruyen.Top

Tags: