4562 views|10 replies

305

Posts

0

Resources
The OP
 

[RISC-V MCU CH32V103 Review] - 10: Rewritten DC brushed motor driver [Copy link]

 This post was last edited by MianQi on 2021-3-26 11:14

Based on the content of these two posts:

1. Chapter 80, CH32V103 Application Tutorial - Brushless DC Motor Key Control - https://bbs.21ic.com/icview-3088792-1-1.html

2. [RISC-V MCU CH32V103 Review] - 9: Motor Control Board - https://bbs.eeworld.com.cn/thread-1159389-1-1.html

Based on the original code, I added two files:

motor_DC_brush.h

/*
 * motor_DC_brush.h
 *
 *  Created on: Mar 23, 2021
 *      Author: MQ
 */

#ifndef HARDWARE_MOTOR_MOTOR_DC_BRUSH_H_
#define HARDWARE_MOTOR_MOTOR_DC_BRUSH_H_

    static u16 pwmChannelPulse = 0;
    static u8  directionMotorCurrent = 0;
    static u8  stateMotorCurrent = 0;


/*
 * 这是源文件“motor_DC_brush.c”的头文件;
 * 声明5个函数,分别用于电动机的停止、左转、右转、加速、减速;
 * 这5个功能对应着5个按键:key_0, key_1, key_2, key_3, key_4;
 * 本程序专为一个PNP加一个NPN的推挽输出驱动电路用;
 * 本程序使用的MCU电路板为南京沁恒产CH32V103(C8T6);
 * 驱动对象为:直流有刷电动机。
* 两路PWM信号 - SetCompare1 与 SetCompare4 分别接PA8和PA11。
* PA8接 PNP(上管),PA11接NPN(下管)
*/

    void motorDCStop(void);
    void motorDCForward(void);
    void motorDCBackward(void);
    void motorDCSpeedUp(void);
    void motorDCSpeedDown(void);

#endif /* HARDWARE_MOTOR_MOTOR_DC_BRUSH_H_ */

motor_DC_brush.c

/*
 * motor_DC_brush.c
 *
 *  Created on: Mar 23, 2021
 *      Author: MQ
 */


/*
 * 这是头文件“motor_DC_brush.h”的源文件;
 * 定义5个函数,分别用于电动机的停止、左转、右转、加速、减速;
 * 这5个功能对应着5个按键:key_0, key_1, key_2, key_3, key_4;
 * 本程序专为一个PNP加一个NPN的推挽输出驱动电路用;
 * 上管(PNP)发射极接VCC(MCU),下管(NPN)发射极接GND。
 * 驱动对象为:直流有刷电动机。
 */

/*
 * 本程序使用的MCU电路板为南京沁恒产CH32V103(C8T6);
 * 本程序适用的电路板连接为:
 * key_0 --> PA2
 * key_1 --> PA3
 * key_2 --> PA4
 * key_3 --> PA5
 * key_4 --> PA6
 * PNP 基极 --> PA8
 * NPN 基极 --> PA11
 */

#include "timer.h"
#include "motor_DC_brush.h"

//电动机停车
void motorDCStop(void){
    //,。关闭PWM输出:
    TIM_CtrlPWMOutputs(TIM1, DISABLE);

    /*LED指示当前开关状态:
     *LED0 和 LED1 都灭,电动机关;
     *LED0 开,LED1 关,电动机左转;
     *LED1 开,LED0 关,电动机右转。
     */
    GPIO_SetBits(GPIOA,GPIO_Pin_0);
    GPIO_SetBits(GPIOA,GPIO_Pin_1);

    //《》两路信号均无输出:
    //TIM_SetCompare1(TIM1,0);
    //TIM_SetCompare4(TIM1,0);
    stateMotorCurrent = 0;

    Delay_Ms(200);
}

//左转
void motorDCForward(void){
    //,。开启PWM输出:
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

    /*LED指示当前开关状态:
     *LED0 和 LED1 都灭,电动机关;
     *LED0 开,LED1 关,电动机左转;
     *LED1 开,LED0 关,电动机右转。
     */
    GPIO_SetBits(GPIOA,GPIO_Pin_0);
    GPIO_ResetBits(GPIOA,GPIO_Pin_1);

    //《》两路信号均无输出:
    //TIM_SetCompare1(TIM1,0);
    //TIM_SetCompare4(TIM1,0);
    //Delay_Ms(200);

    TIM_SetCompare4(TIM1,pwmChannelPulse);
    TIM_SetCompare1(TIM1,0);

    directionMotorCurrent = 0;
    printf("DIR:%d\r\n",directionMotorCurrent);
}

//右转
void motorDCBackward(void){
    //,。开启PWM输出:
    TIM_CtrlPWMOutputs(TIM1, ENABLE);

    /*LED指示当前开关状态:
     *LED0 和 LED1 都灭,电动机关;
     *LED0 开,LED1 关,电动机左转;
     *LED1 开,LED0 关,电动机右转。
     */
    GPIO_ResetBits(GPIOA,GPIO_Pin_0);
    GPIO_SetBits(GPIOA,GPIO_Pin_1);

    //《》两路信号均无输出:
    //TIM_SetCompare1(TIM1,0);
    //TIM_SetCompare4(TIM1,0);
    //Delay_Ms(10);

    TIM_SetCompare1(TIM1,pwmChannelPulse);
    TIM_SetCompare4(TIM1,0);

    directionMotorCurrent = 1;
    printf("DIR:%d\r\n",directionMotorCurrent);
}

//加速
void motorDCSpeedUp(void){
    pwmChannelPulse += 50;                  //增加占空比,提高电机转速
    pwmChannelPulse = ARR < pwmChannelPulse ? ARR : pwmChannelPulse;    // 检查占空比的合法性
}

