OSDN Git Service

[USERFERQ] Store userbands to external EEPROM.
[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 = 1; // 20130709 Not Mute
49     } else {
50         _PORT_MUTE = 0;
51     }
52     idle_time_ms(200);
53 }
54
55 void set_radiopower(unsigned char f)
56 {
57     if(f == 0x00) {
58         _PORT_RADIOPOW = 0; // OFF
59     } else {
60         _PORT_RADIOPOW = 1; // ON
61     }
62 }
63
64 void set_powerlamp(unsigned char f)
65 {
66     if(f == 0x00) {
67         _POW_STATLAMP = 0;
68     } else {
69         _POW_STATLAMP = 1;
70     }
71 }
72
73
74 void TMR3_set(void)
75 {
76     TMR3L = 1000 & 0xff;
77     TMR3H = 1000 >> 8;
78     T3CONbits.TMR3ON = 1;
79 }
80
81 void io_intcountinit(void)
82 {
83     statecount = 0;
84     T3CON = 0b10001100; // TMR3 = 16bit, CCP is not use TMR3, Prescaler=1:2(1uS)
85                         // DO-not-use SYNC, Internal clock, TMR3 temporally off.
86     PIR2bits.TMR3IF = 0;
87     PIE2bits.TMR3IE = 1;
88     IPR2bits.TMR3IP = 0; // Low priority.
89     INTCONbits.PEIE = 1;
90     RCONbits.IPEN = 0;
91     TMR3L = 0;
92     TMR3H = 0;
93     TMR3_set();
94 }
95
96
97
98 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
99     defined(_18F23K22)  || defined(_18F24K22)  || defined(_18F25K22)  || defined(_18F26K22)
100
101 /*
102  * For 28Pin PIC(18F2xK22), I2C lcd using.
103  */
104
105 void keyin_ioinit(void)
106 {
107     /* Initialize IOPORTS*/
108     PORTA = 0x00;
109     LATA = 0x00;
110     ANSELA = AN_A_VAL;
111     TRISA = TRIS_A_VAL;
112
113     PORTB = 0x00;
114     LATB = 0x00;
115     ANSELB = AN_B_VAL;
116     TRISB = TRIS_B_VAL;
117
118     PORTC = 0x00;
119     LATC = 0x00;
120     ANSELC = AN_C_VAL;
121     TRISC = TRIS_C_VAL_O;
122  //   lcd_backlightinit();
123 //    io_intcountinit();
124 }
125 #endif
126
127 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
128     defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20)
129
130 /*
131  * For 28Pin PIC(18F2xK20), I2C lcd using.
132  */
133
134 void keyin_ioinit(void)
135 {
136     /* Initialize IOPORTS*/
137     PORTA = 0x00;
138     LATA = 0x00;
139     ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
140     ANSELH = 0x00; //
141     TRISA = TRIS_A_VAL;
142
143     PORTB = 0x00;
144     LATB = 0x00;
145     TRISB = TRIS_B_VAL;
146
147     PORTC = 0x00;
148     LATC = 0x00;
149     TRISC = TRIS_C_VAL_O;
150 //    lcd_backlightinit();
151     io_intcountinit();
152 }
153 #endif
154
155 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
156     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)
157
158 /*
159  * For 40Pin PIC(18F4xK20), paralell or I2C lcd using.
160  */
161 void keyin_ioinit(void)
162 {
163     /* Initialize IOPORTS*/
164     PORTA = 0x00;
165     LATA = 0x00;
166     ANSEL = 0x80; // Use RA7 AS ADC, Another is not used.
167     ANSELH = 0x00; //
168     TRISA = TRIS_A_VAL;
169
170     PORTB = 0x00;
171     LATB = 0x00;
172     TRISB = TRIS_B_VAL;
173
174     PORTC = 0x00;
175     LATC = 0x00;
176     TRISC = TRIS_C_VAL_O;
177
178     /*
179      * You can use PORTD,RE0-RE2 extention, when using I2C lcd.
180      */
181     PORTD = 0x00;
182     LATD = 0x00;
183     TRISD = TRIS_D_VAL;
184
185     TRISE = TRIS_E_VAL;
186     PORTE = 0b00000000; // Mute OK
187
188     // Interrupts
189     INTCONbits.INT0IE = 0;
190     INTCONbits.INT0IF = 0;
191     INTCON3bits.INT1IF = 0;
192     INTCON3bits.INT2IF = 0;
193     INTCON3bits.INT1IE = 0;
194     INTCON3bits.INT2IE = 0;
195
196 //    lcd_backlightinit();
197     io_intcountinit();
198 }
199 #else
200 void keyin_ioinit(void)
201 {
202     /* Initialize IOPORTS*/
203     PORTA = 0x00;
204     LATA = 0x00;
205 //    ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
206 //    ANSELH = 0x00; //
207     TRISA = TRIS_A_VAL;
208
209     PORTB = 0x00;
210     LATB = 0x00;
211     TRISB = TRIS_B_VAL;
212
213     PORTC = 0x00;
214     LATC = 0x00;
215     TRISC = TRIS_C_VAL_O;
216 //    lcd_backlightinit();
217     io_intcountinit();
218 }
219 #endif
220
221 /*
222  * Read IOPORTS for KEY. You should modify if you modify circuit.
223  */
224 void readkey_io(unsigned char state)
225 {
226     char i;
227     __bitops_t portvar;
228  
229     switch(state) {
230         case 0:
231             for(i = 0; i < 2; i++){
232                 keyin_old[1].byte[i] = keyin_old[0].byte[i];
233                 keyin_old[0].byte[i] = keyin_now.byte[i];
234                 keyin_now.byte[i] = 0x00;
235             }
236             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
237             break;
238         case 1:
239             _KEY_SEND_GRP1 = 0; // Group1 DOWN.
240             break;
241         case 2:
242             portvar.byte = _KEY_RECVPORT;
243             if(!_KEY_POS1MASK(portvar)) {
244                 keyin_now.BIT0A = 1;
245             }
246             if(!_KEY_POS2MASK(portvar)) {
247                 keyin_now.BIT1A = 1;
248             }
249             if(!_KEY_POS3MASK(portvar)) {
250                 keyin_now.BIT2A = 1;
251             }
252             if(!_KEY_POS4MASK(portvar)) {
253                 keyin_now.BIT3A = 1;
254             }
255             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
256             break;
257         case 3:
258             _KEY_SEND_GRP2 = 0; // Group2 DOWN.
259             break;
260         case 4:
261             portvar.byte = _KEY_RECVPORT;
262             if(!_KEY_POS1MASK(portvar)) {
263                 keyin_now.BIT0B = 1;
264             }
265             if(!_KEY_POS2MASK(portvar)) {
266                 keyin_now.BIT1B = 1;
267             }
268             if(!_KEY_POS3MASK(portvar)) {
269                 keyin_now.BIT2B = 1;
270             }
271             if(!_KEY_POS4MASK(portvar)) {
272                 keyin_now.BIT3B = 1;
273             }
274             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
275             break;
276         case 5:
277             _KEY_SEND_GRP3 = 0; // Group1 DOWN.
278             break;
279         case 6:
280             portvar.byte = _KEY_RECVPORT;
281             if(!_KEY_POS1MASK(portvar)) {
282                 keyin_now.BIT0C = 1;
283             }
284             if(!_KEY_POS2MASK(portvar)) {
285                 keyin_now.BIT1C = 1;
286             }
287             if(!_KEY_POS3MASK(portvar)) {
288                 keyin_now.BIT2C = 1;
289             }
290             if(!_KEY_POS4MASK(portvar)) {
291                 keyin_now.BIT3C = 1;
292             }
293             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
294             break;
295         case 7:
296             _KEY_SEND_GRP4 = 0; // Group4 up
297             break;
298         case 8:
299             portvar.byte = _KEY_RECVPORT;
300             if(!_KEY_POS1MASK(portvar)) {
301                 keyin_now.BIT0D = 1;
302             }
303             if(!_KEY_POS2MASK(portvar)) {
304                 keyin_now.BIT1D = 1;
305             }
306             if(!_KEY_POS3MASK(portvar)) {
307                 keyin_now.BIT2D = 1;
308             }
309             if(!_KEY_POS4MASK(portvar)) {
310                 keyin_now.BIT3D = 1;
311             }
312             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
313             break;
314         default:
315             break;
316     }
317 }