Getting half of the values in 16-bit ADC ADS1118 !!!

Status
Not open for further replies.

sakthivelr

Newbie level 5
Joined
Feb 17, 2017
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
88
Hi friends,

Actually i am using the 16-bit ADC ADS1118 to read the micro voltage variations.

Issues
1. I have given pre-defined voltage like 100mV mean i am getting the output code 50mV. Nearly half of the values i am getting. i checked with the other voltages also. how to fix this problem?

2. I/p to 16bit ADC is 41.3mv to 44.5mV but the mA is not changing until it get 3mV difference.
( ex. 41.3mV = 7.801mA but 42.5mV ,43.5mV,43.9mV & 44.5mV we are getting the same 7.801mA. our expectation is it should vary when the input varies.) When ever it gets 3mV difference then only it changes. please guide how to solve this issue.




Thanks & Regards,
SAKTHIVEL R
 

Attachments

  • readings.jpg
    53.2 KB · Views: 157

Hi,

* show your schematic with all your ADC connections
* and your code.

Klaus
 

ADS1118 has no option to reduce the resolution below 16 bit. Thus I assume, you have a problem in ADC data processing.
 

Hi,

* show your schematic with all your ADC connections
* and your code.

Klaus

ADC code: Code run for 5sec. meanwhile adc read the voltage variations and converted into current using ohms law.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
void test_1()
{
    
    char s_value[10], s_integral[10], s_fractional[10];
    int i, found = 0, count1 = 1, d_integer_hex,adc_result[5];
    char buf[10];
    int adc_value_key8 = -1 ;
    unsigned char buff1[10];
    double current_buf;
    float voltage0[5];
    gpio_clr_gpio_pin(OUTEN);
    gpio_set_gpio_pin(OUTEN);
    lcdClear();
    lcdMoveCursor(0,0);
    lcdPrintStr("Voltage 6.3V");
    lcdMoveCursor(1,0);
    lcdPrintStr("Time Remain: 5");
        
    for(i=5;i>=0;i--)     // loop runs for 5 sec menwhile adc sense the voltage variations. display in terms of current 
    {
        
        lcdClear();
        lcdMoveCursor(0,0);
        lcdPrintStr(" Test Voltage 6.3V");
        if(i!=5)
        {
            lcdMoveCursor(1,0);
            lcdPrintStr("Time Remain: ");
            lcdMoveCursor(1,15);
            print_integer(i);
            spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
 
            SPI_transfer(ADS1118_SPI1, 0X4A43);     // 0X4843 ADC config.register write function. 
            adc_result[i]=ADS1118_read_device(0X0000); // read address
 
            spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
            voltage0[i] = adc_result[i] * 0.0000039063; //= 0.256V/65536 resolution
            
        }
        else{
            lcdMoveCursor(1,0);
            lcdPrintStr("Time Remain: ");
            lcdMoveCursor(1,15);
            print_integer(i);
        }
        
        delay_s(1);
    }
    
        
    voltageavg =((voltage0[4]+voltage0[1]+voltage0[2]+voltage0[3])/4); // Average
    
    
    gpio_clr_gpio_pin(OUTEN);
    current=(voltageavg/(0.075*68.63)*1000);   // volt. to current conversion formula // 0.075 shunt resistor// 68.63 is gain  
    
    current1 = (current*2);// getting half of the value so multplied by 2.
 
    sprintf(buf, "%0.3f", current1);
    
    
    
    lcdMoveCursor(3,4);
    lcdPrintStr(buf);
 
}








Thanks & regards,
Sakthivel.r
 

Attachments

  • Schematic ADC section.png
    64 KB · Views: 146
Last edited by a moderator:

Hi,

did you read post#4?
voltage0 = adc_result * 0.0000039063; //= 0.256V/65536 resolution


Next time - if you want someone to go through your code - you should give documented and complete code.
* Don´t expect that someone takes a lot of time to find out what SPI clock frequency, what microcontroller, what microcontroller_clock_frequency... you use
* Don´t expect that someone takes a lot of time to find out what each bit of the ADC setup means.

Schematic:
The same as with code: give complete informations:
* Where does OP_AMP1 come from?
* don´t leave any IC input floating. Neither digital inputs nor analog inputs. --> ADC AIN2, AIN3

Read datasheets:
* ADC: V_OS
* ADC: V_O(min)

Klaus

- - - Updated - - -

Hi,

Added:

Read datasheet:
* section 9.5.6 Data Format
--> you declare "adc_result[x]" as "int" --> what bit width is "int" and is it signed or unsigned?

You perform "SPI_transfer". --> is it able to process 16 bits of data immediately? Maybe you need to perform two 8 bit operations. (BTW: this function is missing)

