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.

How to control more than one port in pic microcontroller?

Status
Not open for further replies.

00100

New Member
HELLO ,
I want to control two ports (or more) in pic microcontroller in the same time without any shift delay between them for all time (infinite loop).
for example I want (portb) to give this sequence:
{portb=1;delay(100s);portb=2;delay(100s);portb=4;delay(100s)}
and at the same time portc :
{portc=1;delay(700s);portb=2;delay(700s);portb=4;delay(700s)}
I want this to control more than one stepper motor with different rpm in the same time
I use pic16f877a if this function does not exist in this pic please tell me for another one.
Thanks in advance:eek:
 
700s for stepper? looks a little weird. Anyway, you can´t set two different ports closer than 1 instruction cycle apart, which would be around 50ns apart. Is that close enough? It is many orders of magnitude shorter than the delay, so I bet the motors will happily work with it.
 
Yes, it's ludicrous to specify 'no shift delay' - it would be EXTREMELY unusual if that was a requirement, and certainly anything mechanical (such as a motor) doesn't care within VERY wide parameters.
 
{portb=1;delay(100s);portb=2;delay(100s);portb=4;delay(100s)}
and at the same time portc :
{portc=1;delay(700s);portb=2;delay(700s);portb=4;delay(700s)}

You have different delay values between port transitions, but you don't want any delay between them? I'm confused.

If you really want zero delay, you need to use a shift register circuit, or something similar, to first store the new values and then synchronously enable them with one control pulse.

But I agree with previous posts. You do not need to "align" the pulses with stepper motors that accurately. Different friction etc. between the motors will cause hundred times more "delay" between them than a few microsecond delay in the control pulses.. and you would also need to synchronize your power amplifiers. Little more capacitance/resistance in the other amplifier input will cause the pulse to delay.
 
Last edited:
Anyway, you can´t set two different ports closer than 1 instruction cycle apart, which would be around 50ns apart.

Actually, at the fastest speed that you can clock a 16F877A (20Mhz) it would allow for an instruction clock of 200nS (PIC instruction clock is Fosc / 4).
 
Last edited:
00100, you're asking how to drive two stepper motors at the same time but at different rates, correct?
 
Last edited:
00100, you're asking how to drive two stepper motors at the same time but at different rates, correct?

Good question, after reading your post and then re-reading the original I think the first three to answer misunderstood the question.
 
Yes I know I can make a very short delay between every instruction but this method will be very complex and hard to deal with when I want to change the values of motors speed alot of times
I heard about pwm function which is internal the pic but I do not know how to use it
 
Yes I know I can make a very short delay between every instruction but this method will be very complex and hard to deal with when I want to change the values of motors speed alot of times
I heard about pwm function which is internal the pic but I do not know how to use it

I don't see how that would help you?.

In exactly what manner do you want the stepper motors to move?.
 
PWM is simple.

The timer 2 prescaler and the period register (register PR2) will set the period of the PWM signal, which in turn sets the frequency of the PWM signal. Writing a value to CCP1RL changes the duty cycle, which changes the average voltage output. CCPR1L contains the 8 MSBs of the duty cycle while bits 4 and 5 in CCP1CON are the two LSBs for a 10-bit resolution.

Changing the duty cycle by modifying the value written to CCPR1L changes the average voltage output. The lower the duty cycle of the PWM signal, the lower the average voltage output.

Quite honestly, to drive an H-bridge driven motor with PWM you need 4 PWM outputs. The 16F877A only has 2. The 16F887 has an enhanced PWM module with 4 PWM outputs for this exact purpose.
 
Last edited:
Quite honestly, to drive an H-bridge driven motor with PWM you need 4 PWM outputs. The 16F877A only has 2. The 16F887 has an enhanced PWM module with 4 PWM outputs for this exact purpose.

He drives two stepper motors. And honestly, you need only one PWM signal to drive one H-bridge.
 
I think Jon means 4 without using a driver chip.....

I have never seen an H-bridge driven by 4 PWM signals. Why would you generate the same signal 4 times?
 
No... The PWM module inside the pics (extended) uses four outputs with full PWM control ... There are three modes one output or half / full bridge mode.

Still.. it is crazy to use 4 microcontroller outputs to drive single H-bridge. I have never seen that stupid design. Sorry for off-topic, I'll leave it here.
 
Still.. it is crazy to use 4 microcontroller outputs to drive single H-bridge. I have never seen that stupid design. Sorry for off-topic, I'll leave it here.

I agree.. but if they are doing nothing else.... Why not ! saves the price of a dedicated driver.
 
Last edited:
As the OP is talking about delays of 100 and 700 Seconds, the PWM module is useless.

I suggest setting up Timer 2 to give a period of 10mS and use that to keep time of the delays.

Mike.
 
Still.. it is crazy to use 4 microcontroller outputs to drive single H-bridge. I have never seen that stupid design. Sorry for off-topic, I'll leave it here.
You are right ,this is the main problem: that I cannot use one controller to control each channel of the H-bridge (impractical way)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top