OSDN Git Service

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