--- /dev/null
+/*
+ * OpenI2CRADIO
+ * Internal ADCONVERTER Handler
+ * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ * This library / program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; see the file COPYING. If not, write to the
+ * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * As a special exception, if you link this(includeed from sdcc) library
+ * with other files, some of which are compiled with SDCC,
+ * to produce an executable, this library does not by itself cause
+ * the resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why
+ * the executable file might be covered by the GNU General Public License.
+ */
+
+#include "adc_int.h"
+
+void intadc_init(void)
+{
+#if defined(pic18f23k22) || defined(pic18f24k22) || defined(pic18f25k22) || defined(pic18f26k22)
+ ANSELA = AN_A_VAL;
+ ANSELB = AN_B_VAL;
+ ANSELC = AN_C_VAL;
+#endif
+#if defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20)
+ ANSEL= _ADC_INITF;
+ ANSELH = _ADCH_INITF;
+#endif
+#if defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20)
+ ANSEL= _ADC_INITF;
+ ANSELH = _ADCH_INITF;
+#endif
+ PIR1bits.ADIF = 0;
+ PIE1bits.ADIE = 0;
+ IPR1bits.ADIP = 1; // High
+}
+
+void startadc(unsigned char ch)
+{
+ unsigned char a;
+ ADCON1bits.VCFG1 = 0;
+ ADCON1bits.VCFG0 = 0;
+ ADCON2 = 0b10110110;
+ a = 0b00000001; // Select AN7, Start.
+ switch(ch){
+ case 0:
+ a |= 0b00011100; // AN7
+ break;
+ default:
+ a |= 0b00011100; // AN7
+ break;
+ }
+ ADCON0 = a;
+ // Delay = 5uS + Tc + (tempalature - 25) * 0.05uS
+// delay10tcy(8); // Tc = -13.5pF*(1K + 700 + (13K//39K)) = 1.52uS
+ // Wait < 1.52 + 5 + 1.25 = 7.77uS-> 8uS
+ PIR1bits.ADIF = 0;
+ PIE1bits.ADIE = 0;
+ IPR1bits.ADIP = 1; // High
+ ADCON0bits.GO_NOT_DONE = 1;
+}
+
+unsigned int polladc(void)
+{
+ unsigned int a;
+ if(ADCON0bits.DONE == 1){ // converting or not enable.
+ PIE1bits.ADIE = 1;
+ PIR1bits.ADIF = 0;
+ return 0xffff;
+ } else { // Done, Clear interrupt
+// if(ADCON0bits.ADON == 0){
+// a = 0xffff;
+// } else {
+ a = ((ADRESH << 8) + ADRESL) & 0x03ff;
+// }
+ PIE1bits.ADIE = 0;
+ PIR1bits.ADIF = 0;
+// ADCON0bits.GO_NOT_DONE = 0;
+// ADCON0bits.ADON = 0;
+ return a;
+ }
+}
+
+unsigned int polladc2(void)
+{
+ unsigned int a = 0;
+ do {
+ delay10tcy(8); // 10uS
+ a = ((ADRESH << 8) + ADRESL) & 0x03ff;
+ } while(ADCON0bits.DONE == 1);
+ return a;
+}
+
+void stopadc(void)
+{
+ ADCON0bits.GO_NOT_DONE = 0;
+// ADCON0bits.ADON = 0;
+ PIR1bits.ADIF = 0;
+ PIE1bits.ADIE = 0;
+}
+
+unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
+{
+ unsigned int i;
+ // raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
+ // Fullscale = 0.5 (1.625V, raw = 6.5V)
+ // raw = b * 0.5 * 4 = b * 2 (b = 1024 6.500V)
+ i = b >> 3; // div = 1/8
+ // Fullscale = 13.2v =
+ i = (i * reflevel) >> 5;
+ return i;
+}
\ No newline at end of file
--- /dev/null
+/*
+ * OpenI2CRADIO
+ * Internal ADCONVERTER Handler
+ * Copyright (C) 2013-06-20 K.Ohta <whatisthis.sowhat ai gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ * This library / program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; see the file COPYING. If not, write to the
+ * Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * As a special exception, if you link this(includeed from sdcc) library
+ * with other files, some of which are compiled with SDCC,
+ * to produce an executable, this library does not by itself cause
+ * the resulting executable to be covered by the GNU General Public License.
+ * This exception does not however invalidate any other reasons why
+ * the executable file might be covered by the GNU General Public License.
+ */
+
+#ifndef ADC_INT_H
+#define ADC_INT_H
+
+#include <sdcc-lib.h>
+#include <pic18fregs.h> /* ONLY FOR PIC18x */
+#include <signal.h>
+
+#include "iodef.h"
+#include "idle.h"
+#include "ui.h"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+extern void intadc_init(void);
+extern void startadc(unsigned char ch);
+extern unsigned int polladc(void);
+extern void stopadc(void);
+extern unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel);
+extern unsigned int polladc2(void);
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ADC_INT_H */
+
idle(65535 - 488 + 4);
}
-void idle_35ms(void)
+void idle_time_35ms(void)
{
// Tim = 35 / 0.128 = 273.44
idle(65535 - 274 + 1);
#else
#define TRIS_D_RVAL 0b11111111 /* FOR INPUT*/
#endif
-#define TRIS_E_VAL 0b00000111 /* FOR OUTPUT, ADC, PSP=OFF */
+#define TRIS_E_VAL 0b00000100 /* FOR OUTPUT, ADC, PSP=OFF */
+#define _BATT_ADC AN7
+#define _ADC_INITF _ANS7
+#define _ADCH_INITF 0x00
+#define _PORT_MUTE LATEbits.LATE0
+#define _PORT_RADIOPOW LATEbits.LATE1
#define _LCD_BACKLIGHT _PORTC_RC2
#define _LCD_PORT LATC
}
}
+void set_examp_mute(unsigned char f)
+{
+ if(f == 0x00) {
+ _PORT_MUTE = 0; // Not Mute
+ } else {
+ _PORT_MUTE = 1;
+ }
+}
+
+void set_radiopower(unsigned char f)
+{
+ if(f == 0x00) {
+ _PORT_RADIOPOW = 0; // OFF
+ } else {
+ _PORT_RADIOPOW = 1; // ON
+ }
+}
void set_powerlamp(unsigned char f)
{
/* Initialize IOPORTS*/
PORTA = 0x00;
LATA = 0x00;
- ANSEL = 0x00; // Use RA0 AS ADC, Another is not used.
+ ANSEL = 0x80; // Use RA7 AS ADC, Another is not used.
ANSELH = 0x00; //
TRISA = TRIS_A_VAL;
LATD = 0x00;
TRISD = TRIS_D_VAL;
- PORTE = 0x00;
TRISE = TRIS_E_VAL;
+ PORTE = 0b00000000; // Mute OK
+
+ // Interrupts
INTCONbits.INT0IE = 0;
INTCONbits.INT0IF = 0;
INTCON3bits.INT1IF = 0;
extern void io_intcountinit(void);
extern void TMR3_set(void);
+extern void set_radiopower(unsigned char f);
+extern void set_examp_mute(unsigned char f);
+
+
extern void readkey_io(unsigned char state);
extern unsigned char statecount;
#ifdef __cplusplus
#include "ioports.h"
#include "menu.h"
#include "power.h"
+#include "adc_int.h"
/*
* Config words.
//#pragma config WRTC=OFF,WRTB=OFF,WRTD=OFF
//#pragma config EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF
#endif
+unsigned int amfreq;
+unsigned int fmfreq;
+unsigned char amband;
+unsigned char fmband;
+unsigned char fm;
+unsigned char am_mode3k;
+unsigned char am_userbandnum;
+unsigned char fm_userbandnum;
+_userband_t am_usrbands[USER_BAND_NUM];
+_userband_t fm_usrbands[USER_BAND_NUM];
+unsigned char volume;
+unsigned char prevolume;
+unsigned char fmbandwidth;
+
+int backlight_long;
+unsigned int ui_idlecount;
+unsigned char scanflag;
+
+/*
+ * Statuses
+ */
+unsigned char stereoflag;
+unsigned char tuneflag;
+unsigned char cnrlevel;
+int diffstat;
+unsigned int batlevel_6955;
+unsigned int battlevel;
+
+int recv_signal;
+int backlight_counter;
+unsigned char backlight_level;
+unsigned char pollkeybuf[33];
//#define _LCD_DEBUG 1
}
SIGHANDLER(RBIF_handler)
{
- power_on();
+ power_on_inthook();
// if(chk_powerbutton(0, 0) != 0) { // If pressed on
// power_off(1); //
// }
// }
}
+SIGHANDLER(INADC_handler)
+{
+ unsigned int a;
+// a = polladc();
+// if(a != 0xffff) {
+// battlevel = adc_rawtobatt(a);
+// }
+}
DEF_INTLOW(intlow_handler)
DEF_HANDLER(SIG_TMR0, TMR0_handler)
DEF_HANDLER(SIG_TMR0, TMR0_handler)
DEF_HANDLER(SIG_INT1, EXINT_Handler)
DEF_HANDLER(SIG_INT2, EXINT_Handler)
+ DEF_HANDLER(SIG_AD, INADC_handler)
END_DEF
-unsigned int amfreq;
-unsigned int fmfreq;
-unsigned char amband;
-unsigned char fmband;
-unsigned char fm;
-unsigned char am_mode3k;
-unsigned char am_userbandnum;
-unsigned char fm_userbandnum;
-_userband_t am_usrbands[USER_BAND_NUM];
-_userband_t fm_usrbands[USER_BAND_NUM];
-unsigned char volume;
-unsigned char prevolume;
-unsigned char fmbandwidth;
-
-int backlight_long;
-unsigned int ui_idlecount;
-unsigned char scanflag;
-
-/*
- * Statuses
- */
-unsigned char stereoflag;
-unsigned char tuneflag;
-unsigned char cnrlevel;
-int diffstat;
-unsigned int batlevel_6955;
-
-
-int recv_signal;
-int backlight_counter;
-unsigned char backlight_level;
-unsigned char pollkeybuf[33];
void update_status(void)
{
-#if 1
+
+ unsigned int adc;
+#if 0
recv_signal = akc6955_read_level();
if(fm != 0){
fmfreq = akc6955_get_freq();
prevolume = akc6955_get_prevolume();
batlevel_6955 = akc6955_get_battery();
fmbandwidth = akc6955_get_fmbandwidth();
+#else
+ batlevel_6955 = 330;
#endif
+ startadc(0);
+ idle_time_ms(1);
+ adc = polladc2();
+ battlevel = adc_rawtobatt(adc, batlevel_6955);
}
_LOCATE(0,0);
printstr("S=");
print_numeric_nosupress(recv_signal, 3);
+ _LOCATE(16-5,0);
+ print_numeric_nosupress(battlevel / 100, 1);
+ _PUTCHAR('.');
+ print_numeric_nosupress(battlevel % 100, 2);
+ _PUTCHAR('V');
_LOCATE(0,1);
// _PUTCHAR(' ');
if(fm != 0){ // FM
}
}
// _LOCATE(15-5 ,1);
- _LOCATE(15-3-6, 1);
+ _LOCATE(16-3-6, 1);
if(fm != 0){
int freq_lo = fmfreq % 100;
int freq_hi = fmfreq / 100;
print_numeric_nosupress(amfreq, 5);
}
// Signal
- _LOCATE(15-3, 1);
+ _LOCATE(16-3, 1);
if(fm != 0){
printstr("MHz");
} else {
unsigned char p;
unsigned char pbutton;
unsigned char reset_status;
+ unsigned int adc;
-#ifdef _LCD_DEBUG
- unsigned char power_flag;
-#endif
- // OSCCON = (_IDLEN & 0b11111100) | 0b00111000;
-// power_on();
+ OSCCON = (_IDLEN & 0b11111100) | 0b00111000;
idle_init();
keyin_init();
keyin_ioinit();
+
+ batlevel_6955 = 0;
+ battlevel = 0;
WDTCONbits.SWDTEN = 1; // WDT ON.
reset_status = chk_reset();
switch(reset_status){
default:
break;
}
+ idle_init();
+ keyin_init();
+ keyin_ioinit();
i2c1_init();
+ intadc_init();
set_powerlamp(1);
idle_time_ms(125);
-#ifdef _LCD_DEBUG
- power_flag = 0xff;
-#endif
backlight_long = 256;
backlight_counter = backlight_long;
backlight_level = 255;
ui_idlecount = 250; // 0.25Sec
+ power_on(1);
acm1602_init(0xa0, 1); //Init LCD
idle_time_ms(125);
/* Check EEPROM */
/* Push default parameters to AKC6955*/
scanflag = 0;
-#if 1
+#if 0
akc6955_chg_fm(fm); // Set to AM
akc6955_set_amband(amband);
akc6955_set_freq(amfreq); // Dummy, TBS (954KHz)
_CLS();
_LOCATE(0,0);
_PUTCHAR(' ');
-// update_status();
-// update_display();
+ update_status();
+ update_display();
ClrWdt();
- idle_time_ms(ui_idlecount);
// _LOCATE(0,0);
// printstr("OK");
do {
/* Main routine*/
- c = pollkeys(pollkeybuf, 60, 1);
- p = 0;
- while(c > 0) {
- setfreq_updown(pollkeybuf[p]);
- c--;
- p++;
- }
+ // c = pollkeys(pollkeybuf, 60, 1);
+ // p = 0;
+ // while(c > 0) {
+ // setfreq_updown(pollkeybuf[p]);
+ // c--;
+ // p++;
+ // }
+// idle_time_ms(ui_idlecount);
// Check battery (include idle?)
// Read AKJC6955's status
- update_status();
+ //update_status();
// Putstring to LCD.
_LOCATE(0,0);
- update_display();
pbutton = chk_powerbutton();
if(pbutton != 0) shutdown(1); // Button pressed.
} else {
lcd_setbacklight(0x00, 0); // Turn OFF
}
-// idle_time_ms(ui_idlecount);
+ idle_time_ms(ui_idlecount);
+ update_status();
+ update_display();
} while(1);
}
DISTDIR=dist/${CND_CONF}/${IMAGE_TYPE}
# Object Files Quoted if spaced
-OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/ui.o ${OBJECTDIR}/i2c_io.o ${OBJECTDIR}/main.o ${OBJECTDIR}/idle.o ${OBJECTDIR}/lcd_acm1602.o ${OBJECTDIR}/akc6955.o ${OBJECTDIR}/eeprom.o ${OBJECTDIR}/ioports.o ${OBJECTDIR}/menu.o ${OBJECTDIR}/power.o
-POSSIBLE_DEPFILES=${OBJECTDIR}/ui.o.d ${OBJECTDIR}/i2c_io.o.d ${OBJECTDIR}/main.o.d ${OBJECTDIR}/idle.o.d ${OBJECTDIR}/lcd_acm1602.o.d ${OBJECTDIR}/akc6955.o.d ${OBJECTDIR}/eeprom.o.d ${OBJECTDIR}/ioports.o.d ${OBJECTDIR}/menu.o.d ${OBJECTDIR}/power.o.d
+OBJECTFILES_QUOTED_IF_SPACED=${OBJECTDIR}/ui.o ${OBJECTDIR}/i2c_io.o ${OBJECTDIR}/main.o ${OBJECTDIR}/idle.o ${OBJECTDIR}/lcd_acm1602.o ${OBJECTDIR}/akc6955.o ${OBJECTDIR}/eeprom.o ${OBJECTDIR}/ioports.o ${OBJECTDIR}/menu.o ${OBJECTDIR}/power.o ${OBJECTDIR}/adc_int.o
+POSSIBLE_DEPFILES=${OBJECTDIR}/ui.o.d ${OBJECTDIR}/i2c_io.o.d ${OBJECTDIR}/main.o.d ${OBJECTDIR}/idle.o.d ${OBJECTDIR}/lcd_acm1602.o.d ${OBJECTDIR}/akc6955.o.d ${OBJECTDIR}/eeprom.o.d ${OBJECTDIR}/ioports.o.d ${OBJECTDIR}/menu.o.d ${OBJECTDIR}/power.o.d ${OBJECTDIR}/adc_int.o.d
# Object Files
-OBJECTFILES=${OBJECTDIR}/ui.o ${OBJECTDIR}/i2c_io.o ${OBJECTDIR}/main.o ${OBJECTDIR}/idle.o ${OBJECTDIR}/lcd_acm1602.o ${OBJECTDIR}/akc6955.o ${OBJECTDIR}/eeprom.o ${OBJECTDIR}/ioports.o ${OBJECTDIR}/menu.o ${OBJECTDIR}/power.o
+OBJECTFILES=${OBJECTDIR}/ui.o ${OBJECTDIR}/i2c_io.o ${OBJECTDIR}/main.o ${OBJECTDIR}/idle.o ${OBJECTDIR}/lcd_acm1602.o ${OBJECTDIR}/akc6955.o ${OBJECTDIR}/eeprom.o ${OBJECTDIR}/ioports.o ${OBJECTDIR}/menu.o ${OBJECTDIR}/power.o ${OBJECTDIR}/adc_int.o
CFLAGS=
${RM} ${OBJECTDIR}/power.o
${MP_CC} --debug-ralloc --use-non-free -V --pstack-model=small --obanksel=2 --optimize-cmp --optimize-df --opt-code-size libio18f25k22.lib libc18f.lib -c -mpic16 -p18f45k20 power.c -o${OBJECTDIR}/power.o
+${OBJECTDIR}/adc_int.o: adc_int.c nbproject/Makefile-${CND_CONF}.mk
+ ${MKDIR} ${OBJECTDIR}
+ ${RM} ${OBJECTDIR}/adc_int.o
+ ${MP_CC} --debug-ralloc --use-non-free -V --pstack-model=small --obanksel=2 --optimize-cmp --optimize-df --opt-code-size libio18f25k22.lib libc18f.lib -c -mpic16 -p18f45k20 adc_int.c -o${OBJECTDIR}/adc_int.o
+
else
${OBJECTDIR}/ui.o: ui.c nbproject/Makefile-${CND_CONF}.mk
${MKDIR} ${OBJECTDIR}
${RM} ${OBJECTDIR}/power.o
${MP_CC} --debug-ralloc --use-non-free -V --pstack-model=small --obanksel=2 --optimize-cmp --optimize-df --opt-code-size libio18f25k22.lib libc18f.lib -c -mpic16 -p18f45k20 power.c -o${OBJECTDIR}/power.o
+${OBJECTDIR}/adc_int.o: adc_int.c nbproject/Makefile-${CND_CONF}.mk
+ ${MKDIR} ${OBJECTDIR}
+ ${RM} ${OBJECTDIR}/adc_int.o
+ ${MP_CC} --debug-ralloc --use-non-free -V --pstack-model=small --obanksel=2 --optimize-cmp --optimize-df --opt-code-size libio18f25k22.lib libc18f.lib -c -mpic16 -p18f45k20 adc_int.c -o${OBJECTDIR}/adc_int.o
+
endif
# ------------------------------------------------------------------------------------
#
-#Mon Jun 24 19:21:22 JST 2013
+#Wed Jun 26 20:49:49 JST 2013
default.languagetoolchain.dir=/usr/local/bin
default.br-unifei-rmaalmeida-toolchainSDCC-SDCCtoolchain.md5=b67cce1ad75b450308d7806e430931b3
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=8fe1589514540343a5279c082104bce0
<itemPath>menu.h</itemPath>
<itemPath>power.c</itemPath>
<itemPath>power.h</itemPath>
+ <itemPath>adc_int.c</itemPath>
+ <itemPath>adc_int.h</itemPath>
</logicalFolder>
<sourceRootList>
<Elem>/usr/local/share/sdcc/lib/src/pic16/libc</Elem>
<defaultConf>0</defaultConf>
<confs>
<conf name="default" type="2">
- <platformToolSN></platformToolSN>
+ <platformToolSN>:=MPLABCommUSB:=04D8:=900A:=0002:=Microchip Technology Inc.:=PICkit 3:=DEFAULT_PK3 :=x:=en</platformToolSN>
<languageToolchainDir>/usr/local/bin</languageToolchainDir>
<mdbdebugger version="1">
<placeholder1>place holder 1</placeholder1>
return RESET_MCLR;
}
-void power_on(void)
+void power_on_inthook(void)
{
IOCB = 0x00;
IOCB |= 0x10; // IOCB4 ONLY.
INTCON |= (_GIE | _PEIE);
}
+void power_on(unsigned char f)
+{
+ if(f == 0x00){
+ set_examp_mute(1);
+ idle_time_ms(200);
+ set_radiopower(0);
+ } else {
+ set_examp_mute(0);
+ idle_time_ms(100);
+ set_radiopower(1);
+ }
+}
+
unsigned char chk_powerbutton(void)
{
unsigned char count = 0;
set_powerlamp(0);
set_amlamp(0);
set_fmlamp(0);
+ power_on(0);
power_off(save);
}
RESET_POWERDOWN,
RESET_INTEXIT
};
- unsigned char chk_reset(void);
- void power_on(void);
- unsigned char chk_powerbutton(void);
- void power_off(unsigned char save);
- void shutdown(unsigned char save);
+ extern unsigned char chk_reset(void);
+ extern void power_on_inthook(void);
+ extern void power_on(unsigned char f);
+ extern unsigned char chk_powerbutton(void);
+ extern void power_off(unsigned char save);
+ extern void shutdown(unsigned char save);
#ifdef __cplusplus
}