# Whats wrong with my program? MikroBasic

Status
Not open for further replies.

#### bigal_scorpio

##### Active Member
Hi to all,

I have just roughed out a small program that is todrive a stepper motor.

It was just to find the basics of using the stepper and it worked ok, the 0, 1,2 and 4 leds on my dev board scrolling happily.

Then I read about high torque stepper driving and decided to incorporate that, it just meant adding a few lines and it seemed plausible until I tried it and for some reason the 0 led NEVER lights but the others scroll as they should.

I have been staring at the code for an hour or more and cannot see what is wrong! Please have a look and try to point out my error before my eyes get screen burn with the code

BTW I am using MikroBasic on an EasyPic5 board.

Here is the code that's ok.

program stepper2

main:
DIM q as byte
q = 1
GPIO = 00101000
TRISIO = 00101000

DO
GPIO.0 = 1
delay_ms (30)
GPIO.0 = 0
delay_ms (30)
GPIO.1 = 1
DELAY_MS (30)
GPIO.1 = 0
DELAY_MS (30)
GPIO.2 = 1
DELAY_MS (30)
GPIO.2 = 0
DELAY_MS (30)
GPIO.4 = 1
DELAY_MS (30)
GPIO.4 = 0
DELAY_MS (30)

LOOP UNTIL q = 0
end.

And this is the High Torque effort that is iffy.

program stepper6

main:
DIM q as byte
q = 1
GPIO = 00101000
TRISIO = 00101000

DO
GPIO.0 = 1
GPIO.4 = 1
delay_ms (130)
GPIO.0 = 0
GPIO.4 = 0
DELAY_MS (130)
GPIO.0 = 1
GPIO.1 = 1
DELAY_MS (130)
GPIO.0 = 0
GPIO.1 = 0
DELAY_MS (130)
GPIO.1 = 1
GPIO.2 = 1
delay_ms (130)
GPIO.1 = 0
GPIO.2 = 0
DELAY_MS (130)
GPIO.2 = 1
GPIO.4 = 1
DELAY_MS (130)
GPIO.2 = 0
GPIO.4 = 0
DELAY_MS (130)

LOOP UNTIL q = 0
end.

Thanks for looking..........Al

Last edited:

#### bananasiong

##### New Member
Hi,
It's possible to be
So you may want to do this:
Code:
DO
GPIO = 00010001
delay_ms (130)
GPIO = 00000011
delay_ms (130)
...and so on

Last edited:

#### be80be

##### Well-Known Member
Hi,
It's possible to be
So you may want to do this:
Code:
DO
GPIO = 00010001
delay_ms (130)
GPIO = 00000011
delay_ms (130)
...and so on

#### bigal_scorpio

##### Active Member

Hi guys,

Ok so if thats the problem why does it work ok on the last 3 leds?

Its ONLY led 0 that fails to light!

Al

#### bananasiong

##### New Member
It could be the capacitance on bit 0 is higher than others. Regardless on a bread board or a PCB. And this is a known issue for all time. The BKM is not to write to the same port after the next instruction cycle of writing it.

#### be80be

##### Well-Known Member
I don't really know but I wrote some code for 12f683 in assembly it worked fine running threw four leds. I switched to picbasic pro and used the

Code:
HIGH (GPIO,0)
delay_MS (120)
LOW (GPIO,0)
deylay_MS (120)
// and so on till 4
It would only flash 3 leds in roll last didn't light

#### bananasiong

##### New Member
I don't really know but I wrote some code for 12f683 in assembly it worked fine running threw four leds. I switched to picbasic pro and used the

Code:
HIGH (GPIO,0)
delay_MS (120)
LOW (GPIO,0)
deylay_MS (120)
// and so on till 4
It would only flash 3 leds in roll last didn't light
If you have the last one didn't light it may not be the same problem as the RMW because you're having delay in between the write that gives enough time for the charging and discharging.

#### AllVol

##### New Member
I'm sure you guys have ANSEL set 0x00?

#### bigal_scorpio

##### Active Member
Hi again,

Well I have tried new ideas and have got further, but now another problem.

