OSDN Git Service

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