0
Follow
0
View

MCU DC motor intermittent rotation

crchenrong2004 注册会员
2023-02-27 15:23
I don't know if your problem has been solved, if not:
  • helps you find a similar problem, you can read: https://ask.csdn.net/questions/7730104 < / font > < / a > < / li > < li > you also can refer to this article: Method principle of measuring capacitance value by 51 microcontroller and some solutions to the problems
  • 51 MCU control 42 step motor -- program implementation(interrupt PWM/ delay function) program part part may be able to solve your problem, You can read the following carefully or jump to the source blog:

    program implementation is keil software written in c language program, with two ideas written, can be realized, the following are posted out.
    The first is the pulse with the delay function.

    #include < reg51.h>
    #define uint unsigned int

    void delayms(uint);

    sbit ENA=P1^0;
    sbit DIR=P1^1;
    sbit PWM=P1^2;

    void main()
    {
    ENA = 0;
    DIR = 0;
    PWM = 0;
    while(1)
    {
    PWM = ~PWM;
    delayms(1);
    }
    }

    void delayms(uint xms)
    {
    uint i,j;
    for(i=xms; i> 0; I -) < br / > for(j=110; j> 0; J -);
    }


    The second, with 51 MCU T1 timer to achieve the pulse.
    #include < reg51.h>
    #define uint unsigned int

    unsigned char timer1;

    sbit DIR=P1^0;
    sbit ENA=P1^1;
    sbit PWM=P1^2;

    void system_Ini()
    {
    TMOD = 0x00; / / 13 » I < br / > TH1 = 0xfd; //253
    TL1 = 0x06; //6
    EA = 1;
    ET1 = 1;
    TR1 = 1;
    }

    /*********************************************************
    2^13=8192
    253 32=8096
    8096+6=8102
    8192-8102=90
    12
    (1/11059200)= 1.09us
    *********************************************************/

    main()

    {
    system_Ini();
    DIR = 0;
    ENA = 0;
    while(1)
    {
    if(timer1==40)
    {
    timer1=0;
    }
    if(timer1< 20)
    {
    PWM=0;
    }
    else
    {
    PWM=1;
    }
    }
    }

    void Ti1(void) interrupt 3

    {
    TH1 = 0xfd;
    TL1 = 0x06;
    timer1++;
    }


If you have solved the problem, I hope you can share the solution, write a blog about it, and put a link to it in the comments section to help more people. ^-^ /div>
duck0083 注册会员
2023-02-27 15:23

To realize the motor stops after a certain amount of rotation, you can set a timer in the program to control the running time and residence time of the motor. The specific code writing process is as follows:

  • Set a timer to calculate how long the motor runs and how long it stays, which can be done using the timer interrupt. For example, if the timer interrupt time is set to 1 second, a two-second run time can be achieved with two timer interrupts.
  • Write PWM speed regulation code in the main program, control the speed of the motor. You can use the PWM module of 51 MCU or realize it through software.
  • In the timer interrupt service function, set a timer to record the motor's run time and dwell time. When the motor running time reaches two seconds, turn off the PWM output, let the motor stop running; When the motor residence time reaches two seconds, restart the PWM output and allow the motor to start running again.

Here is an example of code:

#include 

unsigned char count; // 定时器计数器
unsigned char running_time; // 电机运行时间计数器
unsigned char stop_time; // 电机停留时间计数器

void timer0_isr() interrupt 1 // 定时器0中断服务函数
{
    TH0 = 0x4C; // 重新赋初值
    TL0 = 0x00;
    count++; // 计时器加1
    if(count == 50) // 1秒钟
    {
        count = 0;
        running_time++; // 运行时间计时器加1
        stop_time++; // 停留时间计时器加1
        if(running_time == 2) // 运行时间达到2秒
        {
            running_time = 0;
            stop_time = 0;
            P1 &= 0xFE; // 关闭PWM输出,电机停止运转
        }
        else if(stop_time == 2) // 停留时间达到2秒
        {
            stop_time = 0;
            P1 |= 0x01; // 开启PWM输出,电机重新开始运转
        }
    }
}

void main()
{
    TMOD |= 0x01; // 定时器0工作在模式1
    TH0 = 0x4C; // 初值
    TL0 = 0x00;
    TR0 = 1; // 启动定时器0
    ET0 = 1; // 允许定时器0中断
    EA = 1; // 允许中断
    P1 |= 0x01; // 初始化为PWM输出,电机开始运转
    while(1)
    {
        // 在主程序中编写PWM调速的代码
        // 比如使用定时器2和PCA模块来实现PWM输出
    }
}

The timer in the code above uses a counter count. When the counter reaches 50(1 second), the interrupt service function clears the counter count to zero and then sets the run-time counter and stops

.

About the Author

Question Info

Publish Time
2023-02-27 15:22
Update Time
2023-02-27 15:22