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.
36 #include <pic18fregs.h> /* ONLY FOR PIC18x */
48 #include "lcd_acm1602.h"
55 #include "i2c_eeprom.h"
56 #include "backlight.h"
62 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
63 //#pragma stack 0x200 256 // Set stack size to 256bytes.
64 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
65 #pragma config WDTEN=ON,WDTPS=32768
66 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON,XINST=ON
67 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
68 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
69 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
70 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
72 // For 4xK20 or 2xK20 Series
73 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
74 defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
75 defined(_18F43K20) || defined(_18F44K20) || defined(_18F45K20) || defined(_18F46K20) || \
76 defined(_18F23K20) || defined(_18F24K20) || defined(_18F25K20) || defined(_18F26K20)
79 #pragma stack 0x200 256
82 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=NOSLP,BORV=27, \
83 WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=OFF,LPT1OSC=OFF, \
84 MCLRE=ON,STVREN=ON,DEBUG=ON, \
90 unsigned char stereoflag;
91 unsigned char tuneflag;
92 unsigned char cnrlevel;
94 unsigned int batlevel_6955;
95 unsigned int battlevel;
96 unsigned char ui_idlekey;
97 unsigned char ui_idlepad;
100 unsigned char pollkeybuf[33];
102 //#define _LCD_DEBUG 1
104 void TMR0_handler(void)
106 SIGHANDLER(TMR0_handler)
111 T0CONbits.TMR0ON = 0;
113 // Clear interrupt flag
114 // INTCONbits.TMR0IF = 0;
115 INTCONbits.TMR0IE = 0;;
120 * Interrupt wake up every 1ms.
123 void TMR1_Handler(void)
125 SIGHANDLER(TMR1_Handler)
129 // rencoder_restart();
130 T1CON = 0b10110100;// RD16, T1RUN=0, 1/32 fOSC, TNR1CS=0, TMR1ON=0;
131 TMR1H = 63036 >> 8; // Tick = 4us, Count = 2500
136 T1CONbits.TMR1ON = 1;
140 void EXINT_Handler(void)
142 SIGHANDLER(EXINT_Handler)
145 INTCONbits.INT0IE = 0;
146 INTCONbits.INT0IF = 0;
147 INTCON3bits.INT1IF = 0;
148 INTCON3bits.INT2IF = 0;
149 INTCON3bits.INT1IE = 0;
150 INTCON3bits.INT2IE = 0;
154 void RBIF_handler(void)
156 SIGHANDLER(RBIF_handler)
160 // if(chk_powerbutton(0, 0) != 0) { // If pressed on
165 void EEPROM_handler(void)
167 SIGHANDLER(EEPROM_handler)
172 // if(chk_powerbutton(0, 0) != 0) { // If pressed on
178 void INADC_handler(void)
180 SIGHANDLER(INADC_handler)
186 // battlevel = adc_rawtobatt(a);
191 void I2C_handler(void)
193 SIGHANDLER(I2C_handler)
199 void I2CBus_handler(void)
201 SIGHANDLER(I2CBus_handler)
210 DEF_INTLOW(intlow_handler)
211 DEF_HANDLER(SIG_TMR0, TMR0_handler)
212 DEF_HANDLER(SIG_INT0, EXINT_Handler)
213 // DEF_HANDLER(SIG_TMR1, I2C_handler)
216 DEF_INTHIGH(inthigh_handler)
217 DEF_HANDLER(SIG_RBIF, RBIF_handler)
218 DEF_HANDLER(SIG_EEIF, EEPROM_handler)
219 DEF_HANDLER(SIG_TMR1, TMR1_Handler)
220 DEF_HANDLER(SIG_INT1, EXINT_Handler)
221 DEF_HANDLER(SIG_INT2, EXINT_Handler)
222 DEF_HANDLER(SIG_AD, INADC_handler)
223 //DEF_HANDLER(SIG_SSP, I2C_handler)
224 //DEF_HANDLER(SIG_BCOL, I2CBus_handler)
227 void interrupt low_priority intlow_handler(void)
229 if(INTCONbits.TMR0IF) TMR0_handler();
230 if(INTCONbits.INT0IF) EXINT_Handler();
233 void interrupt high_priority inthigh_handler(void)
235 if(INTCONbits.RBIF) RBIF_handler();
236 if(PIR2bits.EEIF) EEPROM_handler();
237 if(PIR1bits.TMR1IF) TMR1_Handler();
238 // if(INTCONbits.TMR0IF) TMR0_handler();
239 // if(INTCON3bits.INT1IF) EXINT_Handler();
240 // if(INTCON3bits.INT2IF) EXINT_Handler();
241 if(PIR1bits.ADIF) INADC_handler();
243 // if(PIR1bits.SSPIF) I2C_handler();
256 // idle_time_ms(100);
257 printhelp_2lines("Low battery X)", "Press key to suspend");
264 unsigned char pbutton;
265 unsigned char reset_status;
267 unsigned char lvcount = 0;
268 unsigned char dispf = 0xff;
271 OSCCON = (0x80 & 0b11111100) | 0b00111000;
272 // OSCCON = (0x80 & 0b11111100) | 0b00110010; // 8MHz
277 reset_status = chk_reset();
278 idle_time_ms(300); // Wait for setup.
279 WDTCONbits.SWDTEN = 0; // WDT OFF.
280 switch(reset_status){
283 shutdown(0); // Save and halt on BOR.
285 case RESET_SOFTWARE: //
287 pbutton = chk_powerbutton();
288 if(pbutton == 0) shutdown(0); // Not-Pressed power-button -> shutdown( not save).
291 case RESET_WDT: // Workaround random reset.
297 WDTCONbits.SWDTEN = 1; // WDT ON.
302 acm1602_init(LCD_I2CADDR, 1); //Init LCD
303 lcd_setbacklight(0xff, 255);
306 /* Push default parameters to AKC6955*/
314 ui_idlekey = setup.ui_idlecount / 92;
315 ui_idlepad = setup.ui_idlecount % 23;
320 if(battlevel < 340) { // 3.4V
324 acm1602_resume(LCD_I2CADDR);
327 lowbatt(); //Zap 4Times on LowVoltage.
336 for(i = 0; i < 4; i++) {
337 c = pollkey_single_timeout(ui_idlekey, 1); // 23*41 = 943ms
339 if(c != charcode_null) {
342 acm1602_resume(LCD_I2CADDR);
351 if(renc_dir == RENC_LEFT) {
352 akc6955_down_freq(renc_count);
354 } else if(renc_dir == RENC_RIGHT) {
355 akc6955_up_freq(renc_count);
361 dispf = backlight_dec(dispf);
362 if(dispf != 0) update_display();
363 if(ui_idlepad != 0) idle_time_ms(ui_idlepad);