有很多种方案可以选择,我现在你解决的方案是用定时器和DMA来做的,还可以有其他很多方法我还没有去试,如果有其他方法成功了的期待分享,不多说了,直接上程序:
- #include "stm32f10x.h"
- #define TIM1_CCR3_Address 0x40012C3C
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- uint16_t SRC_Buffer[3] = {500, 500, 500};
- ErrorStatus HSEStartUpStatus;
- /* Private function prototypes -----------------------------------------------*/
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void DMA_Configuration(void);
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- DMA_Configuration();
- /* TIM1 Peripheral Configuration --------------------------------------------*/
- /* Time Base configuration */
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 1000-1;//72M/x=frq
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 2;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /* Channel 3 Configuration in PWM mode */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 127;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
- TIM_OC3Init(TIM1, &TIM_OCInitStructure);
- /* TIM1 Update DMA Request enable */
- TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
- /* TIM1 counter enable */
- TIM_Cmd(TIM1, ENABLE);
- /* Main Output Enable */
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- while (1)
- {}
- }
- void RCC_Configuration(void)
- {
- SystemInit();
- /* TIM1, GPIOA and GPIOB clock enable */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA |
- RCC_APB2Periph_GPIOB, ENABLE);
- /* DMA clock enable */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* GPIOA Configuration: Channel 3 as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* GPIOB Configuration: Channel 3N as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- /* DMA1 Channel5 Config */
- DMA_DeInit(DMA1_Channel5);
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR3_Address;
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SRC_Buffer;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
- DMA_InitStructure.DMA_BufferSize = 3;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel5, &DMA_InitStructure);
- /* DMA1 Channel5 enable */
- DMA_Cmd(DMA1_Channel5, ENABLE);
- }
复制代码
上面的数组SRC_Buffer[3] = {100, 200, 500};
是作为SPWM波的产生数组,需要自己去取码表,还有就是用一些软件取出来的码表可能有负值,所以需要将这样的码表整体抬升成正值。
关键的问题来了,就是这数组怎么取出来的这是关键的,lz说说你怎么取得数组
本来想在上面说明一下,我以为大家都知道,很简单,有一个SPWM码表产生的软件,网上应该能找到,只是取出来的数应该有负数,所以需要抬升,至于抬升的办法就看自己了。可以用EXCEL来做,也可以自己写一个简单的c程序。上面的SPWM码表的产生其实也可以自己写一个简单的C程序来完成,这就需要自己想一点算法了。
我就好人做到底把SPWM波码表的产生软件和我自己写的一个码表的抬升程序都上传了吧。抬升程序解压之后在码表抬升软件文件夹下的debug文件夹下,直接运行daydayup。exe就可以了,第一次提醒输入抬升的码表的码值个数,然后提醒输入码表(可以直接粘贴上去),然后会提醒输入要抬升多少,最后会以一定的格式输出,自己再复制出来就可以了。
SPWM波产生软件.rar
(159.56 KB, 下载次数: 83)
|