tweak voltage divider for my config
[digitaldcpower] / kbd.c
1 /* vim: set sw=8 ts=8 si : */
2 /*********************************************
3 * Author: Guido Socher, Copyright: GPL 
4
5 * read the keyboard
6 **********************************************/
7 #include <avr/io.h>
8 #define F_CPU 8000000UL  // 4 MHz
9 #include <util/delay.h>
10
11
12 // it looks like output port settings need time to propagate. Maybe
13 // caused by input capacitors on the lcd which connect to the same ports.
14 static void kbd_wait(void){
15         _delay_ms(7.5);
16 }
17
18 // kbd connection:
19 // U+=PC3, U-=PC4, I+,store=PB2, I-=PC5
20 // common input wires: PB5, PB3 for store
21 // PC3, PC4, PB2, PC5 are already initialized as output lines
22 // by the LCD driver code
23 void init_kbd(void) 
24 {
25         // init lcd sets those already so we comment them out here:
26         /*
27         DDRC|=(1<<DDC3); // output line
28         DDRC|=(1<<DDC4); // output line
29         DDRB|=(1<<DDB2); // output line
30         DDRC|=(1<<DDC5); // output line
31         */
32
33         DDRB&= ~(1<<DDB5); // input line
34         DDRB&= ~(1<<DDB3);
35
36         PORTB|= (1<<PINB5); // internal pullup resistor on
37         PORTB|= (1<<PINB3); // internal pullup resistor on
38 }
39
40 uint8_t check_u_button(int16_t *u) 
41 {
42         // check U+ button:
43         PORTC&=~(1<<PORTC3); // to gnd
44         PORTC|=(1<<PORTC4); // to vcc
45         PORTB|=(1<<PORTB2); // to vcc
46         PORTC|=(1<<PORTC5); // to vcc
47         kbd_wait();
48         if (bit_is_clear(PINB,PINB5)){
49                 (*u)++;
50                 return(1);
51         }
52         // check U- button:
53         PORTC|=(1<<PORTC3); // to vcc
54         PORTC&=~(1<<PORTC4); // to gnd
55         PORTB|=(1<<PORTB2); // to vcc
56         PORTC|=(1<<PORTC5); // to vcc
57         kbd_wait();
58         if (bit_is_clear(PINB,PINB5) && (*u)>0){
59                 (*u)--;
60                 if((*u)<0){
61                         (*u)=0;
62                 }
63                 return(1);
64         }
65         return(0);
66 }
67
68 uint8_t check_i_button(int16_t *i) 
69 {
70         // check I+ button:
71         PORTC|=(1<<PORTC3); // to vcc
72         PORTC|=(1<<PORTC4); // to vcc
73         PORTB&=~(1<<PORTB2); // to gnd
74         PORTC|=(1<<PORTC5); // to vcc
75         kbd_wait();
76         if (bit_is_clear(PINB,PINB5)){
77                 (*i)++;
78                 return(1);
79         }
80         // check I- button:
81         PORTC|=(1<<PORTC3); // to vcc
82         PORTC|=(1<<PORTC4); // to vcc
83         PORTB|=(1<<PORTB2); // to vcc
84         PORTC&=~(1<<PORTC5); // to gnd
85         kbd_wait();
86         if (bit_is_clear(PINB,PINB5) && (*i)>0){
87                 (*i)--;
88                 if((*i)<0){
89                         (*i)=0;
90                 }
91                 return(1);
92         }
93         return(0);
94 }
95
96 uint8_t check_store_button(void) 
97 {
98         // check store button:
99         PORTB&=~(1<<PORTB2); // to gnd
100         kbd_wait();
101         if (bit_is_clear(PINB,PINB3)){
102                 return(1);
103         }
104         return(0);
105 }