pwm生成正弦波

#include"stdio.h"
#include"gd32f30x.h"
#include"keydiver.h"
#include "systick.h"
	timer_parameter_struct timercfg;
	timer_oc_parameter_struct pwmcfg;	
	dma_parameter_struct dmacfg;

  float t=0;
	float dt=0.02;
	float basefreq=0.5;
	unsigned short cvs[1000]={0};
int main()
{
	int i;
	for( i=0;i<1000;++i)
	{
		unsigned short cv=timercfg.period*0.5f*(sin(2.0f*3.1415f*basefreq*i*dt)+1.0f);
		cvs[i]=cv;
	}
	rcu_periph_clock_enable(RCU_GPIOB);
  systick_config();
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_AF);
	gpio_pin_remap_config(GPIO_TIMER0_PARTIAL_REMAP, ENABLE);
  gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);
  gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);

	rcu_periph_clock_enable(RCU_TIMER0);
	timer_struct_para_init(&timercfg);
	timercfg.prescaler =2400-1;
	timercfg.alignedmode=TIMER_COUNTER_EDGE;
	timercfg.counterdirection=TIMER_COUNTER_UP;
	timercfg.period=100u-1;
	timercfg.clockdivision=TIMER_CKDIV_DIV1;
	timercfg.repetitioncounter=0;
	timer_init(TIMER0,&timercfg);
	
	pwmcfg.outputstate  = (uint16_t)TIMER_CCX_ENABLE;
  pwmcfg.outputnstate = TIMER_CCXN_ENABLE;
  pwmcfg.ocpolarity   = TIMER_OC_POLARITY_HIGH;
  pwmcfg.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
  pwmcfg.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  pwmcfg.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
	timer_channel_output_config(TIMER0,TIMER_CH_0,&pwmcfg);
  timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
	timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,2000);
	
  timer_channel_output_state_config(TIMER0,TIMER_CH_0,ENABLE);
  timer_primary_output_config(TIMER0,ENABLE);
	timer_dma_enable(TIMER0,TIMER_DMA_UPD);
  timer_enable(TIMER0);
  rcu_periph_clock_enable(RCU_DMA0);
	dmacfg.periph_addr  = TIMER0+0x34;
  dmacfg.periph_width =DMA_PERIPHERAL_WIDTH_16BIT; 
  dmacfg.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
  dmacfg.memory_addr  = (uint32_t) cvs;
  dmacfg.memory_width = DMA_MEMORY_WIDTH_16BIT;
  dmacfg.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
  dmacfg.number       = 1000U;
  dmacfg.direction    = DMA_MEMORY_TO_PERIPHERAL;
  dmacfg.priority     = DMA_PRIORITY_LOW;
	dma_init(DMA0,DMA_CH4,&dmacfg);
	dma_circulation_enable(DMA0,DMA_CH4);
	dma_channel_enable(DMA0,DMA_CH4);
	
	while(1)
	{
		 
	}

	
}

配channel4对应中断,置为更新事件使100周期满了之后触发一次中断,配置dma外设宽度和内存宽度16位,外设地址不增加,内存地址增加,方向内存到外设,循环发送,dma将cvs的值传送到比较值里面,使不用通过中断就能修改比较值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值