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

float and int with PIC18F4550

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



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

float and int with PIC18F4550
PostPosted: Thu Apr 17, 2014 8:48 am     Reply with quote

Hello,

I'm using PCW VER4.134 with PIC18F4550.

My problem is the following: from a CAN I get 2 value:
VHCAN (Most significant data byte) and VLCAN (Least significant data byte).

I would like to convert VHCAN and VLCAN to get my voltage value through USB.

So for example: input voltage in the CAN = 24.6V

This code is working very well, I can read 24.6V through USB COM
Code:

byte    VHCAN=0,VLCAN=0;

VHCAN=192;  //to simulate the CAN
VLCAN=48;
printf(usb_cdc_putc,"%2.1fV",((float)VHCAN*16+((float)VLCAN/16))/4095*4.096*8);     // 12bits=>4095    Vref=4.096V 2.75V=>2749 divisor by 8


But as I have 8 channel of CAN, I would like to put the value inside a table:

This is not working:
Code:

byte    VHCAN=0,VLCAN=0;
float    M_V[8];

VHCAN=192;  //to simulate the CAN
VLCAN=48;
M_V[0]=((float)VHCAN*16+(float)VLCAN/16)/4095*4.096*8;
printf(usb_cdc_putc,"%2.1fV",M_V[0]);     // 12bits=>4095    Vref=4.096V 2.75V=>2749


I can read 2.0V through USB COM. I think I have a trouble of cast.
I tried lots of think without result...

Could someone help me?

Thanks a lot

Fabrice
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 17, 2014 10:23 am     Reply with quote

I made a test program for your routine, I installed vs. 4.134, and I ran
the program in MPLAB simulator. I used the UART1 feature of MPLAB for
output (not USB CDC). So this method tests your code. It works.
I got this output:
Quote:
24.6V


Test program
Code:

#include <18F4550.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, UART1, ERRORS)

// Dummy routine to send output to MPLAB
// output window, instead of to USB CDC.
int8 usb_cdc_putc(int8 data)
{
putc(data);
}

//============================
void main()
{
byte VHCAN=0, VLCAN=0;
float M_V[8];

VHCAN=192;  //to simulate the CAN
VLCAN=48;
M_V[0]=((float)VHCAN*16+(float)VLCAN/16)/4095*4.096*8;
printf(usb_cdc_putc,"%2.1fV",M_V[0]);     

printf("\r");
while(1);
}
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 1:45 am     Reply with quote

Dear PCM programmer,

I've tested your code and it's working. Shocked

I'm very disturbed because inside my whole software it's not working.

I'm going to integrate this code in my global program and I will tell you if it's ok or not.

Thanks,

Fabrice
neo19



Joined: 14 Aug 2012
Posts: 30
Location: France

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 3:39 am     Reply with quote

So I tested again in my whole software.

I realized that I had another subroutine which generated trouble on my M_V[].

Terrible!!

Thanks again for your help.
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