Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Control the speed of dc motor using PIC16F877A

Status
Not open for further replies.

apizbaygon

Junior Member level 3
Junior Member level 3
Joined
Jan 5, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,431
hi..i just wanna try simple project which is to control the speed of dc motor using PIC16F877A..

my problem is at the coding because i'm not good in coding (MikroC)

here is my coding
Code:
void main()
{
  short duty  = 0; //initial value for duty

  TRISD = 0xFF; //PORTD as input
  TRISC = 0x00; //PORTC as output
  TRISB = 0x00; //PORTB as output
  PORTB = 0x02; //Run motor in anticlock wise

  PWM1_Init(1000);  //Initialize PWM1
  PWM1_Start();  //start PWM1
  PWM1_Set_Duty(10); //Set current duty for PWM1

  while (1)        // endless loop
  {
     if (!RD0_bit && duty<250) //if button on RD0 pressed
     {
        Delay_ms(40);
        duty = duty + 10;  //increment current_duty
        PWM1_Set_Duty(240);  //Change the duty cycle
     }
     if (!RD1_bit && duty >0) //button on RD1 pressed
     {
       Delay_ms(40);
       duty = duty - 10;  //decrement duty
       PWM1_Set_Duty(10);
     }
    Delay_ms(10);      // slow down change pace a little
  }
}

after compile it, it show the following errors:

- undeclared identifier [ PWM1_Set_Duty] in expression
- undeclared identifier [ RD0_bit] in expression
- undeclared identifier [ RD1_bit] in expression

how to solve this problem..
 

hi..i just wanna try simple project which is to control the speed of dc motor using PIC16F877A..

my problem is at the coding because i'm not good in coding (MikroC)

here is my coding
Code:
void main()
{
  short duty  = 0; //initial value for duty

  TRISD = 0xFF; //PORTD as input
  TRISC = 0x00; //PORTC as output
  TRISB = 0x00; //PORTB as output
  PORTB = 0x02; //Run motor in anticlock wise

  PWM1_Init(1000);  //Initialize PWM1
  PWM1_Start();  //start PWM1
  PWM1_Set_Duty(10); //Set current duty for PWM1

  while (1)        // endless loop
  {
     if (!RD0_bit && duty<250) //if button on RD0 pressed
     {
        Delay_ms(40);
        duty = duty + 10;  //increment current_duty
        PWM1_Set_Duty(240);  //Change the duty cycle
     }
     if (!RD1_bit && duty >0) //button on RD1 pressed
     {
       Delay_ms(40);
       duty = duty - 10;  //decrement duty
       PWM1_Set_Duty(10);
     }
    Delay_ms(10);      // slow down change pace a little
  }
}

after compile it, it show the following errors:

- undeclared identifier [ PWM1_Set_Duty] in expression
- undeclared identifier [ RD0_bit] in expression
- undeclared identifier [ RD1_bit] in expression

how to solve this problem..

make sure pwm library is selected in your project. simply open library manager window which is in right side of the screen & check wether pwm library is checked there. you can access port d pins using following commands.
bit 0 - PORTD.F0
bit1 - PORTD.F1
 

Hi,

Can anyone help me on MicroC code.
Im using a PIC16F877A with L298 to control a 12V DC Motor.
I want to control the speed of the motor by PWM.
I already draw a circuit with external OSC 20MHz .

Code:
unsigned short current_duty, current_duty1;

//define motor1 ports
sbit motor1a at RD0_bit;
sbit motor1b at RD1_bit;


//define motor2 ports
sbit motor2a at RD2_bit;
sbit motor2b at RD3_bit;


//define controller 1 ports
#define increase PORTB.F1
#define decrease PORTB.F0
#define forward PORTB.F2
#define reverse PORTB.F3

void InitMain() {
  PORTA = 255;        //100% PWM duty ratio
  TRISA = 255;
  TRISB = 0XFF;       // configure PORTB pins as input
  PORTB = 0;
  TRISD = 0X00;        // configure PORTD pins as output
  PORTD = 0;
  TRISC = 0X00;       // configure PORTC pins as output
  PORTC = 0;

  CMCON=0X07; //      or CMCON register = 0X00000111
  ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;
  CCP1CON = 12;     //PWM mode = 0;
  CCP2CON = 12;     //PWM mode = 0;

  PWM1_Init(5000);                    // Initialize PWM1 module at 5KHz
  PWM2_Init(5000);                    // Initialize PWM2 module at 5KHz
}

void main() {
  InitMain();
  current_duty  = 0X00;                 // initial value for current_duty
  current_duty1 = 0X00;                 // initial value for current_duty1

  PWM1_Start();                       // start PWM1
  PWM2_Start();                       // start PWM2
  PWM1_Set_Duty(current_duty);        // Set current duty for PWM1
  PWM2_Set_Duty(current_duty1);       // Set current duty for PWM2


while (1) {                         // endless loop

    if (forward == 1) {    // button on RB0 pressed
      motor1a  = 0;
      motor1b  = 1;    //Motor Forward
      Delay_ms(500);    //1 Second Delay
      current_duty++;                 // increment current_duty
      PWM1_Set_Duty(127);
     }

    if (reverse == 1) {      // button on RB1 pressed
      motor2a  = 1;
      motor2b  = 0;     //Motor Reverse
      Delay_ms(30);     //1 Second Delay
      PWM1_Set_Duty(127);
     }

    if (increase == 1) {            // button on RB2 pressed
      Delay_ms(40);
      current_duty1++;                // increment current_duty1
      PWM2_Set_Duty(127);
     }

    if (decrease == 1) {                    // button on RB3 pressed
      Delay_ms(40);
      current_duty1--;                // decrement current_duty1
      PWM2_Set_Duty(127);
     }

    Delay_ms(5);                      // slow down change pace a little
  }
}


**broken link removed**
 

Hi All,

Based on my question, when I simulate by using Proteus, nothing happen to my both DC motor. Its does not work.

I want my motor to be run forward and reverse movement, also when the user want to turn right, motor 1 will slow while motor 2 will going faster. It be vice verse when the user want to turn left.

anyone can help me on the MicroC code.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top