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 CCS Technical Support

int32 division will not display in terminal via RS232

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



Joined: 24 Apr 2009
Posts: 1

View user's profile Send private message

int32 division will not display in terminal via RS232
PostPosted: Fri Apr 24, 2009 12:45 am     Reply with quote

Good morning,

I am working on a project that uses two light sensors (one inside and one outside) to determine how much an electrochromic window has tinted. I am trying accomplish this by comparing the ratio of the two sensors which are read into the ADC. Right now I am just trying to read the ratio of the two sensors. Eventually I will set of IF statements to see if the window is 0%,25%,50%,75%, or 100% tinted (the one for 0% is set up already). I can get values for the sensors, but when I try to read the ratio from the RS232 I get 0.

I am using a PIC16F887 and version 4.020B of the compiler.

Code:
#if defined(__PCM__)
#include <16F887.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


long read_adc_average1()
{
   long value;
   int i;

   //set_adc_channel(A/D_Input);
   set_adc_channel(2);
   delay_ms(150);
   value=0;
   for ( i=0 ; i<8 ; i++ ) {
      value += Read_ADC();
      delay_ms(10);
   }
   /* divide by 8 */
   value = value / 8;

   return value;
}
long read_adc_average2()
{
   long value;
   int i;

   //set_adc_channel(A/D_Input);
   set_adc_channel(3);
   delay_ms(150);
   value=0;
   for ( i=0 ; i<8 ; i++ ) {
      value += Read_ADC();
      delay_ms(10);
   }
   /* divide by 8 */
   value = value / 8;

   return value;
}
void main()
{
   int8 scaleratio;
   int16 VoltsScaleFactor;
   int32 ScaledReading1;
   int32 ScaledReading2;
   int32 ratio;
   
   VoltsScaleFactor=4790;

   setup_adc_ports(ALL_ANALOG);
      setup_adc(ADC_CLOCK_INTERNAL);
while(1)
   {
      //Here we read the A/D converter, and display the appropriate reading on the LCD.
      ScaledReading1=((int32)read_adc_average1()*VoltsScaleFactor)/1000;
     ScaledReading2=((int32)read_adc_average2()*VoltsScaleFactor)/1000;
      ratio=(ScaledReading1/ScaledReading2);
     if (ratio==1){
     printf("0 tint\n\r");
     }//endif

     else{


   ratio *=100000;
   printf("ratio: %04.3w\n\r",ratio);
//      scaleratio = (int8)(ratio);
     // Here we send the reading to the serial port
      printf("Inside_Sensor: %04.3w\n\rOutside_Sensor: %04.3w\n\r", ScaledReading1,ScaledReading2);
      delay_ms(250);
     }//endelse
   }//endwhile

}


output in terminal:
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 24, 2009 12:51 am     Reply with quote

Do a test program. Get rid of 90% of your code, and make a little test
program that emulates what you're doing, except that it loads 'ratio' with
some fixed value, and test if it works. This will help you to concentrate
on the problem and find out exactly what's wrong.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

Integer divsion displays correctly!
PostPosted: Fri Apr 24, 2009 1:16 am     Reply with quote

You have ScaledReading1 < ScaledReading2, so the integer quotient ScaledReading1/ScaledReading2 should be supposed to be zero. Try with a calculator!

To get a correct 4.3 fixedpoint result, calculate 1000*ScaledReading1/ScaledReading2 instead.
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