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.

Basic training

Status
Not open for further replies.

Totally blank

New Member
Hi
As my last post fell flat on its face, I have searche dthe web to no avail.

I want to create/programme a PIC16F84a to run a stepper motor. I still cant find how to do it properly, so far I understand you need to generate a HEX file to upload to the PIC, I have no problem communicating/uploading to the PIC, my problems are writing the programme, heres my questions.

what is the process for writing a file to run the motor

does there need to be a SET chunk of code at the top of the file

using MPLAB can I write/edit the code and export it or must it be done in C

I just can't find any straight forward instructions, please help
 
Totally blank said:
Hi
As my last post fell flat on its face, I have searche dthe web to no avail.

I want to create/programme a PIC16F84a to run a stepper motor. I still cant find how to do it properly, so far I understand you need to generate a HEX file to upload to the PIC, I have no problem communicating/uploading to the PIC, my problems are writing the programme, heres my questions.

what is the process for writing a file to run the motor

does there need to be a SET chunk of code at the top of the file

using MPLAB can I write/edit the code and export it or must it be done in C

I just can't find any straight forward instructions, please help

hi,
This program is for a stepper, written in assembler, it may give you some pointers.

You can download a 30 day trial version from www.oshonsoft.com of a PIC Simulator, this program will run on the simulator, also there is a working simulation of a stepper mtr.
 
Last edited:
There are a number of ways to run stepper motors. The easiest model to get your arms around is the 4 phase unipolar model. In this model there are four coils also known as phases. Call them A, B, C, and D. There are two extra wires connected as center taps between coils A/B and coils C/D. The center tap wires are connected to a positive supply voltage. The phases are connected to grounded switches. A phase that is "on" draws current from the positive supply, through the coil, through the switch, to ground. A phase that is "open" draws no current, and must be equal to the value of the positive supply.

Only certain combinations of phases being "on" and "off" are meaningful. There are four possible ways to have on phase turned on; call them A, B, C, and D. This is four things taken one at a time. There are six possible ways to have two phases on at the same time. This is four things taken two at a time. From these six we eliminate the case where A and B are on at the same time, and we eliminate the case where C and D are on at the same time.

This leaves us with eight cases of one or two phases on at a time. If you output one of the eight cases to the motor, the rotor will go to an "initial" position from whereever it is. It may go clockwise, it may go counterclockwise, we just don't know where it started. Once this initial position is established we can determine the set of states that will make the shaft move clockwise or counter-clockwise as follows:

Change the value of one phase to get to the next state.
Code:
 A  B  C  D
-----------
 1  0  1  0    
 1  0  0  0
 1  0  0  1
 0  0  0  1
 0  1  0  1
 0  1  0  0
 0  1  1  0
 0  0  1  0
When you get to the end of the table go back to the beginning. Notice also that each column is a shifted version of each of the adjacent columns. This suggests that a single bi-directional 8-bit shft register with the serial output looped back to the serial input makes a dandy stepper motor state machine. You can implement this shift register in a PIC just as you would in hardware.

Checkout the state diagram on page 5 of the following datasheet
https://www.st.com/stonline/products/literature/ds/1334/l297.pdf
 
Last edited:
Papabravo has given you a good information on how to get a stepper motor to work.
But to me, it seems you may be confused regarding the process of actually programming the microcontroller.

You are correct about needing to generate a HEX file that later, you put onto the chip (and the instructions are then executed, line-by-line, by the microcontroller).

Since it is very difficult to actually write code in ones and zeros (for humans, anyways), most of the people here write in a language called assembly (more specifically in this case, PIC Assembly) that has instructions such as "SET" or "MOVE" (thus more understandable to humans). Once the code is written, it is converted into HEX, and a file is generated.
You would then take that HEX file, and program the microcontroller.

However, if you know C or BASIC for example, there are many compilers out there that let you specifically write code in those languages, convert the code to assembly, and then using an external program (such as MPASM)... convert the code into a HEX file.

I use the latter method, using a compiler called "Great Cow Basic".
http://gcbasic.sourceforge.net/
It may not seem like much, but if you look around the website-- there is so much information regarding how to set up the compiler, how to start writing code, etc.

Through Great Cow Basic, you could simply set the specific ports to be either inputs or outputs (for the PIC16F84A), and then output logic HIGHs or LOWs to those specific pins which then would change the direction of the stepper motor. Papabravo has given some very good information upon which logic outputs would result in which directions.

MPLAB is an IDE that lets you write / edit code, debug / simulate it, and then to export it as you said. Compilers such as Great Cow Basic use the MPASM component of MPLAB to generate the HEX file, but let you write code freely through a language you are more comfortable with.
Hope this somewhat helps,
-Omar
 
Last edited:
@ scoobystu, thanks I had just read and done their lesson 4 this afternoon.

@ ericgibbs, thanks, but the last thing I need right now is more software to understand

@ papabravo, I have read that on the web, or something similar

@ omar.m, you got it, the process is what I didn't understand, I have MPLAB, I can read the .asm I am also able to output it to HEX and load it into the chip, the commands within the code are for humans to understand, I do understand some of the lines of code, a few things I woukd like to know are.

Example:
Code:
option_reg	equ	0x81

in the above, is option_reg user defined or must it be used from the asm dictionary? the same with equ and the HEX number, what does it mean and how is it reached, as the same number can appear numerous times in the code.

Example:
Code:
;bank 0
indf		equ	0x00		;indirect file register
tmr0		equ	0x01		;timer0 register
pcl		equ	0x02		;program counter - low byte
status		equ	0x03		;status register
fsr		equ	0x04		;file select register
porta		equ	0x05		;porta register
portb		equ	0x06		;portb register
reg_b	equ	0x0c
reg_a	equ	0x0d

And on a different script

        #DEFINE PORTA   0005                    ;PAGE 0
        #DEFINE PORTB   0006                    ;PAGE 0

What is needed at the start of a script, I have seen the above and items listed similar to the above, do they mean the same thing? I notice they both have porta as a 5 but why 0x05 + 0005 are they just different ways each script creator has chosen? again, as 0005 and 0x05 seem to mean the same, how are they reached? or what determines them

On some of the #DEFINE entries I see #DEFINE SWA1 (2, 3 & 4) in the same style following on below, can these be user defined or are they part o fthe asm dictionary, where can I find this info?

Is there any set order that splits the code into chunks or sections IE,

headers (containing processor 16f84a include <p16f84a.inc>)

DEFINE or settings piece (what determines this, are there MUST have components to go here)

Initialize PIC or preparing it for action (I noticed the clearing of ports or loading of registers etc, how do you determine this area, are there set entities that would have to be before any actual motor functions)

I am sure the penny will drop at some stage, thanks for the help so far.
 
hi,
I assume you are running MPLAB IDE V7.*

Use windows explorer and look in this directory

Program Files/microchip/Template/code

and you will lots of *.asm files, these are microchips templates, copy the one for the PIC you are working with and paste it into MPLAB.

Use it as the template for you study program.

Most of the questions you are asking are in the MPLAB help files also download Nigel's tutorials...

If you use the template for you PIC or

list p=16f877
include <p16f877.inc>

,you do not have to define the PORTs ie; Define PORTA 0005 etc
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top