CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

decimal position

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

decimal position
PostPosted: Fri Apr 06, 2018 12:43 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 06, 2018 3:19 pm     Reply with quote

It's not limited to 2 digits after the decimal point.

Do this:
Code:
float a = 3.3333;
PROMOD



Joined: 01 Feb 2018
Posts: 42

View user's profile Send private message

PostPosted: Fri Apr 06, 2018 7:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 06, 2018 8:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 07, 2018 2:03 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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