Share a dual-channel SPWM source code that can drive the motor using
a sine table calculation:
The C language library function version of the STC15W4K60S2 microcontroller source program is as follows:
#include "config.h"
#include "PWM.h"
/************* Function Description **************
Demonstrates the use of two PWM channels to generate complementary or in-phase SPWM.
The main clock selects 24MHZ, the PWM clock selects 1T, the PWM period is 2400, and the dead zone is 12 clocks (0.5us). The sine wave table uses 200 points.
Output sine wave frequency = 24000000 / 2400 / 200 = 50 HZ.
This program is just a demonstration program of SPWM. Users can modify the PWM period and the number of points and amplitude of the sine wave through the above calculation method.
The output frequency of this program is fixed. If frequency conversion is required, please design the frequency conversion scheme yourself.
This program outputs positive phase pulse from P2.1 (PWM3) and negative phase pulse (complementary) from P2.2 (PWM4).
If you need P2.2 to output the same phase, please select 0 in the "Start_IO_Level" item in the initialization configuration (set the initial level of the PWM output port, 0 or 1).
Project Files:
delay.c
pwm.c
main.c
******************************************/
/**************** Local constant declaration **************/
/************* Local variable declaration **************/
/**************** local function declaration **************/
/************* External function and variable declaration*****************/
//========================================================================
// Function: void PWM_config(void)
// Description: PWM configuration function.
// Parameters: none.
// Returns: none.
// Version: VER1.0
// Date: 2014-8-15
// Remark:
//========================================================================
void PWM_config(void)
{
PWMx_InitDefine PWMx_InitStructure; //Structure definition
PWMx_InitStructure.PWMx_IO_Select = PWM3_P21; //PWM output IO selection. PWM2_P37,PWM2_P27,PWM3_P21,PWM3_P45,PWM4_P22,PWM4_P44,PWM5_P23,PWM5_P42,PWM6_P16,PWM6_P07,PWM7_P17,PWM7_P06
PWMx_InitStructure.Start_IO_Level = 0; //Set the initial level of the PWM output port, 0 or 1
PWMx_InitStructure.PWMx_Interrupt = DISABLE; //Interrupt enable, ENABLE or DISABLE
PWMx_InitStructure.FirstEdge_Interrupt = DISABLE; //First flip interrupt enabled, ENABLE or DISABLE
PWMx_InitStructure.SecondEdge_Interrupt = DISABLE; //Second flip interrupt enabled, ENABLE or DISABLE
PWMx_InitStructure.FirstEdge = 65; //First flip count, 1~32767
PWMx_InitStructure.SecondEdge = 1220; //Second flip count, 1~32767
PWMx_Configuration(PWM3_ID, &PWMx_InitStructure); //初始化PWM, PWM2_ID,PWM3_ID,PWM4_ID,PWM5_ID,PWM6_ID,PWM7_ID
P21 = 0;
P2n_push_pull(1<<1); //IO initialization, high impedance when powered on
PWMx_InitStructure.PWMx_IO_Select = PWM4_P22; //PWM output IO selection. PWM2_P37,PWM2_P27,PWM3_P21,PWM3_P45,PWM4_P22,PWM4_P44,PWM5_P23,PWM5_P42,PWM6_P16,PWM6_P07,PWM7_P17,PWM7_P06
PWMx_InitStructure.Start_IO_Level = 1; //Set the initial level of the PWM output port, 0 or 1
PWMx_InitStructure.PWMx_Interrupt = DISABLE; //Interrupt enable, ENABLE or DISABLE
PWMx_InitStructure.FirstEdge_Interrupt = DISABLE; //First flip interrupt enabled, ENABLE or DISABLE
PWMx_InitStructure.SecondEdge_Interrupt = DISABLE; //Second flip interrupt enabled, ENABLE or DISABLE
PWMx_InitStructure.FirstEdge = 65-PWM_DeadZone; //First rollover count, 1~32767
PWMx_InitStructure.SecondEdge = 1220+PWM_DeadZone; //Second flip count, 1~32767
PWMx_Configuration(PWM4_ID, &PWMx_InitStructure); //初始化PWM, PWM2_ID,PWM3_ID,PWM4_ID,PWM5_ID,PWM6_ID,PWM7_ID
P22 = 1;
P2n_push_pull(1<<2); //IO initialization, high impedance when powered on
PWM_SourceClk_Duty(PwmClk_1T, 2400); //Clock source: PwmClk_1T, PwmClk_2T, ... PwmClk_16T, PwmClk_Timer2, PWM period: 1~32767
PWMCR |= ENPWM; // Enable the PWM waveform generator and the PWM counter starts counting
// PWMCR &= ~ECBI; // Disable PWM counter reset interrupt
PWMCR |= ECBI; //Enable PWM counter reset interrupt
// PWMFDCR = ENFD | FLTFLIO | FDIO; // PWM failure interrupt control, ENFD | FLTFLIO | EFDI | FDCMP | FDIO
}
/**********************************************/
void main(void)
{
PWM_config(); //Initialize PWM
EA = 1; // Enable global interrupts
while (1)
{
}
}
Copy code
Another program:
#define MAIN_Fosc 24000000L //Define the main clock
#include "STC15Fxxxx.H"
#include "T_SineTable.h"
#define PWM_DeadZone 12 /* Number of dead zone clocks, between 6 and 24 */
u8 PWM_Index; //SPWM lookup table index
//========================================================================
// Function: void PWM_config(void)
// Description: PWM configuration function.
// Parameters: none.
// Returns: none.
// Version: VER1.0
// Date: 2014-8-15
// Remark:
//========================================================================
void PWM_config(void)
{
u8 xdata *px;
EAXSFR(); // Access XFR
px = PWM3T1H; // pointer points to PWM3
*px = 0; // The first flip count high byte
px++;
*px = 65; // The first flip count low byte
px++;
*px = 1220 / 256; // The second flip count high byte
px++;
*px = 1220 % 256; // Second flip count low byte
px++;
*px = 0; // PWM3 output selects P2.1, no interrupt
PWMCR |= 0x02; // The port of the corresponding PWM channel is the PWM output port, which is controlled by the PWM waveform generator
PWMCFG &= ~0x02; // Set the initial level of the PWM output port to 0
// PWMCFG |= 0x02; // Set the initial level of the PWM output port to 1
P21 = 0;
P2n_push_pull(1<<1); //IO initialization, high impedance when powered on
px = PWM4T1H; // pointer points to PWM4
*px = 0; // The first flip count high byte
px++;
*px = 65-PWM_DeadZone; // The first flip count low byte
px++;
*px = (1220+PWM_DeadZone) / 256; // The second flip count high byte
px++;
*px = (1220+PWM_DeadZone) % 256; // The second flip count low byte
px++;
*px = 0; // PWM4 output selects P2.2, no interrupt
PWMCR |= 0x04; // The port of the corresponding PWM channel is the PWM output port, which is controlled by the PWM waveform generator
// PWMCFG &= ~0x04; // Set the initial level of the PWM output port to 0
PWMCFG |= 0x04; // Set the initial level of the PWM output port to 1
P22 = 1;
P2n_push_pull(1<<2); //IO initialization, high impedance when powered on
px = PWMCH; // High byte of PWM counter
*px = 2400 / 256;
px++;
*px = 2400 % 256; // low byte of PWM counter
px++; // PWMCKS, PWM clock selection
*px = PwmClk_1T; // 时钟源: PwmClk_1T,PwmClk_2T, ... PwmClk_16T, PwmClk_Timer2
EAXRAM(); // Restore access to XRAM
PWMCR |= ENPWM; // Enable the PWM waveform generator and the PWM counter starts counting
// PWMCR &= ~ECBI; // Disable PWM counter reset interrupt
PWMCR |= ECBI; // Enable PWM counter reset interrupt
// PWMFDCR = ENFD | FLTFLIO | FDIO; // PWM failure interrupt control, ENFD | FLTFLIO | EFDI | FDCMP | FDIO
}
/**********************************************/
void main(void)
{
PWM_config(); //Initialize PWM
EA = 1; // Enable global interrupts
while (1)
{
}
}
/************************* PWM interrupt function****************************/
void PWM_int (void) interrupt PWM_VECTOR
{
u8 xdata *px;
u16 j;
u8 SW2_tmp;
if(PWMIF & CBIF) //PWM counter reset interrupt flag
{
PWMIF &= ~CBIF; // Clear interrupt flag
SW2_tmp = P_SW2; //Save SW2 settings
EAXSFR(); //Access XFR
px = PWM3T2H; // points to PWM3
j = T_SinTable[PWM_Index];
*px = (u8)(j >> 8); //The second flip count high byte
px++;
*px = (u8)j; //Second flip count low byte
j += PWM_DeadZone; //Dead zone
px = PWM4T2H; // points to PWM4
*px = (u8)(j >> 8); //The second flip count high byte
px++;
*px = (u8)j; //Second flip count low byte
P_SW2 = SW2_tmp; //Restore SW2 settings
if(++PWM_Index >= 200) PWM_Index = 0;
}
/*
if(PWMIF & C2IF) //PWM2 interrupt flag
{
PWMIF &= ~C2IF; // Clear interrupt flag
}
if(PWMIF & C3IF) //PWM3 interrupt flag
{
PWMIF &= ~C3IF; // Clear interrupt flag
}
if(PWMIF & C4IF) //PWM4 interrupt flag
{
PWMIF &= ~C4IF; // Clear interrupt flag
……………………
Previous article:Design of 51 single chip infrared remote control decoder (assembly language)
Next article:MCU traffic light with interrupt
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [RVB2601 Creative Application Development] Temperature and Humidity Sensor Measurement
- 【Actual test】Quality test of data line with running light
- Some questions about air conditioning automatic control system
- Sensorless FOC commutation frequency 700HZ
- Added performance test for STM32L432
- ADI's smoke detector integrated solution based on ADPD188BI has started the registration for the prize live broadcast~
- Prize-winning live broadcast: TI chip technology is helping motor development in progress!
- C2000 MCU DesignDRIVE Solutions for Industrial Motor Drives
- New predictions for cloud computing in 2019
- Typical applications of Zigbee technology in street light control systems