Take PWM control of DC motor as an example to build a simple 51 project framework

Publisher:火星叔叔Latest update time:2017-01-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Table of contents

1) Functional Overview

2) Pin connection

3) Framework Introduction

4) Module Description

5) Reuse rules

6) Engineering Links


 

1) Functional Overview

  Name: Independent button control of DC motor speed regulation
  Content: The corresponding motor interface needs to be connected to the uln2003 motor control terminal with a Dupont line;
     5V-12V low-power motors can be accelerated and decelerated by 2 buttons respectively;
     the last two digits of 8 8-segment digital tubes are used to display 0~10 levels of speed;


 

2) Pin connection

  P3 is connected to the button; P0 is connected to 8x eight-segment digital tubes, P22 is connected to the segment code latch, and P23 is connected to the bit code latch; P1.1 is connected to an input of ULN2003; the DC motor end is connected to the corresponding output of ULN2003, and the other end is connected to VCC.


 

3) Framework Introduction

  The main purpose of this article is not to introduce how to implement functions, but how to make a framework that is easy to reuse and understand. As shown in the figure below, the files in the USER file belong to the highest layer (closest to the user); the files in the FUNC folder belong to the function layer (the user layer files can call various functions implemented by the function layer, and there are also a small number of mutual calls within the function layer); the INTE folder is mainly the interrupt layer (I originally wanted to take the interrupt out separately, but the interrupt subroutine and the user layer overlap too much, so only the interrupt initialization function is implemented here, and the interrupt subroutine is still placed in the main.c file)

Note: Pay attention to the reference in main.c! (Because there is no direct correspondence between the folder in Keil and the external one, the external one was built manually by me)


 

4) Module Description

--> 4.1 Delay Description: Provides two delay functions (used in I2C, key debounce, etc.)

1 #include
2 
3 void DelayUs2x(unsigned char t); //us level delay function declaration 
4 void DelayMs(unsigned char t); //ms level delay

 

--> 4.2 key8 Description: Provides a key scanning function to the outside world. If a key is pressed, the corresponding key value is returned (this is 8 keys, not 4x4)

Copy code

1 #include
2 
3 #define KeyPort P3
4 
5 unsigned char KeyScan(void); //key scan function, returns the scan key value

Copy code

Application example: The following KeyScan() is called to detect the key to implement the key control function~

 

--> 4.3 led8 Description: Provides 8x eight-segment digital tube display function (special processing is required here)

Copy code

1 #include
2 
3 #define DataPort P0 //define the data port. When encountering DataPort in the program, replace it with P0.
4 sbit LATCH1=P2^2; //define latch enable port segment latch
5 sbit LATCH2=P2^3; // bit latch
6 
7 void Display(unsigned char FirstBit,unsigned char Num);

Copy code

Application example: The 2-digit integer PWN_ON to be displayed is displayed on the 8x 8-segment digital tube as follows. At the 1, the tens and ones of PWN_ON are taken respectively and then converted into the display value of the corresponding digital tube light through DuanMa; then the display function is called in the interrupt or function body with frequent periodic refresh to display (the first parameter of display is to select the number of the 8x 8-segment digital tube to start displaying, 0 means starting from the 1st position; the second parameter is to indicate how many positions to display)

Special note: display(6,2) here means to display a two-digit integer on the last two of the eight digital tubes, and the value assigned to TempData is not TempData[6] and TempData[7]!


 

5) Reuse rules

In the future, the same method can be used to continuously expand the FUNC layer, and when encountering specific problems, adjustments can be made in main and interrupts.


 

6) Engineering Links

https://github.com/beautifulzzzz/stc89c51-or-stc89c52


Reference address:Take PWM control of DC motor as an example to build a simple 51 project framework

Previous article:Starting from 0, drive the 4-digit 8-segment common cathode digital tube 3461AS
Next article:Four-phase five-wire stepper motor drive design with a reduction ratio of 1/64

Latest Microcontroller Articles
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号