|
|
View previous topic :: View next topic |
Author |
Message |
zhiling0229
Joined: 13 Sep 2006 Posts: 10
|
Non-ascii data stream via RS232 communication problem? |
Posted: Tue Oct 03, 2006 11:41 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 11:53 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 11:53 pm |
|
|
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
|
|
Posted: Tue Oct 03, 2006 11:55 pm |
|
|
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
|
|
Posted: Wed Oct 04, 2006 1:07 am |
|
|
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
|
|
Posted: Wed Oct 04, 2006 1:25 am |
|
|
ERRORS doesn't work with a software UART. The compiler ignores it.
You can remove it. |
|
|
zhiling0229
Joined: 13 Sep 2006 Posts: 10
|
|
Posted: Wed Oct 04, 2006 1:50 am |
|
|
Thanks PCM programmer and asmallri you both have been a great help. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Oct 04, 2006 6:26 am |
|
|
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
|
|
Posted: Wed Oct 04, 2006 11:29 am |
|
|
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
|
|
Posted: Wed Oct 04, 2006 1:33 pm |
|
|
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
|
|
Posted: Fri Oct 06, 2006 4:25 am |
|
|
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
|
|
Posted: Fri Oct 06, 2006 6:20 am |
|
|
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 |
|
|
|
|
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
|