Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Displaying floating point numbers in a seven segment display

Status
Not open for further replies.

achar.deepak

Member level 1
Joined
Dec 12, 2016
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Displaying floating point numbers in seven segment display

Dear all,

I am facing problem in displaying floating point number coming from ADC after conversion. I have attached the code that I have attached the code for this. I want to display like 92.66677.... I have attached the image also. Please help.

Code:
/**
 * \file
 *
 * \brief Empty user application template
 *
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * Bare minimum empty user application template
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# "Insert system clock initialization code here" comment
 * -# Minimal main function that starts with a call to board_init()
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
#include <asf.h>
#include "lcd.h"
#include "gpio.h"
#include "twi_master.h"
#include "gpio.h"
#include "pm.h"
#include "delay.h"
#include "stdio.h"

#include "conf_clock.h"
#include <math.h>


///////////////////////////////////////////////////////////////////////ADC INIT ///////////////////////////////////////////////

#define OUTEN            AVR32_PIN_PB22
#define VREF             AVR32_PIN_PB21
#define BUZZER_CONTROL   AVR32_PIN_PB23
#define Test             AVR32_PIN_PB04   //TEST SW29
#define RESET            AVR32_PIN_PA18
#define shift            AVR32_PIN_PB07
#define LED              AVR32_PIN_PA09


/* Connection of the Key switch (from ADC6, PIN PA26) up right down */
#  define ADC_KEYSWITCH_CHANNEL      6
#  define ADC_KEYSWITCH_PIN          AVR32_ADC_AD_6_PIN
#  define ADC_KEYSWITCH_FUNCTION     AVR32_ADC_AD_6_FUNCTION

//Connection of the temperature sensor(from ADC3) CALL(sw14) HOLD(sw15) V-REV(SW16)  
#  define ADC_KEYSWITCH1_CHANNEL     3
#  define ADC_KEYSWITCH1_PIN         AVR32_ADC_AD_3_PIN
#  define ADC_KEYSWITCH1_FUNCTION    AVR32_ADC_AD_3_FUNCTION

  
  /** GPIO pin/adc-function map. */
  const gpio_map_t ADC_GPIO_MAP = {
	  
	  #if defined(ADC_BMS_CHANNEL)
	  {ADC_BMS_PIN ,ADC_BMS_FUNCTION}
	  #endif
  };

  #define ADC_RESOLUTION  0.00322580  // ADC resolution in volts = 3.3 / 1023
 
  int adc_value_key   = -1;
  int adc_value_key1   = -1;
  float voltage;
  float voltage1;
  float current;
  float currentfinal;
  float currentfinalfinal;
  char data_1,dat,dat1;

//////////////////////////////////// 7 segment init ///////////////////////////////////////////////////////////////////////////////////////
/* Digit registers */
#define AS1116_DIGIT0_REG		0x01	/* Digit 0 */
#define AS1116_DIGIT1_REG		0x02	/* Digit 1 */
#define AS1116_DIGIT2_REG		0x03	/* Digit 2 */
#define AS1116_DIGIT3_REG		0x04	/* Digit 3 */
#define AS1116_DIGIT4_REG		0x05	/* Digit 4 */
#define AS1116_DIGIT5_REG		0x06	/* Digit 5 */

/* Control registers */
#define AS1116_DECODEREN_REG	0x09	/* Decode-Mode */
#define AS1116_INTENSITY_REGG	0x0A	/* Global Intensity */
#define AS1116_SCANLTD_REG		0x0B	/* Scan Limit */
#define AS1116_SHUTDOWN_REG		0x0C	/* Shutdown */
#define AS1116_FEATURE_REG		0x0E	/* Feature */
#define AS1116_DISPLAY_TEST_REG	0x0F	/* Display Test Mode */
#define AS1116_INTENSITY_REG1	0x10	/* DIG0:DIG1 Intensity */
#define AS1116_INTENSITY_REG2	0x11	/* DIG2:DIG3 Intensity */
#define AS1116_INTENSITY_REG3	0x12	/* DIG4:DIG5 Intensity */
#define AS1116_INTENSITY_REG4	0x13	/* DIG6:DIG7 Intensity */



