Bread board Challenge 2012
[BC12-02] Gattai LED Hotaru
#1 3-LED Module
(1) Parts
(2) Layout
(3) Schematic
1
( ) 3-
a
LED Module
( )
b Assembled Modules
(4) Firmware Source Code
// Gattai LED Hotaru
// INT0 : Falling Edge Int. by Optical transistor 0:Blight 1:Dark // INT1 : Falling Edge Int. by Aux Input
// PD0 : Buzzer
// PD1 : Aux.Signal Output
// PB0, PB1, PB2 : LED1,2,3 1:OFF 0:ON
#include <avr/io.h> #include <avr/interrupt.h>
// Light Pattern ID
#define P_STANDBY 0x00 #define P_SHIFT 0x01 #define P_BLINK 0x02
// Buzzer Control ID #define ON 0x01 #define OFF 0x00
// Initial Light Pattern
volatile int PATTERN = P_STANDBY;
void wait(volatile long i) {
while (i-- > 0); }
void buz(char c) // buzzer control {
switch (c) { case ON:
PORTD |= (1<<PD0); break;
case OFF:
PORTD &= ~(1<<PD0); break;
} }
void beep(int length, int height) // beep !
{
volatile long i, j;
for ( i = 0; i <length; i++) { buz(ON);
for ( j = 0; j < height; j++); buz(OFF);
for ( j = 0; j < height; j++); }
}
void led(int led1, int led2, int led3) // led light on / off
// led1,2,3 : 0...OFF, 1...ON {
PORTB |= (1<<PB0) | (1<<PB1) | (1<<PB2) ;
PORTB &= ~( (led1<<PB0) | (led2<<PB1) | (led3<<PB2) ); }
void auxout(void)
// Output Auxiliary Signal {
PORTD |= (1<<PD1); wait(100); PORTD &= ~(1<<PD1); wait(10000); }
void pattern(int no) // Light Pattern Control {
int i; switch (no) {
case P_SHIFT:
led(1, 0, 0); wait(10000); led(1, 1, 0); wait(10000); led(1, 1, 1); wait(10000); led(1, 1, 0); wait(10000); led(1, 0, 0); wait(10000); led(0, 0, 0); wait(10000); break;
case P_BLINK:
for ( i = 0; i < 3; i++) {
led(0, 0, 0); wait(20000); led(1, 1, 1); wait(20000); }
break; default:
led(0, 0, 0); wait(200000); }
}
ISR(INT0_vect)
// Interrupt 0 : Optical Transistor input {
beep(100, 10); switch (PATTERN) {
case P_STANDBY:
PATTERN = P_SHIFT; auxout();
break; default :
PATTERN = P_STANDBY; }
pattern(PATTERN);
}
ISR(INT1_vect)
// Interrupt 1 : Arrival Auxiliary Signal from Another Module {
beep(50, 5); switch (PATTERN) {
case P_STANDBY:
PATTERN = P_SHIFT; break;
case P_SHIFT:
PATTERN = P_BLINK; break;
case P_BLINK:
PATTERN = P_SHIFT; break;
}
pattern(PATTERN); auxout();
}
int main(void) {
// Initialize I/O Port PORTD = 0xff;
DDRD = (1<<PD0) | (1<<PD1) | (0<<PD2) | (0<<PD3); DDRB = (1<<PB0) | (1<<PB1) | (1<<PB2) | (1<<PB4);
// Intialize Interrupt MCUCR |= (1<<ISC01); MCUCR &= ~(1<<ISC00); GIMSK |= (1<<INT0);
MCUCR |= (1<<ISC11); MCUCR &= ~(1<<ISC10); GIMSK |= (1<<INT1);
// STANDBY
led(1, 1, 1); wait(10000);
// Interrupt Enable sei();
// Start Sequence beep(400, 2); while (1); return 0; }