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

Weird Float and USART Problem

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



Joined: 21 Aug 2008
Posts: 34

View user's profile Send private message Send e-mail MSN Messenger

Weird Float and USART Problem
PostPosted: Tue Feb 17, 2009 8:42 am     Reply with quote

Hello, i am using PIC18F4620 and the compiler version of 4.085
and trying to execute the following code
Code:


#include <18F4620.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast=400000,sda=PIN_D1,scl=PIN_D0, FORCE_SW)


#include "C:\Documents and Settings\Bernard Kazour\My Documents\Course Finder\Java Website\C code\main.h"

#include <stdlib.h>
#include <input.c>

#include <math.h>


void ComputeFormCD();
void crsdist();

//int signlat1,signlat2,signlon1,signlon2,dc
float degLat1,degLat2,degLon1=0,degLon2=0;
float lat1,lat2,lon1,lon2;
float d,crs12,crs21;
float argacos;
float a,invf;




void main()
{
   setup_oscillator(OSC_8MHZ|OSC_TIMER1|OSC_IDLE_MODE|OSC_31250|OSC_PLL_OFF);
   delay_cycles(4);
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!
  // while (1)
   {

      degLat1 = 31.3;
      printf ("%f",degLat1);
      printf ("\n\r");

      degLon1=12.26;
      printf ("%f",deglon1);
      printf ("\n\r");

      degLat2=21.4225;
      degLon2=39.8261;
      ComputeFormCD()
      crsdist();
      crs12 =crs12*(180/PI);
      printf ("The course from 1 to 2 is: %f",crs12);
     
   }

}

void ComputeFormCD()


   lat1=degLat1*PI/180.0;
   printf("Lat1: %f",lat1);
   printf ("\n\r");
   lat2=degLat2*(PI/180.0);
   printf("Lat2: %f",lat2);
   printf ("\n\r");
   lon1=degLon1*(PI/180.0);
   printf("Lon1: %f",lon1);
   printf ("\n\r");
   lon2=degLon2*(PI/180.0);
   printf("Lon2: %f",lon2);
   printf ("\n\r");


}
void crsdist() // radian args
   /* compute course and distance (spherical) */
{
//!   float a1;
//!   float a2;
//!   float b1;
//!   float b2;
//!   float b3;
//!   
//!   a1=sin(lat1);
//!   a2=sin(lat2);
//!   b1=cos(lat1);
//!   b2=cos(lat2);
//!   b3=cos(lon1-lon2);
   
   d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2));
   printf("d: %f",d);
   printf ("\n\r");
}

When I try to execute it like this, all the printing on the USART are
successful, but the "d" calculation is always returning 0.
But when I remove the comments of the following lines:
Code:

//!   float a1;
//!   float a2;
//!   float b1;
//!   float b2;
//!   float b3;
//!   
//!   a1=sin(lat1);
//!   a2=sin(lat2);
//!   b1=cos(lat1);
//!   b2=cos(lat2);
//!   b3=cos(lon1-lon2);

These lines are used to divide the calculation of the "d" variable" because I thought that this calculation is failing because it is done directly at once, all the printing are returning 0.
Does anyone have an idea why this is happening, and how can solve this problem?
Thanks
_________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 17, 2009 10:50 pm     Reply with quote

Quote:
but the "d" calculation is always returning 0.


I get the following results in MPLAB simulator. Is your complaint
about the last line, shown in bold ?
Quote:

31.30
12.26
Lat1: 0.54
Lat2: 0.37
Lon1: 0.21
Lon2: 0.69
d: 0.46
The course from 1 to 2 is: 0.0


It's computed and displayed by these lines:
Quote:
crs12 =crs12*(180/PI);
printf ("The course from 1 to 2 is: %f",crs12);

But 'crs12' is never initialized anywhere in your program.
It happens to be 0 upon power-up (at least in the simulator).
That's why the result is always 0.
MicroManiac



Joined: 21 Aug 2008
Posts: 34

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Feb 18, 2009 1:14 am     Reply with quote

well, when i am using the USART, the "d" is always returning 0.
i am surprised that it is returning a result in the MPLAB simulator. the other values are the same.
i didn't post my whole code, the calculation of the "crs12" is based on "d".
i thought there was a problem in "d" and there is no need to post everything.
Why do you think when i send "d" over the USART it is returning 0?
_________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 1:20 am     Reply with quote

Quote:
Why do you think when i send "d" over the USART it is returning 0?

If possible, post a short test program that demonstrates this problem.
(The program should be much shorter than the program you already
posted).
Also, post the name of the terminal program that runs on your PC.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Feb 18, 2009 1:31 am     Reply with quote

I only see, that crs12 is never set to a value in your code, so it can be expected zero. Is this the problem?
MicroManiac



Joined: 21 Aug 2008
Posts: 34

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Thu Feb 19, 2009 1:26 am     Reply with quote

Well that is not the problem
i created a new project and recompiled everything and it worked smoothly
dunno what was happening before
thank you all for your help
_________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 3:46 am     Reply with quote

Quote:
Well that is not the problem
In other words, your previously posted code has been meaningless or misleading.
MicroManiac



Joined: 21 Aug 2008
Posts: 34

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Fri Feb 20, 2009 3:18 am     Reply with quote

FvM wrote:
In other words, your previously posted code has been meaningless or misleading.

Actually Not,
i've taken that exact code, recompiled it in a new project and it worked fine.
My problem was in the "d" variable not the "crs12" variable, but it was solved now and i dunno what was the problem
_________________
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."
Albert Einstein
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