STC15W4K60S2 MCU 2-way SPWM source program can drive the motor

Publisher:来来去去来来Latest update time:2020-12-05 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

……………………


Reference address:STC15W4K60S2 MCU 2-way SPWM source program can drive the motor

Previous article:Design of 51 single chip infrared remote control decoder (assembly language)
Next article:MCU traffic light with interrupt

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号