/* Diagnostic Register */
#define AS1116_DIAGNOSTIC_DIG0	0x14	/* Diagnostic Digit 0 */
#define AS1116_DIAGNOSTIC_DIG1	0x15	/* Diagnostic Digit 1 */
#define AS1116_DIAGNOSTIC_DIG2	0x16	/* Diagnostic Digit 2 */
#define AS1116_DIAGNOSTIC_DIG3	0x17	/* Diagnostic Digit 3 */
#define AS1116_DIAGNOSTIC_DIG4	0x18	/* Diagnostic Digit 4 */
#define AS1116_DIAGNOSTIC_DIG5	0x19	/* Diagnostic Digit 5 */
#define AS1116_DIAGNOSTIC_DIG6	0x1A	/* Diagnostic Digit 6 */
#define AS1116_DIAGNOSTIC_DIG7	0x1B	/* Diagnostic Digit 7 */


#define AS1115_TWI               (&AVR32_TWI)
#define AS1115_SDA_PIN           AVR32_TWI_SDA_0_0_PIN
#define AS1115_SDA_FUNCTION      AVR32_TWI_SDA_0_0_FUNCTION
#define AS1115_SCL_PIN           AVR32_TWI_SCL_0_0_PIN
#define AS1115_SCL_FUNCTION      AVR32_TWI_SCL_0_0_FUNCTION
#define CONF_TWI_IRQ_LEVEL       3
#define TWI_SPEED1               700
#define AS1115_ADDRESS           0x00

float voltage;
float voltage1;
float voltage2;
float voltage3;
float voltage4;
float voltage5;
float voltageavg;
float adcvalue;
int AS1115_read_byte(uint8_t byte_address);
void AS1115_write_byte(uint8_t byte_address,uint8_t byte_data);

unsigned char data ;

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 DS1339_write_byte(uint8_t byte_address, uint8_t byte_value) 
{		
	twi_package_t package;
	//byte_value = (byte_value/10*16) + (byte_value % 10); 
	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 DS1339_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;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

void adc_1()
{
	const gpio_map_t ADC_GPIO_MAP = {
		
		#if defined(ADC_BMS_CHANNEL)
		{ADC_BMS_PIN ,ADC_BMS_FUNCTION}
		#endif
        };

	unsigned char keystatus;
	float voltage;
	int adc_value_key   = -1;
	float current;
	float currentfinalfinal;
	/*  Assign and enable GPIO pins to the ADC function.  */
	gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) /
	 sizeof(ADC_GPIO_MAP[0]));
		
	/* Configure the ADC peripheral module.
		* Lower the ADC clock to match the ADC characteristics (because we
		 * configured the CPU clock to 12MHz, and the ADC clock characteristics are
		 *  usually lower; cf. the ADC Characteristic section in the datasheet). */
		AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
		adc_configure(&AVR32_ADC);
		
		/* Enable the ADC channels. */
		//adc_enable(&AVR32_ADC,ADC_BMS_CHANNEL);
		adc_enable(&AVR32_ADC,ADC_KEYSWITCH_CHANNEL);	
		adc_enable(&AVR32_ADC,ADC_KEYSWITCH1_CHANNEL);
}
unsigned int count=0;val;
char buf[10];

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

int SevSeg_PrintInt (float currentfinalfinal)
{
	unsigned  a;
	a=currentfinalfinal;
	sprintf(buf, "%0.2f", a);//capture numeric into string 
    AS1115_write_byte(0x08,LEDcode[(a/10000000)]);
    a=a%10000000;
    AS1115_write_byte(0x07,LEDcode[(a/1000000)]);
    a=a%1000000;
    AS1115_write_byte(0x06,LEDcode[(a/100000)]);
    a=a%100000;
    AS1115_write_byte(0x05,LEDcode[(a/10000)]);
    a=a%10000;
    AS1115_write_byte(0x04,LEDcode[(a/1000)]);
    a=a%1000;
	AS1115_write_byte(0x03,LEDcode[(a/1000)]);
	a=a%1000;
	AS1115_write_byte(0x03,LEDcode[(a/100)]);
	a=a%100;
	AS1115_write_byte(0x01,LEDcode[(a/10)]);
	a=a%10;
	AS1115_write_byte(0x02,LEDcode[a]);
}


