Been there - done that - just spank yourself, take notes & move on...I made a huge mistake![]()
Been there - done that - just spank yourself, take notes & move on...I made a huge mistake![]()
I did
Hi again !
I would like your opinions guys in terms of "state of the art" way to write a picbasic pro program
I am getting inputs from an encoder, I have 12 positions possible, therefore 12 values. here is an example of my code for 2 values. I was wondering if the use of "Case" would be a better idea, that way if I decide to turn off my "switch" my program stops immediatly and is not finishing its loop before seeing that the switch's value changed.
Getadc is my subroutine for my pot and pwm to adjust the duty cycle regarding the value of the pot.Code:encoderstate1 var Byte duty var Word Main: encoderstate1 = PORTA If switch = 1 Then If encoderstate = 0 Then GoSub GetADC GoSub SetPWM OVDCOND = %00100010 'Q4 (PWM5) and Q10 (PWM1) PORTC = %00000011 'Q1 and Q7 ON EndIf If encoderstate1 = 1 Then GoSub GetADC GoSub SetPWM OVDCOND = %00000110 PORTC = %00001100 EndIf GoTo Main
Suggestions ?
Hi,
Sure, Select-Case is an option. It's "cleaner" but does result in slightly larger code than multiple IF-THEN statements. But if you're not short on space go ahead.
Another option might be to use BRANCH.
Also, perhaps you can move the GOSUB GetADC and GOSUB SetPWM to right after the If Switch=1 Then statement? That will save you a couple of bytes of program space and since, if I'm not mistaken, all twelve different encoder states calls both those routines you might as well do before evaluting the encoder value.
/Henrik.
Hello Henrik,
I tried the code below with case and it does not work![]()
What am I doing wrong here ???? in my 2 cases, portA = 0 and portA = 1 in the other one (PORTA.0=1). The first code given with IF... THEN works but not this one ?Code:encoderstate1 var Byte duty var Word Main: encoderstate1 = PORTA If switch = 1 Then Select case encoderstate1 Case 0 GoSub GetADC GoSub SetPWM OVDCOND = %00100010 'Q4 (PWM5) and Q10 (PWM1) PORTC = %00000011 'Q1 and Q7 ON Case 1 GoSub GetADC GoSub SetPWM OVDCOND = %00000110 PORTC = %00001100 End Select EndIf GoTo Main
PS : I'll try tomorrow morning (don't have access to my pic right now) to put the subroutines just once as well as that "Branch"
Hello,
I can't really see any problem with it.... What does doesn't work mean in this case, what does it do?
The only think I can think of is if you have anything else connected to PortA so that the actual value assigned to EncoderState1 isn't 0 even when the encoder is in the zero-position. But since it works with the IF-THEN version it can't be that (?).
Do you have a serial connection setup? If so you can HSEROUT some debug information which will make it easier to figure out what's going on. If not perhaps you could blink a LED the same number of times as the value that gets assigned to EncoderState1, just to see what's going on.
/Henrik.
Hello,
Thank you for your reply,
The IF...THEN code still works (thanks for the tip putting just the subroutines at the top of main).
I am only using a PICkit 2 programmer. ?!Do you have a serial connection setup? If so you can HSEROUT some debug information which will make it easier to figure out what's going on.
What do you mean ??If not perhaps you could blink a LED the same number of times as the value that gets assigned to EncoderState1, just to see what's going on.
This morning I've tried a different code :
In case 0, it is doing what it should do, now if I'm trying to give the value of 1 (I have a switch on portA.0, that is all !!) it jumps to case else, it means that it does not see the value 1 ??? Why not ? it is working with IF... THEN...Code:Main: encoderstate1 = PORTA Select encoderstate1 Case %000000 GoSub GetADC GoSub SetPWM OVDCOND = %00100010 'Q4 (PWM5) and Q10 (PWM1) PORTC = %00000011 'Q1 and Q7 ON Case %000001 GoSub GetADC GoSub SetPWM OVDCOND = %00100010 'Q4 (PWM5) and Q10 (PWM1) PORTC = %00000011 'Q1 and Q7 ON Case Else PORTC = 0 OVDCOND = 0 EndSelect
Moreover, after switching my switch and giving the value 1 to portA.0, 4seconds after, it acts like if the value on portA.0 was 0... ???
AHHH sometimes those pics know how to make you crazy, doesn't they ?
Thanks for the help. Very much appreciated![]()
Last edited by barneyfrance; - 19th July 2011 at 11:28.
Hi,
It's strange if it really does work with the IF-THEN version. However, as is common on these PICs there are analog inputs on PortA. These normaly needs to be switched to digital in order to work in digital mode (no surprise) and this PIC is no exception. I know you're using the ADC in your GetADC rotuine so I know you have it setup some way or another but are you sure that the pins used for the encoder are setup as digital? (ANSEL0 and ANSEL1 registers)
My thought with the LED was something in the line of:This should make the LED blink the number of times equal to the value loaded into EncoderState1. It should help you figure out what's going on.Code:i VAR BYTE LED VAR PortB.7 EncoderState1 = PortA For i = 0 to EncoderState1 High LED Pause 200 Low LED Pause 200 NEXT 'Rest of code Pause 2000 'Just to make a visual "mark" that the loop starts over Goto Main
My money is on the ADC being configured to have its inputs on some of the pins you're trying to use as digital. I don't know why it seems to work with the IF-THEN version though....
If this doesn't help can you post a schematic and the complete code?
/Henrik.
Bookmarks