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

Delays using getc() and kbhit() on PIC 16F84a

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







Delays using getc() and kbhit() on PIC 16F84a
PostPosted: Tue Feb 04, 2003 11:53 pm     Reply with quote

<font face="Courier New" size=-1>I'm having some problems with the getc() command delaying for too long. I have 1 MCU sending a byte to the other MCU a few times per second using RS232. When the second MCU receives that signal, it resets a counter. If that signal is not received, the counter increments and when it hits a specified limit, it will trigger a buzzer.

When the MCU's are connected, the buzzer does not sound which is good. The problem I'm having is that when I disconnect the serial link, it takes a long time for the buzzer to start sounding. I think I've narrowed the problem down to something having to do with the getc() function. I believe it is trying to check for a signal on the RS232 receive line but since it is disconnected, it waits for some time before realizing there is no signal. I need this time to be as short as possible. The way it is now, it take about 6 seconds before it realizes there is no signal.

Here is my code.....

#include <16F84A.H>
#fuses XT,NOWDT,NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B4, rcv=PIN_B5)

main() {

int mobile1; // Data received from mobile unit 1
int timermobile1=0; //Counter for max timer limit
SET_TRIS_B( 0x20 ); //RB0-RB4,RB6,RB7=outputs, RB5=inputs
OUTPUT_LOW(PIN_B3); //used to prevent inital beep

do {
mobile1=0; //Resets mobile1
mobile1=getc();
if(mobile1==1) //received signal from mobile untit 1
timermobile1=0;
else
++timermobile1;
DELAY_MS(50)
if(timermobile1>10) //no signals
OUTPUT_HIGH(PIN_B2);
else
OUTPUT_LOW(PIN_B2);

} while (TRUE);
}


I've been reading up on the kbhit() function and I think it may help in my problem but I can't seem to get it working properly either. I tried using it so that if there is a signal on the RS232 line, it will read it using the getc(). Otherwise it will skip the getc() and just increment the counter variable. But when I did this, it seemed to never seemed to process the getc() function. In other words, the buzzer would always sound even when the 2 MCU's were connected.

Any thoughts would be GREATLY appreciated on how I can get the delay on the getc() function lower. </font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 11286
Sherpa Doug
Guest







Re: Delays using getc() and kbhit() on PIC 16F84a
PostPosted: Wed Feb 05, 2003 8:06 am     Reply with quote

getc() will wait literally forever for a character to come in, or at least for a Start bit. I suspect your program is hanging on getc() until some noise on the open serial line causes a spurious Start bit. You need to use kbhit() or poll the serial line yourself. I use kbhit like this in my main polling loop:


if (kbhit()) //If a serial command is present
comms();
.
.
.

void comms() { //Parses commands:
char key;
long sum = 0;

switch (toupper(getch())) {
case 'F': // 'F' takes frequency argument (long)
key = getch();
while (isdigit(key)) {
sum = (sum * 10) + (key - '0');
key = getch();
}

Sherpa Doug

:=<font face="Courier New" size=-1>I'm having some problems with the getc() command delaying for too long. I have 1 MCU sending a byte to the other MCU a few times per second using RS232. When the second MCU receives that signal, it resets a counter. If that signal is not received, the counter increments and when it hits a specified limit, it will trigger a buzzer.
:=
:=When the MCU's are connected, the buzzer does not sound which is good. The problem I'm having is that when I disconnect the serial link, it takes a long time for the buzzer to start sounding. I think I've narrowed the problem down to something having to do with the getc() function. I believe it is trying to check for a signal on the RS232 receive line but since it is disconnected, it waits for some time before realizing there is no signal. I need this time to be as short as possible. The way it is now, it take about 6 seconds before it realizes there is no signal.
:=
:=Here is my code.....
:=
:=#include <16F84A.H>
:=#fuses XT,NOWDT,NOPROTECT
:=#use delay(clock=4000000)
:=#use rs232(baud=9600, xmit=PIN_B4, rcv=PIN_B5)
:=
:=main() {
:=
:= int mobile1; // Data received from mobile unit 1
:= int timermobile1=0; //Counter for max timer limit
:= SET_TRIS_B( 0x20 ); //RB0-RB4,RB6,RB7=outputs, RB5=inputs
:= OUTPUT_LOW(PIN_B3); //used to prevent inital beep
:=
:= do {
:= mobile1=0; //Resets mobile1
:= mobile1=getc();
:= if(mobile1==1) //received signal from mobile untit 1
:= timermobile1=0;
:= else
:= ++timermobile1;
:= DELAY_MS(50)
:= if(timermobile1>10) //no signals
:= OUTPUT_HIGH(PIN_B2);
:= else
:= OUTPUT_LOW(PIN_B2);
:=
:= } while (TRUE);
:=}
:=
:=
:=I've been reading up on the kbhit() function and I think it may help in my problem but I can't seem to get it working properly either. I tried using it so that if there is a signal on the RS232 line, it will read it using the getc(). Otherwise it will skip the getc() and just increment the counter variable. But when I did this, it seemed to never seemed to process the getc() function. In other words, the buzzer would always sound even when the 2 MCU's were connected.
:=
:=Any thoughts would be GREATLY appreciated on how I can get the delay on the getc() function lower. </font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 11298
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