Welcome to MSPM0 Smart Robot Car repository
1.Servo control
Servo Control
Servo Control1. Software and Hardware2. Basic Principles2.1 Hardware Schematic2.2 Physical Connection Diagram2.3 Control Principle3. Project Experience3.1 Opening the Project4. Main Functions4.1 User Functions5. Experimental Phenomenon
This tutorial demonstrates: controlling a servo connected to the S1 interface on the development board through timer interrupt simulated PWM.
The first tutorial in this chapter will be more detailed than subsequent tutorials, with the purpose of demonstrating the complete process from creating a new project to achieving the final effect
1. Software and Hardware
KEIL
MSPM0G3507 Development Board
180° Servo
Type-C data cable or DAP-Link
For programming download or simulation to the development board
2. Basic Principles
2.1 Hardware Schematic


2.2 Physical Connection Diagram
.png)
Note: For servo wiring, refer to the servo wire colors. This experiment requires connecting 5V and the choose jumper cap
If using our 20k/25kg servo, you need to connect VM and choose to ensure proper operation.
| Servo | Development Board |
|---|---|
| VCC(Red) | 5V |
| SIG(Yellow) | S1/S2 |
| GND(Brown) | GND |
2.3 Control Principle
Control the servo rotation angle by changing the duty cycle of the PWM signal
- PWM (Pulse Width Modulation)
PWM is the abbreviation for Pulse Width Modulation, a technique that controls levels by adjusting the pulse width of signals.
Period: The duration of one complete PWM waveform;
Duty Cycle: The ratio of high-level duration to the total period time;
Frequency: The reciprocal of the period is called frequency, i.e., the number of PWM cycles generated per second;
Set the PWM control signal period to 20ms, i.e., 50Hz frequency; the high-level time of the pulse determines the servo rotation angle

Common angles and corresponding high-level pulse widths
| Servo (180°) | High-level Pulse Width (us) |
|---|---|
| 0° | 500 |
| 45° | 1000 |
| 90° | 1500 |
| 135° | 2000 |
| 180° | 2500 |
Using the TimerA timer interrupt function on the MSPM0G3507 development board
| PWM Servo (Schematic Name) | Control Pin | Function |
|---|---|---|
| S1 | PA28 | Simulated PWM output to control S1 servo |
| S2 | PA7 | Simulated PWM output to control S2 |
3. Project Experience
Use the project files we provide to directly experience the corresponding functions of the development board.
Later tutorials will not provide this content to avoid repetition. You can refer to 【3. Development Environment Setup and Usage: 5. Project Porting】 for operations
3.1 Opening the Project
Project File Location
Project file path: Under the corresponding course folder in the 【Program Source Code Summary】 folder

Opening the Project File
Copy the project file to the msp_sdk directory under an English path, use KEIL5 to open the project file, and select the .project file when opening

Pin Configuration



4. Main Functions
Mainly introduces the functional code written by users. For detailed code, you can open the project files we provide and view the source code in the Bsp folder.
4.1 User Functions
Function: servo_init
| Function Prototype | void servo_init(void) |
|---|---|
| Function Description | Initialize timer simulated PWM signal output |
| Input Parameters | None |
| Output Parameters | None |
Function: Set_Servo270_Angle
| Function Prototype | void Set_Servo270_Angle(unsigned int angle) |
|---|---|
| Function Description | Control 270-degree servo rotation |
| Input Parameters | angle: rotation angle |
| Output Parameters | None |
Function: Set_Servo_Angle
| Function Prototype | void Set_Servo_Angle(unsigned int angle) |
|---|---|
| Function Description | Control 180-degree servo rotation |
| Input Parameters | angle: rotation angle |
| Output Parameters | None |
Function: get_servo
| Function Prototype | void get_servo () |
|---|---|
| Function Description | Control servo to rotate from 0° then sequentially 0°,90°,180°,90°,0° back and forth |
| Input Parameters | None |
| Output Parameters | None |
5. Experimental Phenomenon
After successfully downloading the program, press the RESET button on the development board and observe the phenomenon!
xxxxxxxxxxFor program download, refer to 【3. Development Environment Setup and Usage: 3. Uniflash burning】
Phenomenon:
S1 Interface Servo: Rotates cyclically from 0°→ 90° →180° → 90° → 0°.
The provided project only controls the S1 interface servo. To control other servos, you can modify the interface function parameters yourself