|
|
View previous topic :: View next topic |
Author |
Message |
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
decimal position |
Posted: Fri Apr 06, 2018 12:43 pm |
|
|
Usually float type variable can have two decimal position after point.
example: Code: | float a; //So a can have 3.33 but can't have 3.3333 |
But I would like to declare a variable which can have four decimal position after point, in other words which can have 3.3333, then what should I do? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 06, 2018 3:19 pm |
|
|
It's not limited to 2 digits after the decimal point.
Do this:
|
|
|
PROMOD
Joined: 01 Feb 2018 Posts: 42
|
|
Posted: Fri Apr 06, 2018 7:59 pm |
|
|
Code: | #include<18f4431.h>
#device adc=10
#fuses xt,nowdt,nolvp
#use delay(clock=20000000)
#include "flex.c"
float k=0,a=0,r=0,p=0,pav=0.0000,en=0.0000;int16 i=0,n=0;
#int_timer2
void pwm_interrupt()
{
i=i+1;
set_adc_channel(0);
k=read_adc();//to covert voltage value of a certain moment in channel 0 into digital value
k=((k/204)-2)*(313/2);// here offset value is 2; (311/2) is used to transfer scaled voltage value into original value.
set_adc_channel(1);
a=read_adc();//to covert current value of a certain moment in channel 1 in to digital value.
a=((a/204)-2)*(1/0.033);//here offset value is 2. 1023/5=204; (1/0.033) is used to transfer scaled current value into original value.
p=p+a*k; //a*k= power of that certain moment. p storing the sumation of power of 49 different moment.
if(i==50)
{
n=n+1;
pav=p/i;en=en+pav*0.02; //pav is the averag of whole some of 50 different moment value; setting all variable to 0.
if(n>100)
{
lcd_putc("\f");
printf(lcd_putc,"PAVRAGE = %fKW",pav/1000);//printing pav after converting it into KW.
lcd_gotoxy(1,2);
printf(lcd_putc,"ENERGY = %fKWH",en/(1000*60*60)); //printing en after converting it into KWH.
n=0;
}
i=0;p=0;pav=0;
}
}
void main()
{
lcd_init();
printf(lcd_putc,"PAVRAGE = %fKW",pav/1000);
lcd_gotoxy(1,2);
printf(lcd_putc,"ENERGY = %fKWH",en/(1000*60*60);
setup_timer_2(t2_div_by_16,125,1); //interrupt setting time(in every 4*10^-4 seconds interrupt is happening)
enable_interrupts(global);
enable_interrupts(int_timer2);
setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);
while(1)
{
}
}
| Then in this program why is the variable "en, pav, etc" not showing four decimal position after point in LCD display?
Thanks in Advance! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 06, 2018 8:55 pm |
|
|
Add the width and precision numbers shown in bold below:
Quote: |
printf("lcd_putc, PAVRAGE = %7.4fKW", pav/1000);
lcd_gotoxy(1,2);
printf("lcd_putc, ENERGY = %7.4fKWH", en/(1000*60*60)); |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Apr 07, 2018 2:03 am |
|
|
Why have you created yet another version of your average power measurement post?
You've been given loads of advice and hints on how to correct numerous errors.
The code you're persisting in using hasn't a snowball in Hell's chance of success.
At this stage, I give up.
Mike |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|