//减速
void motorDCSpeedDown(void){
    pwmChannelPulse -= 50;                  //增加占空比,降低电机转速
    pwmChannelPulse = ARR < pwmChannelPulse ? ARR : pwmChannelPulse;    // 检查占空比的合法性
}

The main file after the corresponding modifications:

    /********************************** (C) COPYRIGHT *******************************
    * File Name          : main.c
    * Author             : WCH
    * Version            : V1.0.0
    * Date               : 2020/09/29
    * Description        : Main program body.
    *******************************************************************************/
    #include "debug.h"
    #include "led.h"
    #include "key.h"
    #include "timer.h"
    #include "motor_DC_brush.h"

    int main(void)
    {
        u8 t=0;

        USART_Printf_Init(115200);
        Delay_Init();                         //延时函数初始化
        LED_Init();                           //LED初始化
        KEY_Init();                           //按键初始化
        TIM1_PWMOut_Init();                   //PWM输出初始化
        TIM_CtrlPWMOutputs(TIM1, DISABLE);

        printf("SystemClk:%d\r\n",SystemCoreClock);

        while(1)
        {
            t=KEY_Scan(0);      //得到键值

            switch(t)
            {
                case KEY0_PRESS:
                    motorDCStop();
                    break;

                case KEY1_PRESS:
                    if(stateMotorCurrent == 1) {
                        motorDCStop();
                        //Delay_Ms(200);
                        motorDCForward();
                    } else {
                        motorDCForward();
                    }
                    break;

                case KEY2_PRESS:
                    motorDCSpeedUp();
                    break;

                case KEY3_PRESS:
                    motorDCSpeedDown();
                    break;


                case KEY4_PRESS:
                    if(stateMotorCurrent == 0) {
                        motorDCStop();
                        //Delay_Ms(200);
                        motorDCBackward();
                    } else {
                        motorDCBackward();
                    }
                    break;

                default:
                    Delay_Ms(10);
            }
        }
    }

This post is from Domestic Chip Exchange

Latest reply

NICE!!!!!!!!!!   Details Published on 2024-6-25 14:13
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 

1w

Posts

204

Resources
From 10
Personal signature

玩板看这里:

https://bbs.eeworld.com.cn/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

305

Posts

0

Resources
2
 

First, I used the dual BJT driver circuit I built on the breadboard, which can realize start-stop and forward rotation, but not reverse rotation. Later, I changed to the L298N driver board, which can realize start-stop and forward and reverse rotation, but still cannot accelerate or decelerate.

Two problems were found during the test:

1. The power switch on the Qinheng board does not seem to work. 2. The pins on the Qinheng board sometimes have poor contact, or the jumper plug I used may not be tight enough.

Real photos:

video:


This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

305

Posts

0

Resources
3
 

After making some modifications to the header and source files in MOTOR, start/stop, forward/reverse, acceleration/deceleration can now be realized.

test_motor_DC_brush.zip (992.85 KB, downloads: 0)

This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

9717

Posts

24

Resources
4
 

Remember that the minimum voltage of this large L298N is 5V, what volts is your 103 power supply?

This post is from Domestic Chip Exchange

Comments

103 is powered by PC. External 12v is used to power L298. L298 has a 5v output, which is not used. In other words, the motor is powered by 12v - I use a 12v high-speed aircraft model motor.  Details Published on 2021-3-27 09:04
 
 
 

305

Posts

0

Resources
5
 
littleshrimp posted on 2021-3-27 08:52 Remember that the minimum voltage of this large L298N is 5V, what is the volt of your 103 power supply?

103 is powered by PC. External 12v is used to power L298. L298 has a 5v output, which is not used. In other words, the motor is powered by 12v - I use a 12v high-speed aircraft model motor.

This post is from Domestic Chip Exchange

Comments

I see in the schematic diagram that the PC 5V input of 103 is converted to 3.3V through an LM1117, so the I/O output level of 103 is 3.3V at maximum. If L298N does not support 3.3V input, there may be problems in this aspect.  Details Published on 2021-3-27 11:03
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

9717

Posts

24

Resources
6
 
MianQi posted on 2021-3-27 09:04 103 is powered by PC. The external 12v is used to power L298. There is a 5v output on L298, which is useless. In other words, the motor is powered by 12v -&m ...

I see in the schematic diagram that the PC 5V input of 103 is converted to 3.3V through an LM1117, so the I/O output level of 103 is 3.3V at maximum. If L298N does not support 3.3V input, there may be problems in this aspect.

This post is from Domestic Chip Exchange
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

305

Posts

0

Resources
7
 

This is a problem, but in actual tests, the speed can be adjusted. As for the impact, further investigation is needed.

This post is from Domestic Chip Exchange
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

305

Posts

0

Resources
8
 

I checked the technical manual of ST L298 and found:

It can be seen that there is no problem with the 3.3v input signal.

This post is from Domestic Chip Exchange

Comments

This maximum value is the voltage limit at which the chip will not be damaged, and does not mean that this voltage can work normally.  Details Published on 2021-4-20 11:24
Personal signature

“Everyone wants the project to be good, fast, and cheap... pick two.”

- Unknown

 
 
 

9717

Posts

24

Resources
9
 
MianQi posted on 2021-3-29 09:42 I checked the technical manual of ST L298 and it says: It can be seen that the 3.3v input signal is fine.

This maximum value is the voltage limit at which the chip will not be damaged, and does not mean that this voltage can work normally.

This post is from Domestic Chip Exchange
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 
 
 

3

Posts

0

Resources
11
 

NICE!!!!!!!!!!

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list