#include "stm32f10x.h"
#include "stm32f10x_dma.h"
#include "spwm.h"
#define TIM1_CCR1_Address ((uint32_t)0x40012C34)
#define TIM8_CCR2_Address ((uint32_t)0x40013438)
#define TIM1_CCR3_Address ((uint32_t)0x40012C3C)
#define TIM8_CCR3_Address ((uint32_t)0x4001343C)
float data[506]={
0.012516,0.02503,0.03754,0.050044,0.062541,0.075027,0.087502,0.099963,0.11241,0.12484,
0.13724,0.14963,0.16199,0.17433,0.18664,0.19893,0.21118,0.22339,0.23557,0.24772,0.25983,
0.27189,0.28392,0.29589,0.30783,0.31971,0.33154,0.34333,0.35505,0.36673,0.37834,0.3899,
0.40139,0.41283,0.42419,0.43549,0.44673,0.45789,0.46898,0.48000,0.49094,0.50181,0.51259,
0.52330,0.53392,0.54446,0.55492,0.56529,0.57557,0.58576,0.59586,0.60586,0.61577,0.62558,0.6353,
0.64492,0.65443,0.66384,0.67315,0.68235,0.69145,0.70044,0.70932,0.71808,0.72674,0.73528,0.7437,
0.75201,0.76020,0.76827,0.77623,0.78406,0.79176,0.79935,0.80680,0.81414,0.82134,0.82841,0.83536,
0.84218,0.84886,0.85541,0.86182,0.86810,0.87425,0.88026,0.88613,0.89186,0.89745,0.9029,0.90821,
0.91338,0.91840,0.92328,0.92802,0.93261,0.93705,0.94135,0.94550,0.94950,0.95335,0.95705,0.96061,
0.96401,0.96726,0.97036,0.97331,0.97611,0.97875,0.98124,0.98358,0.98576,0.98779,0.98966,0.9913,
0.99294,0.99435,0.99560,0.99669,0.99763,0.99841,0.99904,0.99951,0.99982,0.99998,0.99998,0.99982,
0.99951,0.99904,0.99841,0.99763,0.99669,0.99560,0.99435,0.99294,0.99138,0.98966,0.98779,0.98576,
0.98358,0.98124,0.97875,0.97611,0.97331,0.97036,0.96726,0.96401,0.96061,0.95705,0.95335,0.9495,
0.94550,0.94135,0.93705,0.93261,0.92802,0.92328,0.91840,0.91338,0.90821,0.9029,0.89745,0.89186,
0.88613,0.88026,0.87425,0.86810,0.86182,0.85541,0.84886,0.84218,0.83536,0.82841,0.82134,0.81414,
0.80680,0.79935,0.79176,0.78406,0.77623,0.76827,0.76020,0.75201,0.74370,0.73528,0.72674,0.71808,
0.70932,0.70044,0.69145,0.68235,0.67315,0.66384,0.65443,0.64492,0.63530,0.62558,0.61577,0.60586,
0.59586,0.58576,0.57557,0.56529,0.55492,0.54446,0.53392,0.52330,0.51259,0.50181,0.49094,0.48,
0.46898,0.45789,0.44673,0.43549,0.42419,0.41283,0.40139,0.38990,0.37834,0.36673,0.35505,0.34333,
0.33154,0.31971,0.30783,0.29589,0.28392,0.27189,0.25983,0.24772,0.23557,0.22339,0.21118,0.19893,
0.18664,0.17433,0.16199,0.14963,0.13724,0.12484,0.11241,0.099963,0.087502,0.075027,0.062541,0.050044,
0.03754,0.02503,0.012516,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
};
uint16_t data1[506],data2[506],data3[506],data4[506];
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
/* Private function prototypes -----------------------------------------------*/
void spwm_Init1(int f)
{
RCC_Configuration();
GPIO_Configuration();
DMA_Configuration();
TIM_Configuration(f);
}
void RCC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1|RCC_APB2Periph_TIM8|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC , ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //tim1_ch1,2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //tim8_ch2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel5);
DMA_DeInit(DMA2_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR1_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&data1;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 506;
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);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM8_CCR2_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&data2;
DMA_Init(DMA2_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel5, ENABLE);
DMA_Cmd(DMA2_Channel1, ENABLE);
}
void TIM_Configuration(int f)
{
u16 i;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure;
TimerPeriod = (SystemCoreClock / (4*f) ) - 1;
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
for(i=0;i<506;i++)
{
data1= (uint16_t)(data*(TimerPeriod - 1));
data2[505-i]= (uint16_t)(data*(TimerPeriod - 1));
}
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE);
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 = Channel1Pulse;
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_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM_DeadTime = 0x00;
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_BDTRConfig(TIM8,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
void spwm_Init2(void)
{
RCC_Configuration2();
GPIO_Configuration2();
DMA_Configuration2();
TIM_Configuration2();
}
void RCC_Configuration2(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_TIM1|RCC_APB2Periph_TIM8|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC , ENABLE);
}
void GPIO_Configuration2(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_13 | GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8|GPIO_Pin_9 | GPIO_Pin_10; //tim1_ch1,2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6; //tim8_ch2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void DMA_Configuration2(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel6);
DMA_DeInit(DMA2_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR3_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&data3;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 506;
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_Channel6, &DMA_InitStructure);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM8_CCR3_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&data4;
DMA_Init(DMA2_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel6, ENABLE);
DMA_Cmd(DMA2_Channel4, ENABLE);
}
void TIM_Configuration2(void)
{
u16 i;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure;
TimerPeriod = (SystemCoreClock / 25300 ) - 1;
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
for(i=0;i<506;i++)
{
data3= (uint16_t)(data*(TimerPeriod - 1));
data4[505-i]= (uint16_t)(data*(TimerPeriod - 1));
}
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE);
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 = Channel1Pulse;
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_OCNIdleState_Reset;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM_DeadTime = 0x00;
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_BDTRConfig(TIM8,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
}
就是这个程序,,一直是TIM8 CH3和TIM1 CH3没有值输出,,感觉DMA没有动静