|
|
View previous topic :: View next topic |
Author |
Message |
Bob Sorenson Guest
|
16F873 problem with getch and kbhit. |
Posted: Tue Apr 01, 2003 6:32 pm |
|
|
I have tried many variations of the source code below to no
avail; all have not worked as expected. The symptoms of this
particular code are as follows:
On power on, the program displays the expected string
"Program started..." followed by a newline.
On the first keystroke, nothing happens. On subsequent key
strokes I get a variety of responses, none of which is the
expected one, ranging from nothing at all to the string
"Character received: :" <== Note the second colon. If I hold
down any key I get sporadic strings like the above but they
are sometimes cut short with garbage and/or erroneous chars.
None of the characters are the ones pressed, and holding the
'x' or 'X' key does not cause the program to exit. Also, if I
reset the PIC by grounding and releasing pin one, an infinite
loop is entered sending the string "Character received: :"
(again note the second colon), and the only way to stop it is
to turn the power off. Turning it back on starts the process
all over again. The "Program ended..." string never appears.
I am using TeraTerm Pro with an RS-232 to RS-485 translator to
drive the on-chip USART, and other developers here have used
the same hardware interface successfully, but they are baffled
by the problem I am having.
I am by no means a novice C programmer, but I am new to PICs,
so I suspect that I am missing a configuration bit or some
other less-than-obvious thing. I will be grateful for any help
with this as it is hanging up a much larger program for the
chip and will also help other developers on the project.
Thank you,
Bob Sorenson
______________________________________________________________
#include <16F873.H>
#fuses XT, NOWDT, NOPROTECT, NOBROWNOUT
#use delay(clock = 14745600)
#use rs232(baud = 115200, xmit = PIN_C6, rcv = PIN_C7, enable = PIN_C2, PARITY = N, BITS = 8, ERRORS)
char key = 'A';
#INT_RDA
void serial_isr()
{
key = getc();
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
printf("Program started...\r\n");
while (TRUE) {
if (TRUE == kbhit()) {
printf("Character received: \%c\r\n", key);
if (('x' == key) || ('X' == key)) break;
}
}
printf("Program ended...\r\n");
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13297 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: 16F873 problem with getch and kbhit. |
Posted: Tue Apr 01, 2003 9:37 pm |
|
|
:=I have tried many variations of the source code below to no
:=avail; all have not worked as expected. The symptoms of this
:=particular code are as follows:
:=
:=On power on, the program displays the expected string
:="Program started..." followed by a newline.
--------------------------------------------------
The first thing I would do, is fix all obvious things,
and then also, simplify the hardware and the software.
1. Put a "while(1);" statement right before the closing
brace of main(), so the program doesn't hit the hidden
sleep instruction that CCS puts there.
2. Add NOLVP to your #fuses statement (unless you're using
low voltage programming).
----
A. Initially, I would get rid of the RS-485 interface and
just use plain RS-232. Use a MAX232A or equivalent.
All you need is Rx and Tx (and GND).
B. Change the crystal to a more standard value. You could
use 4 MHz, and then use the XT fuse.
C. Change the baud rate to 9600.
D. Maybe use a different terminal program besides TeraTerm,
unless you know for sure that it works. (ie., you've
tested it between two PCs, at 9600 baud).
---
If you get it working after you do all of the above, then
add your new features one at a time.
Also, if you're not using the latest version of the compiler
(or at least, a late version that works ), then post what
you are using.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13302 |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
Re: 16F873 problem with getch and kbhit. |
Posted: Tue Apr 01, 2003 10:38 pm |
|
|
:=I have tried many variations of the source code below to no
:=avail; all have not worked as expected. The symptoms of this
:=particular code are as follows:
:=
:=On power on, the program displays the expected string
:="Program started..." followed by a newline.
:=On the first keystroke, nothing happens. On subsequent key
:=strokes I get a variety of responses, none of which is the
:=expected one, ranging from nothing at all to the string
:="Character received: :" <== Note the second colon. If I hold
:=down any key I get sporadic strings like the above but they
:=are sometimes cut short with garbage and/or erroneous chars.
:=None of the characters are the ones pressed, and holding the
:='x' or 'X' key does not cause the program to exit. Also, if I
:=reset the PIC by grounding and releasing pin one, an infinite
:=loop is entered sending the string "Character received: :"
:=(again note the second colon), and the only way to stop it is
:=to turn the power off. Turning it back on starts the process
:=all over again. The "Program ended..." string never appears.
:=
:=I am using TeraTerm Pro with an RS-232 to RS-485 translator to
:=drive the on-chip USART, and other developers here have used
:=the same hardware interface successfully, but they are baffled
:=by the problem I am having.
:=
:=I am by no means a novice C programmer, but I am new to PICs,
:=so I suspect that I am missing a configuration bit or some
:=other less-than-obvious thing. I will be grateful for any help
:=with this as it is hanging up a much larger program for the
:=chip and will also help other developers on the project.
:=
:=Thank you,
:=Bob Sorenson
:=______________________________________________________________
Bob:
Suscribing all the recommendations suggested by PCM programer, I also would check with a scope the serial data waveform in PIN_C7, if the idle state is HIGH (stable, no glitches) and if the first transition (start bit) is a falling edge.
Then try with this. (Itīs what I understand you want to do:
_______________________________________________________________
#include <16F873.H>
//#fuses XT, NOWDT, NOPROTECT, NOBROWNOUT
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 14745600) // (????)
#use rs232(baud = 115200, xmit = PIN_C6, rcv = PIN_C7, enable = PIN_C2, ERRORS)
char key_pressed;
char char_rcved, running; // FLAGS
#INT_RDA
void serial_isr()
{
if(kbhit())
{
key_pressed = getc();
char_rcved = TRUE;
}
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
printf("Program started...\r\n");
running = TRUE;
while(running)
{
if(char_rcved)
{
printf("Character received: \%c\r\n", key_pressed);
char_rcved = FALSE;
delay_ms(10);
}
if ((key_pressed == 'x') || (key_pressed == 'X'))
{
running = FALSE;
}
}
printf("Program ended...\r\n"); // Be aware that after
// this the PIC enter in sleep MODE !!
}
_______________________________________________________________
Best wishes
Humberto
___________________________
This message was ported from CCS's old forum
Original Post ID: 13303 _________________ Humber |
|
|
Bruce R. Knox Guest
|
Re: 16F873 problem with getch and kbhit. |
Posted: Wed Apr 02, 2003 8:26 am |
|
|
:=I have tried many variations of the source code below to no
:=avail; all have not worked as expected. The symptoms of this
:=particular code are as follows:
:=
:=On power on, the program displays the expected string
:="Program started..." followed by a newline.
:=On the first keystroke, nothing happens. On subsequent key
:=strokes I get a variety of responses, none of which is the
:=expected one, ranging from nothing at all to the string
:="Character received: :" <== Note the second colon. If I hold
:=down any key I get sporadic strings like the above but they
:=are sometimes cut short with garbage and/or erroneous chars.
:=None of the characters are the ones pressed, and holding the
:='x' or 'X' key does not cause the program to exit. Also, if I
:=reset the PIC by grounding and releasing pin one, an infinite
:=loop is entered sending the string "Character received: :"
:=(again note the second colon), and the only way to stop it is
:=to turn the power off. Turning it back on starts the process
:=all over again. The "Program ended..." string never appears.
:=
:=I am using TeraTerm Pro with an RS-232 to RS-485 translator to
:=drive the on-chip USART, and other developers here have used
:=the same hardware interface successfully, but they are baffled
:=by the problem I am having.
:=
:=I am by no means a novice C programmer, but I am new to PICs,
:=so I suspect that I am missing a configuration bit or some
:=other less-than-obvious thing. I will be grateful for any help
:=with this as it is hanging up a much larger program for the
:=chip and will also help other developers on the project.
:=
:=Thank you,
:=Bob Sorenson
:=______________________________________________________________
:=
:=#include <16F873.H>
:=
:=#fuses XT, NOWDT, NOPROTECT, NOBROWNOUT
:=
:=#use delay(clock = 14745600)
:=#use rs232(baud = 115200, xmit = PIN_C6, rcv = PIN_C7, enable = PIN_C2, PARITY = N, BITS = 8, ERRORS)
:=
:=char key = 'A';
:=
:=#INT_RDA
:=void serial_isr()
:={
:= key = getc();
:=}
:=
:=void main()
:={
:= enable_interrupts(GLOBAL);
:= enable_interrupts(INT_RDA);
:=
:= printf("Program started...\r\n");
:= while (TRUE) {
:= if (TRUE == kbhit()) {
:= printf("Character received: \%c\r\n", key);
:= if (('x' == key) || ('X' == key)) break;
:= }
:= }
:= printf("Program ended...\r\n");
:=}
Bob:
Try this:
:= printf("Program started...\r\n");
:= while (TRUE) {
:= if (key) {
:= printf("Character received: \%c\r\n", key);
:= if (('x' == key) || ('X' == key)) break;
:= key = 0;
:= }
:= }
:= printf("Program ended...\r\n");
:= while(1); // per PCM's advice
:=}
I think the problem is interaction between kbhit() annd getc(). This should solve that problem. The program will ignore a received character of 0x00...... also, set "key" to zero at the beginning of the program.
Good luck...
Bruce
___________________________
This message was ported from CCS's old forum
Original Post ID: 13308 |
|
|
|
|
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
|