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 CCS Technical Support

PIC <--> Sensor RS232 Communications...Please Help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PIC <--> Sensor RS232 Communications...Please Help
PostPosted: Fri Jul 08, 2005 7:01 am     Reply with quote

Hello all,

I recently purchased an IMU from Microstrain that tx and rx data serially. When connecting the sensor to the PC and pinging it (thru hyperterminal) for the device serial # (using the command BYTE 241 as defined by Microstrain), it sends back 5 BYTES which correspond to:

BYTE1: 241 (command that I sent to the IMU)
BYTE2: 6 (MSB of the serial number)
BYTE3: 251 (LSB of the serial number)
BYTE4: xxx (MSB of checksum -- used for sanity check...not important)
BYTE5: xxx (LSB of checksum -- used for sanity check...not important)

I'd like to get the same response when pinging the sensor using my PIC micro. I am using a PIC 16Fxxx and am trying to use the getc() command to receive this data packet. But this is not working for some reason (the format of my code is shown below). I get a different number every single time -- maybe my timing is off? The default baud rate for the sensor is 38400. Any suggestions would be much appreciated.

#use rs232(baud=38400,xmit=pin_B1,rcv=pin_B0)

// main starts here...
int sn1, sn2, sn3, sn4, sn5; // define 5 BYTES

putc(241);

while(!kbhit())
{
sn1 = getc(); // get 1st BYTE
sn2 = getc(); // get 2nd BYTE
sn3 = getc(); // get 3rd BYTE
sn4 = getc(); // get 4th BYTE
sn5 = getc(); // get 5th BYTE
}

Thanks in advance,
-Bill
Ttelmah
Guest







PostPosted: Fri Jul 08, 2005 7:25 am     Reply with quote

Your while loop is wrong/silly...
getc, will wait for a character if no character is present.
Just use the getc commands without the while. As it stands, it'll only enter your while loop, if no character is 'waiting' following the command. You should probably clear the buffer first. So:

Code:

   //flush any spurious characters waiting
   while (kbhit) getc();
   putc(241);
   sn1 = getc(); // get 1st BYTE
   sn2 = getc(); // get 2nd BYTE
   sn3 = getc(); // get 3rd BYTE
   sn4 = getc(); // get 4th BYTE
   sn5 = getc(); // get 5th BYTE

Otherwise if the UART has received a character, before arriving at the reading code, the values will never be fetched.
Where kbhit would come in useful, would be if you don't want the code to hang if there is no reply, where you could add a test for a character arriving, before you start looking for the returned data.

As it stands, assuming that the UART input buffer is empty, it ought to work, so timing is a possible problem. You do not say what crystal rate you are using, and this is vital to work out if this is likely to be the fault.
The other question, is how are the connections made?. Have you got a MAX232 or equivalent buffer between the PIC and the sensor?. Is the ground line also connected?.

Best Wishes
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 9:33 am     Reply with quote

Thanks for the reply. I tried both with and without the while loop. I also tried flushing the buffer as you suggested, but I am still getting unexpected values back from the sensor.

I am using a 10 MHz crystal and a MAX233 converter chip. I'm assuming you mean are there common grounds between the PIC and MAX...the answer is yes.

Any more help you (or anyone else) could provide is greatly appreciated.

Thanks,
-Bill
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 10:28 am     Reply with quote

Quote:
I am using a PIC 16Fxxx


What PIC exactly are you using ?
Ttelmah
Guest







PostPosted: Fri Jul 08, 2005 10:30 am     Reply with quote

What about the grounds between the Max, and the sensor?.
At 10MHz, the error on the baud rate, should be 1.73%, which ought not to be a problem. What are the actual values you are seeing, compared to the expected values (after all you know what the first byte is meant to be). It may be possible to see what is going wrong from the values received.

Best Wishes
Guest








PostPosted: Fri Jul 08, 2005 12:34 pm     Reply with quote

Well, let me start over by describing the setup I have in more detail. On the PIC, I am using pins A0,A1 to tx/rcv RS232 commands to the MAX which is connected with the sensor. Then, to see what I get back from the IMU, I am using pins B0,B1 to tx/rcv RS232 commands to another MAX chip which is connected with the PC (this is so I can see the sensor response via hyperterminal). Before your last posting, I had the sensor powered through an AC adapter and the PIC circuit powered through a separate power supply.

