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

RS-232 Communication with PIC and Prog C++

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







RS-232 Communication with PIC and Prog C++
PostPosted: Tue Apr 04, 2006 8:44 am     Reply with quote

I have this code for my RS-232 receiver. My transmitter program sends
( A,10| ) with no braket but when I use kbhit I receive only ( A, ). If I remove ( if kbhit()==1 ) I receive ( A,10| ). Can anyone help finding out why there is only ( A, ) in the buffer when I use kbhit?

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

if(kbhit()==1)
{
for(i=0;Lecture[i-1]!='|';i++)
{
Lecture = getc();
}
Lecture[i]='\0';
lcd_gotoxy(21,1);
printf(LCD_PUTC,"%s",Lecture);
}


Thanks.
Ttelmah
Guest







PostPosted: Tue Apr 04, 2006 9:14 am     Reply with quote

The answer is probably luck!....
Your test is dangerous. What does 'Lecture[i-i]' contain when i=0?...
It will actually address another piece of memory outside the array, which may contain something legitimate or not. On a PC, this code would fail, with a memory access error, but the PIC, contains no memory manager, and will perform the operation.
Adding the kbhit test, is probably just resulting in this illegal access returning a different value, and failing.
Change your addressing, so that either you start at address '1', and put your own defined value in addres '0', _or_ move the test till after the character has been fetched (do..while).

Best Wishes
Volkanos
Guest







PostPosted: Tue Apr 04, 2006 11:56 am     Reply with quote

I tried this code and it read 2 characters, and it waited for a another characters. The buffer is empty after 2 characters. How much characters can the buffer contain.

int i = 0;

while(kbhit()==1)
{
Lecture[i]=getc();
printf(LCD_PUTC,"%i",i);
printf(LCD_PUTC,"%c",Lecture[i]);
i++;
}

Thanks.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Apr 04, 2006 3:19 pm     Reply with quote

The UART can buffer only two characters. Just a wild guess but, since I can't see the rest of your code, are you getting to the end of the code and the processor is htting the hidden sleep instruction and going to sleep?

maybe try something like:

Code:

 while (1)
    {
      if kbhit()
                {
                Lecture[i]=getc();
                printf(LCD_PUTC,"%i",i);
                printf(LCD_PUTC,"%c",Lecture[i]);
                i++;
               }
}
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