[I2C] Make i2c_writechar() for XC8.
}
}
+/*
+ * ADCON1bits.VCFG1 = 0;
+ * ADCON1bits.VCFG0 = 0;
+ * ADCON2 = 0b10110110;
+ */
+void startadc(unsigned char ch)
+{
+#ifdef __SDCC
+ adc_open(ch, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD);
+ adc_setchannel(ch);
+ adc_conv();
+#else
+ OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD, (ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS & (0x0f << 2)) | (ch << 2) , 0x0001 << ch);
+ ADCON0bits.GO = 1;
+#endif
+}
+#ifdef __SDCC
+void stopadc(void)
+{
+ ADCON0bits.GO_DONE = 0;
+ PIR1bits.ADIF = 0;
+ PIE1bits.ADIE = 0;
+}
+#endif
+
+unsigned char polladc2(void)
+{
+ unsigned int a;
+ PIE1bits.ADIE = 0;
+ PIR1bits.ADIF = 0;
+ while (ADCON0bits.DONE != 0)
+ { // converting or not enable.
+ idle_time_ms(1);
+ }
+ a = ((ADRESH << 8) + ADRESL) & 0x03ff;
+ ADCON0bits.GO_DONE = 0;
+ return a;
+}
+
+// Masure raw battery voltage, Reference = 1.20V±4%
+// Multiply = 39:13 = 4:1.
unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel)
{
+
// raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
- return ((b >>2) * reflevel) >> 6;
+// return ((b * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
+ reflevel = reflevel / 4;
+ return ((b * 120) / reflevel) * 4 / 4;// Divide = 4
}
\ No newline at end of file
#endif
extern void intadc_init(void);
extern unsigned int polladc(void);
-#if 0
+extern unsigned char polladc2(void);
extern void startadc(unsigned char ch);
-extern unsigned int polladc2(void);
-#else
#ifdef __SDCC
-#define polladc2(a) { \
- while(adc_busy()); \
- a = adc_read(); \
- }
-
-#define stopadc() { \
- ADCON0bits.GO_DONE = 0; \
- PIR1bits.ADIF = 0; \
- PIE1bits.ADIE = 0; \
- }
-/*
- * ADCON1bits.VCFG1 = 0;
- * ADCON1bits.VCFG0 = 0;
- * ADCON2 = 0b10110110;
- */
-#define startadc(c) { \
- adc_open(c, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD); \
- adc_setchannel(c); \
- adc_conv(); \
- }
-#else
-#define polladc2(a) { \
- while(BusyADC()); \
- a = ReadADC(); \
- }
-
-#define stopadc() { \
- CloseADC(); \
- }
-/*
- * ADCON1bits.VCFG1 = 0;
- * ADCON1bits.VCFG0 = 0;
- * ADCON2 = 0b10110110;
- */
-#define startadc(c) { \
- OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD, (ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS & (0x0f << 2)) | (c << 2) , 0x0001 << c); \
- ADCON0bits.GO = 1; \
- }
-
-#endif
+extern void stopadc(void);
#endif
extern unsigned int adc_rawtobatt(unsigned int b, unsigned int reflevel);
-
-
#ifdef __cplusplus
}
#endif
#include "i2c_eeprom.h"
+
+#ifndef __SDCC
+void i2c_writechar(unsigned char c)
+{
+ WriteI2C(c);
+ while(SSPCON2bits.ACKSTAT);
+}
+#endif
+
+
static unsigned char i2c_eeprom_write_1block(unsigned char i2caddr, unsigned int addr, unsigned char *data, unsigned int size)
{
unsigned char i;
StartI2C();
while(SSPCON2bits.SEN);
- WriteI2C(i2caddr & 0xfe);
- while(SSPCON2bits.ACKSTAT);
-
- WriteI2C(page_hi);
- while(SSPCON2bits.ACKSTAT);
-
- WriteI2C(page_lo);
- while(SSPCON2bits.ACKSTAT);
+ i2c_writechar(i2caddr & 0xfe);
+ i2c_writechar(page_hi);
+ i2c_writechar(page_lo);
if(!SSPCON2bits.ACKSTAT){
for(i = 0; i < size ; i++){
if(SSPCON1bits.SSPM3) {
- WriteI2C(*p);
- while(SSPCON2bits.ACKSTAT);
- IdleI2C();
- p++;
+ i2c_writechar(*p);
+ IdleI2C();
+ p++;
}
}
}
return 0xff;
}
+
+
// I2C_IO
unsigned char i2c_eeprom_bytewrite(unsigned char i2caddr, unsigned int addr, unsigned char data)
{
StartI2C();
while(SSPCON2bits.SEN);
- WriteI2C(i2caddr & 0xfe);
- while(SSPCON2bits.ACKSTAT);
-
- WriteI2C(page_hi);
- while(SSPCON2bits.ACKSTAT);
-
- WriteI2C(page_lo);
- while(SSPCON2bits.ACKSTAT);
-
- WriteI2C(data);
- while(SSPCON2bits.ACKSTAT);
+ i2c_writechar(i2caddr & 0xfe);
+ i2c_writechar(page_hi);
+ i2c_writechar(page_lo);
+ i2c_writechar(data);
StopI2C();
+
while(SSPCON2bits.PEN);
do {
StartI2C();
I2C_START();
i2c_writechar(addr | 0x01);
for(i = 0; i < I2C_ROM_PAGE_SIZE - 1 ; i++){
- *p = i2c_readchar();
- p++;
+ *data = i2c_readchar();
+ data++;
I2C_ACK();
}
- *p = i2c_readchar();
+ *data = i2c_readchar();
I2C_NACK();
I2C_STOP();
i2c_close();
#define TRIS_E_VAL 0b00000100 /* FOR OUTPUT, ADC, PSP=OFF */
#define _BATT_ADC AN7
+#define _REF_ADC 0x0f
#define _ADC_INITF ANS7
#define _ADCH_INITF 0x00
static void _ACM1602_TC_WAIT(void)
{
#ifdef __SDCC
- delay10tcy(4)
+ delay10tcy(4);
#else
__delay_us(5);
#endif
#
-#Tue Oct 01 21:09:36 JST 2013
+#Wed Oct 02 00:34:28 JST 2013
default.languagetoolchain.dir=/opt/microchip/xc8/v1.20/bin
com-microchip-mplab-nbide-embedded-makeproject-MakeProject.md5=0d2b1469ad71adb787c711a416386331
default.languagetoolchain.version=1.20
{
unsigned int adc;
+ unsigned int ref;
setup.fm = akc6955_get_fm();
recv_signal = akc6955_read_level();
tuneflag = akc6955_tune();
cnrlevel = akc6955_get_cnr();
// Battery
- batlevel_6955 = akc6955_get_battery();
- // ADC
+// batlevel_6955 = akc6955_get_battery();
+ //ADC:Battery
startadc(ADC_BATT_CH);
- do {
- idle_time_ms(1);
- adc = polladc();
- } while(adc == 0xffff);
- battlevel = adc_rawtobatt(adc, batlevel_6955);
+ adc = polladc2();
+ //ADC:reference
+ CVRCON2bits.FVREN = 1;
+ while(CVRCON2bits.FVRST == 0);
+ startadc(_REF_ADC);
+ ref = polladc2();
+ CVRCON2bits.FVREN = 0;
+ //ADC:reference
+ battlevel = adc_rawtobatt(adc, ref);
}