![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
I really need help, I have a problem with rs232 string! |
Posted: Sat Jun 30, 2012 11:05 am |
|
|
My project has 2 pic microcontrollers Master/Slave connected through rs232.
The master reads analog input from sensor and calculates the value and finally sends result to Slave then when Slave receives it, it displays the result to LCD.
But this is not the problem I'm reading and receiving and displaying correct values but beside the values i get the % signed on the lcd.
For example when analog inputs +5V the slave displays 1023 because I'm using ADC 10-bit, which is correct, but this is what I see on LCD 1023 %,
I tried many things to avoid seeing the % sign but nothing helps please guys help me.
//This is the Master code:
Code: |
#include <16F877A.h>
#device ADC = 10
#use delay(clock= 8M)
#use rs232(baud=9600,xmit=pin_d6,rcv=pin_d7) //select software UART
int16 adc_value; //used to read analog input(AN0)
char volt[4]; //to store each digit for calculations
char string[4]; //stores values to be sent
unsigned int c; //counter
void main() {
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
do {
adc_value = read_adc(); // adc_value reads analog input
//calculate and convert each digit to ASCII and store them in volt array
volt[0] = adc_value/1000 + 0x30;
volt[1] = (adc_value/100)%10 + 0x30;
volt[2] = (adc_value/10)%10 + 0x30;
volt[3] = adc_value % 10 + 0x30;
//four iterations to copy stored values in volt to string
for(c=0; c<4; c++)
{
string[c]=volt[c]; //string gets each digit
}
puts(string); //send values to Slave
delay_ms(500);
} while(1);
}
|
//Slave code:
Code: |
#include <16F877A.h>
#use delay(clock=8M)
#use rs232(baud=9600,xmit=pin_b0,rcv=pin_b1) //select software UART
#include "lcd.c"
char rec[4]; //used to store read value from Master
main()
{
lcd_init(); //configure lcd
//start message for 2sec
lcd_putc("<<<<<<<<<< Welcom >>>>>>>>>>");
delay_ms(2000);
lcd_putc("\f");
while(1)
{
gets(rec); //read each data and store it in rec
lcd_putc("\f"); //clear lcd
lcd_putc("<<<<<<<<<< Degital Voltmeter >>>>>>>>>>"); //display title
//display read values in rec converted to string
printf(lcd_putc,"\nVolt Measured = %s",rec);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Sat Jun 30, 2012 12:12 pm |
|
|
A _string_ in C, is "a _null terminated_ sequence of characters".
Note the _null terminated_.
You cannot store four text characters into a four character array, and use it as a string. A four character 'string', _must have_ a fifth character of storage, containing the terminating 'null'.
What happens is first, when you print from the master to the slave, extra characters are sent, till the code just happens to find something equal to zero. This is then put into the again too small storage area in the slave, and when this is printed, it again prints extra stuff, until a zero is found....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Sat Jun 30, 2012 12:18 pm |
|
|
Ttelmah wrote: | A _string_ in C, is "a _null terminated_ sequence of characters".
Note the _null terminated_.
You cannot store four text characters into a four character array, and use it as a string. A four character 'string', _must have_ a fifth character of storage, containing the terminating 'null'.
What happens is first, when you print from the master to the slave, extra characters are sent, till the code just happens to find something equal to zero. This is then put into the again too small storage area in the slave, and when this is printed, it again prints extra stuff, until a zero is found....
Best Wishes |
Thanks Ttelmah.
can you please show me how to put terminating null , i haven't done that before in CCS?
I spent like most of my time figuring out this before i post the my problem but couldn't solve it! |
|
![](templates/subSilver/images/spacer.gif) |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
Finally !! |
Posted: Sat Jun 30, 2012 12:25 pm |
|
|
I did it finally. I had to add a fifth element which will hold the terminating null('\0').
Thank you my friend you really helped me. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|