You perform: "ADS1118_read_device" --> is it able to process 16 bits of data immediately? Maybe you need to perform two 8 bit operations. (BTW: this function is missing)

Then you multiply: voltage = adc_result x constant ---> float = int x float. Read compiler documentation if this is correct. Maybe you need to transform the "int" into a "float" first.

current=(voltageavg/(0.075*68.63)*1000); // volt. to current conversion formula // 0.075 shunt resistor// 68.63 is gain
This does not meet your schematic at all...nor does it make sense to me.--> Please give valid informations.

Klaus
 

Code:
voltage0[i] = adc_result[i] * 0.0000039063; //= 0.256V/65536 resolution

As already suggested by pauljulfo, you scale the ADC result incorrectly. 16 bit range is +/-0.256 V, LSB resolution respectively 0.256/2^15.

Getting about 3 mV resolution with +/-0.256 V range indicates that you are only processing the highword of 16 bit ADC data. Probably faulty ADS1118_read_device() isn't shown in your code.
 




Thanks for your support. Here I have copied the entire code and the schematics. The whole code function is to read the adc 5sec

Code:
/*
 * CAL.c
 *
 * Created: 7/7/2017 4:11:50 PM
 *  
 */ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <avr32/io.h>
#include <asf.h>
#include <math.h>


#include "LCD.h"
#include "gpio.h"
#include "twi_master.h"
#include "EEPROM.h"


#include "pm.h"
#include "delay.h"
#include "keyboard.h"
#include "conf_clock.h"
#include "Menu_String.h"
#include "CAL.h"
#include "stringz.h"
//#include "main.h"





extern struct my_spi_options;	



unsigned int count=0;val;

char *Get_Calibrationtimestrings()
{
	return(UI_Calibrationtimestrings);
}

//char *Get_Change_CalibrationtimeSrings()
//{
//	return(UI_Change_CalibrationtimeStrings);
//}






float voltage0[4];
float avg_volt0;
float voltage1[4];
float voltage2;
float voltage3;
float voltage4;
float voltage5;
float voltage6;
float voltage7;
float voltage8;
float voltage9;
float voltageavg;
float voltageavg1;
float voltageavg2;
float voltageavg3;
float voltageavg4;
float voltageavg5;
float voltageavg6;
float voltageavg7;
float voltageavg8;
float voltageavg9;
float current,current1;
float currentfinal;
float currentfinalfinal;
char data_1,dat,dat1;
int digit1, digit2, digit3, digit4, digit5, digit6, d_integer, d_fraction,adc_result,read_data,readdata,key1;
int adc_result0[4],adc_result1,adc_result2,adc_result3,adc_result4,adc_result5,adc_result6,adc_result7,adc_result8,adc_result9;
int adc_value0,adc_value1,adc_value2,adc_value3;

int TRNSEN_PB20 = AVR32_PIN_PB20;
int CHIP_ENABLE = AVR32_PIN_PB19;
int DIGITAL_SELECT1 = AVR32_PIN_PB10;
int DIGITAL_SELECT2 = AVR32_PIN_PB11;
int DIGITAL_SELECT3 = AVR32_PIN_PB12;
int DIGITAL_SELECT4 = AVR32_PIN_PB13;


unsigned char const LEDcode[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10};




struct menu calibration_setting_function(struct menu Setting)
{
	lcdClear();
	
	int key;
	
	int cursor_position1 = 1,ascii= 0x30,cursor_position=6;
	
	unsigned char status = 0,count=0;
	
	char *return_ascii;
	
	lcdMoveCursor(0,0);
	lcdPrintStr("calibration time");
	lcdMoveCursor(1,9);
	lcdPrintStr("sec");
	
	while(1)
	{
		
		
		return_ascii =calibration_ascii_function();
		
		
		lcdClear();
		lcdMoveCursor(3,3);
		lcdPrintChar(return_ascii[0]);
		lcdMoveCursor(3,4);
		lcdPrintChar(return_ascii[1]);
		
		delay_ms(1000);
		
		if((key == ZERO)&&(status == 1))
		{
			status = 0;
		}
		
		return ;
	}
	
	
}


