OSDN Git Service

[POWER] Shutdown EX-RF-AMP on turning off power, now still use 3mA.
[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 void lcd_setbacklight(unsigned char flag, unsigned char level)
74 {
75     __bitops_t  b;
76     __bitops_t d;
77 #if 0
78     d.byte = _LCD_BACKLIGHT_TRIS;
79     d._LCD_BACKLIGHT_BIT = 0;
80    _LCD_BACKLIGHT_TRIS = d.byte;
81
82     b.byte = _LCD_BACKLIGHT;
83     b._LCD_BACKLIGHT_BIT = 0;
84     if(flag != 0){
85        b._LCD_BACKLIGHT_BIT = 1;
86     }
87     _LCD_BACKLIGHT = b.byte;
88 #else
89     if(flag == 0) {
90         d.byte = _LCD_BACKLIGHT_TRIS;
91         d._LCD_BACKLIGHT_BIT = 1;
92         _LCD_BACKLIGHT_TRIS = d.byte;
93         PSTRCONbits.STRA = 0;
94         T2CONbits.TMR2ON = 0;
95         TMR2 = 0x00;
96         CCP2CON = 0x00;
97         CCPR2L = 0;
98         CCPR2H = 0;
99     } else {
100       unsigned char h,l;
101 //      lv = level;
102 //      lv <<= 2;
103 //      level = (level * 25) / 10;
104 //      level = 255;
105       h = level >> 1;
106       if(h == CCPR1L) return;
107  //     l = 2 << 4; //(lv & 0x0300) >> 4;
108       d.byte = _LCD_BACKLIGHT_TRIS;
109       d._LCD_BACKLIGHT_BIT = 1;
110       _LCD_BACKLIGHT_TRIS = d.byte;
111       //PSTRCON = 0;
112       CCPR2L = h;
113       CCPR2H = h;
114       b.b2 = 1;
115       b.b3 = 1;
116       CCP2CON = b.byte;
117       PR2 = 50;
118       PIR1bits.TMR2IF = 0;
119       T2CON = 0b01111000; // Pre-scaler=1/1,Post-scaler = 1/16
120       // -> Freq = 2.44KHz * 16 = 39.4KHz.
121       TMR2 = 0x00;
122       T2CONbits.TMR2ON = 1;
123       do {
124           idle_time_ms(1);
125       } while(PIR1bits.TMR2IF ==0);
126       d.byte = _LCD_BACKLIGHT_TRIS;
127       d._LCD_BACKLIGHT_BIT = 0;
128       _LCD_BACKLIGHT_TRIS = d.byte;
129
130 //      _LCD_PORT |= _LCD_BACKLIGHT;
131     }
132 #endif
133 }
134
135 void TMR3_set(void)
136 {
137     TMR3L = 1000 & 0xff;
138     TMR3H = 1000 >> 8;
139     T3CONbits.TMR3ON = 1;
140 }
141
142 void io_intcountinit(void)
143 {
144     statecount = 0;
145     T3CON = 0b10001100; // TMR3 = 16bit, CCP is not use TMR3, Prescaler=1:2(1uS)
146                         // DO-not-use SYNC, Internal clock, TMR3 temporally off.
147     PIR2bits.TMR3IF = 0;
148     PIE2bits.TMR3IE = 1;
149     IPR2bits.TMR3IP = 0; // Low priority.
150     INTCONbits.PEIE = 1;
151     RCONbits.IPEN = 0;
152     TMR3L = 0;
153     TMR3H = 0;
154     TMR3_set();
155 }
156
157
158
159 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22) || \
160     defined(_18F23K22)  || defined(_18F24K22)  || defined(_18F25K22)  || defined(_18F26K22)
161
162 /*
163  * For 28Pin PIC(18F2xK22), I2C lcd using.
164  */
165
166 void keyin_ioinit(void)
167 {
168     /* Initialize IOPORTS*/
169     PORTA = 0x00;
170     LATA = 0x00;
171     ANSELA = AN_A_VAL;
172     TRISA = TRIS_A_VAL;
173
174     PORTB = 0x00;
175     LATB = 0x00;
176     ANSELB = AN_B_VAL;
177     TRISB = TRIS_B_VAL;
178
179     PORTC = 0x00;
180     LATC = 0x00;
181     ANSELC = AN_C_VAL;
182     TRISC = TRIS_C_VAL_O;
183  //   lcd_backlightinit();
184     io_intcountinit();
185 }
186 #endif
187
188 #if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
189     defined(_18F23K20)  || defined(_18F24K20)  || defined(_18F25K20)  || defined(_18F26K20)
190
191 /*
192  * For 28Pin PIC(18F2xK20), I2C lcd using.
193  */
194
195 void keyin_ioinit(void)
196 {
197     /* Initialize IOPORTS*/
198     PORTA = 0x00;
199     LATA = 0x00;
200     ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
201     ANSELH = 0x00; //
202     TRISA = TRIS_A_VAL;
203
204     PORTB = 0x00;
205     LATB = 0x00;
206     TRISB = TRIS_B_VAL;
207
208     PORTC = 0x00;
209     LATC = 0x00;
210     TRISC = TRIS_C_VAL_O;
211 //    lcd_backlightinit();
212     io_intcountinit();
213 }
214 #endif
215
216 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
217     defined(_18F43K20)  || defined(_18F44K20)  || defined(_18F45K20)  || defined(_18F46K20)
218
219 /*
220  * For 40Pin PIC(18F4xK20), paralell or I2C lcd using.
221  */
222 void keyin_ioinit(void)
223 {
224     /* Initialize IOPORTS*/
225     PORTA = 0x00;
226     LATA = 0x00;
227     ANSEL = 0x80; // Use RA7 AS ADC, Another is not used.
228     ANSELH = 0x00; //
229     TRISA = TRIS_A_VAL;
230
231     PORTB = 0x00;
232     LATB = 0x00;
233     TRISB = TRIS_B_VAL;
234
235     PORTC = 0x00;
236     LATC = 0x00;
237     TRISC = TRIS_C_VAL_O;
238
239     /*
240      * You can use PORTD,RE0-RE2 extention, when using I2C lcd.
241      */
242     PORTD = 0x00;
243     LATD = 0x00;
244     TRISD = TRIS_D_VAL;
245
246     TRISE = TRIS_E_VAL;
247     PORTE = 0b00000000; // Mute OK
248
249     // Interrupts
250     INTCONbits.INT0IE = 0;
251     INTCONbits.INT0IF = 0;
252     INTCON3bits.INT1IF = 0;
253     INTCON3bits.INT2IF = 0;
254     INTCON3bits.INT1IE = 0;
255     INTCON3bits.INT2IE = 0;
256
257 //    lcd_backlightinit();
258     io_intcountinit();
259 }
260 #else
261 void keyin_ioinit(void)
262 {
263     /* Initialize IOPORTS*/
264     PORTA = 0x00;
265     LATA = 0x00;
266 //    ANSEL = 0x01; // Use RA0 AS ADC, Another is not used.
267 //    ANSELH = 0x00; //
268     TRISA = TRIS_A_VAL;
269
270     PORTB = 0x00;
271     LATB = 0x00;
272     TRISB = TRIS_B_VAL;
273
274     PORTC = 0x00;
275     LATC = 0x00;
276     TRISC = TRIS_C_VAL_O;
277 //    lcd_backlightinit();
278     io_intcountinit();
279 }
280 #endif
281
282 /*
283  * Read IOPORTS for KEY. You should modify if you modify circuit.
284  */
285 void readkey_io(unsigned char state)
286 {
287     char i;
288     __bitops_t portvar;
289  
290     switch(state) {
291         case 0:
292             for(i = 0; i < 2; i++){
293                 keyin_old[1].byte[i] = keyin_old[0].byte[i];
294                 keyin_old[0].byte[i] = keyin_now.byte[i];
295                 keyin_now.byte[i] = 0x00;
296             }
297             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
298             break;
299         case 1:
300             _KEY_SEND_GRP1 = 0; // Group1 DOWN.
301             break;
302         case 2:
303             portvar.byte = _KEY_RECVPORT;
304             if(!_KEY_POS1MASK(portvar)) {
305                 keyin_now.BIT0A = 1;
306             }
307             if(!_KEY_POS2MASK(portvar)) {
308                 keyin_now.BIT1A = 1;
309             }
310             if(!_KEY_POS3MASK(portvar)) {
311                 keyin_now.BIT2A = 1;
312             }
313             if(!_KEY_POS4MASK(portvar)) {
314                 keyin_now.BIT3A = 1;
315             }
316             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
317             break;
318         case 3:
319             _KEY_SEND_GRP2 = 0; // Group2 DOWN.
320             break;
321         case 4:
322             portvar.byte = _KEY_RECVPORT;
323             if(!_KEY_POS1MASK(portvar)) {
324                 keyin_now.BIT0B = 1;
325             }
326             if(!_KEY_POS2MASK(portvar)) {
327                 keyin_now.BIT1B = 1;
328             }
329             if(!_KEY_POS3MASK(portvar)) {
330                 keyin_now.BIT2B = 1;
331             }
332             if(!_KEY_POS4MASK(portvar)) {
333                 keyin_now.BIT3B = 1;
334             }
335             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
336             break;
337         case 5:
338             _KEY_SEND_GRP3 = 0; // Group1 DOWN.
339             break;
340         case 6:
341             portvar.byte = _KEY_RECVPORT;
342             if(!_KEY_POS1MASK(portvar)) {
343                 keyin_now.BIT0C = 1;
344             }
345             if(!_KEY_POS2MASK(portvar)) {
346                 keyin_now.BIT1C = 1;
347             }
348             if(!_KEY_POS3MASK(portvar)) {
349                 keyin_now.BIT2C = 1;
350             }
351             if(!_KEY_POS4MASK(portvar)) {
352                 keyin_now.BIT3C = 1;
353             }
354             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
355             break;
356         case 7:
357             _KEY_SEND_GRP4 = 0; // Group4 up
358             break;
359         case 8:
360             portvar.byte = _KEY_RECVPORT;
361             if(!_KEY_POS1MASK(portvar)) {
362                 keyin_now.BIT0D = 1;
363             }
364             if(!_KEY_POS2MASK(portvar)) {
365                 keyin_now.BIT1D = 1;
366             }
367             if(!_KEY_POS3MASK(portvar)) {
368                 keyin_now.BIT2D = 1;
369             }
370             if(!_KEY_POS4MASK(portvar)) {
371                 keyin_now.BIT3D = 1;
372             }
373             _KEY_SENDPORT |= _KEY_SENDPORTMASK; // Group1 DOWN.
374             break;
375         default:
376             break;
377     }
378 }