OSDN Git Service

[General] Add low-battery-save.
[openi2cradio/OpenI2CRadio.git] / ioports.c
1 /*
2  * OpenI2CRADIO
3  * I/O Port Handler
4  * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2,
9  *  or (at your option) any later version.
10  *  This library / program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *  See the GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this library; see the file COPYING. If not, write to the
17  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18  *  MA 02110-1301, USA.
19  *
20  *  As a special exception, if you link this(includeed from sdcc) library
21  *  with other files, some of which are compiled with SDCC,
22  *  to produce an executable, this library does not by itself cause
23  *  the resulting executable to be covered by the GNU General Public License.
24  *  This exception does not however invalidate any other reasons why
25  *  the executable file might be covered by the GNU General Public License.
26  */
27 #include "commondef.h"
28 #include "ioports.h"
29 #include <signal.h>
30
31 unsigned char statecount;
32
33 void set_amfmlamp(unsigned char f)
34 {
35     if(f == 0x00) {
36         _AM_STATLAMP = 0;
37         _FM_STATLAMP = 1;
38     } else {
39         _AM_STATLAMP = 1;
40         _FM_STATLAMP = 0;
41     }
42 }
43
44
45 void set_examp_mute(unsigned char f)
46 {
47     if(f == 0x00) {
48         _PORT_MUTE = 0; // Not Mute
49     } else {
50         _PORT_MUTE = 1;
51     }
52 }
53
54 void set_radiopower(unsigned char f)
55 {
56     if(f == 0x00) {
57         _PORT_RADIOPOW = 0; // OFF
58     } else {
59         _PORT_RADIOPOW = 1; // ON
60     }
61 }
62
63 void set_powerlamp(unsigned char f)
64 {
65     if(f == 0x00) {
66         _POW_STATLAMP = 0;
67     } else {
68         _POW_STATLAMP = 1;
69     }
70 }
71
72 void lcd_setbacklight(unsigned char flag, unsigned char level)
73 {
74     unsigned char b;
75 #if 1
76     b = _LCD_PORT;
77     if(flag == 0){
78         b &= ~_LCD_BACKLIGHT;
79     } else {
80         b |= _LCD_BACKLIGHT;
81     }
82     _LCD_PORT = b;
83 #else
84     if(flag == 0) {
85         T2CONbits.TMR2ON = 0;
86         TMR2 = 0x00;
87         CCP1CON &= 0x00;
88         CCPR1L = 0;
89     } else {
90       unsigned char h,l;
91       unsigned char b;
92       if(level > 100) level = 100;
93       h = (level & 0xfc) >> 2;
94       l = (level & 0x03) << 4;
95       b = CCP1CON & 0x0c;
96       b = b | 0x0c | l;
97       CCPR1L = h;
98       TMR2 = 0x00;
99       CCPR1L = h;
100        T2CONbits.TMR2ON = 1;
101 //      _LCD_PORT |= _LCD_BACKLIGHT;
102     }
103 #endif
104 }
105
106 void TMR3_set(void)
107 {
108     TMR3L = 1000 & 0xff;
109     TMR3H = 1000 >> 8;
110     T3CONbits.TMR3ON = 1;
111 }
112
113 void io_intcountinit(void)
114 {
115     statecount = 0;
116     T3CON = 0b10001100; // TMR3 = 16bit, CCP is not use TMR3, Prescaler=1:2(1uS)
117                         // DO-not-use SYNC, Internal clock, TMR3 temporally off.
118     PIR2bits.TMR3IF = 0;
119     PIE2bits.TMR3IE = 1;
120     IPR2bits.TMR3IP = 0; // Low priority.
121     INTCONbits.PEIE = 1;
122     RCONbits.IPEN = 0;
123     TMR3L = 0;
124     TMR3H = 0;
125     TMR3_set();
126 }
127
128 void lcd_backlightinit(void)
129 {
130 #if 0
131     PSTRCON = 0b00000001; // P1A = PWM, P1B~P1D=PORT, Steeling sync to PWM.
132     CCP1CON = 0b00001100; // PWM, Low-Active.
133     ECCP1AS = 0b10000111; // Auto shutdown is disabled
134     // Use TMR2 as source.
135     PIE1   &= ~0b00000010; // TMR2 Interrupt disabled.
136     PIR1   &= ~0b00000010; // TMR2 Interrupt clear.
137     IPR1   &= ~0b00000010; // Priority = LOW
138     T2CON   = 0b00000011; // Prescaler/Postscaler = 1:1, Temporally disabled TMR2.
139     TMR2 = 0x00; // Value;
140     PR2 = 0x65; // 19.61KHz
141     CCPR1L = 100; // Duty = 100;
142 #endif
143 }
144
145
146 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
147 /*
148  * For 28Pin PIC(18F2xK22), I2C lcd using.
149  */
150
151 void keyin_ioinit(void)
152 {
153     /* Initialize IOPORTS*/
154     PORTA = 0x00;
155     LATA = 0x00;
156     ANSELA = AN_A_VAL;
157     TRISA = TRIS_A_VAL;
158
159     PORTB = 0x00;
160     LATB = 0x00;
161     ANSELB = AN_B_VAL;
162     TRISB = TRIS_B_VAL;
163
164     PORTC = 0x00;
165     LATC = 0x00;
166     ANSELC = AN_C_VAL;
167     TRISC = TRIS_C_VAL_O;
168     lcd_backlightinit();
169     io_intcountinit();
170 }
171 #endif
172
173 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
174 /*
175  * For 28Pin PIC(18F2xK20), I2C lcd using.
176  */
177
178 void keyin_ioinit(void)
179 {
180     /* Initialize IOPORTS*/
181     PORTA = 0x00;
182     LATA = 0x00;
183     ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
184     ANSELH = 0x00; //
185     TRISA = TRIS_A_VAL;
186
187     PORTB = 0x00;
188     LATB = 0x00;
189     TRISB = TRIS_B_VAL;
190
191     PORTC = 0x00;
192     LATC = 0x00;
193     TRISC = TRIS_C_VAL_O;
194     lcd_backlightinit();
195     io_intcountinit();
196 }
197 #endif
198
199 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20)
200 /*
201  * For 40Pin PIC(18F4xK20), paralell or I2C lcd using.
202  */
203 void keyin_ioinit(void)
204 {
205     /* Initialize IOPORTS*/
206     PORTA = 0x00;
207     LATA = 0x00;
208     ANSEL = 0x80; // Use RA7 AS ADC, Another is not used.
209     ANSELH = 0x00; //
210     TRISA = TRIS_A_VAL;
211
212     PORTB = 0x00;
213     LATB = 0x00;
214     TRISB = TRIS_B_VAL;
215
216     PORTC = 0x00;
217     LATC = 0x00;
218     TRISC = TRIS_C_VAL_O;
219
220     /*
221      * You can use PORTD,RE0-RE2 extention, when using I2C lcd.
222      */
223     PORTD = 0x00;
224     LATD = 0x00;
225     TRISD = TRIS_D_VAL;
226
227     TRISE = TRIS_E_VAL;
228     PORTE = 0b00000000; // Mute OK
229
230     // Interrupts
231     INTCONbits.INT0IE = 0;
232     INTCONbits.INT0IF = 0;
233     INTCON3bits.INT1IF = 0;
234     INTCON3bits.INT2IF = 0;
235     INTCON3bits.INT1IE = 0;
236     INTCON3bits.INT2IE = 0;
237
238     lcd_backlightinit();
239     io_intcountinit();
240 }
241 #else
242 void keyin_ioinit(void)
243 {
244     /* Initialize IOPORTS*/
245     PORTA = 0x00;
246     LATA = 0x00;
247 //    ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
248 //    ANSELH = 0x00; //
249     TRISA = TRIS_A_VAL;
250
251     PORTB = 0x00;
252     LATB = 0x00;
253     TRISB = TRIS_B_VAL;
254
255     PORTC = 0x00;
256     LATC = 0x00;
257     TRISC = TRIS_C_VAL_O;
258     lcd_backlightinit();
259     io_intcountinit();
260 }
261 #endif
262
263 /*
264  * Read IOPORTS for KEY. You should modify if you modify circuit.
265  */
266 void readkey_io(unsigned char state)
267 {
268     char i;
269     __bitops_t portvar;
270  
271     switch(state) {
272         case 0:
273             for(i = 0; i < 2; i++){
274                 keyin_old[1].byte[i] = keyin_old[0].byte[i];
275                 keyin_old[0].byte[i] = keyin_now.byte[i];
276                 keyin_now.byte[i] = 0x00;
277             }
278             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
279             break;
280         case 1:
281             _KEY_SEND_GRP1 = 0; // Group1 DOWN.
282             break;
283         case 2:
284             portvar.byte = _KEY_RECVPORT;
285             if(!_KEY_POS1MASK(portvar)) {
286                 keyin_now.BIT0A = 1;
287             }
288             if(!_KEY_POS2MASK(portvar)) {
289                 keyin_now.BIT1A = 1;
290             }
291             if(!_KEY_POS3MASK(portvar)) {
292                 keyin_now.BIT2A = 1;
293             }
294             if(!_KEY_POS4MASK(portvar)) {
295                 keyin_now.BIT3A = 1;
296             }
297             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
298             break;
299         case 3:
300             _KEY_SEND_GRP2 = 0; // Group2 DOWN.
301             break;
302         case 4:
303             portvar.byte = _KEY_RECVPORT;
304             if(!_KEY_POS1MASK(portvar)) {
305                 keyin_now.BIT0B = 1;
306             }
307             if(!_KEY_POS2MASK(portvar)) {
308                 keyin_now.BIT1B = 1;
309             }
310             if(!_KEY_POS3MASK(portvar)) {
311                 keyin_now.BIT2B = 1;
312             }
313             if(!_KEY_POS4MASK(portvar)) {
314                 keyin_now.BIT3B = 1;
315             }
316             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
317             break;
318         case 5:
319             _KEY_SEND_GRP3 = 0; // Group1 DOWN.
320             break;
321         case 6:
322             portvar.byte = _KEY_RECVPORT;
323             if(!_KEY_POS1MASK(portvar)) {
324                 keyin_now.BIT0C = 1;
325             }
326             if(!_KEY_POS2MASK(portvar)) {
327                 keyin_now.BIT1C = 1;
328             }
329             if(!_KEY_POS3MASK(portvar)) {
330                 keyin_now.BIT2C = 1;
331             }
332             if(!_KEY_POS4MASK(portvar)) {
333                 keyin_now.BIT3C = 1;
334             }
335             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
336             break;
337         case 7:
338             _KEY_SEND_GRP4 = 0; // Group4 up
339             break;
340         case 8:
341             portvar.byte = _KEY_RECVPORT;
342             if(!_KEY_POS1MASK(portvar)) {
343                 keyin_now.BIT0D = 1;
344             }
345             if(!_KEY_POS2MASK(portvar)) {
346                 keyin_now.BIT1D = 1;
347             }
348             if(!_KEY_POS3MASK(portvar)) {
349                 keyin_now.BIT2D = 1;
350             }
351             if(!_KEY_POS4MASK(portvar)) {
352                 keyin_now.BIT3D = 1;
353             }
354             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
355             break;
356         default:
357             break;
358     }
359 }