1- روتينات الدوال :
أ- دالة التهيئة
كود:
//initialize program
كود:
void init()
{
ADCON1=0X07; //set A PORT general I/O PORT
TRISA =0X0F; //A PORT high 2 bits OUTPUT,low 4 bits INPUT
TRISB=0X0F; //B PORT low 5 bits INPUT
TRISD=0X00; //D PORT OUTPUT
PORTA=0XFF;
PORTD=0XFF; //close all display
}
//------------------------------------------------
ب- دالة المسح :
كود:
//------------------------------------------------
كود:
//Scan
void scan()
{
result=0x8; //initialize key scan result
if(PORTA.B0==0) // judge if a0 press
result=0x0; //yes£¬the scan result is 0
if(PORTA.B1==0) // judge if a1 press
result=0x1; //yes£¬the scan result is 1
if(PORTA.B2==0) // judge if a2 press
result=0x2;
if(PORTA.B3==0) // judge if a3 press
result=0x3;
if(PORTB.B0==0) //judge if B0 press
result=0x4;
if(PORTB.B1==0) //judge if B1 press
result=0x5;
if(PORTB.B2==0) //judge if B2 press
result=0x6;
if(PORTB.B3==0) //judge if B3 press
result=0x7;
}
ت- دالة العرض :
كود:
//display result program
كود:
void display(int x)
{
switch(x) //different x,different deal
{
case 0x0:
PORTD=0X88;PORTA=0X2F;Delay_ms(1);PORTD=0XC0;PORTA=0X1F;Delay_ms(1);break; //A0
case 0x1:
PORTD=0X88;PORTA=0X2F;Delay_ms(1);PORTD=0XF9;PORTA=0X1F;Delay_ms(1);break; //A1
case 0x2:
PORTD=0X88;PORTA=0X2F;Delay_ms(1);PORTD=0XA4;PORTA=0X1F;Delay_ms(1);break; //A2
case 0x3:
PORTD=0X88;PORTA=0X2F;Delay_ms(1);PORTD=0XB0;PORTA=0X1F;Delay_ms(1);break; //A3
case 0x4:
PORTD=0X83;PORTA=0X2F;Delay_ms(1);PORTD=0XC0;PORTA=0X1F;Delay_ms(1);break; //B0
case 0x5:
PORTD=0X83;PORTA=0X2F;Delay_ms(1);PORTD=0XF9;PORTA=0X1F;Delay_ms(1);break; //B1
case 0x6:
PORTD=0X83;PORTA=0X2F;Delay_ms(1);PORTD=0XA4;PORTA=0X1F;Delay_ms(1);break; //B2
case 0x7:
PORTD=0X83;PORTA=0X2F;Delay_ms(1);PORTD=0XB0;PORTA=0X1F;Delay_ms(1);break; //B3
case 0x8:
PORTD=0X8E;PORTA=0X2F;Delay_ms(1);PORTD=0X8E;PORTA=0X1F;Delay_ms(1);break; //no key press
}
}