3 * Config & Main routine.
4 * Copyright (C) 2013-06-10 K.Ohta <whatisthis.sowhat ai gmail.com>
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.
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,
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.
34 #include <pic18fregs.h> /* ONLY FOR PIC18x */
42 #include "lcd_acm1602.h"
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
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
73 SIGHANDLER(TMR0_handler)
86 // Clear interrupt flag
95 DEF_INTLOW(intlow_handler)
96 DEF_HANDLER(SIG_TMR0, TMR0_handler)
102 unsigned char amband;
103 unsigned char fmband;
105 unsigned char am_mode3k;
106 unsigned char am_userbandnum;
107 unsigned char fm_userbandnum;
110 int backlight_counter;
111 unsigned char backlight_level;
118 _userband_t am_usrbands[4];
119 _userband_t fm_usrbands[4];
122 void toggle_amfm(void)
128 akc6955_set_amband(amband);
129 akc6955_set_freq(amfreq);
135 akc6955_set_fmband(fmband);
136 akc6955_set_freq(fmfreq);
141 void update_display(void)
145 if(fmband < AKC6955_BAND_TV1) {
147 _PUTCHAR('1' + (fmband & 7));
148 } else if(fmband < AKC6955_BAND_FMUSER){
150 _PUTCHAR('1' + fmband - AKC6955_BAND_TV1);
156 if(amband == AKC6955_BAND_LW) {
158 } else if(amband <AKC6955_BAND_SW1) { //MW
160 _PUTCHAR('1' + amband - AKC6955_BAND_MW1);
161 } else if(amband < AKC6955_BAND_AMUSER) { //MW
163 _PUTCHAR('1' + amband - AKC6955_BAND_SW1);
164 } else if(amband == AKC6955_BAND_MW4){
178 int freq_lo = fmfreq % 100;
179 int freq_hi = fmfreq / 100;
180 print_numeric(freq_hi);
182 print_numeric(freq_lo);
190 print_numeric(recv_signal);
194 void setfreq_updown(unsigned char ctlword)
198 case charcode_8: // Change band
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();
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();
217 case charcode_2: // Change band
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();
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();
238 case charcode_4: // Down Freq;
240 fmfreq = akc6955_down_freq(10); // DOWN 100KHz
242 amfreq = akc6955_down_freq(10); // DOWN 10KHz
245 case charcode_6: // Down Freq;
247 fmfreq = akc6955_up_freq(10); // UP 100KHz
249 amfreq = akc6955_up_freq(10); // UP 10KHz
252 case charcode_7: // Down Fast;
254 fmfreq = akc6955_down_freq(50); // DOWN 500KHz
256 amfreq = akc6955_down_freq(50); // DOWN 50KHz
259 case charcode_9: // Down Fast;
261 fmfreq = akc6955_up_freq(50); // UP 100KHz
263 amfreq = akc6955_up_freq(50); // UP 10KHz
266 case charcode_1: // Down Slow;
268 fmfreq = akc6955_down_freq(5); // DOWN 50KHz
270 amfreq = akc6955_down_freq(5); // DOWN 50KHz
273 case charcode_3: // Down Slow;
275 fmfreq = akc6955_up_freq(5); // UP 50KHz
277 amfreq = akc6955_up_freq(5); // UP 5KHz
280 case charcode_0: // Step
287 amfreq = akc6955_mode3k(am_mode3k);
301 unsigned char input_flag;
304 unsigned char power_flag;
312 _AKC6955_WAIT_125_0MS(); // Wait 125ms
316 backlight_counter = 128;
317 backlight_level = 255;
318 acm1602_init(0xa0, 1); //Init LCD
319 // _AKC6955_WAIT_125_0MS(); // Wait 125ms
321 printstr("Hello;-)");
326 fmfreq = 8000; // 10KHz order.
327 amband = AKC6955_BAND_MW2;
328 fmband = AKC6955_BAND_FM2;
334 for(i = 0; i < 4; i++){
335 am_usrbands[i].start = 0x19;
336 am_usrbands[i].stop = 0x32;
338 for(i = 0; i < 4; i++){
339 fm_usrbands[i].start = 0x19;
340 fm_usrbands[i].stop = 0x32;
344 /* Push default parameters to AKC6955*/
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
360 input_flag = readkey_compare();
362 readchar = pop_keyinfifo();
364 // Top of input-tree.
371 backlight_counter = 128;
379 case charcode_null: // None
382 // Numeric 0to9, or a to f.
383 setfreq_updown(readchar);
388 recv_signal = akc6955_read_level();
390 fmfreq = akc6955_get_freq();
392 amfreq = akc6955_get_freq();
395 // Check battery (include idle?)
396 // Read AKJC6955's status
400 if(backlight_counter > 0) {
402 set_backlight(0xff, backlight_level); // Turn ON
404 set_backlight(0x00, 0); // Turn OFF
407 if(power_flag != 0x00) {
412 setsignal_tune(power_flag);