control DC motor using PIC16F877A

Discussion in 'Programmer's Corner' started by amsyar, Mar 10, 2011.

  1. amsyar

    Thread Starter New Member

    Mar 10, 2011
    1
    0
    hi everyone.I am a student in local university in Malaysia. I have a problem regarding on how to control DC motor using PIC16F877A. I am using MPLAB software. Actually I am trying to control speed of the motor using PWM. Up until now I only manage to write code as below. When I download it into PIC, the motor can not work. Please somebody help me to solve my problem. My coding is as below

    #include <pic.h>
    __CONFIG (0x3F32);
    #define sw1 RB0
    #define ssL C1OUT
    #define led1 RB6
    #define led2 RB7
    #define buzzer RC3
    #define motor_ra RC4
    #define motor_la RC7
    #define pwml CCPR1L
    #define pwmr CCPR2L



    // Function Prototype
    //***************************************************************************

    void init(void);
    void delay(unsigned long data);
    void forward(void);
    void reverse(void);
    void motor_run(void);
    void stop(void);

    // Main
    //***************************************************************************

    void main(void)
    {
    unsigned i;

    init(); // call initialization function
    buzzer = 0; // buzzer OFF
    led1 = 1;
    led2 = 1;
    stop(); // robot stop
    pwml = 200; // set pwm left motor = 200
    pwmr = 200; // set pwm right motor = 200

    while(1) // infinite loop
    {
    if(sw1==0) // sw1 pushed?
    {
    while(!sw1);
    while(1)
    { // if pushed
    for(i=0;i<2;i++) // buzzer ON 2 times
    {
    buzzer = 1;
    delay(10000);
    buzzer = 0;
    delay(10000);
    }
    motor_run(); // call motor run function
    }
    }
    else stop(); // if sw1 not pushed, robot stop
    }
    }

    // PIC Initialization Function Routine
    //***************************************************************************

    void init()
    {

    PR2 = 255; // set period register
    T2CON = 0b00000100; // set prescale 1
    CCP1CON = 0b00001100; // config for RC1 to generate PWM

    TRISE = 0b000; // set PORTE I/O direction
    }

    void delay(unsigned long data) // delay function
    {
    for(;data>0;data--); // delay depend on the data given
    }

    void motor_run(void) // motor_run function
    {
    unsigned n = 0;
    forward(); // motor forward

    while(n==0)
    {

    }
    }
    // Description : Motor functions
    //============================================================================================================

    void forward(void) // forward function
    {
    motor_ra = 1;
    motor_la = 1;

    }

    void stop(void) // stop function
    {
    motor_ra = 1;
    motor_la = 1;

    }
    void reverse(void)
    {
    motor_ra = 0;
    motor_la = 0;

    }
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
Loading...