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

kbhit()

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







kbhit()
PostPosted: Wed Sep 08, 2004 7:10 am     Reply with quote

hi all,
i use two rs232 ports (16F877A)
one is hardware and the other is software.
i want to test kbhit() for incoming serial data.
The problem is how can i understand hardware or software data received by using kbhit().

Can i use kbhit like this:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=pin_b0, stream=host)////harware
#use rs232(baud=1000, xmit=PIN_d0, rcv=PIN_d1,stream=loop)///software

....
....

kbhit(loop)///software serial data test?
kbhit(host)///hardware serial data test?
i tryed these but my code din't work.
Please help...
Ttelmah
Guest







Re: kbhit()
PostPosted: Wed Sep 08, 2004 8:04 am     Reply with quote

arif wrote:
hi all,
i use two rs232 ports (16F877A)
one is hardware and the other is software.
i want to test kbhit() for incoming serial data.
The problem is how can i understand hardware or software data received by using kbhit().

Can i use kbhit like this:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=pin_b0, stream=host)////harware
#use rs232(baud=1000, xmit=PIN_d0, rcv=PIN_d1,stream=loop)///software

....
....

kbhit(loop)///software serial data test?
kbhit(host)///hardware serial data test?
i tryed these but my code din't work.
Please help...

It won't.
The software port, has no ability to receive a character, unless the code is actually sitting 'waiting' for it. If you want to have the code actually detect a character when it arrives, it may be possible, by using one of the external interrupt pins, and triggering the receive code, wen the edge is seen on the srial stream.
The kbhit function, is dependant on there being a hardware register to actually receive and 'hold' a character for you.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 8:17 am     Reply with quote

Quote:

KBHIT()
Syntax: value = kbhit()
Parameters: None
Returns: 0 (or FALSE) if getc() will need to wait for a character to
come in, 1 (or TRUE) if a character is ready for getc()
Function: If the RS232 is under software control this function
returns TRUE if the start bit of a character is being sent
on the RS232 RCV pin. If the RS232 is hardware this
function returns TRUE is a character has been received
and is waiting in the hardware buffer for getc() to read.
This function may be used to poll for data without
stopping and waiting for the data to appear. Note that in
the case of software RS232 this function should be
called at least 10 times the bit rate to ensure incoming
data is not lost.
Availability: All devices.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

Re: kbhit()
PostPosted: Wed Sep 08, 2004 8:23 am     Reply with quote

arif wrote:

Can i use kbhit like this:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=pin_b0, stream=host)////harware
#use rs232(baud=1000, xmit=PIN_d0, rcv=PIN_d1,stream=loop)///software

....
....

kbhit(loop)///software serial data test?
kbhit(host)///hardware serial data test?
i tryed these but my code din't work.
Please help...


This should work fine as long as you are calling the kbhit() for the software stream at least 5 or 10 times per serial bit rate. Kbhit() will detect the beginning of the Start bit and then you can call getc() to get the character. Getc() won't return untill the beginning of the Stop bit.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 8:25 am     Reply with quote

The problem is that he wants to use it for both (I believe). There is no way to tell the compiler which port to use. I believe that it will use the port of the last encountered #use rs232
arif
Guest







PostPosted: Wed Sep 08, 2004 8:44 am     Reply with quote

Thanks everybody...
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Sep 08, 2004 9:11 am     Reply with quote

kbhit(loop) checks the loop stream.
kbhit(host) checks the host stream.

Streams are not real well documented but if you look on this bbs the info it there.
_________________
The search for better is endless. Instead simply find very good and get the job done.
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Wed Sep 08, 2004 1:26 pm     Reply with quote

The kbhit(STREAM) function tests the RCV pin asigned to that stream. If it detects a low, it returns TRUE, even if that low was not the first bit of a character. So, your program needs to call kbhit() often enough to catch the start bit. Once you call getch(STREAM), it will "bit bang" the character in until complete. If your code "missed" the start bit, you will get garbage and or hang in the getch() function.
_________________
David
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Sep 08, 2004 1:45 pm     Reply with quote

The only way getch() should hang is if the low condition detected by kbhit() is gone by the time getch() is called. Even if kbhit() catches some low data bit, getch() should come back with some byte and perhapse a framing error if it didn't find a stop bit where it expects one. Getch() only hangs if it never sees a low to use as a Start bit.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Arif
Guest







