Case function DOESNT work BUT IF (then) DOES
stelath_shadow9
Posts: 23
Having an issue with this code but cant figure out what it is. when I run this code nothing happens when I send in a "u" or an "a" serially. HOWEVER (please see next code below)
When I execute this code everything works EXCEPT for the "a" command. When I send an "a" it still excutes the same as sending "u". So I know that this works but just not for the case function for some reason. AND not with the ELSE..IF function. Any ideas?
CON VAR byte char OBJ bs2 : "bs2" pub here repeat char:= bs2.serin_char(1,9600,1,8) 'makes char the same as character coming in case char 'checks char against following cases "u": ' executes subroutine "this" this "a": that 'executes subroutine "that" Pri this 'blinks LED on and off 2 times dira [14]~~ repeat 4 !outa[14] waitcnt(8_000_000+cnt) here pri that 'blinks LED on and off forever dira[14]~ repeat !outa[14] waitcnt(8_000_000+cnt)
When I execute this code everything works EXCEPT for the "a" command. When I send an "a" it still excutes the same as sending "u". So I know that this works but just not for the case function for some reason. AND not with the ELSE..IF function. Any ideas?
CON VAR byte char OBJ bs2 : "bs2" pub here repeat char:= bs2.serin_char(1,9600,1,8) 'makes char the same as character coming in if char:= "u" ' executes subroutine "this" this elseif "a" that 'executes subroutine "that" Pri this 'blinks LED on and off 2 times dira [14]~~ repeat 4 !outa[14] waitcnt(8_000_000+cnt) here pri that 'blinks LED on and off forever dira[14]~ repeat !outa[14] waitcnt(8_000_000+cnt)
Comments
Change "if char:= "u"" to "if char == "u"". Change "elseif "a"" to "elseif char == "a"". The way you have it currently both expression will evaluate to true.
This unfortunately didnt work. The result was nothing happened when "a" or "u" was pressed.:depressed:
If you're trying to return to here, you don't need it. Subroutines return automatically after their last statement executes.
-Phil
I think that is just left over code from my earlier attempts. I just overlooked it. It shouldnt be there
-Phil
I bet these were on two different lines. The first line break often gets deleted when posting code (at least in Chrome). I often have to go back and fix code I've posted.
I agree with Phil about needing clock settings to use serial input output.
Andy
But this is starting to stray away from the original issue. The true issue I am having with this is not the serin command. that works. Ive tested it and it works fine with the IF command. However it is when i am trying to use CASE that nothing seems to work anymore. I dont understand why one would work but the other doesnt
Deleting the "here" part of that line had no change in performance
-Phil
So you're saying I need to define a constant for my clock settings even if I am not using an external oscillator/crystal? My only question to that is the same as the original post "why does the IF statement work in receiving serial in just fine and executing commands just fine but when I use the CASE function, and send a command, nothing happens? Not trying to argue, but I really don't see how one of these codes is receiving serial input and executing code, but when I change the function to a variation of the same thing ( or at least thats what that prop manual says, is that case and IF...then statements are almost the same thing.) It no longer works. for a rough example. If I use the following code (which is not going to be gramactically perfect, I just want to show the process)
char:= charin 'serin a character and make the variable "char" equal to the character detected
If char:= "a" ' if the variable "char" is "a" then turn on the LED
turn on LED
this code will work
char:= charin 'serin a character and make the variable "char" equal to the character detected
case char ' if the variable "char" is "a" then turn on the LED
"a": turn on LED
this code will not work....so whats happening between the 2. why would one code need clock references to work but the other doesnt?
-Phil
THIS WORKS
THIS DOES NOT WORK And the only difference between the 2 is the IF versus CASE function.
It's really down to the clock speed. The BS2 object calculates its baudrate internals based on clkfreq which defaults to 12MHz but using RCFAST can give you anything between 8 and 20MHz. On my demoboard I can get away with long[0] := 13_000_000 (13MHz). You could try something in that range for now but in the long run a crystal is just easier.
so, out of curiosity what would happen if i use FREQOUT on one pin to XIN? could I trick the prop into thinking there was a crystal?
-Phil
Is the BS2 object returning capitols?
-Phil
Actually the case example is the one that does work but because of the timing problem repeatedly mentioned, there aren't any valid characters received. The "if" version appears to work since it's esentially using "if true" so any character (even the invalid ones) entered will satisfy the conditional.
I am 99% sure that Phil and duane are right. I didn't realize that typing in ANY character would actually trigger my code. So I was under the false assumption my program was working one way but not the other. That being said, I will have to wait until I get my hands on a crystal (something I am not able to do where I am located) BUT, once I do I'll re try the project with the case statement and see where I end up. But I am going to set this thread to solved.
It NOT need be correctly 5MHz.
If You have any crystal that are between 3 - 7 MHZ You can use that ---- If You adjust settings acordingly
Probably not the case here as I don't see how to assign more memory to stelath's program. I'll look around to see if I can come up with my oddball code.
Paul
The CON section has constants defined for the LED pin, and the serial tx and rx pins. You should change these to match your hardware. If you don't have serial transmit wired up just specify an unused pin for transmit.
When the program first starts up push the ENTER key so that it can measure the serial bit time.