OSDN Git Service

[LCD][BUG] Set correct sequence,OOPs!
[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 #include "eeprom.h"
45
46 /*
47  * Config words.
48  */
49 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
50 //#pragma stack 0x200 256 // Set stack size to 256bytes.
51 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
52 #pragma config WDTEN=ON,WDTPS=32768
53 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON//,XINST=ON
54 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
55 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
56 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
57 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
58 #endif
59 // For 4xK20 or 2xK20 Series
60 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
61     defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
62
63 #pragma stack 0x200 256
64 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=ON,BORV=22
65 #pragma config WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=ON,LPT1OSC=OFF,MCLRE=ON
66 #pragma config STVREN=ON,DEBUG=ON
67 #pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
68 #pragma config CPB=OFF,CPD=OFF
69 #pragma config WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF
70 #pragma config WRTC=OFF,WRTB=OFF,WRTD=OFF
71 #pragma config EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF
72 #endif
73
74 //#define _LCD_DEBUG 1
75
76 SIGHANDLER(TMR0_handler)
77 {
78    unsigned char tmr0f;
79    unsigned char t0con;
80
81    // Stop timer0
82    t0con = T0CON;
83    t0con &= ~_IDLEN;
84    T0CON = t0con;
85
86    // Read IOKEYS
87 //   readkey_io();
88
89    // Clear interrupt flag
90    tmr0f = INTCON;
91    tmr0f &= ~(_TMR0IF | _TMR0IE);
92    INTCON = tmr0f;
93
94    return;
95 }
96
97
98 DEF_INTLOW(intlow_handler)
99   DEF_HANDLER(SIG_TMR0, TMR0_handler)
100 END_DEF
101
102
103 unsigned int amfreq;
104 unsigned int fmfreq;
105 unsigned char amband;
106 unsigned char fmband;
107 unsigned char fm;
108 unsigned char am_mode3k;
109 unsigned char am_userbandnum;
110 unsigned char fm_userbandnum;
111 typedef struct {
112     unsigned char mode3k; // mode3k if am
113     unsigned char start;
114     unsigned char stop;
115     unsigned int freq;
116 } _userband_t;
117 #define USER_BAND_NUM 4
118 _userband_t am_usrbands[USER_BAND_NUM];
119 _userband_t fm_usrbands[USER_BAND_NUM];
120
121 unsigned char enter_mode;
122 unsigned char numeric_mode;
123 unsigned char menu_node;
124 int backlight_long;
125 unsigned char help_flag;
126 int help_line;
127 int help_section;
128 int ui_language;
129 unsigned int ui_idlecount;
130 unsigned char scanflag;
131
132 int recv_signal;
133 int backlight_counter;
134 unsigned char backlight_level;
135
136 unsigned int writeword_eeprom(unsigned int p, unsigned int *sum, unsigned int word)
137 {
138     if(eeprom_writebyte(p, word >> 8) == 0) return p; // Error
139     *sum = calcsum_byte(*sum, (word >> 8));
140
141     if(eeprom_writebyte(p, word & 0xff) == 0) return p+1; // Error
142     *sum = calcsum_byte(*sum, word & 0xff);
143     return 0xffff;
144 }
145
146 unsigned int writebyte_eeprom(unsigned int p, unsigned int *sum, unsigned char b)
147 {
148     if(eeprom_writebyte(p, b) == 0) return p; // Error
149     *sum = calcsum_byte(*sum, b);
150     return 0xffff;
151 }
152
153 void save_eeprom(void)
154 {
155     unsigned int p = 0;
156     unsigned int sum = 0x0000;
157     unsigned char i;
158
159     // Magic word
160     writeword_eeprom(p, &sum, 0x1298);
161     p+= 2;
162     // amfreq
163     writeword_eeprom(p, &sum, amfreq);
164     p+= 2;
165     // amfreq
166     writeword_eeprom(p, &sum, fmfreq);
167     p+= 2;
168
169     writebyte_eeprom(p, &sum, amband);
170     p++;
171     writebyte_eeprom(p, &sum, fmband);
172     p++;
173     writebyte_eeprom(p, &sum, fm);
174     p++;
175     writebyte_eeprom(p, &sum, am_mode3k);
176     p++;
177     writebyte_eeprom(p, &sum, am_userbandnum);
178     p++;
179     writebyte_eeprom(p, &sum, fm_userbandnum);
180     p++;
181
182     for(i = 0 ; i < USER_BAND_NUM; i++){
183         writebyte_eeprom(p, &sum, am_usrbands[i].mode3k);
184         writebyte_eeprom(p + 1, &sum, am_usrbands[i].start);
185         writebyte_eeprom(p + 2, &sum, am_usrbands[i].stop);
186         writeword_eeprom(p + 3, &sum, am_usrbands[i].freq);
187         p += 5;
188     }
189     for(i = 0 ; i < USER_BAND_NUM; i++){
190         writebyte_eeprom(p, &sum, fm_usrbands[i].mode3k);
191         writebyte_eeprom(p + 1, &sum, fm_usrbands[i].start);
192         writebyte_eeprom(p + 2, &sum, fm_usrbands[i].stop);
193         writeword_eeprom(p + 3, &sum, fm_usrbands[i].freq);
194         p += 5;
195     }
196     // Write checksum
197     eeprom_writebyte(p, sum >> 8);
198     eeprom_writebyte(p + 1, sum & 0xff);
199     p+= 2;
200 }
201
202 unsigned int readword_eeprom(unsigned int p, unsigned int *sum)
203 {
204     unsigned char h,l;
205     unsigned int s;
206
207     h = eeprom_readbyte(p);
208     *sum = calcsum_byte(*sum, h);
209
210     l = eeprom_readbyte(p + 1);
211     *sum = calcsum_byte(*sum, l);
212
213     s = (h << 8) + l;
214     return s;
215 }
216
217 unsigned char readbyte_eeprom(unsigned int p, unsigned int *sum)
218 {
219     unsigned char b;
220
221     b = eeprom_readbyte(p);
222     *sum = calcsum_byte(*sum, b);
223
224     return b;
225 }
226
227
228 unsigned char load_eeprom(void)
229 {
230     unsigned int p = 0;
231     unsigned int sum = 0x0000;
232     unsigned char i;
233     unsigned int magic;
234
235     // Magic word
236     magic = readword_eeprom(p, &sum);
237     if(magic != 0x1298) return 0x01; // NO MAGICWORD
238     p+= 2;
239     // amfreq
240     amfreq = readword_eeprom(p, &sum);
241     p+= 2;
242     // fmfreq
243     fmfreq = readword_eeprom(p, &sum);
244     p+= 2;
245
246     amband = readbyte_eeprom(p, &sum);
247     p++;
248     fmband = readbyte_eeprom(p, &sum);
249     p++;
250     fm = readbyte_eeprom(p, &sum);
251     p++;
252     am_mode3k = readbyte_eeprom(p, &sum);
253     p++;
254     am_userbandnum = readbyte_eeprom(p, &sum);
255     p++;
256     fm_userbandnum = readbyte_eeprom(p, &sum);
257     p++;
258
259     for(i = 0 ; i < USER_BAND_NUM; i++){
260         am_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
261         am_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
262         am_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
263         am_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
264         p += 5;
265     }
266     for(i = 0 ; i < USER_BAND_NUM; i++){
267         fm_usrbands[i].mode3k = readbyte_eeprom(p, &sum);
268         fm_usrbands[i].start  = readbyte_eeprom(p + 1, &sum);
269         fm_usrbands[i].stop   = readbyte_eeprom(p + 2, &sum);
270         fm_usrbands[i].freq   = readword_eeprom(p + 3, &sum);
271         p += 5;
272     }
273     // Write checksum
274     magic = (eeprom_readbyte(p) << 8) + eeprom_readbyte(p+1);
275
276     p+= 2;
277     if(sum != magic) return 0x00;
278     return 0xff;
279 }
280
281
282 void toggle_amfm(void)
283 {
284     if(fm != 0){
285         fm = 0;
286 #ifndef _LCD_DEBUG
287         akc6955_chg_fm(fm);
288         akc6955_set_amband(amband);
289         akc6955_set_freq(amfreq);
290 #endif
291     } else {
292         fm = 0xff;
293 #ifndef _LCD_DEBUG
294         akc6955_chg_fm(fm);
295         akc6955_set_fmband(fmband);
296         akc6955_set_freq(fmfreq);
297 #endif
298     }
299 }
300
301 static void update_status(void)
302 {
303 #ifndef _LCD_DEBUG
304         recv_signal = akc6955_read_level();
305         if(fm != 0){
306             fmfreq = akc6955_get_freq();
307         } else {
308             amfreq = akc6955_get_freq();
309         }
310 #endif
311
312 }
313
314
315 void set_volume(void)
316 {
317 }
318
319 void update_display(void)
320 {
321     if(fm != 0){ // FM
322         _LOCATE(0,0);
323         if(fmband < AKC6955_BAND_TV1) {
324             printstr("FM");
325             _PUTCHAR('1' + (fmband & 7));
326         } else if(fmband < AKC6955_BAND_FMUSER){
327             printstr("TV");
328             _PUTCHAR('1' + fmband - AKC6955_BAND_TV1);
329         } else { // USER
330             printstr("FMUSR");
331         }
332     } else { // AM
333         _LOCATE(0,1);
334         if(amband == AKC6955_BAND_LW) {
335             printstr("LW");
336         } else if(amband <AKC6955_BAND_SW1) { //MW
337             printstr("MW");
338             _PUTCHAR('1' + amband - AKC6955_BAND_MW1);
339         } else if(amband < AKC6955_BAND_AMUSER) { //MW
340             printstr("SW");
341             _PUTCHAR('1' + amband - AKC6955_BAND_SW1);
342         } else if(amband == AKC6955_BAND_MW4){
343             printstr("MW4");
344         } else {
345             printstr("AMUSR");
346         }
347      }
348      _LOCATE(16-4 ,1);
349      if(fm != 0){
350          printstr("MHz");
351      } else {
352          printstr("KHz");
353      }
354      _LOCATE(16-5, 1);
355      if(fm != 0){
356          int freq_lo = fmfreq % 100;
357          int freq_hi = fmfreq / 100;
358          print_numeric(freq_hi, 0xff);
359          _PUTCHAR('.');
360          print_numeric(freq_lo, 0xff);
361      } else {
362          print_numeric(amfreq, 0xff);
363      }
364      // Signal
365      _LOCATE(0, 0);
366      printstr("S=");
367      print_numeric(recv_signal, 0xff);
368 }
369
370 void scan_start()
371 {
372     unsigned char input_flag;
373     unsigned char c;
374
375     do {
376         if(scanflag == 0){
377         // New Scan
378             _CLS();
379             printstr("Scan A=ABORT");
380             _LOCATE(0,1);
381             printstr("U=6, D=4");
382             do {
383                 input_flag = readkey_compare();
384                 idle(0xff80);
385             } while(input_flag == 0);
386             c = pop_keyinfifo();
387             if(c == charcode_6){
388                     akc6955_do_scan(0xff);
389             } else if(c == charcode_4){
390                     akc6955_do_scan(0);
391             } else {
392                 break;
393             }
394             scanflag = 0xff;
395         } else {
396             do {
397                 input_flag = readkey_compare();
398                 idle(0xff80);
399             } while(input_flag == 0);
400             c = pop_keyinfifo();
401
402             if(c == charcode_a){
403                 akc6955_abort_scan();
404                 break;
405             } else if(c == charcode_4){
406                 akc6955_abort_scan();
407                 akc6955_do_scan(0);
408                 continue;
409             } else if(c == charcode_6){
410                 akc6955_abort_scan();
411                 akc6955_do_scan(0xff);
412                 continue;
413             }
414             if(akc6955_chk_donescan() != 0) break;
415         }
416         idle(0xff00);
417     } while(1);
418     scanflag=0;
419     _CLS();
420     update_status();
421     update_display();
422 }
423
424 void setfreq_direct(void)
425 {
426     unsigned int val;
427     _CLS();
428     if(fm != 0){
429         // FM
430         _LOCATE(0,0);
431         printstr("Set Freq:FM");
432         val = fmfreq;
433         val = read_numeric(val, 5, 7, 1);
434         fmfreq = val;
435         akc6955_set_freq(val);
436     } else {
437         // FM
438         _LOCATE(0,0);
439         printstr("Set Freq:AM");
440         val = amfreq;
441         val = read_numeric(val, 5, 7, 1);
442         amfreq = val;
443         akc6955_set_freq(val);
444     }
445     idle(0xff00);
446     update_status();
447     update_display();
448 }
449
450 void setband_direct(void)
451 {
452     unsigned int band;
453     _CLS();
454     _LOCATE(0,0);
455     if(fm != 0){
456         printstr("Set Band:FM");
457         band = fmband & 7;
458         band = read_numeric(band, 2, 7, 1);
459         akc6955_set_fmband(band);
460         akc6955_do_tune();
461         idle(0xff00);
462     } else {
463         printstr("Set Band:AM");
464         band = amband & 0x1f;
465         band = read_numeric(band, 2, 7, 1);
466         akc6955_set_amband(band);
467         akc6955_do_tune();
468     }
469     idle(0xff00);
470     update_status();
471     update_display();
472 }
473
474 void call_userband(unsigned char num)
475 {
476     unsigned int freq;
477     unsigned int ch;
478     if(num >= USER_BAND_NUM) return;
479     if(fm != 0){
480         freq = fm_usrbands[num].freq;
481         ch = ((freq - 3000) / 25) * 10;
482         akc6955_set_userband(fm_usrbands[num].start, fm_usrbands[num].stop, ch,
483                             fm_usrbands[num].mode3k);
484     } else {
485         unsigned int p = 5;
486         if(am_usrbands[num].mode3k != 0) p = 3;
487         freq = am_usrbands[num].freq;
488         ch = freq / p;
489         akc6955_set_userband(am_usrbands[num].start, am_usrbands[num].stop, ch,
490                             am_usrbands[num].mode3k);
491     }
492     if(fm != 0) {
493         fmband = AKC6955_BAND_AMUSER;
494     } else {
495         amband = AKC6955_BAND_AMUSER;
496     }
497     idle(0xff00);
498     update_status();
499     update_display();
500 }
501
502 void set_userband(void)
503 {
504     unsigned int from,to;
505     unsigned char c;
506     unsigned char p;
507     unsigned char mode3k;
508     unsigned int input_flag;
509     char cc;
510
511     _CLS();
512     _LOCATE(0,0);
513     printstr("User ch:");
514     do {
515         input_flag = readkey_compare();
516         idle(0xff80);
517     } while(input_flag == 0);
518     c = pop_keyinfifo();
519
520     if(c > charcode_0) return;
521     if(c < charcode_1) return;
522     if(c == charcode_0) {
523         c = 0;
524     } else {
525         c = c - charcode_1 + 1;
526     }
527     if(c >= USER_BAND_NUM) return;
528     if(fm != 0){
529         from = fm_usrbands[c].start * 80 + 3000; // 32*25/10
530         to = fm_usrbands[c].stop * 80 + 3000;
531         _CLS();
532         _LOCATE(0,0);
533         printstr("FM #");
534         print_numeric_nosupress(c, 1);
535         printstr(" From:");
536         from = read_numeric(from, 5, 7, 1);
537         _CLS();
538         _LOCATE(0,0);
539         printstr("FM #");
540         print_numeric_nosupress(c, 1);
541         printstr(" To:");
542         to = read_numeric(to, 5, 7, 1);
543         fm_usrbands[c].start = (from - 3000) / 80;
544         fm_usrbands[c].stop = (to - 3000) / 80;
545         fm_usrbands[c].freq = from * 80 + 3000;
546         fm_userbandnum = c;
547     } else {
548         mode3k = am_usrbands[c].mode3k;
549         p = 96; // 3*32
550         if(mode3k == 0) p = 160; // 5*32
551         from = am_usrbands[c].start * p; 
552         to = am_usrbands[c].stop * p;
553         _CLS();
554         _LOCATE(0,0);
555         printstr("AM #");
556         print_numeric_nosupress(c, 1);
557         printstr(" Step:");
558         _LOCATE(0,1);
559         printstr("0=3k 1=5k");
560         do {
561             input_flag = readkey_compare();
562             idle(0xff80);
563         } while(input_flag == 0);
564         cc = pop_keyinfifo();
565
566         if(cc == charcode_0){
567             p = 96;
568             mode3k = 0xff;
569         } else if(cc = charcode_1) {
570             p = 160;
571             mode3k = 0;
572         }
573         _CLS();
574         _LOCATE(0,0);
575         printstr("AM #");
576         print_numeric_nosupress(c, 1);
577         printstr(" From:");
578         from = read_numeric(from, 5, 7, 1);
579         _CLS();
580         _LOCATE(0,0);
581         printstr("AM #");
582         print_numeric_nosupress(c, 1);
583         printstr(" To:");
584         to = read_numeric(to, 5, 7, 1);
585         am_usrbands[c].start = from / p;
586         am_usrbands[c].stop = to  / p;
587         am_usrbands[c].mode3k = mode3k;
588         am_usrbands[c].freq = from * p;
589         am_userbandnum = c;
590     }
591     call_userband(c);
592 }
593
594 void input_userband(void)
595 {
596     unsigned char c;
597     unsigned char input_flag;
598     do{
599     _CLS();
600     _LOCATE(0,0);
601     printstr("User Band");
602     _LOCATE(0,1);
603     printstr("   #");
604     do {
605         input_flag = readkey_compare();
606         idle(0xff80);
607     } while(input_flag == 0);
608     c = pop_keyinfifo();
609
610     if((c >= charcode_a) && (c <= charcode_f)){
611         break;
612     }
613     if(c == charcode_0) {
614         _PUTCHAR('0');
615         if(fm != 0){
616            fm_userbandnum = 0;
617         } else {
618            am_userbandnum = 0;
619         }
620         call_userband(0);
621     } else {
622         c = c - charcode_1 + 1;
623         if(c < USER_BAND_NUM) {
624             _PUTCHAR(c + '0');
625             if(fm != 0){
626                fm_userbandnum = c;
627             } else {
628                 am_userbandnum = c;
629             }
630             call_userband(c);
631         }
632     }
633     idle(0xff00);
634     } while(1);
635     _CLS();
636 }
637
638
639 void main_menu(void)
640 {
641     unsigned char c;
642     unsigned int input_flag;
643     _CLS();
644     _LOCATE(0,0);
645     printstr("Menu:F=HELP");
646     _LOCATE(1,0);
647     printstr("A=CANCEL");
648     do{
649         do {
650             input_flag = readkey_compare();
651             idle(0xff80);
652         } while(input_flag == 0);
653
654         c = pop_keyinfifo();
655         if((c < charcode_1) || ( c >charcode_s3)) {
656             idle(0xff00);
657             continue; // Error
658         }
659         if(c == charcode_f){
660             // HELP
661         } else if(c == charcode_a){
662             // Cancel
663             break;
664         } else if(c == charcode_1){
665             // AM
666             fm = 0;
667             akc6955_chg_fm(fm);
668             akc6955_set_amband(amband);
669             akc6955_set_freq(amfreq);
670             break;
671         } else if(c == charcode_2){
672             // Band
673             setband_direct();
674             break;
675         } else if(c == charcode_3){
676             // Band
677             setfreq_direct();
678             break;
679         } else if(c == charcode_4){
680             // fm
681             fm = 0xff;
682             akc6955_chg_fm(fm);
683             akc6955_set_fmband(fmband);
684             akc6955_set_freq(fmfreq);
685             break;
686         } else if(c == charcode_5){
687             // Scan
688             break;
689         } else if(c == charcode_6){
690             // Set gain
691             break;
692         } else if(c == charcode_7){
693             // Set volume
694             break;
695         } else if(c == charcode_8){
696             // Set sensitivity
697             break;
698         } else if(c == charcode_9){
699             // Set NF
700             break;
701         } else if(c == charcode_0){
702             // Setup Menu
703             break;
704         } else if(c == charcode_b){
705             // Call userband
706             input_userband();
707             break;
708         } else if(c == charcode_c){
709             // Set userband
710             set_userband();
711             break;
712         } else if(c == charcode_d){
713             // Reserve
714             break;
715         } else if(c == charcode_e){
716             // Reserve
717             break;
718         }
719         idle(0xff00);
720     } while(1);
721 }
722
723 void setfreq_updown(unsigned char ctlword)
724 {
725 #ifndef _LCD_DEBUG
726     switch(ctlword){
727         case charcode_8: // Change band
728             if(fm != 0){
729                 amband++;
730                 if(amband > 18) amband = 0;
731 //                amfreq = akc6955_setfreq(amfreq)
732                 akc6955_set_amband(amband);
733                 _AKC6955_WAIT_62_5MS(); // 62.5ms
734                 amband = akc6955_get_amband();
735                 amfreq = akc6955_get_freq();
736             } else {
737                 fmband++;
738                 if(fmband > 7) fmband = 0;
739 //                amfreq = akc6955_setfreq(amfreq)
740                 akc6955_set_fmband(fmband);
741                 _AKC6955_WAIT_62_5MS(); // 62.5ms
742                 fmband = akc6955_get_fmband();
743                 fmfreq = akc6955_get_freq();
744             }
745             break;
746         case charcode_2: // Change band
747             if(fm != 0){
748                 amband--;
749                 if(amband == 0) amband = 18;
750                 if(amband >= 18) amband = 18;
751 //                amfreq = akc6955_setfreq(amfreq)
752                 akc6955_set_amband(amband);
753                 _AKC6955_WAIT_62_5MS(); // 62.5ms
754                 amband = akc6955_get_amband();
755                 amfreq = akc6955_get_freq();
756             } else {
757                 fmband--;
758                 if(fmband == 0) fmband = 7;
759                 if(fmband >= 7) fmband = 7;
760 //                amfreq = akc6955_setfreq(amfreq)
761                 akc6955_set_fmband(fmband);
762                 _AKC6955_WAIT_62_5MS(); // 62.5ms
763                 fmband = akc6955_get_fmband();
764                 fmfreq = akc6955_get_freq();
765             }
766             break;
767         case charcode_4: // Down Freq;
768             if(fm != 0){
769                 fmfreq = akc6955_down_freq(10); // DOWN 100KHz
770             } else {
771                 amfreq = akc6955_down_freq(10); // DOWN 10KHz
772             }
773             break;
774         case charcode_6: // Down Freq;
775             if(fm != 0){
776                 fmfreq = akc6955_up_freq(10); // UP 100KHz
777             } else {
778                 amfreq = akc6955_up_freq(10); // UP 10KHz
779             }
780             break;
781         case charcode_7: // Down Fast;
782             if(fm != 0){
783                 fmfreq = akc6955_down_freq(50); // DOWN 500KHz
784             } else {
785                 amfreq = akc6955_down_freq(50); // DOWN 50KHz
786             }
787             break;
788         case charcode_9: // Down Fast;
789             if(fm != 0){
790                 fmfreq = akc6955_up_freq(50); // UP 100KHz
791             } else {
792                 amfreq = akc6955_up_freq(50); // UP 10KHz
793             }
794             break;
795         case charcode_1: // Down Slow;
796             if(fm != 0){
797                 fmfreq = akc6955_down_freq(5); // DOWN 50KHz
798             } else {
799                 amfreq = akc6955_down_freq(5); // DOWN 50KHz
800             }
801             break;
802         case charcode_3: // Down Slow;
803             if(fm != 0){
804                 fmfreq = akc6955_up_freq(5); // UP 50KHz
805             } else {
806                 amfreq = akc6955_up_freq(5); // UP 5KHz
807             }
808             break;
809         case charcode_0: // Step
810             if(fm == 0){
811                 if(am_mode3k == 0) {
812                     am_mode3k = 0xff;
813                 } else {
814                     am_mode3k = 0;
815                 }
816                 amfreq = akc6955_mode3k(am_mode3k);
817             }
818             break;
819         case charcode_a: // Toggle FM
820             toggle_amfm();
821             break;
822         case charcode_b:
823             input_userband();
824             break;
825         case charcode_c:
826             set_userband();
827             break;
828         case charcode_d:
829             set_volume();
830             break;
831         case charcode_e: // Backlight ON/OFF
832             if(backlight_counter > 0) {
833                backlight_counter = 0;
834             } else {
835                backlight_counter = backlight_long;
836             }
837             break;
838         case charcode_f:
839             main_menu();
840             break;
841         default:
842             break;
843     }
844 #endif
845 }
846 /*
847  * 
848  */
849 static void setdefault(void)
850 {
851     char i;
852     amfreq = 954;
853     fmfreq = 8000; // 10KHz order.
854     amband = AKC6955_BAND_MW2;
855     fmband = AKC6955_BAND_FM2;
856     am_mode3k = 0xff;
857     fm = 0;
858     recv_signal = 0;
859     am_userbandnum = 0;
860     fm_userbandnum = 0;
861     for(i = 0; i < 4; i++){
862         am_usrbands[i].start = 0x19;
863         am_usrbands[i].stop  = 0x32;
864     }
865     for(i = 0; i < 4; i++){
866         fm_usrbands[i].start = 0x19;
867         fm_usrbands[i].stop  = 0x32;
868     }
869
870
871 }
872
873 int main(void)
874 {
875     char readchar;
876     unsigned char input_flag;
877     unsigned char c;
878     unsigned int i;
879 #ifdef _LCD_DEBUG
880     unsigned char power_flag;
881 #endif
882 //    OSCCON =  _IDLEN & 0b11111100;
883
884     keyin_init();
885     keyin_ioinit();
886     idle_init();
887
888     //i2c1_init();
889
890     _AKC6955_WAIT_125_0MS(); // Wait 125ms
891 #ifdef _LCD_DEBUG
892     power_flag = 0xff;
893 #endif
894     backlight_long = 256;
895     backlight_counter = backlight_long;
896     backlight_level = 255;
897     ui_idlecount = 0xf800;
898
899     acm1602_init(0xa0, 1); //Init LCD
900     _AKC6955_WAIT_125_0MS(); // Wait 125ms
901   //  _LOCATE(0,0);
902   //  _PUTCHAR('c');
903     _LOCATE(0,0);
904     printstr("Hello;-)");
905    acm1602_setbacklight(0xff);
906 //   PORTD |= _LCDPORT_CONT_RS;
907 #if 1
908     i = 10001;
909     do {
910         idle(0x8000);
911         ClrWdt();
912         _LOCATE(0,1);
913         print_numeric_nosupress(i, 5);
914 //       print_numeric(i, 1);
915  //       PORTDbits.RD3 = i & 1;
916         i = i + 1;
917     }while(1);
918 #else
919     switch(load_eeprom()) {
920         case 0x01: // No magic-word
921             idle(0xff80);
922             _CLS();
923             setdefault();
924             _LOCATE(0,0);
925             printstr("EEPROM FORMATTING");
926             _LOCATE(0,1);
927             printstr("Press any key");
928             do {
929                 input_flag = readkey_compare();
930                 idle(0xff80);
931             } while(input_flag == 0);
932             c = pop_keyinfifo();
933             format_eeprom(0,254);
934             save_eeprom();
935             break;
936         case 0x00: // Checksum error
937             idle(0xff80);
938             _CLS();
939             _LOCATE(0,0);
940             printstr("X-) Sum Error");
941             _LOCATE(0,1);
942             printstr("Press any key to format");
943             do {
944                 input_flag = readkey_compare();
945                 idle(0xff80);
946             } while(input_flag == 0);
947             
948             c = pop_keyinfifo();
949             format_eeprom(0,254);
950             setdefault();
951             save_eeprom();
952             break;
953         case 0xff: // Success
954             break;
955         default: // Unknown error
956             setdefault();
957             break;
958     }
959     // Init AKC6955
960     /* Check EEPROM */
961     /* Push default parameters to AKC6955*/
962     scanflag = 0;
963 #ifndef _LCD_DEBUG
964     akc6955_chg_fm(fm); // Set to AM
965     akc6955_set_amband(amband);
966     akc6955_set_freq(amfreq); // Dummy, TBS (954KHz)
967     akc6955_set_power(0xff); // Power ON
968 #endif
969 #ifdef _LCD_DEBUG
970     idle(0xf000);
971 #else
972     idle(0xff00);
973 #endif
974     _CLS();
975     update_status();
976     update_display();
977     do {
978         /* Main routine*/
979         input_flag = readkey_compare();
980         if(input_flag != 0){
981             readchar = pop_keyinfifo();
982             if((readchar >= charcode_1) && (readchar <= charcode_f)) {
983                     setfreq_updown(readchar);
984             } else {
985                 // Other is skip
986             }
987         }
988         // Check battery (include idle?)
989         // Read AKJC6955's status
990         update_status();
991         // Putstring to LCD.
992
993         update_display();
994         if(backlight_counter > 0) {
995             backlight_counter--;
996             set_backlight(0xff, backlight_level); // Turn ON
997         } else {
998             set_backlight(0x00, 0); // Turn OFF
999         }
1000 #ifdef _LCD_DEBUG
1001         if(power_flag != 0x00) {
1002             power_flag = 0x00;
1003         } else {
1004             power_flag = 0xff;
1005         }
1006         setsignal_tune(power_flag);
1007 #endif
1008         idle(ui_idlecount);
1009     } while(1);
1010 #endif
1011 }
1012