2014-07-14 03:47 AM
Hello Everyone , I am using STM32F103RETG to create a sound.I am also using elua and minicom to execute the program onto the uc. My plan is to -
*first use the DAC wave generator to create a simple noise. I will be using pin PA4 and TIM6. *Secondly, once the code is working I would extend the same to create a noise using the DMA. *Third I would use some wave files to integrate with the code to produce meaningful sound. So far, i have used the codes present on the discussion board to create a simple noise. The code is listed below.However, it does not seem to be working. Please have a look at the code and tell me if i need to do it differently. Any advice will be much appreciated. Thank you. static int omniexp_dac_pa4( lua_State *L ) { GPIO_InitTypeDef GPIO_InitStructure; DAC_InitTypeDef DAC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xF; TIM_TimeBaseStructure.TIM_Prescaler = 0xF; TIM_TimeBaseStructure.TIM_ClockDivision =0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); /* TIM6 TRGO selection */ TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); /* Enable DAC and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC | RCC_APB1Periph_TIM6, ENABLE); /* Configure PA.04 (DAC) as output -------------------------*/ *GPIO_BB_ODR(GPIOA_BASE, 4)=0; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); /* TIM6 enable counter */ TIM_Cmd(TIM6, ENABLE); while (1) { } /* 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_1, &DAC_InitStructure); }2014-07-14 05:03 AM
Want to use AIN mode
STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\DAC\DualModeDMA_SineWave\main.c Posted some other Sine example, but for F0 and F4 chips2014-07-15 04:12 AM
2014-07-16 02:17 AM
2014-07-16 02:46 AM
I connected a small speaker obtain from one the musical greetings card and connected it to pin PA4 and ground. There was no response.
Look for the datasheet, check the DAC output impedance in unbuffered/buffered mode.
2014-07-17 02:52 AM
Thank you for the reply. Yes I did go through the datasheet and I have enabled the output buffer. Now i have a very low intensity humming on the speaker. Is it possible to increase the pitch of the humming without any amplifier? Also how is it possible to play a valid sound file. I am posting the code where the buffer has been enabled for others to have a reference.
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Triangle; DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_TriangleAmplitude_4095; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure); DAC_Cmd(DAC_Channel_1, ENABLE); Thankss!!2014-07-17 04:01 AM
Is it possible to increase the pitch of the humming without any amplifier?
No. With DAC buffer, you get 15kOhm output impedance. For 3V, this means 200uA, i.e. 0.6 milliWatt. You will need a power amplification of about two orders of magnitude.
Also how is it possible to play a valid sound file.
Just configure a timer with the sample frequency of the waveform, and on each timer interrupt, write the next sample point to the DAC.
2014-07-21 02:01 AM
Thank you for your reply fm..
I found a solution to the low pitch. I cut down a headphone speaker and connected it to the pins instead of the greetings card speaker. As long as i hear the noise it doesn’t matter if its through heard phone or not. However I am still confused aboutJust configure a timer with the sample frequency of the waveform, and on each timer interrupt, write the next sample point to the DAC.
what sample frequency are we talking about?How do i use a .wav file and pass it onto the dac for output on the headphone. As I am very weak in programming so any sample code would be much appreciated.If not please advice me where to make changes to included these files and how... thanks once again.
2014-07-21 03:50 AM
what sample frequency are we talking about?
The wav file represents sound, sampled at a certain frequency. The WAV file format is a subset of the RIFF file, as used by Microsoft. There are plenty of documentation and libraries + code examples for WAV decoding. I never tried that on a Cortex M, but I guess there are already libs around. Once you know the sampling frequency, size and format of the data, one just needs to copy the audio data to the DAC data register, perhaps from the timer interrupt handler. For the latter, code examples are in several firmware libs and standard peripheral libs. I never tried any Cube code...
2014-07-24 04:13 AM
Thank you fm ,,cheerss!!