[HELP] Any MPLAB experts here?

rollanddie

Supremacy Member
Joined
Aug 15, 2012
Messages
5,343
Reaction score
8
#include <p18f4520.h>
#include <delays.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF

#define S0 PORTCbits.RC0
#define S1 PORTCbits.RC1
#define S2 PORTCbits.RC2

void main(void)
{
TRISD = 0x00;
TRISC = 0xff;
PORTD = 0b00001111;


while(1)
{PORTD = 0x00;
if((S0==1)&&(S1==0)&&(S2==0))
{
PORTD = 0b00000001;
//led
(Motor 1 to run)
}
else if((S0==0)&&(S1==1)&&(S2==0))
{
PORTD = 0b00000011;
//led
(Motor 1&2 to run)
}
else if((S0==1)&&(S1==1)&&(S2==0))
{
PORTD = 0b00000111;
//led
(Motor 1&2&3 to run)
}
else if((S0==0)&&(S1==0)&&(S2==1))
{
PORTD = 0b00001111;
//led
(Motor 1&2&3&4 to run)
}
}
}

Any experts can teach me how to add 4 motors running concurrently at the same time?

The number of LEDs lights up depends on the number of motors running.

e.g like the program above in bold

Any inputs appreciated please! :o
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
-- SNIP ORIGINAl CODE --

Any experts can teach me how to add 4 motors running concurrently at the same time?

The number of LEDs lights up depends on the number of motors running.

e.g like the program above in bold

Any inputs appreciated please! :o

Code:
#include <p18f4520.h>
#include <delays.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
#define S0 PORTCbits.RC0
#define S1 PORTCbits.RC1
#define S2 PORTCbits.RC2
void main(void)
{
    TRISD = 0x00;
    TRISC = 0xff;
    PORTD = 0b00001111;
    while(1)
    {PORTD = 0x00;
        if((S0==1)&&(S1==0)&&(S2==0))
        {
            PORTD = 0b00000001;
            (Motor 1 to run)
        }
        else if((S0==0)&&(S1==1)&&(S2==0))
        {
            PORTD = 0b00000011;
            (Motor 1&2 to run)
        }
        else if((S0==1)&&(S1==1)&&(S2==0))
        {
            PORTD = 0b00000111;
            (Motor 1&2&3 to run)
        }
        else if((S0==0)&&(S1==0)&&(S2==1))
        {
            PORTD = 0b00001111;
            (Motor 1&2&3&4 to run)
        }
    }
}

I am no MPLAB expert, so I will just advice you on what I can, until someone with direct experience on this to further advise you.

First of all, I think your code can more compact as follows

Instead of comparing S0, S1, S2 in such laborious manner, consider compacting them using bitwise operators into an integer and compare using integral operations instead.

Code:
int COMBI = S2 << 2 | S1 << 1 | S0;

Hence

if((S0==1)&&(S1==1)&&(S2==0))

can be replaced as

if (COMBI == 3) since bitwise [S2][S1][S0] will give you 011 which is interpreted as 3 as an integer.

I'm not sure what your PORTD should correspond to, but the way you segregate your conditions can be more exclusive.

Each IF case should tackle only 1 motor, so logics will be clearer if it's

IF (MOTOR 1 CASE) { ... }
IF (MOTOR 2 CASE) { ... }
...
IF (MOTOR N CASE) { ... }

Again using bitwise operations,

the shared PORTD can be affected as

PORTD = 0;
PORTD = PORTD | 1 << MOTOR;

For Motor N, let MOTOR variable be (N-1);

So if you are trying to on MOTOR 3 and 4,

PORTD = PORTD | 1 << 2;
PORTD = PORTD | 1 << 3;

That should set the following bits in PORTD, assuming a 16bits integer

0000000000001100 --> 12

As for your codes, I don't think your PORTD is for motor, it is likely for LED lights.

i did a look up on motors, there are 2 types I knew. One is PWM stepper motor, the other is an ON/OFF type with a feedback mechanism to let you know which angle the motor is aligned to. The technique to command these 2 types of motors are different. Which one are you using ?
 

rollanddie

Supremacy Member
Joined
Aug 15, 2012
Messages
5,343
Reaction score
8
Can I control the motor with the switch?

Then LED to light up depending on how many motor is running.

Am trying to do this
(Motor 1 to run) (Motor 1&2 to run) (Motor 1&2&3 to run) (Motor 1&2&3 to run)

Sorry I'm quite new to this programming. Thanks for your help!

Sent from cramped red dot using GAGT
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
Can I control the motor with the switch?

Then LED to light up depending on how many motor is running.

Am trying to do this
(Motor 1 to run) (Motor 1&2 to run) (Motor 1&2&3 to run) (Motor 1&2&3 to run)

Sorry I'm quite new to this programming. Thanks for your help!

Sent from cramped red dot using GAGT

setup the circuit above as kh_chew has advise you. The transitor is your switch. The motor is supplied using a 5V Input, since your micro controller circuitry on 3.3V and insufficient current may not be sufficient to power the motor

You don't have to control both your LED and motor using the same circuitry and ports
 

rollanddie

Supremacy Member
Joined
Aug 15, 2012
Messages
5,343
Reaction score
8
AGY22COl.jpg

Using these MCU and dc motor.

ZGLrVIYl.jpg

My program is like this as of now, have not include the dc motors in yet. PORTC is for switches and PORTD for LEDs.

Any idea how do I go about including the dc motors in my program?

Sent from cramped red dot using GAGT
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,299
Don't seem to work le. Damn sian....

Sent from cramped red dot using GAGT

I think you can be more helpful to yourself. would others know what actually doesn't work for you?

use voltage meter and measure your output. see what output you are getting as you run your program. draw us a diagram how u connect your circuits.l and also what you wrote for your codes
 

jacktan87

Junior Member
Joined
Sep 16, 2015
Messages
7
Reaction score
0
err do you have a circuit diagram for your project?

for motor please add a fly back diode if not u will burn your mcu

I don't think it is advisable to use led as your input... for a mcu u have so many port... use other to port control the lighting and motor ba...

for mplab u need to define the purpose of the port as either source or sink....

sorry... it been a very Long time I use 452 and 4520 Liao.... please refer to the data sheet for the configuration
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top