PostPosted: Thu Sep 09, 2004 12:31 am     Reply with quote

Thanks everbody,
Here is my test program. İt loops 3 times only (i understand by leds) and then pic locks (i think so).
What is wrong with my program..?
Code:

#include <16f877A.h>
#fuses XT,NOPROTECT,PUT,NOLVP,nowdt,BROWNOUT
#use delay(clock=4000000)


#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, enable=pin_b0, stream=host)
#use rs232(baud=1000, xmit=PIN_d0, rcv=PIN_d1,stream=loop)

//#include <a2465.C>

#byte   PORTA               = 0x05
#byte   PORTB               = 0x06
#byte   PORTC               = 0x07
#byte   PORTD               = 0x08
#byte   PORTE               = 0x09


#define  host_led    pin_b7
#define  loop_led    pin_c3



////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

char ch1=0;
char data;
int8 adres=0;



///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void init()
{
    output_high(host_led);
    output_high(loop_led);
    delay_ms(1000);
    output_low(host_led);
    output_low(loop_led);
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void DelayByte()
{
   delay_ms(10);
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int8 SensorTara(int8 adr)
{
   static int ret,adresler;
   int retry=0;
   char ch;
   adresler=adr+0x40;
   output_high(loop_led);
   fputc(adresler,loop);
   retry=0;

   ret=255;
   delay_ms(10);
   while ((!kbhit(loop)) && (retry < 200))
   {
      restart_wdt();
      delay_us(300);
      retry++;
   }
   if (Retry < 200 ) ret=fgetc(loop);
   else fputc('?',loop);
   output_LOW(loop_led);
   DelayByte();
   DelayByte();
   DelayByte();
   if (kbhit()) ch=fgetc(loop);
   if ((ret >='0') && (ret <='9')) return ret;
   if (ret ==':') return ret;
   ret=255;
   return ret;
   }

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


void main()
{

   init();
   delay_ms(100);


   while (1)
   {
      adres='X';
      if(!input(pin_a0)) adres='A';
      if(!input(pin_a1)) adres='B';
      if(!input(pin_a2)) adres='C';
      if(!input(pin_a3)) adres='D';

      if(adres=='X')
      {
         disable_interrupts(int_rda);
         disable_interrupts(global);

         ch1=sensortara(1);                        ///will answer by sending '0'
         if((ch1>='0') && (ch1 <='9')) output_high(loop_led);
         delay_ms(10);
         output_low(loop_led);
         delay_ms(40);
         
      }
      else
      {
         enable_interrupts(int_rda);
         enable_interrupts(global);
      }
      delay_ms(700);

   }
}


#int_rda
receive()
{
   data=fgetc(host);                       ///receives every message from host

   if(data==adres)                         ///test address
   {
      output_high(host_led);
      DELAY_MS(5);

      fputc(1,host);                          ///send answer to host
      fgetc(host);                            ///hardware buffer empty

      output_low(host_led);
   }
}

Ttelmah
Guest







PostPosted: Thu Sep 09, 2004 2:06 am     Reply with quote

I'd suspect you are overflowing the UART input buffer.
When a character is being received on the hardware UART, you are going to the interrupt routine, which then waits for 5mSsec, sends a character, and waits for another character to come back. If there is not a 'reply' character, the code will hang waiting here, or if there is incoming data during the 'wait' (remember a character takes only just under 1mSec at 9600bps), the UART will have overflowed, and the code will again hang.
General rule, avoid delays, waiting for characters, or sending characters inside the interrupt handler. Look at the ex_sisr.c example, on how to buffer incoming data. Then write your main loop, to just monitor the buffer, flash LEDs, and send replies as required.
Also as a general comment, save time, and don't fiddle with the interrupt settings on every loop. If you have enabled the interrupts you require, just toggle the 'global' setting, to turn them all on/off. hence you only need the one instruction to enable/disable everything. Again though, if you are using an interrupt driven system, keep the times when interrupts are disabled, _short_. As it currently stands, the interrupts are turned off for over 40mSec in the main loop, is 'X' is seen, and again the UART will have overflowed, if characters have arrived in this time...

Best Wishes
Arif
Guest







PostPosted: Thu Sep 09, 2004 2:28 am     Reply with quote

Thanks a lot
i will try it.
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