..
//_____________Exo
#define F_CPU 1000000L
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
/******************************
- Pin and Setting Definitions *
******************************/
#define ledPort PORTD
#define ledDDR DDRD
#define BUTTON1 1 // button switch connected to port B pin 1
#define DEBOUNCE_TIME 500 // time to wait while "de-bouncing" button
#define LOCK_INPUT_TIME 2000 // time to wait after a button press
//Milliseconds delay in sweep mode
#define sweepDelay 500
//Milliseconds delay in flasher mode
#define flasherDelay 2000
/*************
- Prototypes *
*************/
void initIO(void);
void delay_ms(int cnt);
void sweep(unsigned char times);
void turn_off_1(unsigned char times);
void turn_off_2(unsigned char times);
void left(unsigned char times);
void right(unsigned char times);
void flasher(unsigned char times);
/************
- Functions *
************/
//Setup the I/O for the LEDs
void initIO(void)
{
ledDDR |= 0xFF; //Set PortD pins as an outputs
ledPort |= 0xFF; //Set PortD pins high to turn on LEDs
DDRB &= ~(1<<BUTTON1);//Makes first pin of PORTB as Input
PORTB = 0xFF; // Set all pins of the PORTB as HIGH. the internal Pull Up resistor of first pin PORTB is enable.
}
//Delay a number of milliseconds
void delay_ms(int cnt)
{
while (cnt-- > 0) _delay_ms(1);
}
unsigned char button_state()
{
/* the button is pressed when BUTTON1 bit is clear */
if (!(PINB & (1<<BUTTON1)))
{
_delay_ms(DEBOUNCE_TIME);
if (!(PINB & (1<<BUTTON1))) return 1;
}
return 0;
}
//Sweep one lighted LED back and forth
void sweep(unsigned char times)
{
//Start with Least Significant Bit illuminated
unsigned char tracker = 0x01; //Setup starting value
ledPort = tracker; //Output new values to LEDs
delay_ms(sweepDelay); //wait a bit
}
//turn off
void turn_off_1(unsigned char times)
{
for (int i=0; i<8; i++)
{
ledPort = (0b11111111<<i);
delay_ms(sweepDelay); //wait a bit
}}
//turn off
void turn_off_2(unsigned char times)
{
for (int i=0; i<8; i++)
{
ledPort = (0b11111111>>i) ;
delay_ms(sweepDelay); //wait a bit
}
}
//Shift bits left until 0b10000000 is reached
void left(unsigned char times)
{
while(ledPort < 0x80)
{
ledPort <<= 1; //Shift bits
//Output new values to LEDs
delay_ms(sweepDelay); //wait a bit
}}
//Shift bits right until 0b00000001 is reached
void right(unsigned char times)
{
while(ledPort > 0x01)
{
ledPort >>= 1; //Shift bits
//Output new values to LEDs
delay_ms(sweepDelay); //wait a bit
}}
//Flash in an XOR pattern
void flasher(unsigned char times)
{
unsigned char tracker = 0xAA; //Setup starting value
ledPort = tracker; //Lights every-other LED
delay_ms(flasherDelay); //wait a bit
while (times--)
{
ledPort ^= 0xFF; //Toggle all values
//Output new values to LEDs
delay_ms(flasherDelay); //wait a bit
;
}
}
int main (void)
{
unsigned char n_led = 1; // LED number is on now
initIO(); //Setup LED pins
while (1)
{
if (button_state()) // If the button is pressed, toggle the LED's state and delay for 300ms (#define LOCK_INPUT_TIME)
{
switch(ledDDR){
case 1:
sweep(1);
turn_off_1(1);
break;
case 2:
turn_off_1(1);
turn_off_2(1);
break;
case 3:
turn_off_2(1);
left(1);
break;
case 4:
left(1);
right(1);
break;
case 5:
right(2);
sweep(2);
ledPort = 0; //Turn off all LEDs
break;
}
n_led++; // next is turn on
_delay_ms(LOCK_INPUT_TIME);
}
}
return (0);
} lp