Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help with MikroBasic program misbehaving please

Status
Not open for further replies.

bigal_scorpio

Active Member
Hi again guys,

due to all the previous help I now have a somewhat working program but it is doing something I can't figure out and am only a beginner with Pics.

When I turn on I expected the program to wait until a button (up or down) was sent high ( I have internal pullups off and the pins pulled low on my dev board - EasyPic5) but the prog just runs through past the button command to the pulses and never stops.

What am I doing wrong?

I am using a 12F629 Pic btw, here is the code.

program stepper992

main:

DIM t as byte
WPU = %00000000 'turn off weak pullups
TRISIO =%00101000 'set GPIO 0,1,2,4 as out and 3,5 as inputs
GPIO = %00000000 'turn off all outputs
CMCON =0x07 'set comparator to off
IF BUTTON(GPIO,5,1,0)then GOTO up 'check for button press for up
END IF
IF BUTTON(GPIO,3,1,0)then GOTO down 'check for button press for down
END IF
GOTO main 'if no presses then restart

up:
FOR t = 1 to 5
GPIO = %00010001 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000011 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000110 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010100 'Set on bits for this pulse
DELAY_MS (110)
NEXT t

GOTO main 'After completing pulses return to start

down:
FOR t = 1 to 5
GPIO = %00010100 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000110 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000011 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010001 'Set on bits for this pulse
DELAY_MS (110)
NEXT t

GOTO main 'After completing pulses return to start
end.

Thanks for looking........Al
 
I don't know what the button function does and so can't comment. However, you could try something like if(GPIO&%00100000) instead of IF BUTTON(GPIO,5,1,0). I'm assuming that IF BUTTON(GPIO,5,1,0) reads a button on GPIO 5. As both your functions have delays there is no need for any debounce.

Mike.
 
I don't know what the button function does and so can't comment. However, you could try something like if(GPIO&%00100000) instead of IF BUTTON(GPIO,5,1,0). I'm assuming that IF BUTTON(GPIO,5,1,0) reads a button on GPIO 5. As both your functions have delays there is no need for any debounce.

Mike.

Mike the IF BUTTON(GPIO,5,1,0). is checking for low on transition from 1 to 0 (release of button), GPIO,5 is inverted:
I think the OP would want
Code:
main:

DIM t as byte
WPU = %00000000 'turn off weak pullups
TRISIO =%00101000 'set GPIO 0,1,2,4 as out and 3,5 as inputs
GPIO = %00000000 'turn off all outputs
CMCON =0x07 'set comparator to off
[COLOR="Red"]IF BUTTON(GPIO,5,0,1)then GOTO up 'check for button press for up
END IF
IF BUTTON(GPIO,3,0,1)then GOTO down 'check for button press for down[/COLOR]
END IF
GOTO main 'if no presses then restart

up:
FOR t = 1 to 5
GPIO = %00010001 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000011 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000110 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010100 'Set on bits for this pulse
DELAY_MS (110)
NEXT t

GOTO main 'After completing pulses return to start

down:
FOR t = 1 to 5
GPIO = %00010100 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000110 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000011 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010001 'Set on bits for this pulse
DELAY_MS (110)
NEXT t

GOTO main 'After completing pulses return to start
end.
this should check gpio3 to see if it went high
 
Last edited:
If he has the pin pulled low and is looking for a low then of course it will run right through and not wait...
 
If the button function checks for a transition then it should always stop. One of the reasons I don't use canned functions, normally quicker to write my own and that way, I know exactly what they do.

Mike.
 
mike2524 that's what it was doing
I think this is what the OP needs
Code:
[COLOR="Red"]
IF BUTTON(GPIO,5,0,1)then GOTO up 'check for button press for up
END IF
IF BUTTON(GPIO,3,0,1)then GOTO down 'check for button press for down
END IF[/COLOR]
I would do it like this
Code:
        IF GPIO.B5 =1 then goto up
        elseif GPIO.B3 =1 then goto down
        endif
 
Last edited:
The Op is just testing for low state he needed to test for High state
 
Hi again guys,

I have tried ALL the suggestions but still no joy.

Burt's code didn't compile as MB does not like the "GPIO.B3 = 1" until I removed the .B so there must be a difference in the software he is using.

Even then with Burt's code modified it then ignored either button altogether.

I have taken on board what was pointed out about the state bit and realised it should be BUTTON GPIO, 5,1,1 and not 5,1,0 as I had it, but still fails!

I have noticed that if I remove the button reference to pin 3 then the program works fine, so I am beginning to wonder if its something to do with the MasterClear pin that I have missed?

Should I have set that to anything particular?

Thanks............Al
 
Are you using MikroBasic or MikroBasic pro there not the same the pro you have more to set up
+
 
Last edited:
this is with microbasic pro
Code:
program steppertest

' Declarations section


dim temp as word

WPU = %00000000 'turn off weak pullups
TRISIO =%00101000 'set GPIO 0,1,2,4 as out and 3,5 as inputs
GPIO = %00000000 'turn off all outputs
CMCON =0x07 'set comparator to off
main:


