OSDN Git Service

[General] Add _LCD_DEBUG flag, without initialize AKC695x.
[openi2cradio/OpenI2CRadio.git] / main.c
1 /*
2  * OpenI2CRADIO
3  * Config & Main routine.
4  * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
5  * License: GPL2+LE
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2,
10  *  or (at your option) any later version.
11  *  This library / program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *  See the GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this library; see the file COPYING. If not, write to the
18  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19  *  MA 02110-1301, USA.
20  *
21  *  As a special exception, if you link this(includeed from sdcc) library
22  *  with other files, some of which are compiled with SDCC,
23  *  to produce an executable, this library does not by itself cause
24  *  the resulting executable to be covered by the GNU General Public License.
25  *  This exception does not however invalidate any other reasons why
26  *  the executable file might be covered by the GNU General Public License.
27  */
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sdcc-lib.h>
34 #include <pic18fregs.h> /* ONLY FOR PIC18x */
35 #include <signal.h>
36 #include <delay.h>
37
38 #include "iodef.h"
39 #include "idle.h"
40 #include "i2c_io.h"
41 #include "akc6955.h"
42 #include "lcd_acm1602.h"
43 #include "ui.h"
44
45 /*
46  * Config words.
47  */
48 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
49 #pragma stack 0x200 256 // Set stack size to 256bytes.
50 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
51 #pragma config WDTEN=ON,WDTPS=32768
52 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON//,XINST=ON
53 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
54 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
55 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
56 #endif
57 // For 4xK20 or 2xK20 Series
58 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
59     defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
60 #pragma stack 0x200 256
61 #pragma config FOSC=INTIO67,FCMEN=ON,PWRT=ON,BOREN=ON,BORV=22
62 #pragma config WDTEN=ON,WDTPS=32768,PBADEN=OFF,LPT1OSC=ON,MCLRE=ON
63 #pragma config STVREN=ON,DEBUG=ON
64 #pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
65 #pragma config CPB=OFF,CPD=OFF
66 #pragma config WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF
67 #pragma config WRTC=OFF,WRTB=OFF,WRTD=OFF
68 #pragma config EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF
69 #endif
70
71 #define _LCD_DEBUG 1
72
73 SIGHANDLER(TMR0_handler)
74 {
75    unsigned char tmr0f;
76    unsigned char t0con;
77
78    // Stop timer0
79    t0con = T0CON;
80    t0con &= ~_IDLEN;
81    T0CON = t0con;
82
83    // Read IOKEYS
84    readkey_io();
85
86    // Clear interrupt flag
87    tmr0f = INTCON;
88    tmr0f &= ~(_TMR0IF);
89    INTCON = tmr0f;
90
91    return;
92 }
93
94
95 DEF_INTLOW(intlow_handler)
96   DEF_HANDLER(SIG_TMR0, TMR0_handler)
97 END_DEF
98
99
100 unsigned int amfreq;
101 unsigned int fmfreq;
102 unsigned char amband;
103 unsigned char fmband;
104 unsigned char fm;
105 unsigned char am_mode3k;
106 unsigned char am_userbandnum;
107 unsigned char fm_userbandnum;
108
109 int recv_signal;
110 int backlight_counter;
111 unsigned char backlight_level;
112
113 typedef struct {
114     unsigned char start;
115     unsigned char stop;
116 } _userband_t;
117
118 _userband_t am_usrbands[4];
119 _userband_t fm_usrbands[4];
120
121
122 void toggle_amfm(void)
123 {
124     if(fm != 0){
125         fm = 0;
126 #ifndef _LCD_DEBUG
127         akc6955_chg_fm(fm);
128         akc6955_set_amband(amband);
129         akc6955_set_freq(amfreq);
130 #endif
131     } else {
132         fm = 0xff;
133 #ifndef _LCD_DEBUG
134         akc6955_chg_fm(fm);
135         akc6955_set_fmband(fmband);
136         akc6955_set_freq(fmfreq);
137 #endif
138     }
139 }
140
141 void update_display(void)
142 {
143     if(fm != 0){ // FM
144         _LOCATE(0,0);
145         if(fmband < AKC6955_BAND_TV1) {
146             printstr("FM");
147             _PUTCHAR('1' + (fmband & 7));
148         } else if(fmband < AKC6955_BAND_FMUSER){
149             printstr("TV");
150             _PUTCHAR('1' + fmband - AKC6955_BAND_TV1);
151         } else { // USER
152             printstr("FMUSR");
153         }
154     } else { // AM
155         _LOCATE(0,1);
156         if(amband == AKC6955_BAND_LW) {
157             printstr("LW");
158         } else if(amband <AKC6955_BAND_SW1) { //MW
159             printstr("MW");
160             _PUTCHAR('1' + amband - AKC6955_BAND_MW1);
161         } else if(amband < AKC6955_BAND_AMUSER) { //MW
162             printstr("SW");
163             _PUTCHAR('1' + amband - AKC6955_BAND_SW1);
164         } else if(amband == AKC6955_BAND_MW4){
165             printstr("MW4");
166         } else {
167             printstr("AMUSR");
168         }
169      }
170      _LOCATE(16-4 ,1);
171      if(fm != 0){
172          printstr("MHz");
173      } else {
174          printstr("KHz");
175      }
176      _LOCATE(16-5, 1);
177      if(fm != 0){
178          int freq_lo = fmfreq % 100;
179          int freq_hi = fmfreq / 100;
180          print_numeric(freq_hi);
181          _PUTCHAR('.');
182          print_numeric(freq_lo);
183      } else {
184          int freq = amfreq;
185          print_numeric(freq);
186      }
187      // Signal
188      _LOCATE(0, 0);
189      printstr("S=");
190      print_numeric(recv_signal);
191 }
192
193
194 void setfreq_updown(unsigned char ctlword)
195 {
196 #ifndef _LCD_DEBUG
197     switch(ctlword){
198         case charcode_8: // Change band
199             if(fm != 0){
200                 amband++;
201                 if(amband > 18) amband = 0;
202 //                amfreq = akc6955_setfreq(amfreq)
203                 akc6955_set_amband(amband);
204                 _AKC6955_WAIT_62_5MS(); // 62.5ms
205                 amband = akc6955_get_amband();
206                 amfreq = akc6955_get_freq();
207             } else {
208                 fmband++;
209                 if(fmband > 7) fmband = 0;
210 //                amfreq = akc6955_setfreq(amfreq)
211                 akc6955_set_fmband(fmband);
212                 _AKC6955_WAIT_62_5MS(); // 62.5ms
213                 fmband = akc6955_get_fmband();
214                 fmfreq = akc6955_get_freq();
215             }
216             break;
217         case charcode_2: // Change band
218             if(fm != 0){
219                 amband--;
220                 if(amband == 0) amband = 18;
221                 if(amband >= 18) amband = 18;
222 //                amfreq = akc6955_setfreq(amfreq)
223                 akc6955_set_amband(amband);
224                 _AKC6955_WAIT_62_5MS(); // 62.5ms
225                 amband = akc6955_get_amband();
226                 amfreq = akc6955_get_freq();
227             } else {
228                 fmband--;
229                 if(fmband == 0) fmband = 7;
230                 if(fmband >= 7) fmband = 7;
231 //                amfreq = akc6955_setfreq(amfreq)
232                 akc6955_set_fmband(fmband);
233                 _AKC6955_WAIT_62_5MS(); // 62.5ms
234                 fmband = akc6955_get_fmband();
235                 fmfreq = akc6955_get_freq();
236             }
237             break;
238         case charcode_4: // Down Freq;
239             if(fm != 0){
240                 fmfreq = akc6955_down_freq(10); // DOWN 100KHz
241             } else {
242                 amfreq = akc6955_down_freq(10); // DOWN 10KHz
243             }
244             break;
245         case charcode_6: // Down Freq;
246             if(fm != 0){
247                 fmfreq = akc6955_up_freq(10); // UP 100KHz
248             } else {
249                 amfreq = akc6955_up_freq(10); // UP 10KHz
250             }
251             break;
252         case charcode_7: // Down Fast;
253             if(fm != 0){
254                 fmfreq = akc6955_down_freq(50); // DOWN 500KHz
255             } else {
256                 amfreq = akc6955_down_freq(50); // DOWN 50KHz
257             }
258             break;
259         case charcode_9: // Down Fast;
260             if(fm != 0){
261                 fmfreq = akc6955_up_freq(50); // UP 100KHz
262             } else {
263                 amfreq = akc6955_up_freq(50); // UP 10KHz
264             }
265             break;
266         case charcode_1: // Down Slow;
267             if(fm != 0){
268                 fmfreq = akc6955_down_freq(5); // DOWN 50KHz
269             } else {
270                 amfreq = akc6955_down_freq(5); // DOWN 50KHz
271             }
272             break;
273         case charcode_3: // Down Slow;
274             if(fm != 0){
275                 fmfreq = akc6955_up_freq(5); // UP 50KHz
276             } else {
277                 amfreq = akc6955_up_freq(5); // UP 5KHz
278             }
279             break;
280         case charcode_0: // Step
281             if(fm == 0){
282                 if(am_mode3k == 0) {
283                     am_mode3k = 0xff;
284                 } else {
285                     am_mode3k = 0;
286                 }
287                 amfreq = akc6955_mode3k(am_mode3k);
288             }
289             break;
290         default:
291             break;
292     }
293 #endif
294 }
295 /*
296  * 
297  */
298 int main(void)
299 {
300     char readchar;
301     unsigned char input_flag;
302     char i;
303 #ifdef _LCD_DEBUG
304     unsigned char power_flag;
305 #endif
306     keyin_init();
307     keyin_ioinit();
308     idle_init();
309
310     i2c1_init();
311
312     _AKC6955_WAIT_125_0MS(); // Wait 125ms
313 #ifdef _LCD_DEBUG
314     power_flag = 0xff;
315 #endif
316     backlight_counter = 128;
317     backlight_level = 255;
318     acm1602_init(0xa0, 1); //Init LCD
319 //    _AKC6955_WAIT_125_0MS(); // Wait 125ms
320     _LOCATE(0,0);
321     printstr("Hello;-)");
322
323     
324     // Init AKC6955
325     amfreq = 954;
326     fmfreq = 8000; // 10KHz order.
327     amband = AKC6955_BAND_MW2;
328     fmband = AKC6955_BAND_FM2;
329     am_mode3k = 0xff;
330     fm = 0;
331     recv_signal = 0;
332     am_userbandnum = 0;
333     fm_userbandnum = 0;
334     for(i = 0; i < 4; i++){
335         am_usrbands[i].start = 0x19;
336         am_usrbands[i].stop  = 0x32;
337     }
338     for(i = 0; i < 4; i++){
339         fm_usrbands[i].start = 0x19;
340         fm_usrbands[i].stop  = 0x32;
341     }
342
343     /* Check EEPROM */
344     /* Push default parameters to AKC6955*/
345
346 #ifndef _LCD_DEBUG
347     akc6955_chg_fm(fm); // Set to AM
348     akc6955_set_amband(amband);
349     akc6955_set_freq(amfreq); // Dummy, TBS (954KHz)
350     akc6955_set_power(0xff); // Power ON
351 #endif
352
353 #ifdef _LCD_DEBUG
354     idle(0xf000);
355 #else
356     idle(0xff00);
357 #endif
358     do {
359         /* Main routine*/
360         input_flag = readkey_compare();
361         if(input_flag != 0){
362             readchar = pop_keyinfifo();
363             switch(readchar) {
364                 // Top of input-tree.
365                 case charcode_s0:
366                     // Change FM/AM
367                     toggle_amfm();
368                     break;
369                 case charcode_s1:
370                     //
371                     backlight_counter = 128;
372                     break;
373                 case charcode_s2:
374                     // ENTER
375                     break;
376                 case charcode_s3:
377                     // Reserve
378                     break;
379                 case charcode_null: // None
380                     break;
381                 default:
382                     // Numeric 0to9, or a to f.
383                     setfreq_updown(readchar);
384                     break;
385             }
386         }
387 #ifndef _LCD_DEBUG
388         recv_signal = akc6955_read_level();
389         if(fm != 0){
390             fmfreq = akc6955_get_freq();
391         } else {
392             amfreq = akc6955_get_freq();
393         }
394 #endif
395         // Check battery (include idle?)
396         // Read AKJC6955's status
397         // Putstring to LCD.
398
399         update_display();
400         if(backlight_counter > 0) {
401             backlight_counter--;
402             set_backlight(0xff, backlight_level); // Turn ON
403         } else {
404             set_backlight(0x00, 0); // Turn OFF
405         }
406 #ifdef _LCD_DEBUG
407         if(power_flag != 0x00) {
408             power_flag = 0x00;
409         } else {
410             power_flag = 0xff;
411         }
412         setsignal_tune(power_flag);
413 #endif
414         idle(0xf800);
415     } while(1);
416 }
417