]> mj.ucw.cz Git - misc.git/blob - sphinx/sphinx.c
Sphinx: init
[misc.git] / sphinx / sphinx.c
1 /*
2  *      Asking Riddles at the Entrance to Garage
3  *
4  *      (c) 2017 Martin Mares <mj@ucw.cz>
5  */
6
7 /*
8  *      Pin assignment (component side view; "*" marks inverted signals)
9  *
10  *                      +-------------------+
11  *                      | RESET*        VCC |
12  *      button          | PB3           SCK |
13  *      output          | PB4          MISO |
14  *                      | GND      PB0=MOSI |   diagnostic LED*
15  *                      +-------------------+
16  */
17
18 #define F_CPU 1200000UL
19
20 #include <avr/io.h>
21 #include <avr/sleep.h>
22 #include <util/delay.h>
23
24 typedef uint8_t byte;
25
26 #define B(x) (1U<<(x))
27
28 static void sleep(uint16_t millisec)
29 {
30         while (millisec) {
31                 _delay_ms(1);
32                 millisec--;
33         }
34 }
35
36 int main(void)
37 {
38         DDRB |= B(PB0);                 // PB0: output
39         DDRB |= B(PB4);                 // PB4: output
40         PORTB &= ~B(PB3);               // PB3: input, no pullup
41
42         for (byte i=0; i<5; i++) {
43                 PORTB &= ~B(PB0);
44                 sleep(100);
45                 PORTB |= B(PB0);
46                 sleep(100);
47         }
48
49         for (;;) {
50                 sleep(10);
51                 if (PINB & B(PB3)) {
52                         PORTB &= ~B(PB0);
53                         PORTB |= B(PB4);
54                 } else {
55                         PORTB |= B(PB0);
56                         PORTB &= ~B(PB4);
57                 }
58         }
59
60         for (;;) {
61                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
62                 sleep_mode();
63         }
64 }