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

Streamed RS232 RX problem

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



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

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

Streamed RS232 RX problem
PostPosted: Fri Feb 27, 2004 3:54 pm     Reply with quote

Confused
Hi,

Maybe you guys can help me, I have been trying to solve this problem for about 8 hours now.

Dev_Connect is called which calls InitComms. Dev_Connect the calls upon char timed_getc_PORT_KL to retreive data from the PORT_KL stream.

This is the problem. I can see the data on the logic analyzer on the PIN. The BlipMarker shows correctly at the start of the received byte. For some reason the received byte is always zero. This program worked fine until recently and I cannot see why it does not work now. I have checked the port speeds, I have changed the PIC pins from C0,C1 to B0,B1. I have removed blocks of code but to no avail.
The only thing that I can still do is to put on a grass skirt, lite a fire, dance, chant and howl at the moon, nothing else seems to work!!!

Here are the relevant bits of code:

#include <16F877A.h>
#device ICD=TRUE
#use delay(clock=20000000)
#fuses HS,NOPUT,NOBROWNOUT,NOWDT,NOPROTECT,NOLVP

#define K_TX_PIN PIN_C0
#define K_RX_PIN PIN_C1
#define Blip_PIN PIN_C2
#define L_TX_PIN PIN_A0

// PC Comm speed
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT_PC)

// default Dev speed
#use rs232(baud=9600,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8,stream=PORT_KL)


void BlipMarker() {
output_high(blip_PIN);
delay_us(10);
output_low(blip_PIN);
}

char timed_getc_PORT_KL(unsigned long KEYHIT_DELAY) {
unsigned long timeout = 0;
byte retval = 0;

while (TRUE) {
if (kbhit(PORT_KL)==TRUE) {
BlipMarker();
retval = fgetc(PORT_KL);

fprintf(PORT_PC,"ret %X\n\r",retval);

if (retval > 0)
break;
}

// timeout++;
// delay_us(1);

// if (timeout>KEYHIT_DELAY)
// break;
}

return(retval);
}

void InitComms() {
int i;

// hold the bus high for 2 seconds
output_high(K_TX_PIN);
output_high(L_TX_PIN);
delay_ms(2000);

// output a low startbit for 200ms
output_low(K_TX_PIN);
output_low(L_TX_PIN);
delay_ms(200);

output_float(K_TX_PIN);
output_float(L_TX_PIN);
}

void Dev_Connect() {
byte Sync;
byte W1 = 0;
byte W2 = 0;
byte WP1 = 0;
byte WP2 = 0;
int16KWP = 0;

InitComms();
delay_ms(350);

BlipMarker();

// get the incoming data
sync =timed_getc_PORT_KL(5000);
delay_ms(5);
W1 =timed_getc_PORT_KL(5000);
delay_ms(5);
W2 =timed_getc_PORT_KL(5000);
delay_ms(5);

fprintf(PORT_PC,"Sync %X W1 %X W2 %X\n\r",sync,W1,W2);
} Confused
Humberto



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

View user's profile Send private message

PostPosted: Sun Feb 29, 2004 9:23 am     Reply with quote

Hi hillcraft,

Nobody asked yet.
Reading your code it�s not clear to me - doesn�t show the main() function nor explain the functioning - why you expect to receive a char (single char or string???) in the Com PORT_KL (software UART handler).
Quote:

// PC Comm speed
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT_PC)

// default Dev speed
#use rs232(baud=9600,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8,stream=PORT_KL)


Just reversing your RS232 definitions you get the advantage of receiving the incoming char in the UART without wasting processor time in kbhit().
Don't forget to enable the corresponding interrupts.

Quote:

// PC Comm speed
#use rs232(baud=19200,xmit=PIN_C0,rcv=PIN_C1,stream=PORT_PC) // Software UART

// default Dev speed
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=PORT_KL) // Hardware UART


I made some changes in the following function -for debbuging purposes only - to show how you will get some info in the PC once the program abandon this function.

Code:


char timed_getc_PORT_KL(unsigned long KEYHIT_DELAY)
{
unsigned long timeout = 0;
byte retval = 0;


           if (kbhit(PORT_KL))
              {
               do
                 {
                  BlipMarker();
                  retval = fgetc(PORT_KL);
                  timeout++;
                  delay_us(1);
                 }while(( !retval) && ( timeout<KEYHIT_DELAY ));

               if(retval)                 // IF retval != 0
                 {
                  fprintf(PORT_PC,"retval %X\n\r",retval);
                 }
               if(timeout>KEYHIT_DELAY)
                 {
                  fprintf(PORT_PC,"kbhit detected, TIME_OUT error !\r\n");
                 }
              }

     return(retval);
}





Regards,

Humberto
hillcraft



Joined: 22 Sep 2003
Posts: 101
Location: Cape Town (South africa)

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

KL problem
PostPosted: Sun Feb 29, 2004 11:23 am     Reply with quote

I have found the reason for the problem by looking at the serial signal received on the K-Line.

This all occurs at 9600baud.

if you have a byte 0X55 then the data should go lo for 104us, high for 104us, lo for 104us and so on. I see the data go lo for 178us, high for 30us, lo for 178 us, high for 30us and so forth. It seems as if the data that I am receiving brings the line lo at the correct instant i.e. 104us intervals but takes th line high about 74us late.

If the software uart is rigged so as to take the sample in the middle of the bit i.e. 52us after the line goes lo and 104us thereafter, it would miss the high bits entirely.

I will do some R&D tonight so as to prove the point. It seems that I will have to take a number of samples within the 104us range so as to determine if the line has gone high

The question is: Why did the code work in the first place ? I don't really know!
Humberto



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

View user's profile Send private message

PostPosted: Sun Feb 29, 2004 1:55 pm     Reply with quote

hillcraft wrote:
Quote:

I see the data go lo for 178us, high for 30us, lo for 178 us, high for 30us and so forth. It seems as if the data that I am receiving brings the line lo at the correct instant i.e. 104us intervals but takes th line high about 74us late.


You didn�t mention about this kind of signal in the previous post, if you define
Quote:

// default Dev speed
#use rs232(baud=9600,parity=N,xmit=PIN_C0,rcv=PIN_C1,bits=8,stream=PORT_KL)

you will admit that you are dealing with ASCII characters at 9600 bauds, @104usec/bit.

Humberto
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