Cortex_m3_stm32 Embedded Learning Note (18): DAC Experiment (Digital-to-analog conversion)

Source: Internet
Author: User

The STM32 DAC module ( digital/Analog Conversion module ) is a 12-bit digital input, voltage-output DAC. The DAC can be configured in either 8-bit or 12-bit mode, or it can be used with a DMA controller. When the DAC is operating in 12-bit mode, the data can be left-justified or right-aligned. The DAC module has 2 output channels with a separate converter for each channel. in dual DAC mode, 2 channels can be converted independently or simultaneously, and the output of 2 channels can be updated synchronously .

In this section of the experiment, we will use the key (or USMART) to control the STM32 internal DAC1 to output the voltage, collect the output voltage of the DAC via the ADC1 Channel 1, display the voltage value obtained by the ADC on the LCD module, and set the output voltage of the DAC Value, and so on.

stm32 DAC module main features are:
①2 DAC Converter: Each converter corresponds to 1 output channels
②8 bit or 12-bit monotonic output
③12-bit mode data left-justified or right-justified
④ sync Update feature
⑤ noise waveform Generation
Span style= "font-size:10pt" >⑥ triangle waveform generation
⑦ dual DAC channel simultaneous or separate conversion
⑧ each channel has DMA function

The voltage is linear from 0~vref+, 12-bit mode DAC output voltage and vref+ and Dorx are calculated as follows:
                  Dacx output voltage =vref* (dorx/4095)

The understanding of my ADC and DAC is:

The ADC can collect voltage to convert the voltage to a number, the DAC can convert the number to the form of voltage and output


Configuration steps:

1 ) Turn on the PA port clock and set the PA4 as the analog input.

STM32F103RCT6 DAC Channel 1 on the PA4, so we first want to enable the PORTA clock, and then set PA4 as the analog input. The DAC itself is output, but why is the port set to analog input mode? The corresponding GPIO pin (PA4 or PA5) is automatically connected to the analog output of the DAC and is set as input to avoid additional interference after a DACX channel is enabled. 2) enable DAC1 clock.
3 ) to initialize the DAC and set The operating mode of the DAC.
4) enable DAC Conversion Channel
5) set The output value of the DAC .
DAC.C
#include "dac.h" void Dac1_init (void) {gpio_inittypedef gpio_ist;dac_inittypedef dac_ist; Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa, enable);//enable PA clock Rcc_apb1periphclockcmd (RCC_APB1PERIPH_DAC, enable); Enable the DAC clock to gpio_ist. Gpio_pin=gpio_pin_4; Gpio_ist. gpio_mode=gpio_mode_ain;//analog input gpio_ist. Gpio_speed=gpio_speed_50mhz; Gpio_init (gpioa,&gpio_ist); Gpio_setbits (gpioa,gpio_pin_4);//pa.4 output High dac_ist. Dac_trigger=dac_trigger_none; Does not use the trigger function dac_ist. dac_wavegeneration=dac_wavegeneration_none;//does not use waveforms to occur dac_ist. Dac_lfsrunmask_triangleamplitude=dac_lfsrunmask_bit0;dac_ist.  dac_outputbuffer=dac_outputbuffer_disable;//off DAC1 output cache Dac_init (dac_channel_1,&dac_ist); Initialize DAC channel 1dac_cmd (dac_channel_1,enable);//Enable Dac1dac_setchannel1data (dac_align_12b_r,0);//12-bit right-aligned, set DAC initial value}// Set Channel 1 output voltage//vol:0~3300, representing 0~3.3vvoid Dac1_set_vol (U16 vol) {float temp=vol;temp/=1000;temp=temp*4096/3.3;dac_ Setchannel1data (dac_align_12b_r,temp);//12-bit right-aligned set DAC value}u16 dac1_get_vol (void) {return Dac_getdataoutputvalue (dac_ Channel_1);} 


dac.h
#ifndef _dac_h#define _dac_h#include "sys.h" void dac1_init (void); U16 dac1_get_vol (void); void Dac1_set_vol (U16 Vol); # endif

Main function: The strength of the DAC output voltage is adjusted by pressing the key, the wkup is enhanced, the KEY0 is weakened (also can be set by the Usmart component)
#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" #include "adc.h" #include "dac.h" #     Include "Key.h" void init () {delay_init (); The delay function initializes the uart_init (9600);  The serial port is initialized to 9600led_init (); Initialize the hardware interface with the LED connection lcd_init (); Key_init (); Adc_init ();D ac1_init (); point_color=red;//set the font to Red lcd_showstring (60,40,200,24,24, "DAC Test ^-^"); Lcd_showstring (60,70,200,16,16, "bigggg difficulty"); Lcd_showstring (60,90,200,16,16, "2015/1/25"); Lcd_showstring (60,110,200,16,16, "By--mr yh"); Lcd_showstring (60,130,200,16,16, "wk_up:+ key0:-");//Display the message point_color=blue;//set the font to Blue lcd_showstring ( 60,150,200,16,16, "DAC VAL:"); Lcd_showstring (60,170,200,16,16, "DAC vol:0.000v"); Lcd_showstring (60,190,200,16,16, "ADC vol:0.000v");D Ac1_set_vol (330);} int main (void) {u16 adcnum=0;u16 dacnum=0;float tem;u8 Key,t=0;init (); while (1) {t++;key=key_scan (0); if (key==wk_up_ PRES) {if (dacnum<4000) dacnum+=200;dac_setchannel1data (dac_align_12b_r,dacnum);} else if (key==key0_pres) {if (dacnum>200) Dacnum-=200;else dacnum=0;dAc_setchannel1data (dac_align_12b_r,dacnum);} if (t==10| | key==wk_up_pres| | Key==key0_pres) {Adcnum=dac1_get_vol ();d acnum=adcnum; Lcd_showxnum (124,150,adcnum,4,16,0);//Display DAC register value tem= (float) adcnum* (3.3/4096); Get DAC voltage value Adcnum=tem; Lcd_showxnum (124,170,tem,1,16,0);//display voltage value integer part tem-=adcnum;tem*=1000; Lcd_showxnum (140,170,tem,3,16,0x80);//display voltage value of the small part adcnum=get_adc_average (adc_channel_1,10); tem= (float) adcnum* ( 3.3/4096); Adcnum=tem; Lcd_showxnum (124,190,tem,1,16,0); tem-=adcnum;tem*=1000; Lcd_showxnum (140,190,tem,3,16,0x80); t=0; led0=! LED0;} Delay_ms (10);}}



Finally, the PA1 and PA4 are connected through the DuPont line. And why should we even look at these two mouths ?

ADC1 and DAC1 are hung on the PA1 and PA4 respectively, so we want to collect ADC1 output voltage through DAC1, we need to connect these two points.


Cortex_m3_stm32 Embedded Learning Note (18): DAC Experiment (Digital-to-analog conversion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.