After reading your last posting, I eliminated the AC adapter and am now powering everything on the same PS. However, I do not get any response this way. I went back to the old way (separate PS) and this is what characters I expect and what I actually get after sending the 241 command to the sensor:

Should get the following and do when I connect PC directly to sensor:
ñûì

With PIC, I got the following responses...all different:
gÎïÞŽ
nOgO
öžÎÏ
÷ççïç
î÷ïÇÎ
óçÞŸþ
ç–çç
gîæÇÏ
fççÇÏ
gçæÆÏ
gïçï

Here is my exact code in case you see something I might have missed.
Code:

#include <16F84.H>
#include <stdlib.h>

#define A0 PIN_A0
#define A1 PIN_A1
#define B0 PIN_B0
#define B1 PIN_B1

/*-----------------------------------------------------*/
/* No Watch Dog Timer (NOWDT)
/* No Protect
/* Power Up Timer (PUT)
/* 10 MHz Clock
/* RS232 define for PIC-IMU communication (IMU)
/* RS232 define for PIC-PC communication (PC)
/*-----------------------------------------------------*/

#fuses HS,NOWDT,NOPROTECT, PUT
#use delay(clock=10000000)
#use rs232(baud=38400, xmit=B1, rcv=B0, stream=PC)
#use rs232(baud=38400, xmit=A1, rcv=A0, stream=IMU)

main() {

   while(1)
   {
      int sn1=0, sn2=0, sn3=0, sn4=0, sn5=0;
      
      delay_ms(2000);

      // transmit to IMU
      //fputs("Transmitting to IMU 1...", PC);

      //flush any spurious characters waiting
      //while (kbhit()) getc();

      fputc(241,IMU);

      sn1 = fgetc(IMU); // get 1st BYTE
      sn2 = fgetc(IMU); // get 2nd BYTE
      sn3 = fgetc(IMU); // get 3rd BYTE
      sn4 = fgetc(IMU); // get 4th BYTE
      sn5 = fgetc(IMU); // get 5th BYTE
      
      
      fputc(sn1, PC);
      fputc(sn2, PC);
      fputc(sn3, PC);
      fputc(sn4, PC);
      fputc(sn5, PC);
      
      /*------------------------------------------------------*/

   }
}
Guest








PostPosted: Fri Jul 08, 2005 12:34 pm     Reply with quote

Well, let me start over by describing the setup I have in more detail. On the PIC, I am using pins A0,A1 to tx/rcv RS232 commands to the MAX which is connected with the sensor. Then, to see what I get back from the IMU, I am using pins B0,B1 to tx/rcv RS232 commands to another MAX chip which is connected with the PC (this is so I can see the sensor response via hyperterminal). Before your last posting, I had the sensor powered through an AC adapter and the PIC circuit powered through a separate power supply.

After reading your last posting, I eliminated the AC adapter and am now powering everything on the same PS. However, I do not get any response this way. I went back to the old way (separate PS) and this is what characters I expect and what I actually get after sending the 241 command to the sensor:

Should get the following and do when I connect PC directly to sensor:
ñûì

With PIC, I got the following responses...all different:
gÎïÞŽ
nOgO
öžÎÏ
÷ççïç
î÷ïÇÎ
óçÞŸþ
ç–çç
gîæÇÏ
fççÇÏ
gçæÆÏ
gïçï

Here is my exact code in case you see something I might have missed.
Code:

#include <16F84.H>
#include <stdlib.h>

#define A0 PIN_A0
#define A1 PIN_A1
#define B0 PIN_B0
#define B1 PIN_B1

/*-----------------------------------------------------*/
/* No Watch Dog Timer (NOWDT)
/* No Protect
/* Power Up Timer (PUT)
/* 10 MHz Clock
/* RS232 define for PIC-IMU communication (IMU)
/* RS232 define for PIC-PC communication (PC)
/*-----------------------------------------------------*/

#fuses HS,NOWDT,NOPROTECT, PUT
#use delay(clock=10000000)
#use rs232(baud=38400, xmit=B1, rcv=B0, stream=PC)
#use rs232(baud=38400, xmit=A1, rcv=A0, stream=IMU)

