RISC-V MCU development practice: stepping motor

RISC-V MCU development practice (IV): stepping motor

Software platform: MounRiver Studio(MRS), hardware platform: CH32V103 development board, ULN2003 stepper motor drive board, 28BYJ-48 stepper motor, and GPIO is used for stepper motor control.

  1. Introduction to ULN2003 and 28BYJ-48
    ULN2003 is a high voltage and high current composite transistor array, which is composed of seven Silicon NPN composite transistors. Each pair of Darlington is connected with a 2.7K base resistance in series. Under the working voltage of 5V, it can be directly connected with TTL and CMOS circuits, and can directly process the data originally required to be processed by standard logic buffer.

ULN2003 is a high current drive array, which is mostly used in control circuits such as single chip microcomputer, intelligent instrument, PLC, digital output card and so on. It can directly drive loads such as relays.
Input 5VTTL level and output up to 500mA/50V.

ULN2003 is a high voltage and High Current Darlington series, which is composed of seven Silicon NPN Darlington tubes. The characteristics of the circuit are as follows: each pair of Darlington of ULN2003 is connected with a 2.7K base resistor in series. Under the working voltage of 5V, it can be directly connected with TTL and CMOS circuits, and can directly process the data that originally needed standard logic buffer.

As for the stepping motor, the motor model used here is 28BYJ-48 (stepping motor), the reduction ratio is 1:64, and the stepping foot is 5.625/64 degrees. If it needs to rotate for one turn, 360 / 5.625 * 64 = 4096 pulse signals are required.

Stepper motor is an executive equipment that converts electric pulse into angular displacement. The driving signal of the stepping motor is a pulse signal. When the stepping driver receives a pulse signal, it drives the stepping motor to rotate a fixed angle (i.e. stepping angle) in the set direction.

We can control the angular displacement by controlling the number of pulses, so as to achieve the purpose of accurate positioning; At the same time, we can control the speed and acceleration of the motor by controlling the pulse frequency, so as to achieve the purpose of speed regulation

  1. Hardware connection
    The connection mode between CH32V103 development board and ULN2003 stepper motor drive board is as follows:
    PB6 connects the IN1 pin of the drive board
    PB7 connects the IN2 pin of the drive board
    PB8 connects the IN3 pin of the drive board
    PB9 connects the IN4 pin of the drive board

  2. Development process in MRS
    1) First, create a CH32V103C8T6 project, which should correspond to the corresponding chip

The red box at the bottom of the figure above is a brief introduction to the resources of the selected chip for easy query

  1. After the new project is completed, we open the main.c file. We can see that the main function is just some initialization and serial port printing. Our own main function logic can be added under printing;
  2. Create a new hardware folder, right-click project new - > folder, fill in the file name and click finish. We can create SD directory and SPI directory in the hardware directory in the same way, which is clear.
  3. In the SPI directory, new > source file, fill in the file name gpio.c, which contains the motor initialization function and speed regulation and steering stop function. Create a new gpio.h file to declare the function. This new header file needs to be added to the header file addressing path. Click the project attribute configuration button in the menu bar. In the pop-up page, as shown in the figure below, click the green plus sign to add the path

The driver code is as follows:

#include "gpio.h"
#include "debug.h"

//#define N 4
#define N 8

//The value of the stepper motor forward and reverse array, that is, the value of the corresponding GPIO pin
//Single four beat
//uint16_t phasecw[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A.(9-8-7-6)
//uint16_t phaseccw[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D.(6-7-8-9)

Double four beat
//uint16_t phasecw[4] ={0x0300,0x0180,0x00C0,0x0240};// DC-CB-BA-AD.
//uint16_t phaseccw[4]={0x00C0,0x0180,0x0300,0x0240};// AB-BC-CD-DA.

//Four phase eight beat
uint16_t phasecw[8] ={0x0200,0x0300,0x0100,0x0180,0x0080,0x00C0,0x0040,0x0240};// D-DC-C-CB-B-BA-A-AB.
uint16_t phaseccw[8]={0x0040,0x00C0,0X0080,0x0180,0x0100,0x0300,0x0200,0x0240};// A-AB-B-BC-C-CD-D-DA.

//Motor initialization function
void Moto_Init(void)
{
    //Stepper motor initialization
    // IN1: PB6   a
    // IN2: PB7   b
    // IN3: PB8   c
    // IN4: PB9   d
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);

    GPIO_ResetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
}

//Motor forward rotation function
//Among them, the larger the speed value, the slower the speed, and the smaller the value, the faster the speed. Speed is equivalent to adjusting the pulse speed
//The rotation speed is directly proportional to the pulse frequency. Here, the smaller the delay, the higher the frequency
void Motorcw(u8 speed)
{
    uint8_t i=0;

    for(i=0;i<N;i++)
    {
        GPIO_Write(GPIOB,phasecw[i]);
        Delay_Ms(speed);
    }
}

//Motor reversal function
void Motorccw(u8 speed)
{
    uint8_t i;
    for(i=0;i<N;i++)
    {
        GPIO_Write(GPIOB,phaseccw[i]);
        Delay_Ms(speed);
    }
}

//Motor stop function
void MotorStop(void)
{
    //GPIO_ResetBits(GPIOB,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
    GPIO_Write(GPIOB,0x0000);
}

//Forward rotation angle of motor
void Motorcw_angle(int angle,int speed)
{
    int i,j;
    j=(int)(angle/0.70312);
    for(i=0;i<j;i++)
    {
        Motorcw(speed);
    }
}

//Motor reverse angle
void Motorccw_angle(int angle,int speed)
{
    int i,j;
    j=(int)(angle/0.70312);
    for(i=0;i<j;i++)
    {
        Motorccw(speed);
    }
}

The main function can call the forward and reverse functions in our driver to realize the desired functions

int main(void)
{

    USART_Printf_Init(115200);
    Moto_Init();
    Delay_Init();

    printf("This is Stepper motor driver\r\n");

    Motorcw_angle(360,5);   //Forward rotation angle function of stepping motor
    MotorStop();
    Delay_Ms(1000);

    Motorccw_angle(360,5);  //Reverse angle function of stepping motor
    MotorStop();
    Delay_Ms(1000);

}

After the code editing is completed, click the compile button in the menu bar to view the compilation results in the console window. If there is no error, you can enter the debugging to verify the logic. Click the debug button in the menu bar. If the operation phenomenon is inconsistent with the theory, you can find the logic BUG through the Disassembly window, break point, peripheral register and kernel register in the lower left corner


Tip: when the program runs to hardfault_ With the handler function, you can observe the three registers mepc, MTVAL and mcause in the Rregister window, which respectively represent the PC before entering the hardware error interrupt, the value obtained by the CPU, and the reason for entering the exception.

  1. verification
    Download the compiled program to the development version and reset it. Collect the waveform of these GPIO pins through the logic analyzer, as shown in the figure below. Connect the development board, stepper motor drive board and stepper motor, and you can see the forward and reverse rotation of the motor.

Tags: Embedded system Single-Chip Microcomputer risc-v

Posted on Fri, 29 Oct 2021 00:38:03 -0400 by Nicoza