我正在编写一个程序,该程序应该在我的 STM32f3Discovery 板上使用 DAC 生成正弦波。我有几个文件,如下所列。
实用程序.h:
#ifndef UTILITIES_H
#define UTILITIES_H
/** @brief Configure GPIOE, pin 9 (LED) as output PP
*/
void GPIOE_init( void );
/** @brief Configure Timer 2, event frequency - 100Hz
*/
void Timer_init( void );
/** @brief Configure interrupt handler
*/
void NVIC_config( void );
/** @brief Configure and enable DAC channel 1 on pin PA4,
* with TIM2 TRGO as trigger
*/
void DACch1_config( void );
/** @brief Configure DMA to work with DAC
*/
void DMA_config( void );
#endif //UTILITIES_H
实用程序.c:
#include "utilities.h"
#include "stm32f30x.h"
const uint16_t sineWave12bit[32] = {
2048,2447,2831,3185,3495,3750,3939,4056,
4095,4056,3939,3750,3495,3185,2831,2447,
2048,1648,1264,910,600,345,156,39,
0,39,156,345,600,910,1264,1648 };
//This address is taken from reference manual for stm32f30
//See pages 53 and 439
#define DAC_DHR12RD_ADDRESS 0x40007420
void Timer_init( void ){
/* Configuring frequency of timer, thus sinus frequency */
/* Keep in mind, that system clock is set to 80Mhz */
TIM_TimeBaseInitTypeDef timer2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructInit( &timer2 );
timer2.TIM_CounterMode = TIM_CounterMode_Up;
//Setting prescaler so that timer clock will be only 10000Hz
timer2.TIM_Prescaler = 8000 - 1;
//Setting period so Timer will give signal of frequency 100Hz
timer2.TIM_Period = 100 - 1;
TIM_TimeBaseInit( TIM2, &timer2 );
//generate event when counter hits TIM_period value
TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void NVIC_config( void ){
NVIC_InitTypeDef nvic;
nvic.NVIC_IRQChannel = TIM2_IRQn;
nvic.NVIC_IRQChannelPreemptionPriority = 0;
nvic.NVIC_IRQChannelSubPriority = 0;
nvic.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &nvic );
}
void GPIOE_init( void ){
/* GPIOE initialization */
GPIO_InitTypeDef leds;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOE, ENABLE );
leds.GPIO_Pin = GPIO_Pin_9;
leds.GPIO_Mode = GPIO_Mode_OUT;
leds.GPIO_OType = GPIO_OType_PP;
leds.GPIO_Speed = GPIO_Speed_50MHz;
leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &leds);
}
void DACch1_config( void ){
/*******THERE IS ONLY ONE DAC ON STM32F30!!!!!!!!!!!!!!!!!!******************/
DAC_InitTypeDef DACch1;
GPIO_InitTypeDef DACch1_out;
/* Enable GPIOA Periph clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Configure PA04 as analog */
DACch1_out.GPIO_Pin = GPIO_Pin_4;
DACch1_out.GPIO_Mode = GPIO_Mode_AN;
DACch1_out.GPIO_OType = GPIO_OType_PP;
DACch1_out.GPIO_PuPd = GPIO_PuPd_NOPULL;
DACch1_out.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &DACch1_out);
/* Enable DAC clock */
RCC_APB1PeriphClockCmd( RCC_APB1Periph_DAC, ENABLE );
/* Init the DAC_Init structure */
DAC_StructInit(&DACch1);
/* Configure DAC */
DACch1.DAC_Trigger = DAC_Trigger_T2_TRGO;
DACch1.DAC_WaveGeneration = DAC_WaveGeneration_None;
DACch1.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits2_0;
DACch1.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
/* DAC channel 1 configuration*/
DAC_Init( DAC_Channel_1, &DACch1 );
/* Enable DAC channel 1 */
DAC_Cmd( DAC_Channel_1, ENABLE);
}
void DMA_config( void ){
DMA_InitTypeDef mem2DAC;
/* Enable clock for DMA1 */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1, ENABLE );
/* Reset DMA1 channel 1 register values to default */
DMA_DeInit(DMA1_Channel1);
//Specify destination address
mem2DAC.DMA_PeripheralBaseAddr = DAC_DHR12RD_ADDRESS;
//Specify source address - LUT for sine
mem2DAC.DMA_MemoryBaseAddr = (uint32_t) &sineWave12bit;
//Set Peripheral as destination
mem2DAC.DMA_DIR = DMA_DIR_PeripheralDST;
mem2DAC.DMA_BufferSize = 32;
mem2DAC.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
mem2DAC.DMA_MemoryInc = DMA_MemoryInc_Enable;
mem2DAC.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
mem2DAC.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
mem2DAC.DMA_Mode = DMA_Mode_Circular;
mem2DAC.DMA_Priority = DMA_Priority_High;
mem2DAC.DMA_M2M = DMA_M2M_Disable;
DMA_Init( DMA1_Channel1, &mem2DAC );
/* Enable DMA1 Channel1 */
DMA_Cmd( DMA1_Channel1, ENABLE );
/* Enable DMA fo DAC channel 1*/
DAC_DMACmd( DAC_Channel_1, ENABLE );
}
主.c:
/**
******************************************************************************
* @file main.c
* @author Ac6
* @version V1.0
* @date 01-December-2013
* @brief Default main function.
******************************************************************************
*/
#include "stm32f30x.h"
#include "utilities.h"
int main(void)
{
DMA_config();
DACch1_config();
Timer_init();
while(1){
}
}
所以理论上它应该在引脚 A.04 上产生正弦波。但是,当我使用示波器查看此引脚上的电压时,它什么也没有显示(接地电位)。
有谁知道可能出了什么问题?