直接上代码吧,其余的内容可以到别的地方搜索,包括什么是SPWM
/*@Note
PWM output routine:
TIM1_CH1(PA8)
This example demonstrates that the TIM_CH1(PA8) pin outputs PWM in PWM mode 1 and
PWM mode 2.
*/
#include "debug.h"
/* PWM Output Mode Definition */
#define PWM_MODE1 0
#define PWM_MODE2 1
/* PWM Output Mode Selection */
//#define PWM_MODE PWM_MODE1
#define PWM_MODE PWM_MODE2
const int indexWave[] = {
0,2,5,8,10,12,14,16,19,22,24,26,28,30,33,36,38,
40,42,44,47,50,52,54,56,58,61,64,66,68,70,72,74,
77,80,82,84,86,88,91,94,96,98,100,102,104,106,109,112,
114,116,118,120,122,124,126,129,132,134,136,138,140,142,144,146,
148,150,152,155,158,160,162,164,166,168,170,172,174,176,178,180,
182,184,186,188,190,192,194,196,198,200,202,204,206,208,210,212,
214,216,218,220,222,224,226,228,230,231,232,234,236,238,240,242,
244,246,248,249,250,252,254,256,258,260,261,262,264,266,268,270,
271,272,274,276,277,278,280,282,284,285,286,288,290,291,292,294,
295,296,298,300,301,302,304,305,306,308,309,310,312,313,314,316,
317,318,319,320,322,323,324,325,326,327,328,330,331,332,333,334,
335,336,337,338,339,340,341,342,343,344,345,345,346,347,348,349,
350,351,351,352,353,353,354,355,356,357,357,358,359,359,360,361,
361,361,362,363,363,363,364,365,365,365,366,367,367,367,368,369,
369,369,369,369,370,371,371,371,371,371,371,371,371,372,373,373,
373,373,373,373,373,373,373,373,373,373,373,373,373,373,373,373,
373,373,373,373,372,371,371,371,371,371,371,371,371,370,369,369,
369,369,369,368,367,367,367,366,365,365,365,364,363,363,363,362,
361,361,361,360,359,359,358,357,357,356,355,354,353,353,352,351,
351,350,349,348,347,346,345,345,344,343,342,341,340,339,338,337,
336,335,334,333,332,331,330,328,327,326,325,324,323,322,320,319,
318,317,316,314,313,312,310,309,308,306,305,304,302,301,300,298,
296,295,294,292,291,290,288,286,285,284,282,280,278,277,276,274,
272,271,270,268,266,264,262,261,260,258,256,254,252,250,249,248,
246,244,242,240,238,236,234,232,231,230,228,226,224,222,220,218,
216,214,212,210,208,206,204,202,200,198,196,194,192,190,188,186,
184,182,180,178,176,174,172,170,168,166,164,162,160,158,155,152,
150,148,146,144,142,140,138,136,134,132,129,126,124,122,120,118,
116,114,112,109,106,104,102,100,98,96,94,91,88,86,84,82,
80,77,74,72,70,68,66,64,61,58,56,54,52,50,47,44,
42,40,38,36,33,30,28,26,24,22,19,16,14,12,10,8,
5,2,0,-2,-4,-7,-10,-12,-14,-16,-18,-21,-24,-26,-28,-30,
-32,-35,-38,-40,-42,-44,-46,-49,-52,-54,-56,-58,-60,-63,-66,-68,
-70,-72,-74,-76,-79,-82,-84,-86,-88,-90,-93,-96,-98,-100,-102,-104,
-106,-108,-111,-114,-116,-118,-120,-122,-124,-126,-128,-131,-134,-136,-138,-140,
-142,-144,-146,-148,-150,-152,-154,-157,-160,-162,-164,-166,-168,-170,-172,-174,
-176,-178,-180,-182,-184,-186,-188,-190,-192,-194,-196,-198,-200,-202,-204,-206,
-208,-210,-212,-214,-216,-218,-220,-222,-224,-226,-228,-230,-232,-233,-234,-236,
-238,-240,-242,-244,-246,-248,-250,-251,-252,-254,-256,-258,-260,-262,-263,-264,
-266,-268,-270,-272,-273,-274,-276,-278,-279,-280,-282,-284,-286,-287,-288,-290,
-292,-293,-294,-296,-297,-298,-300,-302,-303,-304,-306,-307,-308,-310,-311,-312,
-314,-315,-316,-318,-319,-320,-321,-322,-324,-325,-326,-327,-328,-329,-330,-332,
-333,-334,-335,-336,-337,-338,-339,-340,-341,-342,-343,-344,-345,-346,-347,-347,
-348,-349,-350,-351,-352,-353,-353,-354,-355,-355,-356,-357,-358,-359,-359,-360,
-361,-361,-362,-363,-363,-363,-364,-365,-365,-365,-366,-367,-367,-367,-368,-369,
-369,-369,-370,-371,-371,-371,-371,-371,-372,-373,-373,-373,-373,-373,-373,-373,
-373,-374,-375,-375,-375,-375,-375,-375,-375,-375,-375,-375,-375,-375,-375,-375,
-375,-375,-375,-375,-375,-375,-375,-375,-374,-373,-373,-373,-373,-373,-373,-373,
-373,-372,-371,-371,-371,-371,-371,-370,-369,-369,-369,-368,-367,-367,-367,-366,
-365,-365,-365,-364,-363,-363,-363,-362,-361,-361,-360,-359,-359,-358,-357,-356,
-355,-355,-354,-353,-353,-352,-351,-350,-349,-348,-347,-347,-346,-345,-344,-343,
-342,-341,-340,-339,-338,-337,-336,-335,-334,-333,-332,-330,-329,-328,-327,-326,
-325,-324,-322,-321,-320,-319,-318,-316,-315,-314,-312,-311,-310,-308,-307,-306,
-304,-303,-302,-300,-298,-297,-296,-294,-293,-292,-290,-288,-287,-286,-284,-282,
-280,-279,-278,-276,-274,-273,-272,-270,-268,-266,-264,-263,-262,-260,-258,-256,
-254,-252,-251,-250,-248,-246,-244,-242,-240,-238,-236,-234,-233,-232,-230,-228,
-226,-224,-222,-220,-218,-216,-214,-212,-210,-208,-206,-204,-202,-200,-198,-196,
-194,-192,-190,-188,-186,-184,-182,-180,-178,-176,-174,-172,-170,-168,-166,-164,
-162,-160,-157,-154,-152,-150,-148,-146,-144,-142,-140,-138,-136,-134,-131,-128,
-126,-124,-122,-120,-118,-116,-114,-111,-108,-106,-104,-102,-100,-98,-96,-93,
-90,-88,-86,-84,-82,-79,-76,-74,-72,-70,-68,-66,-63,-60,-58,-56,
-54,-52,-49,-46,-44,-42,-40,-38,-35,-32,-30,-28,-26,-24,-21,-18,
-16,-14,-12,-10,-7,-4,-2
};
//控制输出波形的频率
__IO uint16_t period_class = 24; 正弦频率未24HZ
#define POINT_NUM 1000 // 每个正弦周期对应1000个点
/**
* @brief 配置嵌套向量中断控制器NVIC
* @param 无
* @retval 无
*/
static void NVIC_Config_PWM(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 配置TIM3_IRQ中断为中断源 */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*********************************************************************
* @fn TIM1_OutCompare_Init
*
* @brief Initializes TIM1 output compare.
*
* @param arr - the period value.
* psc - the prescaler value.
* ccp - the pulse value.
*
* @return none
*/
void TIM1_PWMOut_Init( u16 arr, u16 psc, u16 ccp )
{
GPIO_InitTypeDef GPIO_InitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* 基本定时器配置 ,配合PWM表点数、中断服务函数中的period_cnt循环次数设置*/
/* 设置使得整个呼吸过程为3秒左右即可达到很好的效果 */
/* 基本定时器配置 */
//当定时器从0计数到 TIM_Period+1 ,为一个定时周期
TIM_TimeBaseStructure.TIM_Period = arr;
//设置预分频
TIM_TimeBaseStructure.TIM_Prescaler = psc;
//设置时钟分频系数:不分频(这里用不到)
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
//向上计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM模式配置 */
//配置为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//使能输出
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
//设置初始PWM脉冲宽度为0
TIM_OCInitStructure.TIM_Pulse = ccp;
//当定时器计数值小于CCR1_Val时为低电平
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init ( TIM1, &TIM_OCInitStructure ); //使能通道
TIM_OC1PreloadConfig ( TIM1, TIM_OCPreload_Enable ); //使能预装载
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIM重载寄存器ARR
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //使能update中断
NVIC_Config_PWM();
/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE); //使能定时器
TIM_CtrlPWMOutputs(TIM1, ENABLE );
}
/**
* timer1 IRQHandler
*/
void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_UP_IRQHandler(){
static uint16_t pwm_index = 0; //用于PWM查表
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET){
TIM_SetCompare1(TIM1, POINT_NUM / 2 + indexWave[+ pwm_index++]);
if(pwm_index >= POINT_NUM){
pwm_index = 0;
}
TIM_ClearITPendingBit (TIM1, TIM_IT_Update); //必须要清除中断标志位
}
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
USART_Printf_Init(115200);
SystemCoreClockUpdate();
printf("SystemClk:%d\r\n",SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
TIM1_PWMOut_Init( 1000, 3, 10 ); // 设置24KHz的波形
while(1);
}