|
|
View previous topic :: View next topic |
Author |
Message |
meereck
Joined: 09 Nov 2006 Posts: 173
|
Problem with char array[6] |
Posted: Tue Nov 21, 2006 4:12 am |
|
|
Hello, I am trying to make this example work but with no success.
I always receive wrong characters of variable volt_string on PC.
I have also noticed that if I use fuses HSPLL the real baud rate will be 115200 (=12x higher) instead of 9600.
I would like to use HSPLL because I am going to use the USB interface, but together with RS232. I am a newbie.
Thanks in advance.
Code: | #include <18f2455.h>
#include "main.h"
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7, PARITY=N, BITS=8, STREAM=RS232)
char text[15]="testovaci char";
void convert_to_volts(long int data, char volts[6]) {
BYTE i;
long int temp,div;
div=0xCC;//03FF=5Volts
for(i=0;i<5;i++) {
temp=data/div;
volts[i]=(BYTE)temp+'0';
if(i==0) {
volts[1]='.';
i++;
}
temp=div*(BYTE)temp;
data=data-temp;
div=div/10;
}
volts[i]='\0';
putchar(volts[0]);
printf("end\r\n");
}
void display_data(long int data) {
char volt_string[6];
convert_to_volts(data,volt_string);
fprintf(RS232,"Text : %%Lu\r\n",volt_string);//the value volt_string is received wrong on PC
fprintf(RS232,"Variable : %4lX\r\n",data);
}
void main()
{
long int value;
int a;
setup_adc_ports(AN0_TO_AN1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
delay_ms(10);
set_tris_b(0);
fprintf(RS232,"RS232 test\r\n");
output_b(0);
while(1)
{
//a++;
//output_b(a);
value=Read_ADC();
display_data(value);
output_high(PIN_B0);
delay_ms(1000);
output_low(PIN_B0);
delay_ms(1000);
}
}
|
|
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Tue Nov 21, 2006 7:00 am |
|
|
I think your pseudo attempt to convert a float to a string in convert_to_volts is a waste of effort. Just write a simple routine using floats to convert the integer value from the ADC into volts floating point value. Then use printf formatting to display it in the way you'de like.
Also, to maximize processor speed use a 10MHz crystal with the the H4 fuse. That yields 40MHz.
John |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Nov 21, 2006 9:40 am |
|
|
Change the line Code: | fprintf(RS232,"Text : %%Lu\r\n",volt_string); |
to Code: | fprintf(RS232,"Text : %s\r\n",volt_string); |
For simplicity I also changed Code: | display_data(value); | to Code: | display_data(1000); |
When running this modified code in the MPLAB simulator it gave the expected output.
What output do you get when you apply the same changes?
Which compiler version are you running? |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Sat Nov 25, 2006 8:04 am |
|
|
Thank both of you for reply,
all the problems have been solved.
BTW fprintf(RS232,"Text : %s\r\n",volt_string); is not working for me.
There might be a bug in compiler. I am using version 3.242 |
|
|
Ttelmah Guest
|
|
Posted: Sat Nov 25, 2006 10:07 am |
|
|
Separately, on the oscillator.
You need to change the 'clock' statement to reflect the frequency being fed to the CPU, not the crystal frequency. The default setup, with the PLL enabled, is 96MHz to the USB, and 48Mhz to the CPU. You can run the CPU at a lower frequency, by changing the CPUDIV fuses:
CPUDIV1 (the default), gives 48MHz
CPUDIV2 gives 24MHz
CPUDIV3 gives 16MHz
CPUDIV4 gives 12MHz.
As it currently stands, you have a crystal at 4MHz, being multiplied by 24 to give 96MHz, then divided by two for the USB, and also by 2 to feed the CPU. You currently have CPUDIV1, so the real master clock to the CPU, is twelve times your 'declared' 4MHz frequency.
Best Wishes |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Sun Nov 26, 2006 7:05 am |
|
|
Ttelmah thank you, its clear now |
|
|
|
|
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
|