+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Member level 1
    Points: 477, Level: 4

    Join Date
    Mar 2018
    Posts
    40
    Helped
    0 / 0
    Points
    477
    Level
    4

    Need help to understand code for DC motor

    someone can tell me whats happening in the code as I can understand it has written to control the speed of DC motor

    Code:
    /********************************
    FILE NAME:        dcmotor.c
    CHIP TYPE:        AT89C51
    CLOCK FREQUENCY:  12MHZ
    IDE:              VSMStudio
    COMPILER:         IAR for 8051
    TIME:             September 2010
    ********************************/
    
    #include "ioAT89C51.h"
    #include "intrinsics.h"
    
    // Define P3 pins
    #define Inc   (P3_bit.P3_4) 
    #define Dec   (P3_bit.P3_5)
    #define Dir   (P3_bit.P3_6) 
    #define PWM   (P3_bit.P3_7)
    
    // Define new types
    typedef unsigned char   uchar;
    typedef unsigned int    uint;
    
    void delay(uint);
    
    void main(void)
     { int speed;
       // Select initial direction and speed.
       Dir = 1;
       if (Dir)
          speed = 400;
       else
          speed = 100;
       
       // Main control loop
       while(1)
        { if(!Inc)
          // Increase speed   
             speed = speed > 0 ? speed - 1 : 0;
          if(!Dec)
          // Decrease speed
             speed = speed < 500 ? speed + 1 : 500;
          
          // Drive a PWM signal out. 
          PWM=1;
          delay(speed);
          PWM=0;
          delay(500-speed);
        }
     }
    
    void delay(uint j)
     { for(; j>0; j--)
        { __no_operation();
        }
     }

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 78,377, Level: 68
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    15,897
    Helped
    3607 / 3607
    Points
    78,377
    Level
    68

    Re: Need help to understand code for DC motor

    Hi,

    Please don't ask that global questions. The forum can't replace scool or tutorials.

    Please tell us what you understand so far.
    Then ask a detailed question about what is not clear to you.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--