View previous topic :: View next topic |
Author |
Message |
alfonsol Guest
|
problem with getch an rs232 |
Posted: Thu Feb 02, 2006 12:09 pm |
|
|
i'm tryin to make a menu, but always it executes the last IF
if (orden!='C'){
pila=150;
(int32)deltap=9000
i can send an A, B or C, but the program always executes the last if.
here is the code.
orden=getch();
if (orden!='A') {
pila=60;
(int32)deltap=2000;
}
if (orden!='B'){
pila=330;
(int32)deltap=9000;
}
if (orden!='C'){
pila=150;
(int32)deltap=9000; |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 02, 2006 12:54 pm |
|
|
Post a small program that shows the problem.
1. Show all variable declarations.
2. Show your #use rs232() statement, etc.
3. Post your version of the compiler. |
|
|
sonicdeejay
Joined: 20 Dec 2005 Posts: 112
|
|
Posted: Thu Feb 02, 2006 1:00 pm |
|
|
I use Max 232 to buffer between my PIC..that's connection works for me...and my RS232 code is
Code: | #use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8) // RS232 Configuration |
|
|
|
Ttelmah Guest
|
|
Posted: Thu Feb 02, 2006 3:31 pm |
|
|
As written, the code will always execute the last if, unless the character is a 'C' (also remember that if you type a 'C', by default this will be lower case, and will not be accepted).
As it stands, if 'A' arrives, it'll execute the pila=330 code, then the pila=150 code. If 'B' arrives, it'll execute the pila=60 code, and then the pila=150 code. If 'C' arrives, it'll execute the pila=60 code, then the pila=330 code. If anything else arrives, all three statements will execute.
Normally you would test for the character beng equal, rather than 'not equal', and also use the 'else' statement, so it only executes the other tests if the tests so far have failed.
Best Wishes |
|
|
alfonsol Guest
|
|
Posted: Thu Feb 02, 2006 4:29 pm |
|
|
include <18F1320.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_b1, rcv=PIN_b4)
char orden;
int32 pila,deltap;
main(void)
{
orden=getch();
if (orden!='A') {
pila=60;
(int32)deltap=2000;
}
if (orden!='B'){
pila=330;
(int32)deltap=9000;
}
if (orden!='C'){
pila=150;
(int32)deltap=9000;
}
printf("\n\rorde: %4c pila: %4ld deltap: %4ld \n\r",orden,pila,deltap);
}
this program shows what happens |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Feb 03, 2006 8:18 am |
|
|
It seems this should wait for a character, then execute two of the three options depending what the character is, or all three if it is an unknown character. Is this what you want to happen? What do you want this code to do?
Offhand i would say the <if (orden!='A')> is a little odd for a menu. It means that that section of code will execute if anything EXCEPT 'A' comes in. < if (orden ='A')> is more what I would expect. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
dbotkin
Joined: 08 Sep 2003 Posts: 197 Location: Omaha NE USA
|
|
Posted: Fri Feb 03, 2006 8:38 am |
|
|
Sonicdeejay: Here's a little easier and much cheaper way to do it:
You can use 10K resistors in place of the 2.2K. Complete details at http://www.botkin.org/dale/rs232_interface.htm if you want to read it.
Dale |
|
|
sonicdeejay
Joined: 20 Dec 2005 Posts: 112
|
|
Posted: Fri Feb 03, 2006 8:57 am |
|
|
I should Hav know earlier....thx for the info... |
|
|
alfonsol Guest
|
|
Posted: Fri Feb 03, 2006 11:27 am |
|
|
thanks the problem was i used != and i should used == i copied the line from another program and i didn't see that |
|
|
|