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

Floats & printf not working under certain conditions

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



Joined: 15 Jun 2008
Posts: 3

View user's profile Send private message

Floats & printf not working under certain conditions
PostPosted: Sun Jun 15, 2008 1:52 pm     Reply with quote

Hello: I'm having trouble with the next code. PCH 4.068.
When I comment the declarations of the char arrays sized 12 and 16, the code prints the correct string, when I use those arrays, the code prints garbage where it should print the float number.
Can anybody tell me what`s happening or better, how to fix it ?

Regards.

Code:

//the h file:

#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 enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                   //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 enabled
#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 NOMCLR                   //Master Clear pin used for I/O

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3,force_hw)

//now, the C

#include "Ctrl 4620.h"
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define spi_sdo   PIN_D0
#define spi_sck   PIN_D1

#define EETripPoint 10 //direccion donde queda guardado el ultimo TP
#define EEAdcSpan 16
#define EEAdcZero 22
#define EEK0Cal 28
#define EEK1Cal 36
#define EESerialNumber 42
#define EERefresh 46
#define EEFirstRun 61
 
#define PULS_MAS     PIN_B2
#define PULS_MENOS   PIN_B3
#define PULS_RESET   PIN_B4

#define OUT_1  PIN_B6
#define OUT_2  PIN_B7

#define AdcLsb 0.000625
#define ADCAddr 0b10010000

char cadena[4]="";
char parametro[12]="";
char buffer_out[16]="";

int p;
int idx;
float ifl=1;

float ADCFloat=0;
float AdcSPan=1;
float AdcZero=0;
float K1cal=1;
float K0cal=0;

int8 TP_Flag=0;
int8 Flag_Corte=0;
int8 i=0;
int8 Refresh=20;
int8 Puls_Timer=50;

int16 BTimer=234;
int16 CTimer=43;
int16 AdqTimer=25;
int16 DisplayTimer=10;

long SerialNumber;

signed long TripPoint=5000;
signed long Presion=0;
signed long AdcRaw=0;

void main()
{

setup();

while(1)

   {
   delay_ms(1000);
   printf("P=%u ifl=%1.3f\r\n",p,ifl);
   p++;
   ifl=p*0.2;
   
   }

}

void setup()
///////////////////////////////////////////////////////////////////////////////
//Inicialización general
///////////////////////////////////////////////////////////////////////////////
{

   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_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
     
}
Ttelmah
Guest







PostPosted: Sun Jun 15, 2008 2:48 pm     Reply with quote

Try making the field width large enough.
The CCS printf, is not 'error checked', and will do daft things, if you give it starting values that don't work. In printf, the %x.yF format, specifies the _total_ field width as the first number, with the number of digits to place after the decimal as the second. You are asking it to print a field one character wide, and then put at least four characters into it (the decimal point, and three trailing digits). Now most printf's, when you ask to do this, will automatically increase the first number to be large enough, but the CCS one tends to go wrong in this type of setup.
It is a common error, because in some other languages, the first digit, is the number of characters to start with, in front of the decimal point. In C, this is not the case.
Try %5.3F

Best Wishes
matiasnabarro



Joined: 15 Jun 2008
Posts: 3

View user's profile Send private message

Keep trying
PostPosted: Sun Jun 15, 2008 8:22 pm     Reply with quote

Hi, I've tried the 5.3F and this is what I get at 232 output:

P=0 ifl=-777777.777
P=1 ifl=-333333.333
P=2 ifl=-333333.333
P=3 ifl=-;;;;;;.;;;
P=4 ifl=-333333.333
P=5 ifl=-777777.777
P=6 ifl=-;;;;;;.;;;
P=7 ifl=-111111.111
P=8 ifl=-333333.333
P=9 ifl=-555555.555
P=10 ifl=-777777.777
P=11 ifl=-999999.999
P=12 ifl=-;;;;;;.;;;
P=13 ifl=-0.000

I don´t understand.

Regards,
Matias.
matiasnabarro



Joined: 15 Jun 2008
Posts: 3

View user's profile Send private message

Just simulator issue
PostPosted: Sun Jun 15, 2008 9:06 pm     Reply with quote

Ttelmah: I simulating the code in Proteus VSM, with the error noted first. It runs ok on real hardware.
Thanks for your help.

Regards,
Matias.,
Ttelmah
Guest







PostPosted: Mon Jun 16, 2008 2:16 am     Reply with quote

If you do a search here, you will find _lots_ of posts, where people think they have a problem, and then turn out to be using a simulator. Though simulators are 'great' for finding certain types of basic faults, unfortunately, none are perfect.
There are some 'real' problems with the CCS printf though, so being a bit careful is worthwhile (for instance, adding your own limit check on the range of incoming numbers), but in general, it does work.
Glad you have found the 'real' problem. Smile

Best Wishes
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