int calibration_ascii_function()
{
	
	int cursor_position1 = 1,cursor_position=6;

	unsigned char status = 0;

	char ret;

	static char return_ascii[2];
	
	

	while(1)
	{
		
		ret = READ_SWITCHES();
		
		
		
		
		if((ret!=0x30)&&(ret!=0x23)&&(ret!=0x40)&&(ret!=0x28)&&(ret!=0x21)&&(ret!=0x29)&&(ret!=0x22)&&(ret!=0x5E)&&(ret!=0x5F)&&(ret >= 0x30)&&(ret <= 0x39)&&(status == 0))
		{
			
			status = 1;
			
			if(cursor_position1==1)  return_ascii[0]=ret;
			if(cursor_position1==2)  return_ascii[1]=ret;
			
			
		}
		
		
		
		if((ret==nextkeystore)&&(status == 0))
		{
			
			cursor_position1++;
			
			
			if(cursor_position1>=6)
			cursor_position1=5;
			status = 1;
			
			
			{
				cursor_position++;
				if(cursor_position >= 8)
				cursor_position = 7;
			}
			status = 1;
			
			lcdMoveCursor(1,cursor_position);
			lcdWriteCmd(0x0F);
			lcdCursoron();
			
			
		}
		
		
		
		if((ret == leftkeystore)&&(status == 0))
		{
			cursor_position1--;
			if(cursor_position1==0)
			cursor_position1=1;
			
			status = 1;
			
			if(status==1)
			{
				cursor_position--;
				if(cursor_position == 5)
				cursor_position = 6;
			}
			status =1;
			lcdMoveCursor(1,cursor_position);
			lcdWriteCmd(0x0F);
			lcdCursoron();
			
		}
		
		
		
		if((ret==enterkeystore)&&(status == 0))
		{
			status = 1;
			
			//show_Date_Time();
			{
				lcdCursoroff();
				cursor_position = 8;
				cursor_position1=1;
				
				lcdClear();
				return return_ascii;
			}
			
		}
		
		if((ret == ZERO)&&(status == 1))
		{
			status = 0;
		}
		
		if((ret!=0x30)&&(ret!=0x23)&&(ret!=0x40)&&(ret!=0x28)&&(ret!=0x21)&&(ret!=0x5E)&&(ret!=0x29)&&(ret!=0x5F)&&(ret!=0x22)&&(ret >= 0x30)&&(ret <= 0x39))
		
		
		{
			
			
			lcdMoveCursor(1,cursor_position);
			lcdWriteCmd(0x0F);
			lcdCursoron();
			lcdPrintChar(ret);
			delay_ms(50);
		}
		
	}
	
	


}


struct spi_device SPI1_DEVICE_EXAMPLE = {
	//! Board specific select id
	.id = ADS1118_SPI1_NPCS
};

	 




#define ADSCLK 1000000


void AD1118DGSR_init(void)
{/*
	gpio_configure_pin(ADS1118_SPI1_SCK_PIN,GPIO_DIR_OUTPUT);
	gpio_configure_pin(ADS1118_SPI1_MISO_PIN ,GPIO_DIR_INPUT);
	gpio_configure_pin(ADS1118_SPI1_MOSI_PIN,GPIO_DIR_OUTPUT);
	gpio_configure_pin(ADS1118_SPI1_NPCS0_PIN,GPIO_DIR_OUTPUT);
	//gpio of spi configuration*/
	static const gpio_map_t ADS1118DGSR_SPI1_GPIO_MAP =
	{
		{ADS1118_SPI1_SCK_PIN ,  ADS1118_SPI1_SCK_FUNCTION },  // SPI Clock.
		{ADS1118_SPI1_MISO_PIN , ADS1118_SPI1_MISO_FUNCTION},  // MISO.
		{ADS1118_SPI1_MOSI_PIN , ADS1118_SPI1_MOSI_FUNCTION},  // MOSI.
		{ADS1118_SPI1_NPCS0_PIN , ADSS1118_SPI1_NPCS0_FUNCTION}   // Chip Select NPCS.
	};
	
	gpio_enable_module(ADS1118DGSR_SPI1_GPIO_MAP,sizeof(ADS1118DGSR_SPI1_GPIO_MAP) / sizeof(ADS1118DGSR_SPI1_GPIO_MAP[0]));
	//gpio_enable_gpio_pin(AT25160B_SPI_HOLD);//AT25160B_SPI_HOLD connected to pin PB31
	//gpio_set_gpio_pin(AT25160B_SPI_HOLD);
	
	spi_master_init(ADS1118_SPI1);
	spi_master_setup_device(ADS1118_SPI1,&SPI1_DEVICE_EXAMPLE, SPI_MODE_1,ADS1118_SPI1_BAUDRATE, 0);
	spi_enable(ADS1118_SPI1);
	//spi_eeprom_Write_Enable();
		// SPI options.
	spi_options_t spiOptions =
	{
		.reg = ADS1118_SPI1_NPCS, // CS for SDMMC
		.baudrate = 100000,
		.bits = 8,
		.spck_delay = 0,
		.trans_delay = 0,
		.stay_act = 1,
		.spi_mode = SPI_MODE_1,
		.modfdis = 1 // 1 = Disables the mode fault detection.
	};

	spi_initMaster(ADS1118_SPI1, &spiOptions);
	// Set SPI selection mode: variable_ps, pcs_decode, delay.
	spi_selectionMode(ADS1118_SPI1, 0, 0, 0);
	// Enable SPI module.
	spi_enable(ADS1118_SPI1);
	
	//Setup configuration for chip connected to SD/MMC CS1
	spi_setupChipReg(ADS1118_SPI1,&spiOptions,ADSCLK /*sysclk_get_pba_hz()*/);
	
	spiOptions.reg = ADS1118_SPI1_NPCS; // CS for pmod expansion board
	spi_setupChipReg(ADS1118_SPI1,&spiOptions,ADSCLK/*sysclk_get_pba_hz()*/);
}


