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"
59 #if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
60 //#pragma stack 0x200 256 // Set stack size to 256bytes.
61 #pragma config FOSC=INTIO67,BORV=190,BOREN=ON,PWRTEN=ON
62 #pragma config WDTEN=ON,WDTPS=32768
63 #pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF,DEBUG=ON,XINST=ON
64 //#pragma config PBADEN=OFF,MCLRE=EXTMCLR,STVREN=ON,LVP=OFF//,XINST=ON
65 #pragma config CP0=OFF,CP1=OFF,CPB=OFF,CPD=OFF
66 #pragma config WRT0=OFF,WRT1=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
67 #pragma config EBTR0=OFF,EBTR1=OFF,EBTRB=OFF
69 // For 4xK20 or 2xK20 Series
70 #if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) || \
71 defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) || \
72 defined(_18F43K20) || defined(_18F44K20) || defined(_18F45K20) || defined(_18F46K20) || \
73 defined(_18F23K20) || defined(_18F24K20) || defined(_18F25K20) || defined(_18F26K20)
76 #pragma stack 0x200 256
79 #pragma config FOSC=HS,FCMEN=ON,PWRT=ON,BOREN=NOSLP,BORV=27, \
80 WDTEN=ON,WDTPS=32768,PBADEN=OFF,HFOFST=OFF,LPT1OSC=OFF, \
81 MCLRE=ON,STVREN=ON,DEBUG=ON, \
87 unsigned char stereoflag;
88 unsigned char tuneflag;
89 unsigned char cnrlevel;
91 unsigned int batlevel_6955;
92 unsigned int battlevel;
95 int backlight_counter;
96 unsigned char backlight_level;
97 unsigned char pollkeybuf[33];
99 //#define _LCD_DEBUG 1
101 void TMR0_handler(void)
103 SIGHANDLER(TMR0_handler)
108 T0CONbits.TMR0ON = 0;
110 // Clear interrupt flag
111 // INTCONbits.TMR0IF = 0;
112 INTCONbits.TMR0IE = 0;;
117 * Interrupt wake up every 1ms.
120 void TMR3_Handler(void)
122 SIGHANDLER(TMR3_Handler)
127 T3CONbits.TMR3ON = 0;
131 void EXINT_Handler(void)
133 SIGHANDLER(EXINT_Handler)
136 INTCONbits.INT0IE = 0;
137 INTCONbits.INT0IF = 0;
138 INTCON3bits.INT1IF = 0;
139 INTCON3bits.INT2IF = 0;
140 INTCON3bits.INT1IE = 0;
141 INTCON3bits.INT2IE = 0;
145 void RBIF_handler(void)
147 SIGHANDLER(RBIF_handler)
151 // if(chk_powerbutton(0, 0) != 0) { // If pressed on
156 void EEPROM_handler(void)
158 SIGHANDLER(EEPROM_handler)
163 // if(chk_powerbutton(0, 0) != 0) { // If pressed on
169 void INADC_handler(void)
171 SIGHANDLER(INADC_handler)
177 // battlevel = adc_rawtobatt(a);
182 void I2C_handler(void)
184 SIGHANDLER(I2C_handler)
190 void I2CBus_handler(void)
192 SIGHANDLER(I2CBus_handler)
201 DEF_INTLOW(intlow_handler)
202 DEF_HANDLER(SIG_TMR0, TMR0_handler)
203 DEF_HANDLER(SIG_INT0, EXINT_Handler)
204 // DEF_HANDLER(SIG_TMR3, I2C_handler)
207 DEF_INTHIGH(inthigh_handler)
208 DEF_HANDLER(SIG_RBIF, RBIF_handler)
209 DEF_HANDLER(SIG_EEIF, EEPROM_handler)
210 DEF_HANDLER(SIG_TMR3, TMR3_Handler)
211 DEF_HANDLER(SIG_INT1, EXINT_Handler)
212 DEF_HANDLER(SIG_INT2, EXINT_Handler)
213 DEF_HANDLER(SIG_AD, INADC_handler)
214 //DEF_HANDLER(SIG_SSP, I2C_handler)
215 //DEF_HANDLER(SIG_BCOL, I2CBus_handler)
218 void interrupt high_priority intlow_handler(void)
220 if(INTCONbits.TMR0IF) TMR0_handler();
221 if(INTCONbits.INT0IF) EXINT_Handler();
224 void interrupt high_priority inthigh_handler(void)
226 if(INTCONbits.RBIF) RBIF_handler();
227 if(PIR2bits.EEIF) EEPROM_handler();
228 if(PIR2bits.TMR3IF) TMR3_Handler();
229 if(INTCONbits.TMR0IF) TMR0_handler();
230 if(INTCON3bits.INT1IF) EXINT_Handler();
231 if(INTCON3bits.INT2IF) EXINT_Handler();
232 if(PIR1bits.ADIF) INADC_handler();
234 // if(PIR1bits.SSPIF) I2C_handler();
247 // idle_time_ms(100);
248 printhelp_2lines("Low battery X)", "Press key to suspend");
255 unsigned int sum = 0;
256 unsigned char pbutton;
257 unsigned char reset_status;
260 OSCCON = (0x80 & 0b11111100) | 0b00111000;
261 // OSCCON = (0x80 & 0b11111100) | 0b00110010; // 8MHz
265 WDTCONbits.SWDTEN = 0; // WDT OFF.
266 reset_status = chk_reset();
267 idle_time_ms(300); // Wait for setup.
268 switch(reset_status){
270 shutdown(0); // When press reset, state to power_off.
272 case RESET_WDT: // Workaround random reset.
276 shutdown(0); // Save and halt on BOR.
278 case RESET_SOFTWARE: //
280 // pbutton = chk_powerbutton();
282 // if(pbutton == 0) power_off(0); // Button not pressed.
283 // } while(pbutton == 0);
288 WDTCONbits.SWDTEN = 1; // WDT ON.
296 acm1602_init(0xa0, 1); //Init LCD
298 _LOCATE(0,0); // It's BAD-KNOWHOW, but needs AKIZUKI'S LCD :(
301 printstr("Hello;-)");
302 lcd_setbacklight(0xff, 255);
306 /* Push default parameters to AKC6955*/
315 lcd_setbacklight(0xff, 255);
317 if(battlevel <= 340) { // 3.4V
321 c = pollkeys(pollkeybuf, 5, 1); // 23*10=135ms
325 setfreq_updown(pollkeybuf[p]);
329 // idle_time_ms(ui_idlecount);
331 pbutton = chk_powerbutton();
332 if(pbutton != 0) shutdown(1); // Button pressed.
334 if(backlight_counter > 0) {
336 lcd_setbacklight(0xff, backlight_level); // Turn ON
338 lcd_setbacklight(0x00, 0); // Turn OFF