main() {

   while(1)
   {
      int sn1=0, sn2=0, sn3=0, sn4=0, sn5=0;
      
      delay_ms(2000);

      // transmit to IMU
      //fputs("Transmitting to IMU 1...", PC);

      //flush any spurious characters waiting
      //while (kbhit()) getc();

      fputc(241,IMU);

      sn1 = fgetc(IMU); // get 1st BYTE
      sn2 = fgetc(IMU); // get 2nd BYTE
      sn3 = fgetc(IMU); // get 3rd BYTE
      sn4 = fgetc(IMU); // get 4th BYTE
      sn5 = fgetc(IMU); // get 5th BYTE
      
      
      fputc(sn1, PC);
      fputc(sn2, PC);
      fputc(sn3, PC);
      fputc(sn4, PC);
      fputc(sn5, PC);
      
      /*------------------------------------------------------*/

   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 1:12 pm     Reply with quote

What's your version of the compiler ?
This will be a number like 2.734, or 3.191, or 3.225, etc.
You can see it at the top of the .LST file which is generated
when you compile a file successfully.

Also please register (and set your preferences to auto-login)
so that when you get a double post you delete one of them.
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 1:22 pm     Reply with quote

I have version 3.143
Ttelmah
Guest







PostPosted: Fri Jul 08, 2005 2:32 pm     Reply with quote

Post the characters in Hex, rather than as ASCII.
You still have not confirmed that the ground connection is made between the Max at the PIC, and the sensor. Though it probably is, it'd be nice to have this 'in writing'.
Now, the big questionable thing here, is the accuracy of software RS232. 3.143, is fairly old, and there have been problems with some versions in the past with the software RS232 timings. It might be this.

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 2:35 pm     Reply with quote

Replace:

Code:
     
      fputc(sn1, PC);
      fputc(sn2, PC);
      fputc(sn3, PC);
      fputc(sn4, PC);
      fputc(sn5, PC);


for this:

Code:
 
      fprintf(PC, "%X ", sn1);
      fprintf(PC, "%X ", sn2);
      fprintf(PC, "%X ", sn3);
      fprintf(PC, "%X ", sn4);
      fprintf(PC, "%X ", sn5);


And post the values that you get.

Humberto
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 2:40 pm     Reply with quote

The first thing I would do is to prove that the PIC can transmit
correctly to your PC. Disconnect the IMU from the PIC board,
and connect the PIC board to the PC. Then try a program like this:

Code:
int8 sn1,sn2,sn3,sn4,sn5;

main()
{

while(1)
   {
   sn1 = 'A';
   sn2 = 'B';
   sn3 = 'C';
   sn4 = 'D';
   sn5 = 'E';

// Transmit "ABCDE " to the PC.
   fputc(sn1, PC);   
   fputc(sn2, PC);
   fputc(sn3, PC);
   fputc(sn4, PC);
   fputc(sn5, PC);
   fputc(' ', PC);
 
   delay_ms(1000);
  }

}
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 3:26 pm     Reply with quote

Yes, I've done this already. The PIC can successfully communicate to the PC and the PC to the PIC. That is not the issue. When displaying the data in HEX format, I get the following (after 3 loop iterations):

GM*;O+KO/;w´=W<
YÛ^Û÷4ß–6ßv>û
ÓMh=K:[W6ntßtþ

Pretty crazy, huh? Is there a possibility that the baud rate of the sensor (38400) is too fast for the PIC with a 10MHz oscillator? The sensor also has a 19200 baud rate which I am going to try now...

Oh yeah, my grounds are connected.


Last edited by weg22 on Fri Jul 08, 2005 3:27 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 08, 2005 3:46 pm     Reply with quote

Try the lower baud rate, and if it doesn't work then try adjusting
the baud rate in the #use rs232 statements. Try 39000 and then
try 37000. Those values will cause the delay loop in the soft UART
to change by a count of 1 in either direction, from the value used
for 38400 baud.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Jul 08, 2005 4:30 pm     Reply with quote

Quote:
I recently purchased an IMU from Microstrain that tx and rx data serially. When connecting the sensor to the PC and pinging it (thru hyperterminal) for the device serial # (using the command BYTE 241 as defined by Microstrain), it sends back 5 BYTES which correspond to:


How are you sending out 241 with Hyperterminal?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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