Thursday, February 19, 2009

Half step driver with P12F675 and mikroPascal

I'm planning to make a stepper driver with P12F675 Microchip microcontroller and, because recently I discovered mikroPascal, I decied to start the project. mikroPascal is the most complete IDE from the market and a pascal programming language for microcontrollers development. You can download the program and start using for free, the only limitation is the final size of hex code (2Kb) - but enough for 8 pin controllers. mikroElektronika, the producer of this IDE, has also other IDE's like mikroBasic and mikroC and many microcontroller development boards. The IDE is at version and entire project will be available for download. Why pascal? I know it and I like it. Other reasons are that mikroElektronika has plans of porting it under Linux (work in progress) and because I'm learning also Jal, a good, complete and free solution for Win/Linux.

... and with 12F629 (see download link) ...

The project

This is "Work In Progress"! This project will be disponible in many languages: mikroPascal, mikroBasic, mikroC, Jal v.2, Micro Pascal v.1.2.3, PicBasic. The schematics will be disponible in ExpressPCB, Eagle, KiCAD

The stepper

The board will drive a unipolar stepper motor in half steps and, after many days of research on internet, I found good references but for old microcontrollers as 12C508A or controllers with more than 8 pins. Anyway, still a good start. I found programs in assembler and Jal language. A very good reference about stepper motors is a web page of Douglas W. Jones at THE UNIVERSITY OF IOWA Department of Computer Science. And many other places on the web. All references will be mentioned at the end of this article.

The CNC Software

TurboCNC is the main target as a CAM software and, the schematic and microcontroller software will follow the Dakeng rules.

The mikroPascal program

See the download link for source files.

Schematic and pcb

Click to enlarge Here, the ExpressPCB files (also for Eagle and KiCAD will be available at the download link):

And, virtual image of the board (obtained with Eagle, Eagle3D and POVRay):

Eagle schematics are available for download.

Current limiter

I strongly recommend the use of this current limiter, instead of expensive power resistors.

Other languages

When the board will be successfully tested, other languages (with hex file as well) will be available for download. Until now, a Jal v.2 program is ready, but not yet available for download. Is amazing how little is the size of hex file generated by Jal, comparing with mikroPascal ...

"Burning" the chip

As a programmer, I have done this circuit and now I have the opportunity to test it.


On a breadboard, with LED's instead of stepper coils and switches instead of computer pulses. It works!

Tested the program with a mouse microswitch and a capacitor for filtering the sparks.

The final testing board is almost ready (not all connections are made) but I will not transfer the PIC until the test with a pulse generator (be it the PC or BE555). And I want to test other compilers ...

Code size comparison - different compilers
The smallest code is not always the fastest but it matter when is about 8 pin devices, with small memory space.
1. JAL v.2.4i - Jallib 0.2.0
Code area: 104 bytes
Data area: 7 bytes
Hex file size: 632 bytes

2. JAL v.2.4i - Bert van Dam SP 1.53
Code area: 105 bytes
Data area: 8 bytes
Hex file size: 649 bytes

3. mikroPascal PRO 2009
Used ROM: 107 bytes
Used RAM: 6 bytes
Hex file size: 673 bytes

4. mikroC PRO 2009
Used ROM: 108 bytes
Used RAM: 7 bytes
Hex file size: 677 bytes

Final results

Not yet... The logic is ok. I wrote the same program in a few versions of Jal language with different sets of libraries and I am quite happy with the result. I think I will continue in Jal language because have no limitations and have better optimisation (I will provide all the sources at the end - not big deal anyway).


If you want to make this project, you are on your own. I mean, I'm not responsible at all.


This article will be updated as per progress.


Until now...

- Drawing schematics in ExpressSCH (comparing to Eagle and KiCAD) is a nightmare and the result suffer in quality. However, working with ExpressPCB in designing boards is at hand.

- Programing in mikroPascal can be a pleasure, specially for Borland Pascal 7.0/Delphy programmers. It have the best IDE from the market. And they produce the best development boards for microcontrollers. The free version is good enough for MCU's with up to 2Ko of memory. Too bad it is not well optimised... (mean time, mikroElectronica has a new version of mikroPascal, the professional version, available for free with the same limitations about code size but better optimisations - not tested yet...). Update April 22, 2009: Tried the Pro version and I obtained the smallest code.

- Is a pain to work with Jal at first try... The documentation is poor and Yahoo Jallist discussion group is hard to browse. I will not recomend it for beginners. But is my favourite for now.

- Having a breadboard is helpful when you design/test your program. But I really need a development board (and thanks God, I did it). And I think I will develop one for 12f629, 12f675, 12f683 pic's.

Download links

Here you can find all files related to this project

Web References

- mikroElektronika - the site of mikroPascal, mikroBasic, mikroC
- Douglas W. Jones - stepper motors
- DAK Engineering - the home of TurboCNC and stepper controller
- CNC Mill - first good project in Jal v.2
- 3 axis Pic - this one started all - old chip, 12C508A
- ExpressPCB - the page of schematic and pcb design software (I'm using an older version, for Win98)
- PIC 12F675 Microcontroller Tutorial - the best p12f675 tutorial


April 19, 2009:
- Changed the code. Hex file is much more smaller.

April 22, 2009:
- Changed the code again - optimized at maximum. I installed the PRO 2009 version of mikroPascal and I obtained the small code size until now.

April 23, 2009:
- Finished testing other compilers. JAL v.2.4i is the first, seconded by mikroPascal PRO 2009 and mikroC 2009. I like all. All code can be downloaded.


If anyone made this controller, please comment here. Thank you!

User builds

12f675,12f629 half step driver
If you live in Czech Republic, you can order it from here. Made by jyrry site. Congratulations! More courage on reporting this guys, I like to have them as collection...