كود:
// LED Light Sequencer
// PIC16F628 RC CLOCK
// Change for different length patterns.
#define PATTERNLENGTH 221
// Declare functions
unsigned short int getPattern(unsigned short int index);
////////////////////////////////////
unsigned short int count; // Declare loop counter
/////////////////////////////////////
// Main program loop
void main()
{
CMCON = 0x07; // To turn off comparators
TRISB=0; // Set all Port B to outputs
TRISA.F0=1;
PORTB=0;
while(1) // Loop forever
{
if(Button(&PORTA,0,20,1)){
PORTB=0b11111111;
delay_ms(250);
PORTB=0b00000000;
delay_ms(250);
}
else{
// Loop from 0 to PATTERNLENGTH-1
for(count = 0; count < PATTERNLENGTH; count++){
// Get the next pattern and output to LEDs
PORTB = getPattern(count);
// Pause for the specified number of ms (up to 65535)
delay_ms(250);
}
}
}
}
//////////////////////////////////////////////
// getPattern function: returns the index-th output pattern
// in the desired sequence
unsigned short int getPattern(unsigned short int index)
{
const unsigned short int patternArray[PATTERNLENGTH] = {
///////1A-moving from right to left ////////////////////
0b00000001,// For each pattern in the sequence,
0b00000010,// 1 = light on, 0 = off
0b00000100,// Any combination of lights can be on or off
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
///////1B-moving from right to left ///////
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
///////////2A-moving from left to right ////////
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
///////////2B-moving from left to right ////////
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
////////////3A-moving from left to right then moving from left to right ////////
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001,
0b00000000,
//3B-moving from left to right then moving from left to right//
0b00000001,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00100000,
0b01000000,
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000000,
////////4-moving from center to edges//////////////////
0b00011000,
0b00100100,
0b01000010,
0b10000001,
0b00000000,
/////////5-moving from edges to center ////////
0b10000001,
0b01000010,
0b00100100,
0b00011000,
0b00000000,
//////////6A///////////////////////
0b00011000,
0b00100100,
0b01000010,
0b10000001,
0b01000010,
0b00100100,
0b00011000,
0b00000000,
/////////////6B/////////////////
0b00011000,
0b00100100,
0b01000010,
0b10000001,
0b01000010,
0b00100100,
////////7/////////////////////
0b00000000,
0b10000000,
0b11000000,
0b11100000,
0b11110000,
0b01111000,
0b00111100,
0b00011110,
0b00001111,
0b00000111,
0b00000011,
0b00000001,
0b00000000,
//////////8///////////
0b00000000,
0b00000001,
0b00000011,
0b00000111,
0b00001111,
0b00011110,
0b00111100,
0b01111000,
0b11110000,
0b11100000,
0b11000000,
0b10000000,
0b00000000,
///////////9////////////////
0b00000001,
0b00000100,
0b00000010,
0b00001000,
0b00000100,
0b00010000,
0b00001000,
0b00100000,
0b00010000,
0b01000000,
0b00100000,
0b10000000,
0b01000000,
0b00000000,
///////////10///////////////
0b00000000,
0b01000000,
0b10000000,
0b00100000,
0b01000000,
0b00010000,
0b00100000,
0b00001000,
0b00010000,
0b00000100,
0b00001000,
0b00000010,
0b00000100,
0b00000001,
0b00000000,
///////11A////////////////////
0b11000000,
0b00110000,
0b00001100,
0b00000011,
0b00001100,
0b00110000,
0b11000000,
0b00000000,
//////////////11B////////////
0b00110000,
0b00001100,
0b00000011,
0b00001100,
0b00110000,
/////////////12///////////////////
0b00000000,
0b11000000,
0b00110000,
0b00001100,
0b00000011,
0b00000000,
/////////////13//////////////
0b00000000,
0b00000011,
0b00001100,
0b00110000,
0b11000000,
0b00000000,
//////////////14A////////////
0b00000000,
0b00001111,
0b11110000,
0b00001111,
0b11110000,
0b00000000,
///////////14B////////////////////////
0b00001111,
0b11110000,
//////////////15A////////////////////
0b00000000,
0b01010101,
0b10101010,
0b01010101,
0b10101010,
0b00000000,
////////////15B////////////////////
0b01010101,
0b10101010,
//////////////16////////////////
0b00000001, // For each pattern in the sequence,
0b00000011, // 1 = light on, 0 = off
0b00000111, // Any combination of lights can be on or off
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
///////////////
0b01111111,
0b00111111,
0b00011111,
0b00001111,
0b00000111,
0b00000011,
0b00000001,
0b00000000,
///////////17//////////
0b00000001, // For each pattern in the sequence,
0b00000011, // 1 = light on, 0 = off
0b00000111, // Any combination of lights can be on or off
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
/////////////////////
0b11111110,
0b11111100,
0b11111000,
0b11110000,
0b11100000,
0b11000000,
0b10000000,
0b00000000,
///////////18///////////
0b11111111, // For each pattern in the sequence,
0b00000000, // 1 = light on, 0 = off
0b11111111, // Any combination of lights can be on or off
0b00000000,
0b11111111, // NB: Length of this list must be
0b00000000, // the same as PATTERNLENGTH
//////////////////
};
return patternArray[index];
}