How to solve these error? - Page 1
Forum section: electronics / general

author: radhika
author_rank: Regular Contributor
timestamp: November 15, 2018, 05:10:08 am
content: Hello,I have tried building the code for battery level indicator which work for 3.7V battery.Code:#include <stdio.h>#include <stdlib.h>#include <xc.h>#include "OLED_I2C.h"#include "p18f4520.h"// select the pins used on the LCD panel#define OLED_SCL PORTCbits.RC3#define OLED_SDA PORTCbits.RC4int digit1=0, digit2=0, digit3=0, digit4=0;//draw battery level in position x,yvoid batterylevel(int xpos,int ypos){//read the voltage and convert it to voltdouble curvolt = ( readVcc() ) / 1000;// check if voltage is bigger than 4.2 volt so this is a power sourceif(curvolt > 4.2){static unsigned char batlevel = {0000000000000001,0111111111111101,0100000000000100,0100000000000100,0100000000000100,0100000000000100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}if(curvolt <= 4.2 && curvolt > 3.9){static unsigned char batlevel = {0000000000000001,0111111111111101,0100000000111100,0100000000111100,0100000000111100,0100000000111100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}if(curvolt <= 3.9 && curvolt > 3.6){static unsigned char batlevel = {0000000000000001,0111111111111101,0100000000111100,0100000000111100,0100000000111100,0100000000111100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}if(curvolt <= 3.6 && curvolt > 3.3){static unsigned char batlevel = {0000000000000001,0111111111111101,0100001111111100,0100001111111100,0100001111111100,0100001111111100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}if(curvolt <= 3.3 && curvolt > 3.0){static unsigned char batlevel = {0000000000000001,0111111111111101,0101111111111100,0101111111111100,0101111111111100,0101111111111100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}if(curvolt < 3.0){static unsigned char batlevel = {0000000000000001,0111111111111101,0101111111111100,0101111111111100,0101111111111100,0101111111111100,0111111111111101,0000000000000001,};OLED_drawBitmap (108, 4, batlevel, 16, 8);OLED_setPixel(108,4);}}void adc_init(){ADCON0 = 0b00000011; //select adc channelADCON1 = 0b00001110; //select analog and digital i/pADCON2 = 0b10001010; //eqisation time holding cap timeADON = 1;}float adc_value(void){float adc_data=0;while(GO/DONE==1); //higher bit data start conversion adc valueadc_data = (ADRESL)+(ADRESH<<8); //Store 10-bit outputadc_data =((adc_data/1023)*5.0);return adc_data;}void convert(float f){int d=(f*100);digit1=d%10;d=d/10;digit2=d%10;d=d/10;digit3=d%10;digit4=d/10;}void MSDelay (unsigned int itime){unsigned int i;unsigned char j;for (i=0; i<itime; i++);for (j=0; j<165; j++);}long readVcc(){adc_init();convert(adc_value);MSDelay(100);}void main() {TRISAbits.TRISA0 = 1;OLED_Init();MSDelay(100);OLED_ClearDisplay();OLED_Update();MSDelay(100);OLED_FillDisplay();OLED_Update();MSDelay(100);OLED_ClearDisplay();// load battery icon on position 108,4batterylevel(108,4);}But, I am getting many error, I am not getting that how to solve these error. Please suggest.BatteryCode.c:20: warning: (361) function declared implicit int ---- double curvolt = ( readVcc() ) / 1000;BatteryCode.c:31: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:31: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:47: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:47: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:63: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:63: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:80: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:80: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:96: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:96: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:113: error: (240) too many initializers ------- 0111111111111101,BatteryCode.c:113: error: (241) initialization syntax ------- 0111111111111101,BatteryCode.c:140: warning: (356) implicit conversion of float to integer ----- int d=(f*100);BatteryCode.c:158: error: (984) type redeclared ---- long readVcc(){---}BatteryCode.c:158: error: (1098) conflicting declarations for variable "readVcc" (BatteryCode.c:157) --- long readVcc(){---}

author: ejeffrey
author_rank: Super Contributor
timestamp: November 15, 2018, 06:44:09 am
content: Quote from: radhika on November 15, 2018, 05:10:08 am

author: radhika
author_rank: Regular Contributor
timestamp: November 15, 2018, 08:09:34 am
content: Quote from: ejeffrey on November 15, 2018, 06:44:09 am

author: radhika
author_rank: Regular Contributor
timestamp: November 15, 2018, 08:11:37 am
content: Also, how do I solve my these error?BatteryCode.c:20: warning: (361) function declared implicit intBatteryCode.c:140: warning: (356) implicit conversion of float to integerBatteryCode.c:158: error: (984) type redeclaredBatteryCode.c:158: error: (1098) conflicting declarations for variable "readVcc" (BatteryCode.c:157)

author: agehall
author_rank: Frequent Contributor
timestamp: November 15, 2018, 09:24:04 am
content: I would highly recommend you shy away from all floating point operations unless you fully understand the quirks of them. Doing floating point math in a digital world is not the same thing as doing it on paper in the analog world. In your very simple application, it would probably work fine if you fix the obvious issues with your code, but there is also no need for you to use floating point math - all you seem to be interested in is to measure the relative charge of a battery so just use the ranges given by your ADC and keep things as ints.

author: radhika
author_rank: Regular Contributor
timestamp: November 15, 2018, 10:44:51 am
content: Quote from: agehall on November 15, 2018, 09:24:04 amHello, Thanks for replying. But how can I make these as float? It's using battery volatge.Quote

author: agehall
author_rank: Frequent Contributor
timestamp: November 15, 2018, 01:30:10 pm
content: I would suggest you start with a basic C tutorial. Then you should again think very hard about why you would need to use floats. As far as I can tell, there is no real reason to do so.