View previous topic :: View next topic |
Author |
Message |
Oli Guest
|
kbhit, what am I doing wrong?? (PIC24, PCD) |
Posted: Fri Sep 04, 2009 4:38 am |
|
|
Hi all
After having some problems with USB CDC I'm temporarily using rs232 until they are resolved. I have created wrapper functions that are intended to allow me to switch between the two easily.
They all work fine except for the kbhit wrapper - I cut out everything unnecessary and to my understanding these two functions should be the same:
Code: | int8 test_f(void)
{
return(kbhit(EXPANSION_SERIAL));
} |
and
Code: |
kbhit(EXPANSION_SERIAL)
|
calling test_f(); should return the same as kbhit(EXPANSION_SERIAL); but test_f(); doesn't work AT ALL.
Does anybody have any ideas? I'm using PCD 4.099 and PIC24F256GB110.
Many thanks in advance
EDIT: I have tried changing the return type to int1 and boolean also with no luck. |
|
|
Guest
|
|
Posted: Fri Sep 04, 2009 11:21 am |
|
|
I don't do pic24
but assuming that the naked syntax DOES work
AND
if this wrapper is essential how about this approach:
#define kbhx kbdhit(expansion_serial)
does kbhx work or not? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Sep 04, 2009 6:19 pm |
|
|
since there is no such func as kbdhit ,
i'm guessing that what Guest meant was:
Code: |
#define kbhx kbhit(expansion_serial)
char mychar;
...
// then
if (kbhx) mychar=getc();
|
to see if that works for you |
|
|
Oli Guest
|
|
Posted: Mon Sep 07, 2009 10:59 am |
|
|
Thanks for your replies - I know I can do it that way, but what is wrong with the code that I posted? or is this the compiler at fault again?
The actual wrapper is the code below. I posted the simplest possible instance of the problem to aid with identification of the problem initially.
Code: | int8 alt_usb_cdc_kbhit(void)
{
if (system_states.USE_SERIAL_NOT_USB == 1)
return kbhit(EXPANSION_SERIAL);
else
return usb_cdc_kbhit();
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 07, 2009 11:30 am |
|
|
Quote: |
int8 test_f(void)
{
return(kbhit(EXPANSION_SERIAL));
}
but what is wrong with the code that I posted?
|
Try a work-around. Try putting the result in a temp variable and then
return the temp variable:
Code: |
int8 test_f(void)
{
int8 retval;
retval = kbhit(EXPANSION_SERIAL);
return(retval);
}
|
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
just a first guess |
Posted: Mon Sep 07, 2009 11:40 am |
|
|
kbhit and usb_cdc_kbhit
both return an Int1 - single BIT value -
but your function returns an int8
i am not sure how the return statement ( which you define as an int8 ) is going to deal with that w/o explicit type casting in your code
ALSO
since i don't know how you handle the struct containing
system_states.USE_SERIAL_NOT_USB variable
SO
i don't KNOW if it matches what you want it to do
if you are accidentally in the ( I'veGOT USB ==TRUE) state but are calling khbit() you will always see a 0 return unless there happens to be serial data in the buffer
there could be a problem in your STRUCT handling that is steering you into the ditch
you could do a printf on RS-232 of that State var to be sure
your struc vars contain what you think they have |
|
|
Oli Guest
|
|
Posted: Tue Sep 08, 2009 9:40 am |
|
|
Thanks for the replies again
asmboy -
- I tried this as both int1 and as a boolean, it makes no difference (see bottom of original post)
- I am confident my use of variable as part of a structure is not causing the problem. I have checked this by removing it from the structure - no difference.
PCM - Bingo. This fixed it. Question though: Is this my fault, or the compilers? To the best of my knowledge there wasn't a problem with my original code? (apart from the int1 / int8 return value, which didn't make a difference anyway) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 08, 2009 10:56 am |
|
|
I wasn't really sure what you were doing in your original code because
it looked like just a code fragment. There was no semi-colon. So I just
ignored it and looked at the test_f() function. I'm referring to this line:
Quote: | kbhit(EXPANSION_SERIAL) |
If that's actual code (with an semi-colon appended), it won't work.
It compiles to nothing. You have to load the return value into a variable
to get it to compile to some ASM code. You can see this with a test
program:
Code: | #include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=EXPANSION_SERIAL)
//=====================================
void main()
{
int8 result;
kbhit(EXPANSION_SERIAL);
result = kbhit(EXPANSION_SERIAL);
while(1);
} |
Here's part of the .LST file. Notice that the first line compiles to nothing.
But the 2nd statement does compile into code.
Code: | .................... int8 result;
....................
.................... kbhit(EXPANSION_SERIAL);
....................
....................
.................... result = kbhit(EXPANSION_SERIAL);
0022: CLRF 06
0024: BTFSC F9E.5
0026: INCF 06,F
....................
.................... while(1);
0028: BRA 0028
.................... } |
|
|
|
Oli Guest
|
example |
Posted: Wed Sep 09, 2009 3:04 am |
|
|
PCM - yes, I realise that - I should have made it clear how both are used (in an if statement). Here is an example of how they where being used: (referring to my first post)
Code: |
while (true)
{
if (test_f())
{
test = fgetc(EXPANSION_SERIAL);
fputc(test, EXPANSION_SERIAL);
}
} |
the above doesn't work, while the example below does.
Code: |
while (true)
{
if (kbhit(EXPANSION_SERIAL))
{
test = fgetc(EXPANSION_SERIAL);
fputc(test, EXPANSION_SERIAL);
}
}
|
so my question remains, is the compiler at fault here? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 09, 2009 1:23 pm |
|
|
I don't have the PCD compiler so I can't do this for you, but my advice
is to look at the .LST file and see what it's doing. |
|
|
|