int16_t SPI_transfer(volatile avr32_spi_t *spi, int16_t data)
{
	unsigned int timeout = SPI_TIMEOUT;
	
	spi->tdr = data << AVR32_SPI_TDR_TD_OFFSET;
	
	timeout = SPI_TIMEOUT;
	while(!spi_is_tx_ready(spi)) {
		if(!timeout--) return ERR_TIMEOUT;
	}
	return (spi->rdr >> AVR32_SPI_RDR_RD_OFFSET); // get response MIS0
}

int16_t ADS1118_read_device(uint16_t config)
{
	uint16_t temp;
	int16_t result;
	temp = config;
	
	
	spi_select_device(ADS1118_SPI1,&SPI1_DEVICE_EXAMPLE);
	result = SPI_transfer(ADS1118_SPI1,(uint8_t)(temp >> 8 ));
	result = (result << 8) | SPI_transfer(ADS1118_SPI1,(uint8_t)(temp & 0xFF));
	spi_deselect_device(ADS1118_SPI1,&SPI1_DEVICE_EXAMPLE);
	return result;
}


/*
void spi_ads1118_write(uint16_t memAddr)
{
	
	delay_ms(2);
	//spi_eeprom_Write_Enable();
	memAddr |=0x8000;
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	uint8_t packet[2]={memAddr>>8,memAddr&0x00FF};
	//uint8_t packet[1]={memAddr};
	spi_write_packet(ADS1118_SPI1,packet,2);
	//int msb = spi_write_packet(memAddr >>8);
	//spi_write_packet(memAddr&0xFF);
	
	//spi_write_packet(ADS1118_SPI1,data,len);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	delay_ms(100);
	return;
}

uint8_t spi_ads1118_read(uint16_t memAddr)
{
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	uint8_t packet[2]={memAddr>>8,memAddr&0x00FF};
	//	uint8_t packet[1]={memAddr};
	spi_write_packet(ADS1118_SPI1,packet,2);
	spi_read_packet(ADS1118_SPI1,&readdata,1);// read contents of memory address
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	delay_ms(100);
	return readdata;// return data
}*/
		 
/*

void spi_ads1118_write(volatile avr32_spi_t *spi,uint16_t memAddr)
{
	
	delay_ms(2);
	//spi_eeprom_Write_Enable();
	memAddr |=0x8000;
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	uint8_t packet[2]={memAddr>>8,memAddr&0x00FF};
		//uint8_t packet[1]={memAddr};
	spi_write_packet(ADS1118_SPI1,packet,2);
	//int msb = spi_write_packet(memAddr >>8);
    //spi_write_packet(memAddr&0xFF);
	
	//spi_write_packet(ADS1118_SPI1,data,len);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	delay_ms(100);
	return;
}

uint8_t spi_ads1118_read(uint16_t memAddr)
{
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	uint8_t packet[2]={memAddr>>8,memAddr&0x00FF};
	//	uint8_t packet[1]={memAddr};
	spi_write_packet(ADS1118_SPI1,packet,2);
	spi_read_packet(ADS1118_SPI1,&readdata,1);// read contents of memory address
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	delay_ms(100);
	return readdata;// return data
}
*/

int calibration()
{
	lcdClear();
	//DS1339_write_byte(0,0x02);  //6.3 volt diital potentiometer
	gpio_clr_gpio_pin(OUTEN);
	gpio_enable_gpio_pin(VREF);
	gpio_clr_gpio_pin(VREF);
	
    
/*	out3();
	delay_ms(3000);
	out4();
	delay_ms(3000);	
	out1();
	delay_ms(3000);*/
	
	out0();
	delay_ms(3000);
	
	//out2();
	//delay_ms(3000);
	
/*	out5();
	delay_ms(3000);
	out6();
	delay_ms(3000);
	out7();
	delay_ms(3000);*/
	/*out8();
	delay_ms(3000);
	out9();
	delay_ms(3000);*/
	
	
}

	

	