void test_1()
{
	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_key = adc_get_value(&AVR32_ADC,ADC_KEYSWITCH_CHANNEL);
	adc_value_key1 = adc_get_value(&AVR32_ADC,ADC_KEYSWITCH1_CHANNEL);
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	lcdMoveCursor(2,12);
	delay_s(1);
	voltage=adc_value_key*0.0032258;

	lcdClear();
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 4");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	lcdMoveCursor(2,12);
	delay_s(1);
	lcdClear();
	voltage1=adc_value_key*0.0032258;
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 3");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	lcdMoveCursor(2,12);
	delay_s(1);
	lcdClear();
	voltage2=adc_value_key*0.0032258;
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 2");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	lcdMoveCursor(2,12);
	delay_s(1);
	lcdClear();
	voltage3=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)");
	lcdMoveCursor(2,12);
	delay_s(1);
	lcdClear();
	voltage4=adc_value_key*0.0032258;
	lcdMoveCursor(0,0);
	lcdPrintStr("Voltage 6.3V");
	lcdMoveCursor(1,0);
	lcdPrintStr("Time Remain: 0");
	
	lcdMoveCursor(2,0);
	lcdPrintStr("Current(mA)");
	lcdMoveCursor(2,11);
	voltage5=adc_value_key*0.0032258;
	
	voltageavg =((voltage+voltage1+voltage2+voltage3+voltage4+voltage5)/5);
	gpio_clr_gpio_pin(OUTEN);
	current=(voltageavg/1);
	currentfinal=current;
	lcdMoveCursor(2,11);
	currentfinalfinal=((currentfinal*1000));
	print_float(currentfinalfinal);
	gpio_enable_gpio_pin(VREF);
	gpio_clr_gpio_pin(VREF);
	
}




////////////////////////////////////////////SPI EEPROM INIT /////////////////////////////////////////////////////



uint8_t readdata;
uint8_t spi_eeprom_Read(uint16_t memAddr)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_READ,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_read_packet(AT25160B_SPI,&readdata,1);// read contents of memory address
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	return readdata;// return data
}

void spi_eeprom_Write(uint16_t memAddr, uint8_t data[],uint8_t len)
{

	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
}

void spi_eeprom_Write_Enable(void)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	spi_write(AT25160B_SPI,EEPROM_WREN);// send command
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	
}
void spi_eeprom_Write_Disable(void)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	spi_write(AT25160B_SPI,EEPROM_WRDI);// send command
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
}

struct spi_device SPI_DEVICE_EXAMPLE = {
	//! Board specific select id
	.id = AT25160B_SPI_NPCS
};


///////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////

void init_twi_as1115(void);

