|
|
View previous topic :: View next topic |
Author |
Message |
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
Conversion between ASCII and float. |
Posted: Wed Aug 17, 2016 8:11 am |
|
|
Hey guys, I am trying to convert an array of characters to float, do a calculation and then convert that back into chars and then print out the result. I am inputting the resistor value on LabView and printing out the P value over at LabView as well. Here is my code so far:
Code: |
void main() {
while(TRUE) {
int c;
int count;
float V, P;
char arr[50];
c = getchar();
count = 0;
while ((count < 50) && (isdigit(arr[count]))) {
arr[count] = c;
++count;
c = getchar()
}
delay_ms(1000);
V = 25.00;
strcpy(arr, "125");
P = atof(arr);
P = (V*V)/(c);
ftoa(P, arr, 1);
puts(arr);
while(arr[i] = '\0') {
putchar(arr[i]);
i++;
}
}
} |
I put 125 just for testing purposes. Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Wed Aug 17, 2016 8:32 am |
|
|
Er.
Code: |
strcpy(arr, "125"); //The array 'arr' in memory is now '1' '2' '5' '\0'
P = atof(arr); //P now =125.0
//P = (V*V)/(c); //You now throw away P!.....
P=(V*V)/P; //will give (25*25)/125
|
Also, on the earlier code 'reading in' the characters, after the last character is loaded, you need to add the '\0' to the string.
In C, a 'string', is an array of characters _terminated with a NULL_. The NULL needs to be there before you use any other 'string function' on the array. |
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Wed Aug 17, 2016 8:42 am |
|
|
Ttelmah wrote: | Er.
Code: |
strcpy(arr, "125"); //The array 'arr' in memory is now '1' '2' '5' '\0'
P = atof(arr); //P now =125.0
//P = (V*V)/(c); //You now throw away P!.....
P=(V*V)/P; //will give (25*25)/125
|
Also, on the earlier code 'reading in' the characters, after the last character is loaded, you need to add the '\0' to the string.
In C, a 'string', is an array of characters _terminated with a NULL_. The NULL needs to be there before you use any other 'string function' on the array. |
Thanks for the reply! I edited my code and compiled and ran it but whatever number I put in LabView for the resistor is the answer I get for P. I'm not sure why it's not carrying out the calculation. |
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Wed Aug 17, 2016 11:22 am |
|
|
PCM programmer wrote: | Post the complete test program, not just the code in main(). Post the
#include line for the PIC, #fuses, #use delay(), #use rs232(), any other
#include lines, and global variables, and then the main(). |
Code: |
#include <protoalone.h>
#include <stdio.h>
#include <stdlib.h>
#include <input.c>
#include <conversion.c>
#include <math.h>
#include <ctype.h>
protoalone.h includes:
#include <16f877A.h>
#fuses HS, NOLVP, NOWDT, NOPROTECT
#use delay (clock=20000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4
#use rs232 (baud=9600, xmit=PIN_c6, rcv=PIN_C7) |
conversion.c is the C program implementation of ftoa(). |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 17, 2016 1:56 pm |
|
|
The following program compiles and runs in MPLAB vs. 8.92 simulator.
It displays the following output, and it's correct (25 * 25) / 125 = 5
It doesn't do any keyboard input, but that's easily checked by just
printing the buffer after you get the input. If it looks OK, then your
input code is working.
If that works, then the problem is likely in your ftoa() function.
Test program:
Code: | #include <16f877A.h>
#fuses HS, NOLVP, NOWDT, NOPROTECT
#use delay (clock=20000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4
#use rs232 (baud=9600, xmit=PIN_c6, rcv=PIN_C7)
//#include <stdio.h>
#include <stdlib.h>
//#include <input.c>
//#include <conversion.c>
//#include <math.h>
//#include <ctype.h>
void main()
{
int i;
int c;
int count;
float V, P;
char arr[50];
count = 0;
V = 25.00;
strcpy(arr, "125");
P = atof(arr);
P=(V*V)/P; // *** Added by Ttelmah
sprintf(arr, "%7.3f", P); // *** Substituted for ftoa()
puts(arr);
while(TRUE);
} |
|
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Wed Aug 17, 2016 2:05 pm |
|
|
PCM programmer wrote: | The following program compiles and runs in MPLAB vs. 8.92 simulator.
It displays the following output, and it's correct (25 * 25) / 125 = 5
It doesn't do any keyboard input, but that's easily checked by just
printing the buffer after you get the input. If it looks OK, then your
input code is working.
If that works, then the problem is likely in your ftoa() function.
Test program:
Code: | #include <16f877A.h>
#fuses HS, NOLVP, NOWDT, NOPROTECT
#use delay (clock=20000000)
#define GREEN_LED PIN_A5
#define YELLOW_LED PIN_B4
#define RED_LED PIN_B5
#define PUSH_BUTTON PIN_A4
#use rs232 (baud=9600, xmit=PIN_c6, rcv=PIN_C7)
//#include <stdio.h>
#include <stdlib.h>
//#include <input.c>
//#include <conversion.c>
//#include <math.h>
//#include <ctype.h>
void main()
{
int i;
int c;
int count;
float V, P;
char arr[50];
count = 0;
V = 25.00;
strcpy(arr, "125");
P = atof(arr);
P=(V*V)/P; // *** Added by Ttelmah
sprintf(arr, "%7.3f", P); // *** Substituted for ftoa()
puts(arr);
while(TRUE);
} |
|
Thanks! It works! If I wanted to input numbers from LabView instead of use "125" would I need to do get_float() instead of "125"? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 17, 2016 3:40 pm |
|
|
Aayush13 wrote: |
Thanks! It works! If I wanted to input numbers from LabView instead of
use "125" would I need to do get_float() instead of "125"? |
get_float() requires that the incoming ASCII text must end with a
carriage return character (13 in decimal, 0x0D in hex).
If Labview does send a carriage return at the end, then you can replace
these two lines,
Code: | strcpy(arr, "125");
P = atof(arr); |
with this line:
|
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Thu Aug 18, 2016 7:29 am |
|
|
PCM programmer wrote: | Aayush13 wrote: |
Thanks! It works! If I wanted to input numbers from LabView instead of
use "125" would I need to do get_float() instead of "125"? |
get_float() requires that the incoming ASCII text must end with a
carriage return character (13 in decimal, 0x0D in hex).
If Labview does send a carriage return at the end, then you can replace
these two lines,
Code: | strcpy(arr, "125");
P = atof(arr); |
with this line:
|
So I tried this method but whatever I put as the input is what I get for the output. For ex. if I enter 65 for the resistor value on LabView, I get 65 for P as well on LabView. It doesn't seem to be carrying out the calculation? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Aug 18, 2016 8:03 am |
|
|
Maybe it's a Labview problem?
try disconnecting the PIC from the PC, see what happens when you enter 65 ..
try a terminal program like RealTerm ,see what happens...
what's the hardware between PIC and PC ?
The PIC code here by PCM P is solid.
Jay |
|
|
Aayush13
Joined: 12 Aug 2016 Posts: 14
|
|
Posted: Thu Aug 18, 2016 8:24 am |
|
|
temtronic wrote: | Maybe it's a Labview problem?
try disconnecting the PIC from the PC, see what happens when you enter 65 ..
try a terminal program like RealTerm ,see what happens...
what's the hardware between PIC and PC ?
The PIC code here by PCM P is solid.
Jay |
So on LabView I am using the VISA palettes. It writes the value of the resistor and reads the result which is the value of the power.
I tried disconnecting the PIC from the PC and running it but I still get the same answers for the input and output. Unfortunately, I am only limited to using CCS C and LabView for this project.
I am not sure what you mean by hardware between PIC and PC but I'm using ICD-U64 to connect them if that's what you mean? Thanks! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Thu Aug 18, 2016 9:37 am |
|
|
hmm..
Quote: | I tried disconnecting the PIC from the PC and running it but I still get the same answers for the input and output. |
OK, that shouldn't happen !
The ICD-U64 is an in-circuit debugger chunk of hardware, which I don't use.
Other who do will be able to help but it sounds like the PIC is never really connected to the Labview program, so 5 out = 5 in,125 out = 125 in which is what I think you're seeing.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Aug 18, 2016 12:03 pm |
|
|
The ICD, won't give you a serial connection to Labview. That is not what it does. You need separate serial hardware to link the PIC to the PC..... |
|
|
|
|
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
|