void out0()
{
	
	float current0;
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
    //gpio_clr_gpio_pin(PB19ENABLE);
	gpio_set_gpio_pin(TRNSEN_PB20);
	
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_set_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
		for(i=5;i>=0;i--)
		{
			
			lcdClear();
			lcdMoveCursor(0,0);
			lcdPrintStr(" Test Voltage 6.3V");
			if(i!=5)
			{
				lcdMoveCursor(1,0);
				lcdPrintStr("Time Remain: ");
				lcdMoveCursor(1,15);
				print_integer(i);
				spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
				SPI_transfer(ADS1118_SPI1,0X5243); //  0X5243
				adc_result0[i]=ADS1118_read_device(0X0000);
				spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
				voltage0[i] = adc_result0[i] *0.00009375143;
				lcdMoveCursor(2,1);
				print_float(voltage0[i]);
			}
			else{
				lcdMoveCursor(1,0);
				lcdPrintStr("Time Remain: ");
				lcdMoveCursor(1,15);
				print_integer(i);
			}
			
			delay_s(1);
			avg_volt0 = (voltage0[0]+voltage0[1]+voltage0[2]+voltage0[3])/4;
		}
		   current0=2*(avg_volt0/(0.075*50)*1000);
		
		
		
sprintf(buf1, "%0.5f", current0);
lcdMoveCursor(2,0);
//lcdPrintStr("OUT 0");
lcdMoveCursor(3,6);
lcdPrintStr(buf1);
lcdMoveCursor(3,13);
lcdPrintStr("mA");
delay_ms(1000);		

		
		
		//sprintf*/(buf1, "%0.3f", current[i]);
// 		lcdPrintStr(buf1);/
// 		
		//lcdMoveCursor(3,0);
		//print_float(voltageavg);
		
		gpio_set_gpio_pin(CHIP_ENABLE);
		gpio_clr_gpio_pin(VREF);
		gpio_clr_gpio_pin(OUTEN);
        
	
}

/*void out1()
{
	
	float current0;
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_set_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_value0=ADS1118_read_device(0X0000);
	
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result1);
	//lcdClear();
	voltage0 = adc_value0*0.00006250095;
	current0=1.9*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current0);//sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 1");//lcdPrintStr("valu");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/

/*
void out2()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_set_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=5;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5203);
	adc_result2=ADS1118_read_device(0X0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result2);
	//lcdClear();
	voltage0 = adc_result2*0.00009375143;
	current[i]=2*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 2");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf (buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}
*/

/*void out3()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
	gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_set_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_set_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_result3=ADS1118_read_device(0x0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result3);
	//lcdClear();
	voltage0 = adc_result3*0.00006250095;
	current[i]=2.25*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 3");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/

/*void out4()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_set_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_result4=ADS1118_read_device(0X0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result4);
	//lcdClear();
	voltage0 = adc_result4*0.00006250095;
	current[i]=3.3*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 4");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/

/*void out5()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_set_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_set_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_result5=ADS1118_read_device(0X0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result5);
	//lcdClear();
	voltage0 = adc_result5*0.00006250095;
	current[i]=2.16*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 5");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/

/*void out6()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_set_gpio_pin(DIGITAL_SELECT2);
	gpio_set_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_result6=ADS1118_read_device(0X0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result6);
	//lcdClear();
	voltage0 = adc_result6*0.00006250095;
	current[i]=2.03*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 6");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/

/*void out7()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
gpio_set_gpio_pin(TRNSEN_PB20);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_set_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_set_gpio_pin(DIGITAL_SELECT2);
	gpio_set_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);
	
	for(i=4;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5223);
	adc_result7=ADS1118_read_device(0x0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result7);
	//lcdClear();
	voltage0 = adc_result7*0.00006250095;
	current[i]=1.99*(voltage0/(0.075*50)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 7");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf//(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}

/*
void out8()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_set_gpio_pin(DIGITAL_SELECT4);
	
	for(i=5;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5243);
	adc_result8=ADS1118_read_device(0x0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result8);
	//lcdClear();
	voltage0 = adc_result8*0.000062501;
	current[i]=(voltage0/(0.075*40)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 8");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf*///(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	/*gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}

