MicroC Code Problem in DC controller

Status
Not open for further replies.

ishah

New Member
Hi, Can someone help me on the code push button. I'm doing the DC motor interface with PIC16f877A. I want the button to control all the movement (forward, reverse and stop). Current situation for my code,when I push the forward button, 2 motor will run forward and vice verse for the reverse button.

But I want is, when I keep pushing the forward button,the motor will move. and when i release the button .. the motor will stop.. so i dont need the stop button to control.

Thanks and here is the current code


Code:
/*
    This source code is use to control the 2 motor movement by select
    the appropriate state button (Forward and Reverse).
*/


//define motor1 ports
#define motor1a PORTD.F0
#define motor1b PORTD.F1


//define motor2 ports
#define motor2a PORTD.F2
#define motor2b PORTD.F3


//define controller 1 ports
#define switch_frwd PORTB.F1
#define switch_stop PORTB.F2
#define switch_rvs PORTB.F0



void main()
{
//set I/O port
   TRISB = 0XFF;
   TRISD = 0X00;
   CMCON=0X07; //      or CMCON register = 0X00000111
   ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

   switch_frwd = 0X00;   //set initial port = 0
   switch_frwd = 0X00;   //set initial port = 0
   switch_frwd = 0X00;   //set initial port = 0

while (1)
 {
   if(switch_frwd == 1)    //If the switch is pressed
      {
         motor1a  = 0;
         motor1b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_rvs == 1)    //If the switch is pressed
      {
         motor1a  = 1;
         motor1b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_stop == 1)    //If the switch is pressed
      {
         motor1a  = 0;
         motor1b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
      }
   if(switch_frwd == 1)    //If the switch is pressed
      {
         motor2a  = 0;
         motor2b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_rvs == 1)    //If the switch is pressed
      {
         motor2a  = 1;
         motor2b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_stop == 1)    //If the switch is pressed
      {
         motor2a  = 0;
         motor2b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
      }
   }
}
 
Just remove the two lines that say " if (switch_stop == 1) //If the switch is pressed"

i.e.
Code:
/*
    This source code is use to control the 2 motor movement by select
    the appropriate state button (Forward and Reverse).
*/


//define motor1 ports
#define motor1a PORTD.F0
#define motor1b PORTD.F1


//define motor2 ports
#define motor2a PORTD.F2
#define motor2b PORTD.F3


//define controller 1 ports
#define switch_frwd PORTB.F1
#define switch_stop PORTB.F2
#define switch_rvs PORTB.F0



void main()
{
//set I/O port
   TRISB = 0XFF;
   TRISD = 0X00;
   CMCON=0X07; //      or CMCON register = 0X00000111
   ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

   switch_frwd = 0X00;   //set initial port = 0
   switch_frwd = 0X00;   //set initial port = 0
   switch_frwd = 0X00;   //set initial port = 0

while (1)
 {
   if(switch_frwd == 1)    //If the switch is pressed
      {
         motor1a  = 0;
         motor1b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_rvs == 1)    //If the switch is pressed
      {
         motor1a  = 1;
         motor1b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
      }
   else
      {
         motor1a  = 0;
         motor1b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
      }

   if(switch_frwd == 1)    //If the switch is pressed
      {
         motor2a  = 0;
         motor2b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_rvs == 1)    //If the switch is pressed
      {
         motor2a  = 1;
         motor2b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
      }
   else
      {
         motor2a  = 0;
         motor2b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
      }
   }
}
 
switch_frwd = 0X00; //set initial port = 0

Why Three times?


Learn how to use a state transition diagram as apart from a flow chart and your answer will drop out easily.

I know we all write code differently, but a switch statement would make it much easier to read.

At a quick look, have you catered for don't care states where both are pressed together.
 

Sorry, Im newbie in this field, it is a typing error. I wrote the new one.

Code:
/*
    This source code is use to control the 2 motor movement by select
    the appropriate state button (Forward and Reverse).
*/


//define motor1 ports
#define motor1a PORTD.F0
#define motor1b PORTD.F1


//define motor2 ports
#define motor2a PORTD.F2
#define motor2b PORTD.F3


//define controller 1 ports
#define switch_frwd PORTB.F1
#define switch_rvs PORTB.F0



void main()
{
//set I/O port
   TRISB = 0XFF;
   TRISD = 0X00;
   CMCON=0X07; //      or CMCON register = 0X00000111
   ADCON1=0X07; //     Configure ADCON1, or ADCON1=0X06;

   switch_frwd = 0X00;   //set initial port = 0
   switch_rvs = 0X00;   //set initial port = 0


while (1)
 {
   if(switch_frwd == 1)    //If the switch is pressed
      {
         motor1a  = 0;
         motor1b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
         motor2a  = 0;
         motor2b  = 1;    //Motor Forward
         Delay_ms(30); //1 Second Delay
      }
   else
   if (switch_rvs == 1)    //If the switch is pressed
      {
         motor1a  = 1;
         motor1b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
         motor2a  = 1;
         motor2b  = 0;    //Motor Reverse
         Delay_ms(30); //1 Second Delay
      }
   else
      {
         motor1a  = 0;
         motor1b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
         motor2a  = 0;
         motor2b  = 0;    //Motor Stop
         Delay_ms(30); //1 Second Delay
      }

   }
}

Can you give example as your
At a quick look, have you catered for don't care states where both are pressed together.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…