#include <p18f4520.h>
#include <delays.h>
#define KBD_DA PORTCbits.RC6
#define KBD_OE PORTCbits.RC5
#define KBD_DATA PORTC
#define KBD_ENABLE_OUTPUT KBD_OE = 0;
#define KBD_DISABLE_OUTPUT KBD_OE = 0;
unsigned kbd_dec[4] =
{
0x0,
0x1,
0x2,
0x4,
};
void main()
{
unsigned char kbd_data = 0, choice = 0, prev_kbd_data = 0;
unsigned char matches = 0;
int i = 0;
PORTCbits.RC0=0; // Motor off initially.
TRISC=0xff;
KBD_ENABLE_OUTPUT;
while(1)
{
if(KBD_DA==1) //De-bouncing
{
choice = 0;
prev_kbd_data = (KBD_DATA & 0x1E);
matches =0;
while(matches < 3)
{
Delay1KTCYx(50);
kbd_data = (KBD_DATA & 0x1E);
if(prev_kbd_data == kbd_data)
{
matches++;
}
else
{
matches = 0;
}
prev_kbd_data = kbd_data;
}
}
switch(kbd_data)
{
case 0:
PORTCbits.RC0=0; //T 10 PULSE
break;
case 1:
while(i<100)
{
PORTCbits.RC0=0; //T 10 PULSE
Delay100TCYx(90); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay100TCYx(10); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 2:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 3:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
case 4:
while(i<100)
{
PORTCbits.RC0=0; //T 20 PULSE
Delay10TCYx(100); //900?s delay
PORTCbits.RC0=1; //T HIGH PULSE
Delay10KTCYx(20); //100?s delay
i++;
}
PORTCbits.RC0=0; //T 10 PULSE
break;
}
}