void out9()
{
	
	float current[10];
	int adc_value_key0   = -1;
	char buf1[10];
	int i,j;
	CD74HC4067_init();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	//gpio_clr_gpio_pin(PB19ENABLE);

	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_set_gpio_pin(DIGITAL_SELECT1);        // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_set_gpio_pin(DIGITAL_SELECT4);
	
	for(i=5;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr("Cal Voltage 6.3V");
		lcdMoveCursor(1,0);
		lcdPrintStr("Time Remain: ");
		lcdMoveCursor(1,15);
		print_integer(i);
		
		//lcdMoveCursor(2,0);
		//lcdPrintStr("Current0(mA)");
		
		delay_s(1);
	}
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	//  delay_ms(5000);
	SPI_transfer(ADS1118_SPI1, 0X5243);
	adc_result9=ADS1118_read_device(0x0000);
	//delay_ms(2000);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	lcdMoveCursor(3,0);
	//print_integer(adc_result9);
	//lcdClear();
	voltage0 = adc_result9*0.000062501;
	current[i]=(voltage0/(0.075*40)*1000);
	sprintf(buf1, "%0.5f", current[i]);
	lcdMoveCursor(2,0);
	//lcdPrintStr("OUT 9");
	lcdMoveCursor(3,6);
	lcdPrintStr(buf1);
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	delay_ms(1000);

	
	
	//sprintf*///(buf1, "%0.3f", current[i]);
	// 		lcdPrintStr(buf1);/
	//
	//lcdMoveCursor(3,0);
	//print_float(voltageavg);
	
	/*gpio_set_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);

	
}*/
void CD74HC4067_init()
{
	gpio_enable_gpio_pin(DIGITAL_SELECT1);         // Enable all Select Lines in GPIO.
	gpio_enable_gpio_pin(DIGITAL_SELECT2);
	gpio_enable_gpio_pin(DIGITAL_SELECT3);
	gpio_enable_gpio_pin(DIGITAL_SELECT4);
	gpio_enable_gpio_pin(CHIP_ENABLE);                             // Enable chip using active "LOW" Signal
	
	gpio_clr_gpio_pin(CHIP_ENABLE);
	gpio_clr_gpio_pin(DIGITAL_SELECT1);      // Configure all the select lines as Active low
	gpio_clr_gpio_pin(DIGITAL_SELECT2);
	gpio_clr_gpio_pin(DIGITAL_SELECT3);
	gpio_clr_gpio_pin(DIGITAL_SELECT4);

}



void test_1()
{
	
	char s_value[10], s_integral[10], s_fractional[10];
	int i, found = 0, count1 = 1, d_integer_hex,adc_result[5];
	char buf[10];
	int adc_value_key8 = -1 ;
	unsigned char buff1[10];
	double current_buf;
	float voltage0[5];
	//lcdClear();
	gpio_clr_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 5");
		
	for(i=5;i>=0;i--)
	{
		
		lcdClear();
		lcdMoveCursor(0,0);
		lcdPrintStr(" Test Voltage 6.3V");
		if(i!=5)
		{
			lcdMoveCursor(1,0);
			lcdPrintStr("Time Remain: ");
			lcdMoveCursor(1,15);
			print_integer(i);
			spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
			SPI_transfer(ADS1118_SPI1, 0X4A43);  // 0X4843
			adc_result[i]=ADS1118_read_device(0X4A43);
			spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
			voltage0[i] = adc_result[i] * 0.0000078125;
			
		}
		else{
			lcdMoveCursor(1,0);
			lcdPrintStr("Time Remain: ");
			lcdMoveCursor(1,15);
			print_integer(i);
		}
		
		delay_s(1);
	}
	
	 // lcdMoveCursor(2,0);
	//  print_integer(adc_result[1]);	
	voltageavg =((voltage0[4]+voltage0[1]+voltage0[2]+voltage0[3])/4);
	//lcdMoveCursor(2,10);
	//print_float(voltageavg);
 
   //voltageavg =voltage0[1];
	
	gpio_clr_gpio_pin(OUTEN);
	current=(voltageavg/(0.075*68.63)*1000);     
	//lcdMoveCursor(3,4);
	//print_integer(voltage0);
	current1 = (current);

	sprintf(buf, "%0.3f", current1);
	
	
	
	lcdMoveCursor(3,4);
	lcdPrintStr(buf);
	
	lcdMoveCursor(3,13);
	lcdPrintStr("mA");
	gpio_enable_gpio_pin(VREF);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);
	strcpy(s_value, buf);
	
	for (i = 0; s_value[i] != '\0'; i++)
	{
		if (!found)
		{
			if (s_value[i] == '.')
			{
				found = 1;
				s_integral[i] = '\0';
				continue;
			}
			s_integral[i] = s_value[i];
			count1++;
		}
		else
		s_fractional[i - count1] = s_value[i];
	}
	s_fractional[i - count1] = '\0';
	
	if((strlen(s_integral)==3) && strlen(s_fractional)==3)
	{
		
		d_integer = atoi(s_integral);
		
		digit1 = d_integer / 10;
		digit1 = digit1 / 10;
		
		digit2 = d_integer / 10;
		digit2= digit2 % 10;
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(AS1115_DIGIT0_REG, digit1);
		AS1115_write_byte(AS1115_DIGIT1_REG, digit2);
		AS1115_write_byte(AS1115_DIGIT2_REG, digit3);
		AS1115_write_byte(AS1115_DIGIT3_REG, digit4);
		AS1115_write_byte(AS1115_DIGIT4_REG, digit5);
		AS1115_write_byte(AS1115_DIGIT5_REG, digit6);
	}
	
	else if((strlen(s_integral)==2) && strlen(s_fractional)==3)
	
	{
		
		d_integer = atoi(s_integral);
		
		digit2 = d_integer / 10;
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(0x01, 0x00);
		AS1115_write_byte(0x02, digit2);
		AS1115_write_byte(0x03, digit3);
		AS1115_write_byte(0x04, digit4);
		AS1115_write_byte(0x05, digit5);
		AS1115_write_byte(0x06, digit6);
		
	}
	
	else if((strlen(s_integral)==1) && strlen(s_fractional)==3)
	
	{
		
		d_integer = atoi(s_integral);
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(0x01, 0x00);
		AS1115_write_byte(0x02, 0x00);
		AS1115_write_byte(0x03, digit3);
		AS1115_write_byte(0x04, digit4);
		AS1115_write_byte(0x05, digit5);
		AS1115_write_byte(0x06, digit6);
		
	}
	
}
	/*	
void test_1()
{
	
	char s_value[10], s_integral[10], s_fractional[10];
	int i, found = 0, count1 = 1, d_integer_hex;
	char buf[10];
	int adc_value_key8 = -1 ;
	unsigned char buff1[10];
	double current_buf;
	//lcdClear();
	gpio_enable_gpio_pin(OUTEN);
	gpio_set_gpio_pin(OUTEN);
	
	//adc_1();
	//adc_start(&AVR32_ADC);
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 5");
	
	//adc_value_key1 = adc_get_value(&AVR32_ADC,ADC_KEYSWITCH1_CHANNEL);
	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 5");
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	delay_s(1);
	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 4");
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	delay_s(1);
	
	
	
	//voltage=adc_value_key*0.0032258;
	lcdMoveCursor(1,4);
	
	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 3");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	delay_s(1);
	//voltage=adc_value_key*0.0032258;

	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 2");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	delay_s(1);
	//lcdClear();
	//voltage1=adc_value_key*0.0032258;
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 1");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	delay_s(1);
	lcdClear();
	spi_selectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	SPI_transfer(ADS1118_SPI1, 0X4643);
	adc_result=ADS1118_read_device(0x4643);
	spi_unselectChip(ADS1118_SPI1,ADS1118_SPI1_NPCS);
	voltage0 = adc_result * 0.000015625;
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 0");
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	
	
	//voltageavg =((voltage+voltage1+voltage2)/3);
	voltageavg =voltage2 ;
	lcdMoveCursor(3,4);
	print_integer(voltage0);
	gpio_clr_gpio_pin(OUTEN);
	current=(voltage0/(0.075*45.45)*1000);
	
	sprintf(buf, "%0.3f", current);
	lcdPrintStr(buf);
	gpio_enable_gpio_pin(VREF);
	gpio_clr_gpio_pin(VREF);
	gpio_clr_gpio_pin(OUTEN);
	strcpy(s_value, buf);
	
	for (i = 0; s_value[i] != '\0'; i++)
	{
		if (!found)
		{
			if (s_value[i] == '.')
			{
				found = 1;
				s_integral[i] = '\0';
				continue;
			}
			s_integral[i] = s_value[i];
			count1++;
		}
		else
		s_fractional[i - count1] = s_value[i];
	}
	s_fractional[i - count1] = '\0';
	
	if((strlen(s_integral)==3) && strlen(s_fractional)==3)
	{
		
		d_integer = atoi(s_integral);
		
		digit1 = d_integer / 10;
		digit1 = digit1 / 10;
		
		digit2 = d_integer / 10;
		digit2= digit2 % 10;
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(0x01, digit1);
		AS1115_write_byte(0x02, digit2);
		AS1115_write_byte(0x03, digit3);
		AS1115_write_byte(0x04, digit4);
		AS1115_write_byte(0x05, digit5);
		AS1115_write_byte(0x06, digit6);
	}
	
	else if((strlen(s_integral)==2) && strlen(s_fractional)==3)
	
	{
		
		d_integer = atoi(s_integral);
		
		digit2 = d_integer / 10;
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(0x01, 0x00);
		AS1115_write_byte(0x02, digit2);
		AS1115_write_byte(0x03, digit3);
		AS1115_write_byte(0x04, digit4);
		AS1115_write_byte(0x05, digit5);
		AS1115_write_byte(0x06, digit6);
		
	}
	
	else if((strlen(s_integral)==1) && strlen(s_fractional)==3)
	
	{
		
		d_integer = atoi(s_integral);
		
		digit3 = d_integer % 10;
		digit3 = digit3 + 0x80;

		d_fraction = atoi(s_fractional);
		
		digit4 = d_fraction / 10;
		digit4 = digit4 / 10;
		
		digit5 = d_fraction / 10;
		digit5=  digit5 % 10;
		
		digit6 = d_fraction % 10;

		AS1115_write_byte(0x01, 0x00);
		AS1115_write_byte(0x02, 0x00);
		AS1115_write_byte(0x03, digit3);
		AS1115_write_byte(0x04, digit4);
		AS1115_write_byte(0x05, digit5);
		AS1115_write_byte(0x06, digit6);
		
	}
	
}
	*/