while true
IF GPIO.5 =1 then GOTO up 'check for button press for up
END IF
IF GPIO.3 =1 then GOTO down 'check for button press for down
END IF
GOTO main 'if no presses then restart

up:
FOR temp = 1 to 5
GPIO = %00010001 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000011 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000110 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010100 'Set on bits for this pulse
DELAY_MS (110)
NEXT temp

GOTO main 'After completing pulses return to start

down:
FOR temp = 1 to 5
GPIO = %00010100 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000110 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000011 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010001 'Set on bits for this pulse
DELAY_MS (110)
NEXT temp

GOTO main 'After completing pulses return to start
wend
end
make sure you turn mclr off
 
Last edited:
Are you using MikroBasic or MikroBasic pro there not the same the pro you have more to set up
+

Hi Burt,

Its Mikrobasic mate, not pro, I didn't even know about the pro. Is it any better?

Also I may have solved the problem! If I pull UP GPIO3 and pull DOWN GPIO5, then check for the opposite on button it seems to work!

But if I pull BOTH UP or DOWN then the problem is back! Weird?

So I am now baffled about something else then, but at least I can make up the circuit now eh. :)

Al
 
Oh no!!!!!!

Hang on folks!

The switches seemed ok, in the fact that it didn't just run at first putting a high on 5 worked when it was tied to gnd and the button command was looking for a high.

Problem is that putting a gnd on 3 even though it is pulled high does not start the pulses, even though I have 3 pulled high and I can see the relevant led on 3 going out and the button command is looking for a low.

Even more baffled............Al
 
Last edited:
Time to call it a day!

Hi guys,

Thanks for all the help, I've sorted it now!

Migrated to a 16F630 and not a hitch! :)

Shame though as Ithought the 12F629 had allI needed = 4 outputs and 2 inputs. Still wondering why it didn't work but never mind.

Though if anyone does has a definitve answer I would still be interested.

Thanks again........Al
 
Did you turn mclr off you set it here
 

Attachments

  • try_this.PNG
    try_this.PNG
    36.7 KB · Views: 259
Hi guys,

Thanks for all the help, I've sorted it now!

Migrated to a 16F630 and not a hitch! :)

Shame though as Ithought the 12F629 had allI needed = 4 outputs and 2 inputs. Still wondering why it didn't work but never mind.

Though if anyone does has a definitve answer I would still be interested.

Thanks again........Al

That's why I don't miKro boards The code I posted worked with the two buttons pulled low and switch high but i don't have a 12f629.
I have a12f675 witch is big brother and it worked on a solderless bread board

You have a lot of board to play with and two many jumpers LOL
 
Last edited:
Hi Burt,

Thanks for trying it for me, at least it proves my theory at least was ok.

BTW do you think its worth upgrading to MB pro or is it mainly for the larger Pics 18Fs and upwards etc?

PS since I had to use a 16F and have spare pins I wondered about incorporating a couple of limit switches so the stepper wouldn't go beyond a set range and wouldn't go the wrong way IE the same way twice as it is for opening and closing a flap, but I think this is beyond me and I can't even get my head round how to do it. Any ideas?

Regards..........Al
 
if you draw it out and post we could help you out just the circuit would do.
if you have 7.00 or newer mikrbasic keep it it's better then the pro
 
Last edited:
I don't no if your setup has mclr pulled up to vdd but if it dos that's why this didn't work
I tried some ways to see what came up and that was it. Here the code
Code:
program steppertest

' Declarations section 


dim temp as word

WPU = %00000000 'turn off weak pullups
TRISIO =%00101000 'set GPIO 0,1,2,4 as out and 3,5 as inputs
GPIO = %00000000 'turn off all outputs
CMCON =0x07 'set comparator to off

main:


IF GPIO.5 =1 then GOTO up 'check for button press for up
END if
if GPIO.3 =1 then GOTO down 'check for button press for down
END IF
GOTO main 'if no presses then restart

up:
FOR temp = 1 to 5
GPIO = %00010001 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000011 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000110 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010100 'Set on bits for this pulse
DELAY_MS (110)
NEXT temp
GOTO main 'After completing pulses return to start

down:
FOR temp = 1 to 5
GPIO = %00010100 'Set on bits for this pulse
delay_ms (110)
GPIO = %00000110 'Set on bits for this pulse
DELAY_MS (110)
GPIO = %00000011 'Set on bits for this pulse
delay_ms (110)
GPIO = %00010001 'Set on bits for this pulse
DELAY_MS (110)
NEXT temp
GOTO main 'After completing pulses return to start
end.
Here is how I hooked it up
 

Attachments

  • like_it.png
    like_it.png
    26.4 KB · Views: 349
Last edited:
Hi Burt,

Yes thats how I had the circuit, apart from the 1k resistors, I assume the switches on the Easypic5 are just grounded or to pos whichever selected? Can't see any resistors in the paths.

I also tried the MCLR pulled high but as you said being on the dev board must have made it error somehow.

Anyway I am just going to draw my circuit out with the new 16F and will post it so you can give any ideas on improvement.

Thanks mate............Al
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top