void init_twi_as1115(void)
{
	 twi_options_t opt;
	 static const gpio_map_t TWI_GPIO_MAP =
	 {{TWI_DS1339_SDA_PIN , TWI_DS1339_SDA_FUNCTION},
	 {TWI_DS1339_SCL_PIN , TWI_DS1339_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 = DS1339_ADDRESS;
	 // gpio_enable_gpio_pin(LED1);
	 twi_master_init(DS1339_TWI, &opt);
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SevSeg_Clear(void);

void SevSeg_Clear(void)
{
	unsigned char  i;
	for(i=0;i<6;i++)
	AS1115_write_byte(i,0x00);
}





int main (void)
{	
	
	gpio_clr_gpio_pin(OUTEN);
	gpio_enable_gpio_pin(VREF);
	gpio_clr_gpio_pin(VREF);
	
	
	sysclk_init();
	irq_initialize_vectors();
	cpu_irq_enable();
	initLcd();
	
	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);
	
	
	
	
	////////////////////////////////////////INIT SPI Driverr ///////////////////////////////////////////////////////////////
	
	//gpio of spi configuration
	static const gpio_map_t AT25160B_SPI_GPIO_MAP =
	{
		{AT25160B_SPI_SCK_PIN ,  AT25160B_SPI_SCK_FUNCTION },  // SPI Clock.
		{AT25160B_SPI_MISO_PIN , AT25160B_SPI_MISO_FUNCTION},  // MISO.
		{AT25160B_SPI_MOSI_PIN , AT25160B_SPI_MOSI_FUNCTION},  // MOSI.
		{AT25160B_SPI_NPCS0_PIN , AT25160B_SPI_NPCS0_FUNCTION}   // Chip Select NPCS.
	};
	
	gpio_enable_module(AT25160B_SPI_GPIO_MAP,sizeof(AT25160B_SPI_GPIO_MAP) / sizeof(AT25160B_SPI_GPIO_MAP[0]));
	gpio_enable_gpio_pin(AT25160B_SPI_HOLD);
	gpio_set_gpio_pin(AT25160B_SPI_HOLD);
	
	spi_master_init(AT25160B_SPI);
	spi_master_setup_device(AT25160B_SPI,&SPI_DEVICE_EXAMPLE, SPI_MODE_0,SPI_BAUDRATE, 0);
	spi_enable(AT25160B_SPI);
	spi_eeprom_Write_Enable();
	
	///////////////Initialize AS1115 Registers ////////////////////////////////
	
	
	AS1115_write_byte(0x0c,0x01);  //shutdown mode
	AS1115_write_byte(0x0e,0x04);  //feature register
	AS1115_write_byte(0x09,0x3f);  //decode 07
	AS1115_write_byte(0x0b,0x05);  //Scan limit register02
	AS1115_write_byte(0x0a,0x02); ///Intensity register
	//AS1115_write_byte(0x01,0x02);
	AS1115_write_byte(0x02,0x02);
	//AS1115_write_byte(0x03,0x02);
	//AS1115_write_byte(0x04,0x02);
	AS1115_write_byte(0x05,0x02);
	//AS1115_write_byte(0x06,0x02);
	
	AS1115_write_byte(0x14,0x00);
	AS1115_write_byte(0x15,0x00);
	AS1115_write_byte(0x16,0x00);
	AS1115_write_byte(0x17,0x00);
	AS1115_write_byte(0x18,0x00);
	
	
	delay_ms(300);
	
    init_twi_as1115();   ///Initialize AS1115
	
    delay_ms(100);
    DS1339_write_byte(0,0X02);  //09
    lcdMoveCursor(0,4);
    lcdPrintStr("PIONEER GROUP");
    lcdMoveCursor(1,0);
    lcdPrintStr("LACQUER CONDUCTIVITY");
    lcdMoveCursor(2,7);
    lcdPrintStr("TESTER");
    lcdMoveCursor(3,0);
    lcdPrintStr("TEST VOLT:5.0 TIME:5s");
    delay_ms(3000);
    lcdMoveCursor(1,0);
    
    lcdPrintStr("press any key");
    lcdClear();
    lcdMoveCursor(0,0);
    
    lcdPrintStr("SETTINGS");
    lcdMoveCursor(2,0);
    lcdPrintStr("SETTINGS+");
    
    
    unsigned count=0;
    
    unsigned key;
    
    int keyvalue;	
	AS1115_write_byte(0x15,0x10);
	//AS1115_write_byte(0x02,0x02);
	//AS1115_write_byte(0x03,0x03);
	//AS1115_write_byte(0x04,0x01);
	//AS1115_write_byte(0x05,0x04);
	//AS1115_write_byte(0x06,0x05);
   
	delay_ms(5000);
	while(1)
	{
		
		
     //AS1115_write_byte(0x0f,0x07);
		
		 if((gpio_get_pin_value(Test)==1))
		 {
			 
			 gpio_enable_gpio_pin(OUTEN);
			 gpio_set_gpio_pin(OUTEN);
			 
			 test_1();
			 SevSeg_Clear();
			 SevSeg_PrintInt (currentfinalfinal);
			  
			 delay_ms(100);
			 
		 }
			
	}
}
 

Re: Displaying floating point numbers in seven segment display

Too much code. Simply use printf.
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Re: Displaying floating point numbers in seven segment display

Hi,

I doubt you have a floating point ADC....

Therfore I assume you get binary from ADC, and then generate floats in your software.
I usually try to avoid floating point in a microcontroller.

****
What is your display value range:
* positive only?
* largest positive value?
* smallest positive non zero value?
* Do you have a fixed point at the display?

Klaus
 

Re: Displaying floating point numbers in seven segment display

Hi Klaus

I have to display positive current value ranging from 50 mA to 500 mA. I have attached the result I got.

Thanks
Deepak

IMG_20170215_173355.jpg
 

Re: Displaying floating point numbers in seven segment display

Hi,

Because you only have a 10 bit ADC it makes no sense to display more than 4 digits.
In your case "000.0" mA.
So the displayed range is "000.0" to "500.0". So the step size (resolution) is 0.1mA.

Imagine there is no decimal point: then it shows "0000" to "5000".
It means 5000 integer steps of 0.1mA.

Now I can't find how you calculate mA from the ADC value, therefore I assume you want to display "500.0" with an ADC value of
1023.

I recommend to use a multiplication with a constant:
Mul_val = 5000 / 1023 = 4.8876
Then transform the result into in integer value, and display the result in 4 digits. Then set the decimal point.

To safe processing power you could use an integer multiplication.
Mulval = 256 × 5000 / 1023 = 1251
If you use an integer multiplication (16 bit x 16 bit = 32 bit) then the result (0..5000) in the middle two bytes of the multiply_result.
You could also use a "shift 8 bits right" to get the mA value.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top