Balance trolley built with msp432 (I)

Time:2022-4-30

1. Foreword

  The author is a sophomore who once neglected one year of study and now keeps learning,Therefore, I hope to discuss and raise any questions, and your support is my driving force.

As for the steps of hardware construction, the author doesn’t mention too many online schemes. The author uses encoder motor and tb6612 drive. Because the whole circuit adopts hole plate welding, the appearance is too ugly, so the physical picture will not be displayed.

2. Text

Open CCS to create driverlib template project

Establishment method: 1. Find the project wizard in the project toolbar and click

2. Enter your development board model in the input field

3. Find driverlib_ empty_ project_ from_ Source import project

The first import will prompt you to install the SDK, and then import it after the installation is restarted

First step of balancing trolley: drive motor

First of all, we need to know how to use GPIO. The GPIO file in driverlib indicates the use method

Set GPIO to output firstGPIO_setAsOutputPin(port,pin) 

Set output level, high levelGPIO_setOutputHighOnPin(port,pin) 

Low levelGPIO_setOutputLowOnPin(port,pin) 

Turn the two motors forward

int main(void){
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P6,
            GPIO_PIN0
            );
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P5,
            GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2
            );
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P3,
            GPIO_PIN5
            );
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN0); 
    //Set STBY high to make the drive work
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN0); 
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN1); 
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN5); 
    MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN2);
    while(1){
  
    }
}

Of course, we also need to control the motor speed. Let’s use PWM control. First learn how to use msp432 PWM.

There are two ways to generate PWM: use timer A to simulate PWM signal, and the other is timer A to generate a PWM signal

In timer_ A in the documentvoid Timer_A_generatePWMThe method declares generate a PWM with timer running in up mode, so it will also occupy a timer, but the customized module will operate quickly,

Because efficiency is paramount, I adopted this method

     void Timer_A_generatePWM(uint32_t timer, const Timer_A_PWMConfig *config);There are two parameters in the method

The first parameter is the timer. There are four timers

TIMER_A0_BASE
TIMER_A1_BASE
TIMER_A2_BASE
TIMER_A3_BASE

The second parameter is the configuration structure

typedef struct _Timer_A_PWMConfig
{ 
    uint_ fast16_ t clockSource;   // Clock source
    uint_ fast16_ t clockSourceDivider; // Clock division
    uint_ fast16_ t timerPeriod;     // cycle
    uint_ fast16_ t compareRegister; //  Comparison register
    uint_ fast16_ t compareOutputMode; // pattern
    uint_ fast16_ t dutyCycle; // Duty cycle
} Timer_A_PWMConfig;

The first three items affect the PWM frequency: clock frequency / cycle. There is no detailed description about the comparison register in the method. Tell us to query the data manual. After the query, we know that each TimerA timer has five comparison registers

Input / output diagrams are marked in the manual, but I am a little confused. Seven comparator channels (0 – 6) are annotated in the method. What are the remaining two? I hope you can explain what you want to know.

Mode selectionTIMER_A_OUTPUTMODE_RESET_SETRe count. The duty cycle is the number of high levels in PWM. When the duty cycle needs to be adjusted, the dutycycle in the structure can be assigned directly,

Reuse the timer_ A_ PWM configuration method.

Here’s how to use PWM

We build my_ pwm. C. document content

/*
 * my_PWM.C
 *
 *Created on: July 29, 2021
 *      Author: Administrator
 */
#include  

Timer_A_PWMConfig pwmConfig1 =
{
        TIMER_A_CLOCKSOURCE_SMCLK,
        TIMER_A_CLOCKSOURCE_DIVIDER_4,
        3200,
        TIMER_A_CAPTURECOMPARE_REGISTER_2,
        TIMER_A_OUTPUTMODE_RESET_SET,
        0
};
Timer_A_PWMConfig pwmConfig2 =
{
        TIMER_A_CLOCKSOURCE_SMCLK,
        TIMER_A_CLOCKSOURCE_DIVIDER_4,
        3200,
        TIMER_A_CAPTURECOMPARE_REGISTER_3,
        TIMER_A_OUTPUTMODE_RESET_SET,
        0
};
void PWM_Init(void){

      MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN5,GPIO_PRIMARY_MODULE_FUNCTION);
      MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN6,GPIO_PRIMARY_MODULE_FUNCTION);

      MAP_Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig1);
      MAP_Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig2);
}
void GPIO_Motor_Init(void){
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P6,
            GPIO_PIN0
            );
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P5,
            GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2
            );
    MAP_GPIO_setAsOutputPin(
            GPIO_PORT_P3,
            GPIO_PIN5
            );
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P6, GPIO_PIN0);

}

void PWM_config(double Left_cycle,double Right_cycle){

    if(Left_cycle > 0){
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN1);
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P3, GPIO_PIN5);

    }
    else {
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN1);
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P3, GPIO_PIN5);
    }
    pwmConfig1.dutyCycle = abs(Left_cycle);
    MAP_Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig1);

    if(Right_cycle > 0){
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN0);
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN2);
    }
    else {
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P5, GPIO_PIN0);
        MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN2);
    }
    pwmConfig2.dutyCycle = abs(Right_cycle);
    MAP_Timer_A_generatePWM(TIMER_A0_BASE, &pwmConfig2);
}

my_pwm.h

/*
 * my_PWM.H
 *
 *Created on: July 29, 2021
 *      Author: Administrator
 */

#ifndef MY_PWM_H_
#define MY_PWM_H_

#include 

void PWM_Init(void);
void GPIO_Motor_Init(void);
void PWM_config(double Left_cycle,double Right_cycle);
#endif /* MY_PWM_H_ */

mian.c

#include 

/* Standard Includes */
#include 
#include 
#include 
#include 
#include 
#include 
int main(void){
    MAP_WDT_A_holdTimer();
    PWM_Init();
    GPIO_Motor_Init();
    int i;             
    while(1){
    for(i = 0; i <= 3200; i = i + 100){
         PWM_config( i , -i);
          delay_ms(50);   
    }
  }  
}