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

beginners problem

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



Joined: 15 Apr 2004
Posts: 2
Location: North Wales

View user's profile Send private message

beginners problem
PostPosted: Thu Apr 15, 2004 5:26 pm     Reply with quote

Hi, im having difficulty recieving and printing hexadecimal numbers using the rs232 protocol. The PIC im using (16C84) recieves data from a device and then prints to a Visual Basic application running on a PC. The PIC waits for the start character from the device 0x80 or 0x81 and then recieves the next two 8 bit hex numbers from the device. It then prints these two to the PC via the serial port.

The problem is that sometimes the PIC decides to add a '1' to the front of the second of the two numbers i.e. instead of:

01101111 (6F)

it will print:

11101111 (EF)

The problem is intermittent, sometimes it does it, sometimes it doesnt... the problem is only with the second of the numbers, the first number is always fine.

here is the code in question:

--------------------------------------------------------------------------------
void compass(void)
{
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B3)

c = 0x00;

output_high(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);

while ((c != 0x80) && (c != 0x81))
{
c = getch();
}

a = getch();
b = getch();

#use rs232(baud=9600, xmit=PIN_B0, rcv=PIN_B1)

printf("%x \n", a);

printf("%x \n", b);

return;
}
--------------------------------------------------------------------------------
a, b and c are defined as so:
--------------------------------------------------------------------------------
unsigned int8 a;
unsigned int8 b;
unsigned int8 c;
--------------------------------------------------------------------------------

ive tried defining them as:

int a;
int8 a;
int16 a;
unsigned int a;
unsigned int16;

etc... but it still does the same.

Im using MPLAB with CCS.

Any help would be much appreciated, like ive said - im sure its a beginner style problem, but its driving me mad!

Tom
SteveS



Joined: 27 Oct 2003
Posts: 126

View user's profile Send private message

PostPosted: Fri Apr 16, 2004 6:56 am     Reply with quote

Add ERRORS to your #USE RS232 statement and check it each time on inout. Maybe it's getting messed up on the incoming side.

I always try to break the problem down or localize it. Perhaps you can send each received character to a port to verify it's correct. then you know if it's on the receive end or the transmit end.

Try another PC - evidently PCs these days can be off quite a bit on baud.

What is your clock freq? and have you checked that it's correct?

Remember:
"Sometimes it's not what you don't know, but what you know that ain't so"

- SteveS
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Apr 16, 2004 7:27 am     Reply with quote

This sounds like a marginal baud rate error problem.
The real fix is to see which clock is off and fix it.
If you are using a software UART you could try setting the PIC for 10080 baud or 9120 baud (9600 +/- 5%), or tweak the divisors if it is a hardware UART.
You could also try sending data with two stop bits to give the receiver a little more time to synchronize to the next start bit.
_________________
The search for better is endless. Instead simply find very good and get the job done.
soddemfx



Joined: 15 Apr 2004
Posts: 2
Location: North Wales

View user's profile Send private message

PostPosted: Fri Apr 16, 2004 3:56 pm     Reply with quote

Thanks for the advice, ive been playing with it all day... I tried changing the resonator and playing with the baud rates as you suggested. Unfortunately it still produced the same results.

After a lot of fiddling i found out the problem was due to the design of my board. I was trying to recieve the same rs232 data on two PIC's (by sharing a recieve line). Apparantly they didnt like sharing... Ive now changed it so that one PIC 'echoes' the data to the other and it works fine.

My next problem is that when i send 8bit numeric data from one PIC to the other (rs232), it decides to convert it to ASCII as so:

if i send decimal 1 (01 (hex)) it sends it as ASCII '1' which is 00110001 which is hex 31 or decimal 49. How do i stop it from doing this?

Sorry for asking these lame questions... Smile

Tom
Guest








PostPosted: Sat Apr 17, 2004 12:49 pm     Reply with quote

soddemfx wrote:

My next problem is that when i send 8bit numeric data from one PIC to the other (rs232), it decides to convert it to ASCII as so:

if i send decimal 1 (01 (hex)) it sends it as ASCII '1' which is 00110001 which is hex 31 or decimal 49. How do i stop it from doing this?

Sorry for asking these lame questions... Smile

Tom


Sounds like you have the wrong format of printf(). Try just using putc().
Ttelmah
Guest







PostPosted: Sat Apr 17, 2004 3:39 pm     Reply with quote

Anonymous wrote:
soddemfx wrote:

My next problem is that when i send 8bit numeric data from one PIC to the other (rs232), it decides to convert it to ASCII as so:

if i send decimal 1 (01 (hex)) it sends it as ASCII '1' which is 00110001 which is hex 31 or decimal 49. How do i stop it from doing this?

Sorry for asking these lame questions... Smile

Tom


Sounds like you have the wrong format of printf(). Try just using putc().

It sounds a though you are probably misunderstanding how characters/numbers 'work' on the PC/PIC etc..
Start here. A 'character' occupies 8 bits, and can in binary, hold a value from 00000000 to 11111111. These values can be 'represented' in different numeric formats, so 00000000, is '0' in decimal or hex. while 11111111 is 'FF' in hex, or 255 in decimal (or -127, if treated as a 'signed' number, where the top bit is then used as a 'flag' for the number being -ve). The 'hex' representation of '1', is the ASCII character '1'. This is what the printf statement is generating for you.
The basic numeric formats available to you, are:
%c sends a single character, containing the raw value stored in the variable.
%u sends as many ASCII characters as necessary to code the value in normal decimal text.
%h sends as many ASCII characters as necessary to code the value in hexadecimal.
%d sends the characters like %u, but treats the top bit as a sign.
You can also add 'size' parameters, and specifiers to 'pad' the results. So:
%02h, will send '0' as '00', '1' as '01' etc..
If you are sending '8bit numeric data', you can either use the '%c' format, or ignore printf completely (and save space in your code), by just using:
putc(val);

which outputs the value 'val' directly...
If you are outputing '8bit data' directly, then you are not using 'hex'.

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