DECIMAL POINT rounding off plss help

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
lorenz2828
Posts: 32
Joined: 29 Sep 2012 17:25

DECIMAL POINT rounding off plss help

#1 Post by lorenz2828 » 18 Feb 2013 17:38

my code is this...
in simulation...
the output of KW = 2.199999

how am i going to round this off to 2.20
exactly two decimal values only....

Code: Select all

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;

void          Init();

const unsigned char AT_CMGF1[]                   = "AT+CMGF=1\r";
const unsigned char AT_CMGD[]                    = "AT+CMGDA=1\r";
const unsigned char AT_CSQ[]                     = "AT+CSQ\r";
const unsigned char AT_CMGF[]                    = "AT+CMGF=0\r";
const unsigned char AT_CMGL[]                    = "AT+CMGL\r";
const unsigned char AT[]                         = "AT\r";
const unsigned char ATE0[]                       = "ATE0\r";                         //!< Echo off
const unsigned char AT_CNMI[]                    = "AT+CNMI=1,1,,,1\r";              //!< Identification of new sms
const unsigned char AT_CPMS[]                    = "AT+CPMS=\"ME\",\"ME\",\"ME\"\r"; //!< Preferred storage


void main() {
  unsigned char mybuff[54], mypbuff = 0;
  char dat, err, ptr, extra[10], kwdata[10], kwhdata[10],kwd1;
  float wattsec_ = 0;
  float total = 0;
  float ave = 0;
  float ave2 = 0;
  float kw =0;
  float kwh =0;
  int count = 0;

  TRISD=0;
  PORTB=0;
  PORTD=0;
  TRISB = 0;
  ADCON1 = 0x0F;  
  Init();
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1, 2, "Welcome");
  delay_ms(1000);

  Lcd_Out(1,2, "Energy Monitoring");
  Lcd_Out(2,8, "System");
  Lcd_Out(4,1, "AROMAMI");
  delay_ms(5000);



  while(1){
     while(1)
     {
          do{ dat = Soft_Uart_Read(&err); }while(err);
          if(dat == 'O'){ break; }
          if((mypbuff == 0) && (dat != 'I')){ continue; }
          mybuff[mypbuff++] = dat;
          if(mypbuff == 53)
          {
               ptr = strtok(mybuff, ",");     //mybuff here contains the string from power analyzer module
               ptr = strtok(0, ",");          // ptr now contains string of watts
               count++;
               total += wattsec_;
               wattsec_ = atof(ptr);
               kw = wattsec_/1000;
               kwh += kw;
               FloatToStr(kw,kwdata);
               FloatToStr(kwh,kwhdata);
               
               if((count % 30) == 0){
                  ave = kwh / 30;           // recode pagconvert**********************************************
                  FloatToStr(ave, extra);
                  
                                              // send to gsm program*************************************************************
                  //ATE0;
                  //delay_ms(1000);

                  total = 0;
                  count = 0;
                  delay_ms(2000);
               }
               
               Lcd_Cmd(_LCD_CLEAR);
               Lcd_Out(1,1, "KW: ");
               Lcd_Out(1,5, kwdata);

               Lcd_Out(2,1, "KW/hour: ");
               Lcd_Out(2,10, kwhdata);

               Lcd_Out(3,1, "Average/30 sec: ");
               Lcd_Out(4,1, extra);
               Uart1_Write_Text(kwdata);
               mypbuff = 0;
               break;
          }
     }
  }
}

//void COM_puts(const unsigned char *cmd)
//{
//     while(*cmd)
//         UART1_Write(*cmd++);
//}

void Init()
{
        Soft_UART_Init(&PORTD,1,0,9600,0);     // initialize serial extra port
        UART1_Init(2400);
                                               // initializes GSM
        //Delay_ms(1000);
        //COM_puts(ATE0);                      // gsm mode: echo off
       // Delay_ms(350);
        //COM_puts(AT_CMGF);
        //Delay_ms(350);
        //COM_puts(AT_CPMS);
        //Delay_ms(350);
        //COM_puts(AT_CNMI);
        //Delay_ms(1000);
        //COM_puts(AT_CMGD);
        //Delay_ms(2000);
}

lejeff
Posts: 218
Joined: 04 Dec 2011 16:10
Location: France

Re: DECIMAL POINT rounding off plss help

#2 Post by lejeff » 19 Feb 2013 08:42

You can manage the decimal part of your result using the ceil and floor functions

Post Reply

Return to “mikroBasic PRO for AVR General”