The four leds on GPIO 0, 1, 2 and 4 are working as they should with the new prog below BUT now GPIO 5 has joined the party and flashes happily!

What am I doing wrong? GPIO 5 isn't even set as an output in TRISIO.

program stepper93

main:
DIM q as byte
q = 1
TRISIO = 00101000

DO
GPIO = 00010001
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000011
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000110
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00010100
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)

LOOP UNTIL q = 0
end.

Help.........Al

Last edited:

#### bananasiong

##### New Member
Hi,
Why are you connecting the LED to the input pin? If you're not using that pin, just don't bother anything. If you're using it as an input, make sure to have pull up or pull down. Connecting the LED to an input pin doesn't sound right to me.

And BTW, you could actually remove these:
Code:
GPIO = 00000000
DELAY_MS (10)

#### bigal_scorpio

##### Active Member
Hi,
Why are you connecting the LED to the input pin? If you're not using that pin, just don't bother anything. If you're using it as an input, make sure to have pull up or pull down. Connecting the LED to an input pin doesn't sound right to me.

And BTW, you could actually remove these:
Code:
GPIO = 00000000
DELAY_MS (10)

Hi mate,

My dev board has LEDs connected to all the pins, these can only be disconnected as a full port which would mean I wouldn't see any lights.

But even then it still doesn't explain why GPIO 5 is doing anything at all.

Al

PS the delays are just for now so I can see whats happening easier and will be removed/changed when I get the prog working right. Then I will have to decide on the ON times for best driving the motor! Puzzled again! Doh!

#### Mr RB

##### Well-Known Member
On EasyPIC5 you should set the PIC pins you are not using to outputs. Otherwise the pin will float and the LED attached to the that pin will indicate the pin voltage. The LED will probably go out if you set the pull-up resistor for that port to "pull-down" (see the jumpers) AND you need to turn the PIC internal pull-ups OFF and check what other peripherals might be connected to that pin.

But the easiest way is to just make unused pins into outputs, this is good practice with most PICs.

By the way;
Code:
 DO
GPIO = 00010001
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000011
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000110
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00010100
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)
This won't work good for a stepper. Especially to get "high torque" ie 2 windings on, you should never have all windings with no power. I would remove the parts that turn the windings off, so it becomes like this;

Code:
 DO
GPIO = 00010001
delay_ms (100)
GPIO = 00000011
DELAY_MS (100)
GPIO = 00000110
delay_ms (100)
GPIO = 00010100
DELAY_MS (100)

#### be80be

##### Well-Known Member
Rb this works good it picbasic pro for the 12f683
Code:
ANSEL=%00000000
CMCON0=%00000111
TRISIO=%00000000
loop:
GPIO = %00010001
PAUSE 100
GPIO = %00000011
PAUSE 100
GPIO = %00000110
PAUSE 100
GPIO = %00010100
PAUSE 100
Goto loop
end

#### bigal_scorpio

##### Active Member
On EasyPIC5 you should set the PIC pins you are not using to outputs. Otherwise the pin will float and the LED attached to the that pin will indicate the pin voltage. The LED will probably go out if you set the pull-up resistor for that port to "pull-down" (see the jumpers) AND you need to turn the PIC internal pull-ups OFF and check what other peripherals might be connected to that pin.

But the easiest way is to just make unused pins into outputs, this is good practice with most PICs.

By the way;
Code:
 DO
GPIO = 00010001
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000011
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00000110
delay_ms (100)
GPIO = 00000000
DELAY_MS (10)
GPIO = 00010100
DELAY_MS (100)
GPIO = 00000000
DELAY_MS (10)
This won't work good for a stepper. Especially to get "high torque" ie 2 windings on, you should never have all windings with no power. I would remove the parts that turn the windings off, so it becomes like this;

Code:
 DO
GPIO = 00010001
delay_ms (100)
GPIO = 00000011
DELAY_MS (100)
GPIO = 00000110
delay_ms (100)
GPIO = 00010100
DELAY_MS (100)

Hi mate,

I see what you mean with the bits being on, it is better and makes the motor much more powerful. Thanks

