Single chip PWM DC motor speed regulation and display speed

Publisher:平稳心绪Latest update time:2020-03-02 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The PWM DC motor speed regulation simulation schematic diagram is as follows.
 

The PWM DC motor speed regulation schematic diagram and PCB diagram drawn by Altium Designer are as follows:

The PWM DC motor speed control microcontroller program is as follows

#include

#include

#include "LCD1602.h"


#define uchar unsigned char

#define uint unsigned int

/**********Port Assignment***********************************/

sbit add_key=P1^0; //duty cycle increase

sbit dec_key=P1^1; //duty cycle reduction

sbit z_key=P1^2; //Forward key

sbit f_key=P1^3; //Reverse key

sbit run_key=P1^4; //Start/Stop key

                  

sbit beep=P3^2;

//Motor drive related

sbit ENA=P3^5;

sbit IN1=P3^7;

sbit IN2=P3^4;

/**********Variable definitions*******************************/

//Motor related definitions

bit Rotation_f=0; //Stepper motor rotation status flag is 0 forward and 1 reverse


bit power_flag=0; //Master switch flag


#define PWM_cnt_LEN 100 //Count full length

#define PWM_ON 1 //On  

#define PWM_OFF 0 //Off

uint PWM_cnt; //PWM count

uchar PWM_duty; //PWM duty cycle


uint speed_cnt=0; //speed

uint speed_value;

bit dis_speed_flag=0;

uint delay_cnt;

uchar dis_buf[6];

uchar dis_cnt;

uchar dis_buf1[4];

uchar dis_cnt1;

/************Define 1ms delay subroutine****************************/

void delay1ms(uint t) //1ms delay subroutine

