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

Serial communications problem

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



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

Serial communications problem
PostPosted: Mon May 29, 2006 12:22 am     Reply with quote

Using compiler ver 3.203
Using 'siow.exe'

Just playing with serial communications.

If I type:
Quote:
foobar

I get:
Quote:
Power On (sertest.c)\0A
` r (�` r (� (� \0D



If I disable INT_RDA and instead

Code:

while (TRUE) {
    while(!KBHIT()){
    }
    a=getc();
    printf(a);
}


in 'main()' everything I type is echoed correctly.
All my printf's work fine. Ideas?


Code:
#include <16F876a.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#define DIAG_LED_ON   output_low(PIN_A2)
#define DIAG_LED_OFF   output_high(PIN_A2)
#define delay200     delay_ms(200)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void blink (int times, int dl) {
   int i;
   for (i=1;i<=times;++i) {
      delay_ms(dl);
      DIAG_LED_ON;
      delay_ms(dl);
      DIAG_LED_OFF;
   }
}

#INT_RDA
   void SerIn(){
      disable_interrupts(INT_RDA);
      while (kbhit()){
         printf(getc());
      }
      enable_interrupts(INT_RDA);
   }


void main() {
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   printf("Power On (sertest.c)\n\r");

   while (TRUE){
      blink(1,10);
   }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 29, 2006 1:21 am     Reply with quote

Don't check kbhit in the isr. Don't disable and enable INT_RDA inside
the isr. Just get one character and echo it back. Don't use printf()
with a character variable as the only argument. It doesn't work.
Use putc() instead. Or, if you want to use printf(), then use the "%c"
format string to tell printf that you're giving it a character.

Here is your program re-written with the changes above for int_rda.
Code:

#include <16F876A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#int_rda
void rda_isr(void)
{

putc(getc());

}   

//========================
void main()
{

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(1);
}
kd5uzz



Joined: 28 May 2006
Posts: 56

View user's profile Send private message

PostPosted: Mon May 29, 2006 1:41 am     Reply with quote

Thanks! I think I understand my mistakes now.
DClark



Joined: 19 May 2008
Posts: 1

View user's profile Send private message

PostPosted: Wed Jul 09, 2008 7:44 pm     Reply with quote

Would I be able to receive a hex stream (say from a GPS receiver) if I used your code as follows:

Code:
/*********************************************************/
#include <16F876A.H>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#int_rda
void rda_isr(void)
{

printf("%u ",getc());

}   

//========================
void main()
{

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(1);
}
Ttelmah
Guest







PostPosted: Sat Jul 12, 2008 2:38 am     Reply with quote

The problem here, is what is sent, versus what arrives.
If (for instance), you receive the character 'A'. This has a value in ASCII, of '65' (decimal). %u, will then print these _two_ characters back. Now, this will work fine, if you just send one character, but if you have a stream of characters:

ABC etc.

Then the transmission becomes:

656667

Twice as many characters (and gets even worse if a character with a value over 99 arrives). The problem then is that the transmission cannot keep up, and receive data will start to be lost.
Generally, simple rule is to do as little as possible in the ISR. Receive your character, and store it into a buffer.If you are retransmitting, and this is going to take longer than the reception, you have to add 'flow control', and tell the transmitter to 'stop', when the buffer approaches being full.
As a 'demo', sending one character in the ISR, is OK. Sending more, is likely to cause problems...

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