void AS1115_write_byte(uint8_t byte_address, uint8_t byte_value)
{
	twi_package_t package;
	byte_value =(byte_value/10*10)+(byte_value%10);
	package.chip = 0x00;// chip address
	package.addr[0] = byte_address;// data to send
	package.addr_length = 1;// address length
	package.buffer = &byte_value;
	package.length = 1;	// frame length
	twi_master_write(&AVR32_TWI, &package);// internal chip address
}


void init_twi_mcp4018(void)
{
	twi_options_t opt;
	static const gpio_map_t TWI_GPIO_MAP =
	{{TWI_MCP4018_SDA_PIN , TWI_MCP4018_SDA_FUNCTION},
	{TWI_MCP4018_SCL_PIN , TWI_MCP4018_SCL_FUNCTION }};
	// TWI gpio pins configuration
	gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
	// options settings
	opt.pba_hz = FOSC0;
	opt.speed = TWI_SPEED;
	opt.chip = MCP4018_ADDRESS;
	// gpio_enable_gpio_pin(LED1);
	twi_master_init(MCP4018_TWI, &opt);
	
}


void init_twi_sevenseg(void)
{
	twi_options_t opt;
	static const gpio_map_t TWI_GPIO_MAP =
	
	{{AS1115_SDA_PIN , AS1115_SDA_FUNCTION},
	{ AS1115_SCL_PIN  ,AS1115_SCL_FUNCTION}};
	// TWI gpio pins configuration
	gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));
	// options settings
	opt.pba_hz = FOSC0;
	opt.speed = TWI_SPEED1;
	opt.chip = AS1115_ADDRESS;
	// initialize TWI driver with options
	twi_master_init(AS1115_TWI, &opt);
	delay_ms(100);
	}
	



