Skip to main content
jean_prieur
Associate III
April 10, 2013
Question

STM32F4DISCOVERY using DAC pins A4 & A5 without the codec

  • April 10, 2013
  • 15 replies
  • 2508 views
Posted on April 10, 2013 at 15:44

Hello,

I'm using a STM32F4DISCOVERY to program the firmware of my future board. I want to use the integrated 12 bit DAC of the board to generate analog simple signals on the A4 and A5 pins, but I don't want to use the external audio codec CS43L22 of the discovery board. On theSTM32F4DISCOVERY board A4 and A5 are connected to the LIS30 and the CS43L22 but I think it's OK to use these pins for generate analog signals. Am I right ? So I try to program with this code I found on a forum:


int
main(
void
)

{

GPIO_InitTypeDef GPIO_InitStructure;

DAC_InitTypeDef DAC_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


/* Enable DAC and GPIOC clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC | RCC_APB1Periph_TIM2, ENABLE);


/* Configure PA.04/05 (DAC) as output -------------------------*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* DAC channel1 Configuration */

DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;

DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;

DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;

DAC_Init(DAC_Channel_2, &DAC_InitStructure);


/* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is

automatically connected to the DAC converter. */

DAC_Cmd(DAC_Channel_2, ENABLE);


while
(1)

{

int
i;


for
(i=0; i<4096; i++)

DAC_SetChannel2Data(DAC_Align_12b_R, i);

}



/* does not exit - kind of important */


return
(1);

}

This code works on STM32F3xx but there is some problems with STM32F4xxcompactibility. Line 8, ''RCC_APB2Periph_AFIO'' and ''RCC_APB2Periph_GPIOA'' are not declared in librairies. Is anyone know what are the RCC_APB2Periph_AFIO andRCC_APB2Periph_GPIOA equivalent in STM32F4xx ? Thanks for your help ! note: I'm using the latest STM32F4xx librairies, but not thestm32f4_discovery librairy. #dac #stm32f407 #stm32f4discovery
This topic has been closed for replies.

15 replies

jean_prieur
Associate III
April 13, 2013
Posted on April 13, 2013 at 15:29

I try with IAR; same problem...

It seems it's a problem with my code

jean_prieur
Associate III
April 17, 2013
Posted on April 17, 2013 at 11:37

I still have not found a solution ... I try everything... Do you have any ideas ?

Thanks !!

Tesla DeLorean
Guru
April 17, 2013
Posted on April 17, 2013 at 17:19

I built something materially similar in Keil, seemed to function, posted hex from linker. What is the issue here, with the sawtooth output, or the reset? Confused

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
April 17, 2013
Posted on April 17, 2013 at 17:43

Ok the problem is fixed !

init_DAC.c

/* Includes */
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_dac.h''
#include ''stm32f4xx_tim.h''
#define DAC_DHR12R2_ADDRESS 0x40007414
#define DAC_DHR8R1_ADDRESS 0x40007410
DAC_InitTypeDef DAC_InitStructure;
void
DAC_Config(
void
)
{
/* Preconfiguration before using DAC----------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
/* DMA1 clock and GPIOA clock enable (to be used with DAC) */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);
/* DAC Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* TIM6 Configuration ------------------------------------------------------*/
TIM6_Config();
DAC_DeInit();
}
void
TIM6_Config(
void
)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM6 Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 0xFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
/* TIM6 TRGO selection */
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
/* TIM6 enable counter */
TIM_Cmd(TIM6, ENABLE);
}
void
Set_DAC_Ch2_Voltage(
int
voltage)
{
/* DAC channel1 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Enable DAC Channel2 */
DAC_Cmd(DAC_Channel_2, ENABLE);
/* Set DAC channel2 DHR12RD register */
DAC_SetChannel2Data(DAC_Align_12b_R, voltage);
}

main.c

#include ''init_DAC.h''
int
main(
void
)
{
DAC_Config();
Set_DAC_Ch1_Voltage(4095);
Set_DAC_Ch2_Voltage(1000);
while
(1)
{
}
return
(1);
}

yannick
Visitor II
July 15, 2013
Posted on July 15, 2013 at 10:39

Hi,

In fact it's doesn't resolved. I had the same problem. Sometime my DAC was good and after not. You MUST use DAC_StructInit(&

DAC_InitStructure

); or write explicitly

DAC_InitStructure

.DAC_LFSRUnmask_TriangleAmplitude=0; else the member .DAC_LFSRUnmask_TriangleAmplitude of

DAC_InitStructure

will not be initialize to 0.