首页 > 其他分享 >STM32或者RSIC-V输出SPWM波形

STM32或者RSIC-V输出SPWM波形

时间:2024-08-27 17:48:00浏览次数:9  
标签:TIM1 SPWM STM32 TIM RSIC 371 373 PWM 375

直接上代码吧,其余的内容可以到别的地方搜索,包括什么是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);
}

标签:TIM1,SPWM,STM32,TIM,RSIC,371,373,PWM,375
From: https://www.cnblogs.com/maodoudou/p/18383234

相关文章

  • STM32与Linux串口双向通信
    STM32与linux双向串口通信实验    本文记录STM32与linux双向串口通信,包含stm32发送、Linux阻塞式接收;Linux发送,STM32阻塞式接收;本实验的目的在于调通数据链路,为之后使用奠定基础。实验平台为:    STM32方面用的是STM32H723ZGT6为核心的开发板;开发环境为VS......
  • STM32 - 按键控制LED灯
    功能:按键控制LED的亮灭。两个按键:PE3和PE2两个LED:PE5和PB5按键PE3控制LED2-PE5;按键PE2控制LED3-PB5main.c:#include"stm32f10x.h"//Deviceheader#include"Delay.h"#include"LED.h"#include"Key.h"uint8_tKeyNum;i......
  • stm32之I2C通信协议
    文章目录前言一、I2C通信协议二、I2C硬件电路三、I2C时序基本单元3.1起始与终止信号3.2发送与接收一个字节3.3发送与接收应答四、I2C时序分析4.1指定地址写4.2当前地址读4.3指定地址读前言提示:本文主要用作在学习江科大自化协STM32入门教程后做的归纳总结笔......
  • 基于STM32F103的FreeRTOS系列(十一)·信号量·二值信号量与计数信号量详细使用以及移植
    目录1. 信号量简介1.1 同步和互斥1.1.1 同步1.1.2 互斥1.1.3 总结1.2 分类1.2.1 二值信号量1.2.2 计数信号量1.2.3 互斥信号量1.2.4 递归信号量2. 信号量控制块3. 常用信号量API函数3.1 创建信号量函数3.1.1 创建二值信号量 xSe......
  • STM32寄存器操作、模板构建
    目录外设寄存器查找①名称②偏移地址③寄存器位表④位功能说明寄存器基本操作C语言的置位和清零具体方法设置GPIO流程给寄存器赋值带参数宏STM32F1xx芯片识别存储器映射寄存器映射让GPIOB端口的16个引脚输出高电平,要怎么实现?STM32寄存器映射C语言对寄存器的封装新建寄......
  • STM32常用下载程序方式
    常用下载程序的两种方式:、通过下载工具(FlyMCU)将hex文件下载到FLASH存储区。、使用烧写器将xxx.axf文件下载到存储区。(KEIL5经过烧写器配置后,直接点击download)有的朋友肯定好奇说:FLASH存储区存的都是最“干净”的二进制数据,hex文件还有那么多描述信息呢。答案:hex文件当然不是......
  • STM32/ARM-M系列 如何用C语言指针操作寄存器 上篇
    1、操作外设实际上就是操作寄存器使用STM32进行编程,我们一般是用官方提供的库函数(HAL库)来操作各种外设。本质上,每个外设都有自己的一组外设寄存器,操作外设就是操作各种外设寄存器。HAL库的各个库函数就是对他们的寄存器操作的高度抽象后的封装。打开stm32f10x的数据手册的地址......
  • STM32学习记录-05 -1-TIM定时中断
    1TIM简介TIM(Timer)定时器定时器可以对输入的时钟进行计数,并在计数值达到设定值时触发中断(如在STM32中主频为72MHz,计72个数即1MHz也就是1us,计72000个数即1KHz也就是1ms)16位计数器、预分频器(对计数器的时钟进行分频)、自动重装寄存器(计数的目标值,计多少个时钟申请中断)的时基单......
  • STM32学习记录-05 -2-TIM输出比较
    1输出比较简介OC(OutputCompare)输出比较输出比较可以通过比较CNT与CCR寄存器值的关系,来对输出电平进行置1、置0或翻转的操作,用于输出一定频率和占空比的PWM波形每个高级定时器和通用定时器都拥有4个输出比较通道高级定时器的前3个通道额外拥有死区生成和互补输出的功能2......
  • STM32学习记录-04-EXTI外部中断
    1中断系统(1)中断:在主程序运行过程中,出现了特定的中断触发条件(中断源),使得CPU暂停当前正在运行的程序,转而去处理中断程序,处理完成后又返回原来被暂停的位置继续运行(2)中断优先级:当有多个中断源同时申请中断时,CPU会根据中断源的轻重缓急进行裁决,优先响应更加紧急的中断源(3)中断嵌......