STM32或者RSIC-V输出SPWM波形

news/2024/11/16 12:43:28/文章来源:https://www.cnblogs.com/maodoudou/p/18383234

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/788088.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Fins TCP协议理解及C Sharp实现思路

假设本文中使用到设备的ip地址,用于后续内容的理解: 客户端(本机电脑 windows系统)IP: 192.168.1.101 服务端(PLC omron CJ2M系列)IP 和 端口号 : 192.168.1.10 : 9600注意: ①本文中的 FINS TCP 报文都是以16进制(Hex)发送出去的,所以对应的转换也都会转成16进制的形…

poc电路

POC电路概念: POC(Power Over Coaxia)一种基于同轴线缆传输的视频信号、同轴控制,电源叠加的技术。在叠加过程中,难度最大的是解决直流电源与高频视频信号叠加传输的问题,保证高频视频信号不失真,低频控制信号不出现乱码。 POC工作原理:POC设计要点:选择电感时的关键参数…

不劳而获?

天雷无妄卦二爻爻辞:不耕获,不菑畲…… 这六个字啥意思?不耕种而有收获不开荒而有熟田?所以就是说可以,不劳而获?嗯? 醒醒吧! 不劳,怎么可能有获?! 当寄生虫还得劳呢,否则只能饿死!其实,换一种理解,还真是可以做到不劳而获,关键就看“劳”与“获”是怎样的一种…

CF1630F-最小割、Dilworth定理

link:https://codeforces.com/contest/1630/problem/F 给你一个由 \(n\) 个顶点组成的无向图,编号从 \(1\) 到 \(n\) ,其中顶点 \(i\) 的值为 \(a_i\) ,所有值 \(a_i\) 都是不同的。如果 \(a_u\) 整除 \(a_v\) ,则两个顶点 \(u\) 和 \(v\) 之间存在一条边。当删除一个顶点…

DocKylin: A Large Multimodal Model for Visual Document Understanding with Efficient Visual Slimming

DocKylin: A Large Multimodal Model for Visual Document Understanding with Efficient Visual Slimming arxiv:http://arxiv.org/abs/2406.19101 视觉处理器+LLM:视觉处理器:Swin Transformer 创新点:通过:1、去除图片冗余像素;2、去除冗余token。来减小模型中的视觉处…

Lab 2: Key/Value Server

6.5840 Lab 2: Key/Value Server 1.Introduction 本次Lab将构建一个单机的键值服务器,该服务器保证即使存在网络故障,每个操作也都只执行一次,并且这些操作线性化执行。后续Lab中,将复制这样的服务器来处理服务器崩溃的情况。 键值服务器支持三种RPC(远程过程调用)操作:Put…

esp-idf vscode debug command espIdf.getXtensaGdb not found

esp32 idf vscode debug错误 vscode中配置文件采用的是正点原子的,调用gdb的时候,提示报错,找不到相应的命令 launch.json文件中gdb的配置如下 {"version": "0.2.0","configurations": [ { "name": "GDB", "type&qu…

Debian12、Ubuntu22安装英特尔wifi驱动

1、打开英特尔无线适配器的 Linux* 支持查看wifi所需的内核版本 以AX200为例,需要Linux内核版本为5.1,Debian12默认内核版本为6.1,Ubuntu24默认内核版本为6..8,因此不需要更新内核。2、打开适用于 Linux* 的英特尔 无线 Wi-Fi 驱动程序下载内核(非必要步骤)和驱动。3、安…

牛客周赛 Round 57

B 可以直接统计每条边两个点的情况即可,不用DFS。 F 写法和这个差不多。可以用map、set、统计这些方法,计算动态的一个数组的最大数。 可以直接用map统计就行,map已经自动给你排好序了(从小到大)。1 #include <bits/stdc++.h>2 using namespace std;3 #define LL lo…