用STM32F373生成正弦波时发现这样一个奇怪的问题:需要将PA8口或PB1口设置为复用功能时才能够输出正弦波。
部分代码如下:
unsigned int sin_amp[100];
void Clock_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC \
| RCC_AHBPeriph_GPIOD |RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOF , ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable DAC clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Configure PA.04 (DAC1_OUT1) in analog mode -------------------------*/
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#if 1
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
}
int main(void)
{
unsigned char i;
SystemInit();
Clock_Configuration();
TIM_INT_Config();
GPIO_Configuration();
#if 0
GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_2);
#endif
DAC_Config();
TIM3_Configuration();
for(i=0; i<100; i++)
sin_amp[i] = (unsigned int)(0x7FF+(float)0x7FF*sin(2*3.1415926*(float)i*0.2/(float)20));
while(1);
}
void DAC_Config(void)
{
DAC_InitTypeDef DAC_InitStruct;
/* DAC1 channel1 Configuration */
DAC_InitStruct.DAC_Trigger = DAC_Trigger_T3_TRGO;
DAC_InitStruct.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStruct.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC1, DAC_Channel_1, &DAC_InitStruct);
/* Enable DAC1 Channel1 */
DAC_Cmd(DAC1, DAC_Channel_1, ENABLE);
}
void TIM_INT_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuration()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 0x10;
TIM_TimeBaseStructure.TIM_Period = 0x70;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM3,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 = 0x39;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_3;
TIM_BDTRInitStructure.TIM_DeadTime = 4;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM3, &TIM_BDTRInitStructure);
TIM3->SR = 0;
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
TIM_CtrlPWMOutputs(TIM3, ENABLE);
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
}
void TIM3_IRQHandler(void)
{
static unsigned char i=0;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
if(i>= 100)
i=0;
DAC_SetChannel1Data(DAC1, DAC_Align_12b_R, sin_amp[i] );
i++;
}
TIM3->SR &= 0xfffe;
} |