View previous topic :: View next topic |
Author |
Message |
curtis
Joined: 23 Jul 2004 Posts: 3 Location: Brazil
|
Can I use parameters in kbhit() function? |
Posted: Mon Sep 13, 2004 1:38 pm |
|
|
Hi all,
I have two STREAMS in my project with 18F252, one is by hardware and the other is by software implemented with fgetc() and fputc().
#use rs232 (..., stream = output232) // hardware
#use rs485 (..., stream = output485) // software
In my code I'm using # int_rda.
kbhit(output 232) ; Can I use it? I mean, Can I use a parameter in this function?
This worked well in my code with baudrate 19200.
But It couldn't work well when I changed the baud rate.
thanks
Vitor |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Sep 13, 2004 1:51 pm |
|
|
Yes it works. That is how you tell kbhit() what stream to look at. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Mon Sep 13, 2004 1:53 pm |
|
|
other than the stream parameter what other parameter do you want to use. What do you mean by "it didn't work when I changed the baud rate". Was it changed in the USE statement or on the fly? |
|
|
Guest
|
Can I use parameters in kbhit() function? |
Posted: Mon Sep 13, 2004 4:54 pm |
|
|
I used the parameter in my code even the CCS manual don't mention anything about that.
I tested it with 19200 bauderate in the USE statement and it worked well, but when I changed the baudrate in the USE statement it didn't worked. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Mon Sep 13, 2004 5:00 pm |
|
|
What version of the manual are you using? It is documented in the July 2003 version in multiple places (USE RS232, Getc(),Putc(),Fputc() etc).
Post the code showing the initial USE RS232 setup and where you are changing the baudrate so we can see what you are doing. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Sep 13, 2004 9:34 pm |
|
|
You should post a test of the problem. Are you using the kbhit() inside the interrupt? |
|
|
curtis
Joined: 23 Jul 2004 Posts: 3 Location: Brazil
|
|
Posted: Tue Sep 14, 2004 6:42 am |
|
|
My manual is version July 2003, but in the page 108 the function KBHIT() is written "none" for parameters.
Follows my code:
#int_RDA
void read_RS232(void)
{
int cont232 = 0;
long timeout = 0;
restart_wdt();
disable_interrupts(INT_RDA);
disable_interrupts(INT_ext);
// Get 8 bytes
while ( cont232 < 8 )
{
// Wait maximum +/- 5000 us
while ( (!kbhit(OUTPUT232)) && (++timeout<500)){
delay_us(10);
restart_wdt();
}
// Get byte if available
if (kbhit(OUTPUT232)) {
RX232[cont232++] = fgetc(OUTPUT232);
}
// Case no, timeout
else
{
size232 = 0;
received232 = FALSE;
enable_interrupts(INT_EXT);
enable_interrupts(INT_RDA);
enable_interrupts(global);
restart_wdt();
return;
}
}
// If no TIMEOUT
size232 = cont232;
RX232[cont232] = '\0';
enable_interrupts(INT_ext);
enable_interrupts(global);
received232 = TRUE;
restart_wdt();
}
Usart Configuration:
void adjustBaud(void) {
long value;
value = read_eeprom(92);
#USE RS232 (BAUD=19200, XMIT=TX2, RCV=RX2, parity=n, bits=8, stream=OUTPUT485)
#USE RS232 (BAUD=19200, XMIT=PIN_C6, RCV=PIN_C7, parity=n, bits=8, stream=OUTPUT232)
switch(value) {
case 1:
set_uart_speed(9600, OUTPUT232);
break;
case 2:
set_uart_speed(4800, OUTPUT232);
break;
}
} |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Tue Sep 14, 2004 5:36 pm |
|
|
curtis wrote: | My manual is version July 2003, but in the page 108 the function KBHIT() is written "none" for parameters.
|
Always read the ReadMe.TXT file that installs in the compiler directory. It contains the latest updates to the manual.
By the way, you don't need to disable/enable interrupts in the ISR. It is done automatically.
Always use the 'Code' buttons when posting your code. It makes it more readable.
Looking at your while loop:
Quote: | while ( cont232 < 8 )
{
// Wait maximum +/- 5000 us
while ( (!kbhit(OUTPUT232)) && (++timeout<500)){
delay_us(10);
restart_wdt();
}
// Get byte if available
if (kbhit(OUTPUT232)) {
RX232[cont232++] = fgetc(OUTPUT232);
}
// Case no, timeout
else
{
size232 = 0;
received232 = FALSE;
enable_interrupts(INT_EXT);
enable_interrupts(INT_RDA);
enable_interrupts(global);
restart_wdt();
return;
}
}
|
You are not resetting the timeout variable for each iteration. It will eventually reach the maximum limit and cause the code to jump out of the loop prematurely. |
|
|
|