#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 RA0 // Pre- defined in pic.h
unsigned char TABLE1[20] ={ // 10 components
0b10000000, 10,
0b11000000, 10,
0b01100000, 10,
0b00110000, 10,
0b00011000, 10,
0b00001100, 10,
0b00000110, 10,
0b00000011, 10,
0b00000001, 10,
0b00000000, 20};
unsigned char TABLE2[32] ={ // 16 components
0b00000001, 10,
0b00000010, 10,
0b00000100, 10,
0b00001000, 10,
0b00010000, 10,
0b00100000, 10,
0b01000000, 10,
0b10000000, 10,
0b01000000, 10,
0b00100000, 10,
0b00010000, 10,
0b00001000, 10,
0b00000100, 10,
0b00000010, 10,
0b00000001, 10,
0b00000000, 10};
unsigned char TABLE3[4] ={ // 2 components
0b00001111, 20,
0b11110000, 20};
unsigned char TABLE4[4] ={ // 2 components
0b00000001, 25,
0b00000010, 25};
unsigned char TABLE5[20] ={ // 10 components
0b00000001, 10,
0b00000011, 10,
0b00000110, 10,
0b00001100, 10,
0b00011000, 10,
0b00110000, 10,
0b01100000, 10,
0b11000000, 10,
0b10000000, 10,
0b00000000, 20};
void Delay(int speed);
void main(void) // program entry
{
char index;
char mode = 0; // table to use
char size = 0; // new loop variable
unsigned char *tblptr;
CMCON = 0x7; // Comparitors off
SWTRIS = 0b11111111; // Switch port as inputs LED's as outpts
LEDTRIS = 0b00000000;
tblptr = TABLE1;
size = sizeof(TABLE1);
while(1) // Loop forever
{
for(index = 0; index < size ; index+=2)
{
if(!RA0)
{
Delay(20);
while(!RA0);
if(++mode == 5) mode = 0;
index = 0;
switch(mode)
{
case 0:
tblptr = TABLE1;
size = sizeof(TABLE1);
break;
case 1:
tblptr = TABLE2;
size = sizeof(TABLE2);
break;
case 2:
tblptr = TABLE3;
size = sizeof(TABLE3);
break;
case 3:
tblptr = TABLE4;
size = sizeof(TABLE4);
break;
case 4:
tblptr = TABLE5;
size = sizeof(TABLE5);
break;
}
}
PORTB = tblptr[index];
Delay(tblptr[index+1] * 10);
}
}
}
void Delay(int speed) // required to simulate Nigels tutorial
{
while(speed--)
{
__delay_ms(1); // Every 1 ms
}
}
// End!!