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.

PIC16F877A - Problem regarding motordriver~

Status
Not open for further replies.

husni

New Member
Hi there,
Ive been confused on the connection of my motordriver, which i'm not sure the code given is appropriate for my motor driver to work.

1st, I will list down the pin configuration

Motor driver leg >> PIC leg

2[D1] >> 18[RC3]
7[D2] >> 20[RD1]

1[RC2/CCP1] >> 17
10[D3] >> 22[RD3]
15[D4] >> 27[RD4]
9[RC1/CCP2] >> 16

and the code used is:

Code:
/*
 * Project name:
     PWM_Motor
 * Description:
     This project drives motors with speed control (0-255) using PWM.
     It required L293B as the motor driver.
 * Configuration:
     RD1 connects to IN1 (LMotor)
     RD2 connects to IN2 (LMotor)
     RD3 connects to IN3 (RMotor)
     RD4 connects to IN4 (RMotor)
     RC2/CCP1 connects to EN1 (LMotor)
     RC1/CCP2 connects to EN2 (RMotor)
     OUT1 connects to LMotor
     OUT2 connects to LMotor
     OUT3 connects to RMotor
     OUT4 connects to RMotor
*/

void initialize(void);
void lmotorforward(unsigned char);
void lmotorbackward(unsigned char);
void lmotorstop(void);
void rmotorforward(unsigned char);
void rmotorbackward(unsigned char);
void rmotorstop(void);

void main() {
  initialize();		// Call initialization function
  PWM_Start();		// Start PWM for CCP1
  CCP2CON = 0x3C;	// Start PWM for CCP2

  while(1) {			// Loop forever
    lmotorforward(255);		// LMotor forward with 255 speed
    rmotorforward(255);		// RMotor forward with 255 speed
    Delay_ms(2000);		// Delay for 2s

    lmotorstop();		// LMotor stop
    rmotorstop();		// RMotor stop
    Delay_ms(2000);		// Delay for 2s

    lmotorbackward(150);	// LMotor backward with 255 speed
    rmotorbackward(150);	// RMotor backward with 255 speed
    Delay_ms(2000);		// Delay for 2s

    lmotorstop();		// LMotor stop
    rmotorstop();		// RMotor stop
    Delay_ms(2000);		// Delay for 2s
  }

}

void initialize() {	// Initialization function
  PORTD = 0;		// Set PORTD to low
  TRISD = 0;		// Set PORTD as output
  TRISC = 0;            // Set PORTC as output
  PWM_Init(5000);	// Initialize PWM module with 5k frequency
  CCP2CON = 0x30;
}

void lmotorforward(unsigned char speed) {	// LMotor Forward function
  Pwm_Change_Duty(speed);			// Set motor speed
  PORTC.F3=1;					// Control IN1
  PORTD.F1=0;					// Control IN2
}

void lmotorbackward(unsigned char speed) {	// LMotor Backward function
  Pwm_Change_Duty(speed);				// Set motor speed
  PORTC.F3=0;					// Control IN1
  PORTD.F1=1;					// Control IN2
}

void lmotorstop() {	// LMotor Stop function
  PORTC.F3=0;		// Control IN1
  PORTD.F1=0;		// Control IN2
}

void rmotorforward(unsigned char speed) {	// RMotor Forward function
  CCPR2L = speed;				// Set motor speed
  PORTD.F3=1;    				// Control IN3
  PORTD.F4=0;      				// Control IN4
}

void rmotorbackward(unsigned char speed) {	// RMotor Backward function
  CCPR2L = speed;				// Set motor speed
  PORTD.F3=0;					// Control IN3
  PORTD.F4=1;      				// Control IN4
}

void rmotorstop() {	// RMotor Stop function
  PORTD.F3=0;    	// Control IN3
  PORTD.F4=0;		// Control IN4
}

So, did u guys spot any problem there? Seems that my motor didn't responce at all.
 
yes i think


btw, the motor is connected to pic via motor driver.


PIC >> Motor driver >> Motor
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top