{

    uint k,j;

        for(k=0;k                for(j=0;j<120;j++);

}

/**********************************************************/

/**********Buzzer alarm**********************/

void beep_alarm()

{

        beep=0;

        delay1ms(100);

        beep=1;

}

/***********************************************/

/**********Motor stops************************/

void motor_stop()

{

        IN1=1;

        IN2=1;

}

/*******************************************/

/*******Motor rotates forward************************/

void motor_foreward()

{

        IN1=1;

        IN2=0;

}

/****************************************/

/********Motor reverse ****************************/

void motor_reversal()

{

        IN1=0;

        IN2=1;

}

/************************************************/

/************Display speed*********************/

void dis_speed()

{

        dis_cnt=0;

        if(speed_value/10000!=0)

        {

                dis_buf[dis_cnt]=speed_value/10000+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10000/1000+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%1000/100+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%100/10+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10+0x30;

                dis_cnt++;

        }

        else if(speed_value%10000/1000!=0)

        {

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10000/1000+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%1000/100+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%100/10+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10+0x30;

                dis_cnt++;

        }

        else if(speed_value%1000/100!=0)

        {

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%1000/100+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%100/10+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10+0x30;

                dis_cnt++;

        }

        else if(speed_value%100/10!=0)

        {

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%100/10+0x30;

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10+0x30;

                dis_cnt++;

        }

        else

        {

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=' ';

                dis_cnt++;

                dis_buf[dis_cnt]=speed_value%10+0x30;

                dis_cnt++;

        }

        dis_buf[dis_cnt]='';

        LCD1602_Print(2,0,dis_buf); //Display speed

}

/************************************************/

/**********Display duty cycle*********************/

void dis_duty()

{

    dis_cnt1=0;

        if(PWM_duty/100!=0)

        {

                dis_buf1[dis_cnt1]=PWM_duty/100+0x30;

                dis_cnt1++;

                dis_buf1[dis_cnt1]=PWM_duty%100/10+0x30;

                dis_cnt1++;

                dis_buf1[dis_cnt1]=PWM_duty%10+0x30;

                dis_cnt1++;

        }

        else if(PWM_duty%100/10!=0)

        {

                dis_buf1[dis_cnt1]=' ';

                dis_cnt1++;

                dis_buf1[dis_cnt1]=PWM_duty%100/10+0x30;

                dis_cnt1++;

                dis_buf1[dis_cnt1]=PWM_duty%10+0x30;

                dis_cnt1++;

        }

        else

        {

                dis_buf1[dis_cnt1]=' ';

                dis_cnt1++;

                dis_buf1[dis_cnt1]=' ';

                dis_cnt1++;

                dis_buf1[dis_cnt1]=PWM_duty%10+0x30;

                dis_cnt1++;

        }

        dis_buf1[dis_cnt1]='';

        LCD1602_Print(9,1,dis_buf1); //Display duty cycle

}

/************************************************/

/*************Timer initialization procedure***********************/

void timer_init(void)  

{

        TMOD=0x11; //Set timer 0, 1 to 16-bit mode

        TH0=0xD8; //10ms timing initial value 10ms D8F0           

        TL0=0xF0;

        ET0=1; //Timer 0 is disconnected

        TR0=1; //Start timer 0


        TH1=0xFF; //100us timing initial value

        TL1=0x9C;

        ET1=1; //Timer 1 disconnected

        TR1=1; //Start timer 1

        EA=1; // total disconnect

}

/********************************************************/

/***********Timer 0 interrupt service routine*******************/

void timer0(void) interrupt 1  

{

        TH0=0xD8; //10ms timing initial value 10ms D8F0

        TL0=0xF0;

        delay_cnt++;

        if(delay_cnt>=200) //2S timing

        {

                delay_cnt=0;

                speed_value=speed_cnt*30; //30 Multiply the count in two seconds by 30 to get the revolutions per minute (also divided by 2 because there are two magnets) 30/2=15

                speed_cnt=0;

                dis_speed_flag=1;

        }

}

/********************************************************/

/**********External interrupt 1 service routine**************/

void int1() interrupt 2

{

        speed_cnt++; //speed

}

/************************************************/

/***********Timer 1 interrupt service routine***********************/

void timer1(void) interrupt 3  

{

        TH1=0xFF; //100us timing initial value

        TL1=0x9C;

        if(++PWM_cnt==PWM_cnt_LEN)

        {

                PWM_cnt=0;

                if(power_flag==1)

                        ENA=PWM_ON;

                else ENA=PWM_OFF;


        }

        if(PWM_cnt==PWM_duty)

        {

                ENA=PWM_OFF;

        }

}

/********************************************************/

/*******Key processing procedure**********************************/

void key_check()

{

        if(add_key==0) //duty cycle increase

        {

                delay1ms(50);

                if(add_key==0)

                {

                    beep_alarm(); //Buzzer alarm

                        PWM_duty++;

                        if(PWM_duty>100)

                                PWM_duty=100;

                        dis_duty(); //Display duty cycle

                }

        }

        if(dec_key==0) //duty cycle reduction

        {

                delay1ms(50);

                if(dec_key==0)

                {

                    beep_alarm(); //Buzzer alarm

                        PWM_duty--;

                        if(PWM_duty>100)

                                PWM_duty=0;

                        dis_duty(); //Display duty cycle

                }

        }

        if(z_key==0) //Forward key

        {

                delay1ms(50);

                if(z_key==0)

                {

                    beep_alarm(); //Buzzer alarm

                        Rotation_f=0; //Stepper motor rotation status flag is 0 forward and 1 reverse

                        LCD1602_Print(12,0,"pros");

                        motor_stop(); //motor stops

                        delay1ms(1);

                        motor_foreward(); //Motor forward

                }

        }

        if(f_key==0) //Reverse key

        {

                delay1ms(50);

                if(f_key==0)

                {

                    beep_alarm(); //Buzzer alarm

                    Rotation_f=1; //Stepper motor rotation status flag is 0 forward and 1 reverse

                        LCD1602_Print(12,0,"cons");

                        motor_stop(); //motor stops

                        delay1ms(1);

                        motor_reversal(); //Motor reversal

                }

        }

    if(run_key==0) //Start/Stop key

        {

                delay1ms(50);

                if(run_key==0)

                {

                    beep_alarm(); //Buzzer alarm

            power_flag=~power_flag; //Master switch flag

                        if(power_flag==1)

                        {

                            if(Rotation_f==0) //Stepper motor rotation status flag is 0 forward and 1 reverse

                                {

                                        motor_stop(); //motor stops

                                        delay1ms(1);

                                        motor_foreward(); //Motor forward

                                }

                                else

                                {

                                        motor_stop(); //motor stops

                                        delay1ms(1);

                                        motor_reversal(); //Motor reversal

                                }

                        }

                        else motor_stop(); //motor stops

[1] [2]
Keywords:MCU Reference address:Single chip PWM DC motor speed regulation and display speed

Previous article:4*4 matrix button "row and column inversion method" scans and displays 0-f in sequence
Next article:Tracking and obstacle avoidance remote control car based on STC15 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 13:31

A general design scheme for simulating information appliances using single-chip microcomputers
  1 Introduction   Human beings have entered the information age. Information exchange has become a necessity for people's lives and production, and is developing rapidly. Therefore, whether it is mobile phones, PDAs or home appliances, they will eventually be integrated into the network. Traditional IT and tradition
[Microcontroller]
A general design scheme for simulating information appliances using single-chip microcomputers
Design method of frequency meter based on single chip microcomputer 89C51,
1 Introduction   This design comprehensively considers the requirements of frequency measurement accuracy and measurement response time. For example, when the frequency measurement result is required to be 3 significant digits, if the frequency of the signal to be measured is 1 Hz, the counting gate width must be gr
[Microcontroller]
Design method of frequency meter based on single chip microcomputer 89C51,
AVR microcontroller (learning) - (nine), ATMEGA16 analog/digital converter - 02
9. ATMEGA16 A/D Converter 9. (02) Application of ATMEGA16 analog/digital converter - "Schmitt" voltage comparator experiment Directly on the picture~~ Then the program: IAR9_2.c //------------------------------------------------------------------------------ //In automatic control, "Schmitt" voltage comparator i
[Microcontroller]
AVR microcontroller (learning) - (nine), ATMEGA16 analog/digital converter - 02
Driving a Brushed DC Motor Using PWM Output: Losses and Considerations
Using PWM output to drive a brushed DC motor: Loss thinking Since PWM drive is a pulse drive, its power consumption is only the average of the power consumption during the voltage application (conduction) period and the current regeneration (off) period of the motor in one cycle. Strictly speaking, as shown in
[Embedded]
Driving a Brushed DC Motor Using PWM Output: Losses and Considerations
51 MCU peripherals - Play with digital tubes
This blog post will introduce and drive the digital tube. The chips associated with it are the 74HC138 decoder, 74HC02 NOR gate, and 74HC573 latch mentioned earlier. The I/O ports used are still 11 - P2.5P.6P2.7 and P0~P7. First of all, what is a digital tube? The digital tube is an "8" shaped component composed o
[Microcontroller]
51 MCU peripherals - Play with digital tubes
Brief introduction to the production method of SST microcontroller programmer
A new SST microcontroller can usually be programmed using the serial port using SSTFlashFlex51.exe. However, if the chip we get is not a new one but has been erased by a programmer and a new code has been written into it, it will be difficult to deal with. Here I will introduce a method to make a simple programmer t
[Microcontroller]
MCU through PCF8574T module driver 1602/2004LCD
The microcontroller drives the 1602/2004 LCD through the PCF8574T module. Friends in need can take a look. //----------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include reg52.h #include "intrins.h" #d
[Microcontroller]
Realizing DC motor speed regulation through 51 microcontroller
1. Project background and purpose With the widespread use of various industrial production equipment and mechanical equipment, the research and application of DC motor speed regulation technology has attracted more and more attention and has broad application prospects. This project uses the 51 microcontroller to real
[Microcontroller]
Realizing DC motor speed regulation through 51 microcontroller
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号