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

Non-ascii data stream via RS232 communication problem?

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



Joined: 13 Sep 2006
Posts: 10

View user's profile Send private message

Non-ascii data stream via RS232 communication problem?
PostPosted: Tue Oct 03, 2006 11:41 pm     Reply with quote

Hi,

I'm new in serial communication. I wanted to write a serial test code before I intend to write my program. Here is the test code that I'm writting and I need some pointers. Thanks.
Code:

#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A3, rcv=PIN_A2)  // use pin A2 as receiver and pin A3 as transmitter

void main()
{
  //Do I need to disable the ADC to enable serial transmission?
 
   printf("What do I put here?");
   
// Is there anything else I need just to generate 1 line of data stream?

}

I need to generate a stream of data "0D180174002001" which is all in hexadecimal. If I'm not mistake the command printf will generate the ASCII code to be transmitted to the RS232 port. How can I generate the data stream above. Thanks


Last edited by zhiling0229 on Fri Oct 06, 2006 4:26 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 03, 2006 11:53 pm     Reply with quote

The putc() or fputc() functions will send a single byte of raw data.

Example:
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void main()
{
int8 i;
int8 data[7]  = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};

for(i = 0; i < sizeof(data); i++)
     putc(data[i]); 

while(1);   
}
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Oct 03, 2006 11:53 pm     Reply with quote

Code:

char mystring[] = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};
//.....

for (i = 0; i<7, i++)
   putc(mystring[i]);


_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!


Last edited by asmallri on Wed Oct 04, 2006 1:48 am; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Oct 03, 2006 11:55 pm     Reply with quote

One day, possibly sometime in the distant future, I am going to beat PCM Programmer to the posting by a few seconds instead of always the other way around....
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
zhiling0229



Joined: 13 Sep 2006
Posts: 10

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 1:07 am     Reply with quote

Thanks guys,

Was wondering. PCM Programmer use Port C Pin 6 and 7. If I want to use Port A pin 2 and pin 3 is this the soruce code?

Code:
#include <16F877>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A2, rcv=PIN_A3, ERRORS)

void main()
{
int8 i;
int8 data[7]  = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};

for(i = 0; i < sizeof(data); i++)
     putc(data[i]); 

while(1);   
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 1:25 am     Reply with quote

ERRORS doesn't work with a software UART. The compiler ignores it.
You can remove it.
zhiling0229



Joined: 13 Sep 2006
Posts: 10

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 1:50 am     Reply with quote

Thanks PCM programmer and asmallri you both have been a great help.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 6:26 am     Reply with quote

One thing to remember, if you are going to use a Software UART and intend on receiving data through the port you will need to poll your receive pin to determine if you have a character coming in. The hardware interrupt only works if you use the hardware pin associated with it.

Ronald
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Wed Oct 04, 2006 11:29 am     Reply with quote

Quote:
ERRORS doesn't work with a software UART. The compiler ignores it.
You can remove it.

PCM Programmer may be right but in the CCS example of using an RS232 multi-drop bus
You'll see the following
Code:

#bit ninth_bit = RS232_ERRORS.7
#bit collision = RS232_ERRORS.6

Now errors leads to the posting of the variable RS232_ERRORS and the multi-drop BUS is on PIN B0 so it isn't a hardware UART port. You might conclude that ERRORS has some functionality w.r.t the software UART
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 04, 2006 1:33 pm     Reply with quote

What I meant was, only the hardware UART has the capability to get
an overrun error and lock up. If you use ERRORS with a hardware
UART, the compiler will insert code to detect and automatically clear
an overrun error, if one occurs. Since a soft UART can't lock-up,
the compiler ignores the ERRORS directive and won't generate the
special code that it does for the hardware UART.

I wasn't referring to the other aspects of the RS232_ERRORS variable
with respect to a soft UART.
zhiling0229



Joined: 13 Sep 2006
Posts: 10

View user's profile Send private message

PostPosted: Fri Oct 06, 2006 4:25 am     Reply with quote

Hi guys,

I just tested the programme. I used a advanced serial data logger to acquire the data. The problem is while sending the data I monitor the data logger. After the 2 second delay. the data logger only able to acquire part of the transmission:

which is : 0x01, 0x74, 0x00, 0x20, 0x01 (the header 0x0D,0x18 did not register)

Any ideas guys?

The source code I used is as below:

Code:
#include <16F877A.h>
#use delay(clock=4000000)
#fuses XT, NOBROWNOUT, NOLVP, NOWDT
#use rs232(baud=9600, xmit=PIN_A2, rcv=PIN_A3)


struct lcd_pin_def
{
   BOOLEAN B0;    // B0
   BOOLEAN B1;    // B1
   BOOLEAN B2;  // B2
   BOOLEAN B3;  // B3
   BOOLEAN B4;  // B4
   BOOLEAN B5;  // B5
   BOOLEAN B6;  // B6
   BOOLEAN B7;  // B7

};

struct lcd_pin_def  LCD;

#byte LCD = 0x06   // portB address on 16F877A


#use fast_io(D)

void main() {

   int8 i;
   int8 data[7] = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};
   set_tris_b(0x00);  // graphic lcd control lines all output


   LCD.B0 = 1;   
   delay_ms(2000);
   LCD.B0 = 0;

   for(i = 0; i < sizeof(data); i++)
   putc(data[i]);
   
   while(1);


}
Ttelmah
Guest







PostPosted: Fri Oct 06, 2006 6:20 am     Reply with quote

Try the following (note how much nicer it looks with the 'code' button...).
Code:

void main() {
  int8 i;
  int8 data[7] = {0x0D, 0x18, 0x01, 0x74, 0x00, 0x20, 0x01};
  set_tris_b(0x00); // graphic lcd control lines all output

  LCD.B0 = 1;
  output_high(PIN_A2);
  delay_ms(2000);
  LCD.B0 = 0;

  for(i = 0; i < sizeof(data); i++)
    putc(data[i]);

  while(1);
}

Until you carry out some I/O on the serial pins, they are 'idle', which on a booting PIC, means set as an input. The receiver can then miss the first falling edge of the first character. I would have expected a 'garbage' character in front of the stuff that is received.

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