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.

Motor polarity control on two pic pins

Status
Not open for further replies.

vanaardep

New Member
Hi there
How would i control the polarity of two pins on a pic 16F628? ie pin A is + and pin B is negative and then switching them around. I Have no idea of how to code this, does someone have an example mabey:confused:
 
Just BSF the pin you want high, and BCF the pin you want low.

Here are some examples from my PWM 'tutorial'.

Code:
ReverseL:
	BSF	PORTC, RL	;set pins for reverse
	BCF	PORTC, FL
	RETURN

ReverseR:
	BSF	PORTC, RR
	BCF	PORTC, FR
	RETURN

ForwardL:
	BCF	PORTC, RL	;set pins for forward
	BSF	PORTC, FL
	RETURN

ForwardR:
	BCF	PORTC, RR
	BSF	PORTC, FR
	RETURN
 
We need to know a little more about what you have to work with and what in particular you need help with in this project.

Code could be something like this in C. Hook one leg of the motor to a pin on PORTA and one leg to a pin on PORTB...of course you're going to want some sort of buffer in between so your PIC isn't providing the power directly and experiencing any sort of back current from the motor...

Code:
PORTA=0xFF;  //set PORTA to output +5V
PORTB=0xFF;  //set PORTB to output +5V
while(1)
{
  TRISA=0x00;  //set PORTA to be outputs
  TRISB=0xFF;  //set PORTB to be inputs
  delay(100);  //some sort of time delay
  TRISB=0x00;  //set PORTB to be outputs
  TRISA=0xFF;  //set PORTA to be inputs
  delay(100);  //some sort of time delay
}
 
bleh I'm stupid, yeah just toggle back and forth between output of 1 and 0...not set them to inputs and whatnot, too much beer at lunch
 
What sort of motor are we looking at controlling with the PIC?

Anything required to do some work is going to need some form of driver or H-bridge, or something similar, in order to provide bi-directional control.

Two pins can be used to control two transistors, energizing two relay coils. One pin triggers one relay coil, the other pin triggers the other relay coil.

The relay contacts are wired as below:

NC connected to Gnd
NO connected to supply
COM connected to each motor wire

Trigger 1st relay only and NO connects COM to supply, turning motor in one direction.
Trigger 2nd relay only and NO connects COM to supply, turning motor in other direction.

Trigger neither relay, or both relays and no current flows - no motor movement.
 
Just BSF the pin you want high, and BCF the pin you want low.

Here are some examples from my PWM 'tutorial'.

Code:
ReverseL:
	BSF	PORTC, RL	;set pins for reverse
	BCF	PORTC, FL
	RETURN

ReverseR:
	BSF	PORTC, RR
	BCF	PORTC, FR
	RETURN

ForwardL:
	BCF	PORTC, RL	;set pins for forward
	BSF	PORTC, FL
	RETURN

ForwardR:
	BCF	PORTC, RR
	BSF	PORTC, FR
	RETURN

Nigel, isn't that leaving you wide open to the "read-modify-write" problem? At all high clock speeds I would have always added at least one line of code between subsequent bitwise operations on the same port.

Code:
ReverseL:
	BSF	PORTC, RL	;set pins for reverse
	nop
	BCF	PORTC, FL
	RETURN

I know that I have only observed the problem once when I didn't have the nop in there, but it took me hours to work out what was going on. I now always add a nop or move the code around a bit to avoid the risk. I really don't know how necessary that is.

On the higher series of PICs it's possible to write to the LAT registers, which also gets over the problem.
 
well the motor im using is tiny, its 1.2 volt and needs only 40ma, so idont think that an h bridge is neccesary
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top