#include <pic.h> // pic specific identifiers
#define _XTAL_FREQ 4000000 // Xtal speed
__CONFIG(0x3D18); // Config bits
#define LEDPORT PORTB
#define LEDTRIS TRISB
#define SWTRIS TRISA
#define SWPORT PORTA
#define SW1 RA7 // Pre- defined in pic.h
#define SW2 RA6
#define SW3 RA5
#define SW4 RA4
#define LED1 RA3 // Led's
#define LED2 RA2
#define LED3 RA1
#define LED4 RA0
void Dly(void); // C requires function
void chkkey(void); // prototypes
unsigned char TABLE1[14] ={ // 14 components pattern 1
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000};
unsigned char TABLE2[14] ={ // 14 components pattern 2
0b11000000,
0b01100000,
0b00110000,
0b00011000,
0b00001100,
0b00000110,
0b00000011,
0b00000011,
0b00000110,
0b00001100,
0b00011000,
0b00110000,
0b01100000,
0b11000000};
unsigned char TABLE3[14] ={ // 14 components pattern 3
0b01111111,
0b10111111,
0b11011111,
0b11101111,
0b11110111,
0b11111011,
0b11111101,
0b11111110,
0b11111101,
0b11111011,
0b11110111,
0b11101111,
0b11011111,
0b10111111};
unsigned char TABLE4[14] ={ // 14 components pattern 4
0b00111111,
0b10011111,
0b11001111,
0b11100111,
0b11110011,
0b11111001,
0b11111100,
0b11111100,
0b11111001,
0b11110011,
0b11100111,
0b11001111,
0b10011111,
0b00111111};
void main(void) // program entry
{
char index = 0; // new loop variable
CMCON = 0x7; // Comparitors off
SWTRIS = 0b11110000; // Switch port as inputs LED's as outpts
LEDTRIS = 0b00000000; // Led port as outputs
SWPORT = 0x0;
LEDPORT = 0x0;
LED1 = 0x1; // Set initial pattern
while(1) // Loop forever
{
for(index=0;index<14;index++) // loop through tables
{
if(LED1)
LEDPORT = TABLE1[index]; // Check which Led
if(LED2)
LEDPORT = TABLE2[index]; // is lit and
if(LED3)
LEDPORT = TABLE3[index]; // get pattern from
if(LED4)
LEDPORT = TABLE4[index]; // table
Dly();
}
}
}
void Dly() // required to simulate Nigels tutorial
{
int index = 250; // 250 ms
while(index--)
{
__delay_ms(1); // Every 1 ms
chkkey(); // scan keys
}
}
void chkkey()
{
if(!SW1) // Switch 1 on
{
SWPORT = 0x0; // Clear port
LED1 = 0x1; // led 1 on
}
if(!SW2) // Switch 2 on
{
SWPORT = 0x0; // Clear port
LED2 = 0x1; // led 2 on
}
if(!SW3) // Switch 3 on
{
SWPORT = 0x0; // Clear port
LED3 = 0x1; // led 3 on
}
if(!SW4) // Switch 4 on
{
SWPORT = 0x0; // Clear port
LED4 = 0x1; // led 4 on
}
} // End!!