DC motor control Keil c51 source code

Publisher:oplndctkl出Latest update time:2016-08-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
DC motor open loop control Keil c51 source code

//-----------------------Function declaration, variable definition------------------------
#include
#include
#include  
//-----------------------Define pins----------------------------------
sbit PWM=P1^0; //PWM waveform output 
sbit DR=P1^1; //Direction control
#define timer_data (256-100) //Timer preset value, 12M clock, timing 0.1ms
#define PWM_T 100 //Define PWM period T as 10ms
unsigned char PWM_t; //PWM_t is pulse width (0~100) time is 0~10ms
unsigned char PWM_count; //Output PWM period count
unsigned char time_count; //Timing count
bit direction; //Direction flag bit
//-----------------------------------------------------------------
// Function name: timer_init
// Function function: Initialize facility timer
//-----------------------------------------------------------------
void timer_init()
{
      } //----------------------------------------------------------------- // Function name: setting_PWM // Function function: set PWM pulse width and direction //----------------------------------------------------------------- void setting_PWM() { if(PWM_count==0) //Initial       setting {
      PWM_t
      =        20; direction=1;       }       }          //
      -----------------------------------------------------------------      // Function name : IntTimer0 // Function function: timer interrupt handler // ----------------------------------------------------------------- void IntTimer0( ) interrupt 1 { time_count      ++;      DR=direction;      if(time_count>=PWM_T      )          { time_count      =          0      ;          PWM_count++;          setting_PWM(); //Called once for each PWM wave output      }      if(time_count













































//=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=·=

DC motor closed loop control Keil c51 source code