As to setting unused pins to outputs - I intend to use GPIO 3 and 5 as inputs to switches to control the direction of the motor.

I have also tried pulldown on GPIO 5 to no avail, but don't know how to turn off the internal pull ups?

Also from be80be s post below he uses CMCON %111 to turn off the comparator module, but I can't get the command to work in MBasic and cannot find any mention of the CMCON in the MBasic Manual.

Do you have an easypic5 and MBasic? If so could you try the prog just to verify mine isn't acting up?

Thanks........Al

#### bigal_scorpio

##### Active Member
Rb this works good it picbasic pro for the 12f683
Code:
ANSEL=%00000000
CMCON0=%00000111
TRISIO=%00000000
loop:
GPIO = %00010001
PAUSE 100
GPIO = %00000011
PAUSE 100
GPIO = %00000110
PAUSE 100
GPIO = %00010100
PAUSE 100
Goto loop
end

Hi mate,

When you say it works good does that mean that the GPIO 5 pin is not cycling as mine is?

Also as you used a 683 you turned off the Ansel and CMCON, since mine has no AD converter I can ignore the Ansel but do I need to turn off the comparator as you have, if so where does the info on how to use the command come from,there is no mention of it in MBasic and the datasheet for the PIC629 has me entirely baffles as to the CMCON usage.

Thanks for the help anyway mate, I need it!

Al

#### mikeReynolds

##### Member
Hi mate,

When you say it works good does that mean that the GPIO 5 pin is not cycling as mine is?

Also as you used a 683 you turned off the Ansel and CMCON, since mine has no AD converter I can ignore the Ansel but do I need to turn off the comparator as you have, if so where does the info on how to use the command come from,there is no mention of it in MBasic and the datasheet for the PIC629 has me entirely baffles as to the CMCON usage.

Thanks for the help anyway mate, I need it!

Al

OK, you have the datasheet for 12F629, look in the section for the comparator module, Section 6.2, comparator configuration. CMCON registor has 8 bits 7 thru 0, see the top right diagram in figure 6.2 where it says comparator off CM2:CM0 = 111, this means set bits 0, 1, 2 of CMCON to 1's, all others will be 0's so CMCON = 00000111 in binary or 7 in decimal. In MikroBasic binary numbers are preceded by % so %00000111. Now read the MikroBasic help file, under MikroBasic Specifics see the section Predefined Globals and Constants, in that section read and remember this sentence: All PIC SFR registers are implicitly declared as global variables of byte type, and are visible in the entire project.

Hope this helps

#### be80be

##### Well-Known Member
You use CMCON =0x07 for your chip it's n the defs files of microbasic I have it. I installed it when I installed microC

Last edited:

#### bigal_scorpio

##### Active Member
Done it!

Hi guys,

Thanks for all the help, I couldn't have done it alone.

I realised that I didn't have the "%" before any of my GPIO binaries, tried it with and bingo! GPIO 5 is now off for good!

Thanks again guys....Brilliant

Regards.............Al

#### Mr RB

##### Well-Known Member
...

As to setting unused pins to outputs - I intend to use GPIO 3 and 5 as inputs to switches to control the direction of the motor.
...

Great that you go it going!

Re the PIC pin pullups and LEDs etc, the MikroE hardware is quite powerful as you have jumpers to set port pullups to pull UP or DOWN, also the pushbuttons can be configured to connect to 0v when prerssed or to 5v when pressed.

Have a read through your EasyPIC5 manual it is well written with lots of pictures of the jumpers and tells how they affect the external pullups etc.

For development I usually set it up this way;
Code:
PIC internal pullups to OFF
EasyPIC external pullups set to pull DOWN
Buttons to connect to 5v when pressed (active hi)
Leds light up on HI, ie when buttons are pressed (inputs go hi) or when outputs are activated (outputs go hi)

Sometimes I change it later to make the buttons active LO (for when it goes into hardware) but for ease of the development process the above way is what I found to be best.

Status
Not open for further replies.

Replies
9
Views
820
Replies
6
Views
980
Replies
4
Views
2K
Replies
25
Views
2K
Replies
1
Views
1K