cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY using DAC pins A4 & A5 without the codec

jean_prieur
Associate III
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
15 REPLIES 15
jean_prieur
Associate III
Posted on April 13, 2013 at 13:38

I try with two others toolchains but it's always the same result...

This is my Hex generated file:

200007FC 080002D1 08000341 08000341
08000341 08000341 08000341 00000000
00000000 00000000 00000000 08000341
08000341 00000000 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 08000341 08000341
08000341 08000341 B085B480 6078AF00
F04F6039 60FB0300 0300F04F F44F60BB
F2C443E8 681B0300 687B60FB 72FEF640
F303FA02 0303EA6F 401368FA 683B60FB
683B681A 431A685B 689B683B 683B431A
431368DB 687B60BB FA0268BA 68FAF303
60FB4313 43E8F44F 0300F2C4 601A68FA
0714F107 BC8046BD BF004770 B083B480
6078AF00 70FB460B 2B0078FB F44FD010
F2C443E8 F44F0300 F2C442E8 68110200
F04F687A FA000001 430AF202 E011601A
43E8F44F 0300F2C4 42E8F44F 0200F2C4
687A6811 0001F04F F202FA00 0202EA6F
601A400A 070CF107 BC8046BD BF004770
B085B480 6078AF00 807B460B 0300F04F
F44F60FB F2C443E8 60FB0300 687B68FA
F10318D3 60FB0308 887A68FB F107601A
46BD0714 4770BC80 B085B480 6078AF00
807B460B 0300F04F F44F60FB F2C443E8
60FB0300 687B68FA F10318D3 60FB0314
887A68FB F107601A 46BD0714 4770BC80
B082B580 F640AF00 F6C00350 607B0300
0300F240 0300F2C2 E00B603B 681A687B
601A683B F103683B 603B0304 F103687B
607B0304 F240683A F2C20300 429A0300
4809D3EC F04F4909 42880200 F840BFB8
F6FF2B04 F000AFFA F000F985 F107F80D
46BD0708 0000BD80 20000000 20000000
AF00B480 BF00E7FE B082B580 F000AF00
F04FF881 607B0300 687BE00A F04FB29B
46190000 FF98F7FF F103687B 607B0305
F640687A 429A73FE F640DDEF 607B73FF
687BE00A F04FB29B 46190000 FF84F7FF
F1A3687B 607B0308 2B00687B F04FDCF1
F6400000 F7FF71FF E7D2FF5B B083B480
6078AF00 70FB460B 2B0078FB F44FD00C
F2C45360 F44F0302 F2C45260 6B110202
430A687A E00D631A 5360F44F 0302F2C4
5260F44F 0202F2C4 687A6B11 0202EA6F
631A400A 070CF107 BC8046BD BF004770
B083B480 6078AF00 70FB460B 2B0078FB
F44FD00C F2C45360 F44F0302 F2C45260
6C110202 430A687A E00D641A 5360F44F
0302F2C4 5260F44F 0202F2C4 687A6C11
0202EA6F 641A400A 070CF107 BC8046BD
BF004770 B086B580 F04FAF00 F04F0001
F7FF0101 F04FFFA3 F2C00001 F04F0020
F7FF0101 F04FFF9B F04F5000 F7FF0101
F04FFFBF 613B0330 0303F04F F04F753B
75FB0300 0310F107 0000F04F 0002F2C4
F0004619 F04FF825 603B0300 0300F04F
F04F607B 60FB0300 F04F463B 46190010
FE62F7FF F04F463B 46190000 FE5CF7FF
0010F04F 0101F04F FE90F7FF 0000F04F
0101F04F FE8AF7FF 0718F107 BD8046BD
B087B480 6078AF00 F04F6039 617B0300
0300F04F F04F613B 60FB0300 0300F04F
E086617B F04F697B FA020201 613BF303
681A683B 4013693B 68FA60FB 429A693B
687BD173 697B681A 0343EA4F 0103F04F
F303FA01 0303EA6F 687B401A 687B601A
683B681A 4619791B EA4F697B FA010343
431AF303 601A687B 791B683B D0032B01
791B683B D1342B02 689A687B EA4F697B
F04F0343 FA010103 EA6FF303 401A0303
609A687B 689A687B 795B683B 697B4619
0343EA4F F303FA01 687B431A 687B609A
697B685A F04FB29B FA010101 EA6FF303
401A0303 605A687B 685A687B 799B683B
697B4619 FA01B29B B29BF303 687B431A
687B605A 697B68DA EA4FB29B F04F0343
FA010103 EA6FF303 401A0303 60DA687B
68DA687B 79DB683B 697B4619 0343EA4F
F303FA01 687B431A 697B60DA 0301F103
697B617B F67F2B0F F107AF75 46BD071C
4770BC80 AF00B580 5360F44F 0302F2C4
5260F44F 0202F2C4 F0426812 601A0201
5360F44F 0302F2C4 0200F04F F44F609A
F2C45360 F44F0302 F2C45260 68120202
7284F022 3280F422 F44F601A F2C45360
F2430302 F2C20210 605A4200 5360F44F
0302F2C4 5260F44F 0202F2C4 F4226812
601A2280 5360F44F 0302F2C4 0200F04F
F00060DA F44FF809 F2CE436D F04F0300
609A6200 BF00BD80 B083B480 F04FAF00
607B0300 0300F04F F44F603B F2C45360
F44F0302 F2C45260 68120202 3280F442
F44F601A F2C45360 681B0302 3300F403
687B603B 0301F103 683B607B D1032B00
F5B3687B D1EC6FA0 5360F44F 0302F2C4
F403681B 2B003300 F04FD003 603B0301
F04FE002 603B0300 2B01683B 8082F040
5360F44F 0302F2C4 5260F44F 0202F2C4
F0426C12 641A5280 43E0F44F 0300F2C4
42E0F44F 0200F2C4 F4426812 601A4280
5360F44F 0302F2C4 5260F44F 0202F2C4
609A6892 5360F44F 0302F2C4 5260F44F
0202F2C4 F4426892 609A4200 5360F44F
0302F2C4 5260F44F 0202F2C4 F4426892
609A52A0 5360F44F 0302F2C4 4219F245
7240F2C0 F44F605A F2C45360 F44F0302
F2C45260 68120202 7280F042 BF00601A
5360F44F 0302F2C4 F003681B 2B007300
F44FD0F6 F2C45370 F2400302 601A6205
5360F44F 0302F2C4 5260F44F 0202F2C4
F0226892 609A0203 5360F44F 0302F2C4
5260F44F 0202F2C4 F0426892 609A0202
F44FBF00 F2C45360 689B0302 030CF003
D1F62B08 070CF107 BC8046BD BF004770

jean_prieur
Associate III
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
Posted on April 17, 2013 at 11:37

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

Thanks !!

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
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
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
Associate
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.