//-----------------------Function declaration, variable definition------------------------
#include
sbit INT_0 =P3^2; // Set p3.2 as external interrupt 0
sbit pulse_A=P1^2; // P1.2 is pulse A input
sbit PWM=P1^0; //PWM waveform output 
sbit DR=P1^1; //Direction control
//-----------------------Predefined values----------------------------------
#define PWM_T 1800 //Define PWM period T as 18ms
#define Ts 1000 //Define photoelectric encoder sampling time as 10ms
#define timer_data (256-10) //Timer preset value, 12M clock is, timing 0.01ms
//-----------------------Preset value----------------------------------
bit direction; //Direction flag bit User setting
unsigned char R; //DC motor speed to be obtained User setting
//-----------------------Actual running status------------------------------
bit real_direction; //Actual running direction of the motor   
unsigned char Rr; //Actual speed of the DC motor
//-----------------------Calculated compensation status--------------------------
bit compensate_polarity; //Compensation polarity
unsigned char dR; //Speed ​​compensation
//-----------------------Pulse width obtained after compensation------------------------
unsigned char PWM_t; //PWM_t is the pulse width (320~400) and the time is 3.2~4.0ms
unsigned char PWM_count; //Output PWM cycle count
//-----------------------Each intermediate count value------------------------------
unsigned char pulseB_count; //Pulse count
unsigned char time0_count; //Timer count
unsigned char time1_count; //Timer count
//-----------------------------------------------------------------
// Function name: timer_init
// Function function: Initialize and set the timer
//-----------------------------------------------------------------
void timer_init()
{
      TMOD=0x22; /*Timer 1 is working mode 2 (8-bit automatic reload), 0 is mode 2 (8-bit automatic reload) */
      PCON=0x00;
      TF0=0; 
      TH0=timer_data ; //Ensure the timing duration is 0.01ms
      TL0=TH0;
      TH1=timer_data ; //Ensure the timing duration is 0.01ms
      TL1=TH0;
      ET0=1; //Timer 0 interrupt is enabled
      TR0=1; //Timer 0 starts counting
      ET1=1; //Timer 1 interrupt is enabled
      TR1=1; //Timer 1 starts counting
      EA=1; //Interrupt is enabled
}
//-----------------------------------------------------------------
// Function name: INT0_init()
// Function function: Initialization settings
// Set the working mode of INT0
//-----------------------------------------------------------------
void INT0_init(void )  
{
      pulseB_count=0; //Pulse counter clears
      IT0=1; //Select INT0 as edge trigger mode
      EX0=1; //External interrupt enable
      EA=1; //System interrupt enable
}
//-----------------------------------------------------------------
// Function name: setting_PWM
// Function function: Set PWM pulse width and set direction
//-----------------------------------------------------------------
void setting_PWM()
{
 // direction=1; //Set rotation direction
 // R=540; //Set speed
 // dR=0; //Speed ​​compensation is zero
 // calculate_PWM_t(); //Recalculate pulse width
}
//-----------------------------------------------------------------
// Function name: calculate_PWM_t
// Input parameters: R is the DC motor speed to be obtained, dR is speed compensation
// Export parameters: PWM_t is the pulse width (320~400) and the time is 3.2~4.0ms
// Function function: Calculate pulse width, PWM_t=R/150;
//-----------------------------------------------------------------
void calculate_PWM_t()
{
      if(compensate_polarity==1) //Positive compensation
          PWM_t=(R+dR)/150;
      else
          PWM_t=(R-dR)/150; //Negative correction
}
//-----------------------------------------------------------------
// Function name: calculate_Rr
// Input parameter: pulseB_count pulse count
// Output parameter: Rr actual speed of DC motor
// Function function: Calculate the actual speed
//-----------------------------------------------------------------
void calculate_Rr()
{
      Rr=pulseB_count/6;
}
//-----------------------------------------------------------------
// Function name: compensate_dR
// Input parameter: Rr actual speed of DC motor
// R required DC motor speed
// Output parameter: dR speed compensation
// Function function: Calculate the actual compensation value and compensation polarity, and redesign according to different compensation algorithms
//-----------------------------------------------------------------
void compensate_Rr()
{
      Rr=1;
      if(Rr>R)
          compensate_polarity=0; //Compensation polarityelse
      compensate_polarity 
          =1;
}
//-----------------------------------------------------------------
// Function name: INT0_intrupt
// Function: External interrupt 0 handler
//-----------------------------------------------------------------
void INT0_intrupt() interrupt 0 using 1
{
     pulseB_count++;
     if(pulse_A==0)
     {   
          real_direction=1; //If P1.2 is low, the motor is forward, and the value of counter N is increased by 1
     }
     else //If it is high, the motor is reverse, and the value of counter N is reduced by 1.
     {
          real_direction=1;
     }
}
//-----------------------------------------------------------------
// Function name: IntTimer0
// Function function: timer interrupt handler
//-----------------------------------------------------------------
void IntTimer0() interrupt 1
{
     time0_count++;
     DR=direction;
     if(time0_count>=PWM_T)
     {
         time0_count=0;
         PWM_count++;
         setting_PWM(); //Called once for each PWM wave output
     }
     if(time0_count          PWM=1;
     else 
         PWM=0;
}
//-----------------------------------------------------------------
// Function name: IntTimer1
// Function function: timer interrupt handler
//-----------------------------------------------------------------
void IntTimer1() interrupt 3
{
     time1_count++;
     if(time1_count==1)
     {
         INT0_init(); //Initialize external interrupt settings
     }
     if(time1_count>=Ts)
     {
         time1_count=0; //A compensation cycle ends, the counter is cleared
         calculate_Rr(); //Calculate the actual speed
         compensate_Rr(); //Calculate the actual compensation value and compensation polarity
         calculate_PWM_t(); //Recalculate the pulse width
     }
}
//-----------------------------------------------------------------
// Function name: main
// User main function
// Function function: main function
//-----------------------------------------------------------------
void main()
{
     direction=1; //Set the rotation direction
     R=540; //Set the speed
     dR=0; //Speed ​​compensation is zero
     calculate_PWM_t(); //Recalculate the pulse width
     timer_init();
}

Reference address:DC motor control Keil c51 source code

Previous article:Summary of 8051 addressing modes
Next article:Detailed explanation of MCS-51 single-chip microcomputer control word

Recommended ReadingLatest update time:2024-11-16 14:46

C51 simulates SPI interface
include /************************************************           Analog SPI interface I/O definition ********************************************/ sbit spi_cs=P1^2;   sbit spi_di=P1^0;   sbit spi_clk=P1^1; sbit spi_do=P1^3;  #define set_spi_cs  spi_cs=1   #define clr_spi_cs  spi_cs=0   #define set_spi_clk
[Microcontroller]
Keil (MDK-ARM) series tutorial (I)_Detailed process of creating a new software project
Ⅰ. Write in front This article uses the latest version (November 2016) of Keil (MDK-ARM) V5.21a development environment and takes the STM32 processor as an example to describe the detailed process of creating a new software project. Keil (MDK-ARM) V4 and V5 new software projects are basically the same, this article
[Microcontroller]
Keil (MDK-ARM) series tutorial (I)_Detailed process of creating a new software project
Manually compile and link to generate the hex file of c51
This is purely a personal preference. The system UI is getting better and better now, but I still like the command line inexplicably, it's a kind of nostalgia. Once again, this is just a personal hobby. Although it is manually compiled, it still requires a Keil installed environment. detailed steps: 1. Use Not
[Microcontroller]
C51 MCU Study Notes Serial Communication
Introduction Serial communication is a communication method between the microcontroller and the PC. Communication mode: parallel, serial, synchronous, asynchronous (most commonly used) Transmission direction: simplex, half-duplex (different time), full-duplex basic structure Related registers SCON serial por
[Microcontroller]
C51 MCU Study Notes Serial Communication
C51 Programming 6- Bidirectional I/O Port and Quasi-bidirectional I/O Port
Through the previous input and output content (LED control and button use), we have a basic understanding of controlling the I/O port. If you need to output high or low levels, you can write "1" or "0" to the pin; if you need to read the I/O level, you can directly determine whether the pin is high or low. The input
[Microcontroller]
C51 Programming 6- Bidirectional I/O Port and Quasi-bidirectional I/O Port
The logic analysis provided by Keil is good
The software logic analyzer in Keil MDK has powerful functions, which can analyze digital signals, analog signals, CPU bus (UART, IIC and other output pins), and provide debugging function mechanism to generate custom signals, such as Sin, triangle wave, bath sound signal, etc., which can be defined.     Take the bu
[Microcontroller]
The logic analysis provided by Keil is good
C51 MCU Learning - Implementation of Password Lock
I'll say this beforehand: Because my board has an LCD screen and a digital tube, which cannot be used at the same time, I'm using an LCD screen. The following code is easy to understand and has comments. If you don't know how to use some of the controls, you can check out my previous C51 articles. This password lock i
[Microcontroller]
Silabs IDE Compiler Settings (Keil for C51)
Because Silabs IDE has its own compiler, but it is a 2K limited version of Keil for C51. If the program is slightly larger, it will not be able to proceed unless it is registered. Now let's explain how to configure the Keil compiler in Silabs IDE:        If the above dialog box appears when you compile, it means tha
[Microcontroller]
Silabs IDE Compiler Settings (Keil for C51)
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号