void SevSeg_Clear(void)
{
	unsigned char  i;
	for(i=0;i<6;i++)
	AS1115_write_byte(i,0x00);
}
void MCP4018_write_byte(uint8_t byte_address, uint8_t byte_value)
{
	twi_package_t package;
	
	package.chip = 0x2F;// chip address
	package.addr[0] = byte_address;// data to send
	package.addr_length = 1;// address length
	package.buffer = &byte_value;
	package.length = 1;	// frame length
	twi_master_write(&AVR32_TWI, &package);// internal chip address
}

int MCP4018_read_byte(uint8_t byte_address)
{
	twi_package_t package_read;
	package_read.chip = 0x2F;	// chip address
	package_read.addr[0] = byte_address;
	package_read.addr_length = 1;// address length
	package_read.buffer = &data_1;// data to send
	package_read.length = 1;// frame length
	
	twi_master_read(&AVR32_TWI, &package_read);// internal chip address
	
	data_1 = (data_1/16*10)+(data_1%16);
	return data_1;
}





Thanks & regards,
Sakthivel.R
 

Attachments

  • adc schematic.jpg
    126.6 KB · Views: 171
  • controller.jpg
    102 KB · Views: 147

Hi,

Now that you modified your code... does it work or not?
--> Please give detailed informations.

It really takes much time to go through your undocumented code.
But I fear you dind´t follow the section "10.1.6 Pseudo code example".
* I dont see the CS = high after writing configuration byte.
* I don´t see that you wait for conversion complete
* I don´t see the wait after CS=LOW
(There´s a good reason why they write datasheets. And it´s your job to read it and follow their recommendations.
And btw: this seems to be a good and very detailed datasheet - so use it´s informations - there are many worse datasheets around)

I think it´s time for me to stop responding to your posts unless you do some essential jobs by yourself:
* commenting code
* compare datasheet and code by yourself
* answer our questions.

Klaus

added:
Where is your free wheeling diode on LS2?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…