View previous topic :: View next topic |
Author |
Message |
misperry
Joined: 03 Mar 2009 Posts: 40
|
Huge Problem with getch() Need Help |
Posted: Tue Mar 03, 2009 12:44 pm |
|
|
Hey there I have a project that I am working on that I have used a pic16f886 controller to do some transfer of information via rs-232 between two of these chips. However, just randomly it stopped working and I have not touched it. I have simulated the code and I have realized that it is no longer infinitely waiting for the getch() to resolve.
Normally if I simulate the code when the getch() function is called it will
just sit at the getch() function till it receives a character.
For some odd reason now, with no other changes, it checks the getch()
and then moves on like i was using kbhit() but I am not.
Here is the piece of my code that has this in it.
Code: | for (i=0; i<2; i++){ //Get which drawer to open
c[i]=getch();
if(c[i]=='\r')
break;
} |
I am about to pull my hair out if anyone could help me I would be very appreciative.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 12:52 pm |
|
|
Quote: | However, just randomly it stopped working and I have not touched it. |
Look for a bad connection. Look for a broken wire. |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 12:57 pm |
|
|
Thanks for the quick reply,
However, I thought that was it but I have MPLAB's IDE software that has
MPLABSIM on it where you can run a debugger for your code and I see
with the debugger that the code is doing the same thing where it goes
right from getch() to the next line and does not stick and wait for a char
Could the simulator just be wrong? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 1:19 pm |
|
|
1. Post you compiler version.
2. Post a short compilable test program. Post the #include, #fuses, #use
delay(), #use rs232() statements, and a main(). You don't need a loop
to test the getch() problem. Just a simple c = getch(); will be enough.
3. Post your MPLAB version.
4. Post how you make it run in MPSIM. Are you pressing F8 (step over), or are you using "Run to cursor", or are you using a breakpoint ?
5. Tell what goes wrong in MPSIM.
In other words, give enough details to allow duplication of the problem. |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 1:23 pm |
|
|
OK,
I will get to work on the code details for the next post.
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Tue Mar 03, 2009 3:16 pm |
|
|
Remember that for TTL serial (which is what the PIC receives/sends), the 'idle' state is high. What you are describing, is exactly what you would see, if the serial input line is not pulled high, and what you will see in the simulator, unless you have programmed a high level onto the input.
Best Wishes |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 3:45 pm |
|
|
Ok, I have listed below a snippet of code for the problem. The output is
listed below as well. When this runs it should sit at getch() until I send it a value through Hyperterm. I am using a max232 chip in conjunction with the PIC16F676 chip to do the voltage conversion for communication to a pc. This way I can see what is coming out. I am using MPLAB's IDE version 8.10 and this is what the "about MPLAB IDE..." got me for the simulator:
MPLABSIM12 V2.10 , MPLABSIM16 V2.10, MPLABSIM18 V3.10
MPLABSIM30 V4.10, MPLABSIM32 V0.00.10
I have attached code and output in hyper term:
Code: | #if defined(__PCM__)
#include <16f886.h>
#fuses HS, NOWDT, NOPROTECT
#use delay(clock=20000000)
#use fast_io(A)
#use rs232(baud=9600,xmit=PIN_A1, rcv=PIN_A0)
void main(){
char input;
for(;;){
input=getch();
printf("it is:%c",input);
}
} |
Output:
it is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit is:ÿit |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 3:52 pm |
|
|
You're using a software UART in that program. MPLAB can't simulate
a software UART, to my knowledge. Change the pins to C6 and C7
for Tx and Rx.
I don't understand why you can't just take the 16F886 and hook up to
a MAX232 and then to a PC and make it work. You've got too much
intermediary stuff in your test setup. |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 3:55 pm |
|
|
Thanks for the info I will try changing the pins. However, I do have the
set up that you are talking about. I have a diagram below
PIC16F886---->MAX232----->PC
That is what I have hardware wise. I will try changing the pins and reply back with my findings.
Last edited by misperry on Tue Mar 03, 2009 4:17 pm; edited 1 time in total |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 4:16 pm |
|
|
OK I tried switching it to c6 and c7 and it stopped printing a whole lot of stuff to the screen however, it stopped printing at all. when I type in stuff nothing appears on the screen now.
The following is the code:
Code: |
#if defined(__PCM__)
#include <16f886.h>
#fuses HS, NOWDT, NOPROTECT
#use delay(clock=20000000)
#use fast_io(C)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7)
void main(){
char input;
set_tris_c(0b10000000);
for(;;){
input=getch();
printf("it is:%c",input);
}
} |
Oh and I forgot the set_tris_c() command last time. I added it but it did not have any affect. And like I said before I had this all working perfectly about two weeks ago and it would halt at the getch() command and everything. And now, 2 weeks later, it is seeming like it is evaluating getch() whether there is anything coming in on the port or not and then moving on. It is very frustrating. it is a direct violation of what the ccs c help file has in it that it will wait for ever for something to come in. Because it doesn't wait. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 4:33 pm |
|
|
Post your compiler version. It's given at the top of the .LST file, which
is in your project directory. It's a number in this format: x.xxx |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 4:34 pm |
|
|
I have compiler 4.025 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Mar 03, 2009 5:19 pm |
|
|
misperry wrote: | I have compiler 4.025 | Check the sticky thread on top of this forum regarding the early v4.xxx compiler releases. Everything before 4.03x failed to compile the most basic program. The compiler got stable from around v4.070
Revert to the old but stable v3.249 or upgrade to a new compiler version. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Mar 03, 2009 5:50 pm |
|
|
There are problems with the start-up code in vs. 4.025, but they don't
affect the serial port operation.
I used a PicDem2-Plus board with a 16F886, and a 20 MHz crystal, and
vs. 4.025, and Hyperterminal. I typed in capital "A" characters and
I got this, which is correct:
Quote: | it is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait is:Ait |
Your display output is poor. It's difficult to decipher. There are no
carriage return - linefeeds. The "it is:" is totally unecessary. But it is
working. Therefore the problem is in your board or your RS-232 cable
or your terminal program or your PC. |
|
|
misperry
Joined: 03 Mar 2009 Posts: 40
|
|
Posted: Tue Mar 03, 2009 6:12 pm |
|
|
OK, Thank you all soo much for the information. I will take both advice and upgrade the compiler, since I have some other chips I would like to use with this that the older version that I have does not support, as well as checking my connections thoroughly. I will post if I have more problems. thanks guys. |
|
|
|