52. STM32 DAC experiment

Publisher:鑫森淼焱Latest update time:2018-10-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. The principle of digital-to-analog conversion DAC

Limitations of STM32 DAC:

Only the large-capacity STM32F103x (Flash above 256K) models have the DAC function.

1. Principle of digital-to-analog conversion

52. STM32 DAC experiment
The DAC module has 2 converters, one channel each.

2. Main features of the STM32DAC module

52. STM32 DAC experiment

3. DAC module block diagram

52. STM32 DAC experiment
(1) DAC1 corresponds to PA4

        DAC2 for PA5

(2) The number is written in DHRx and is transferred to the DORx register after a certain period of time. We cannot write directly to the DORx register.  

(3) Conversion can be triggered externally.

(4) DMA requests can be generated.

52. STM32 DAC experiment

To use DAC to output to PA4 or PA5, you need to set PA4 or PA5 to analog input.

4. DAC conversion

52. STM32 DAC experiment
(1) For the DAC_DHRx register, depending on the alignment mode, 8-bit or 12-bit data, there are 6 registers, 3 for each channel, such as 8-bit left alignment, 12-bit left alignment, and 12-bit right alignment. The corresponding register must be written in whichever mode.

(2) If external trigger is not selected, the data stored in register DHRx will be stored in register DORx after one APB1 clock.

(3) Once the data is transferred to the DORx register, it will be reflected on the corresponding pin after a period of time T.

5. DAC data format

52. STM32 DAC experiment

6. Select the trigger mode of DAC

You can select an external trigger event:

52. STM32 DAC experiment

7. Calculation of DAC output voltage

DAC output = Vref * (DOR / 4095)

52. STM32 DAC experiment

Example: If the number in DOR is 100 and the reference voltage is 3.3V, then the output is 3.3 * 100 / 4095

2. Register library function configuration

1. DAC related registers

52. STM32 DAC experiment
We cannot write to the DOR register directly, but we can write to the DHRx register, which will be automatically transferred to the DOR register after certain events.

1. DAC control register DAC_CR

52. STM32 DAC experiment

2. DAC configuration steps

52. STM32 DAC experiment
52. STM32 DAC experiment
52. STM32 DAC experiment

3. Experimental Procedure

1. Hardware Connection

52. STM32 DAC experiment
DAC uses the PA4 pin to output analog

PA1 is used to measure the analog input in the ADC experiment.

In order to facilitate testing in the experiment, we can use a jumper cap to connect these two pins together. We can control the output analog quantity through DAC, and then use the ADC of STM32 to measure the analog quantity.

52. STM32 DAC experiment

2. Program Explanation

(1)dac.c

//DAC channel 1 output initialization

void Dac1_Init(void)

{

  

GPIO_InitTypeDef GPIO_InitStructure;

DAC_InitTypeDef DAC_InitType;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //Enable PORTA channel clock

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE ); //Enable DAC channel clock 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // Port configuration

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Analog input

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_SetBits(GPIOA,GPIO_Pin_4); //PA.4 output high

DAC_InitType.DAC_Trigger=DAC_Trigger_None; //Do not use trigger function TEN1=0

DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None; //Do not use waveform generation

DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0; //shield, amplitude setting

DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable; //DAC1 output buffer disabled BOFF1=1

                                                                                                                Otherwise the DAC cannot output to 0.

    DAC_Init(DAC_Channel_1,&DAC_InitType); //Initialize DAC channel 1

DAC_Cmd(DAC_Channel_1, ENABLE); //Enable DAC1

  

    DAC_SetChannel1Data(DAC_Align_12b_R, 0); //Set DAC value in 12-bit right-aligned data format

}

//Set channel 1 output voltage

//vol:0~3300, represents 0~3.3V

void Dac1_Set_Vol(u16 vol)

{

float temp=vol;

temp/=1000;

temp=temp*4096/3.3;

DAC_SetChannel1Data(DAC_Align_12b_R,temp); //Set DAC value in 12-bit right-aligned data format

}

2. Main function main.c

int main(void)

 {  

u16 adcx;

float temp;

  u8 t=0;  

u16 dacval=0;

u8 key;

delay_init(); //delay function initialization  

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt priority group to group 2: 2-bit preemption priority, 2-bit response priority

uart_init(115200); //Serial port initialized to 115200

KEY_Init(); //Initialize key program

  LED_Init(); //LED port initialization

LCD_Init(); //LCD initialization

usmart_dev.init(72); //Initialize USMART

  Adc_Init(); //ADC initialization

Dac1_Init(); //DAC initialization

POINT_COLOR=RED; //Set the font to red 

LCD_ShowString(60,50,200,16,16,"WarShip STM32");

LCD_ShowString(60,70,200,16,16,"DAC TEST");

LCD_ShowString(60,90,200,16,16,"ATOM@ALIENTEK");

LCD_ShowString(60,110,200,16,16,"2015/1/15");

LCD_ShowString(60,130,200,16,16,"WK_UP:+  KEY1:-");

//Display prompt information      

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");

DAC_SetChannel1Data(DAC_Align_12b_R, 0); //Initial value is 0          

while(1)

{

t++;

key=KEY_Scan(0);  

if(key==WKUP_PRES)

{  

if(dacval<4000)dacval+=200;

DAC_SetChannel1Data(DAC_Align_12b_R, dacval); //Set DAC value

}else if(key==KEY1_PRES)

{

if(dacval>200)dacval-=200;

else dacval=0;

 DAC_SetChannel1Data(DAC_Align_12b_R, dacval); //Set DAC value

}  

if(t==10||key==KEY1_PRES||key==WKUP_PRES) //WKUP/KEY1 is pressed, or the timer is up

{  

adcx=DAC_GetDataOutputValue(DAC_Channel_1); //Read the previously set DAC value

LCD_ShowxNum(124,150,adcx,4,16,0); //Display DAC register value

temp=(float)adcx*(3.3/4096); //Get DAC voltage value

adcx=temp;

  LCD_ShowxNum(124,170,temp,1,16,0); //Display the integer part of the voltage value

  temp-=adcx;

temp*=1000;

LCD_ShowxNum(140,170,temp,3,16,0X80); //Display the decimal part of the voltage value

  adcx=Get_Adc_Average(ADC_Channel_1,10); //Get ADC conversion value  

temp=(float)adcx*(3.3/4096); //Get ADC voltage value

adcx=temp;

  LCD_ShowxNum(124,190,temp,1,16,0); //Display the integer part of the voltage value

  temp-=adcx;

temp*=1000;

LCD_ShowxNum(140,190,temp,3,16,0X80); //Display the decimal part of the voltage value

LED0=!LED0;   

t=0;

}    

delay_ms(10);

}

 }

52. STM32 DAC experiment


Keywords:STM32 Reference address:52. STM32 DAC experiment

Previous article:53.PWM DAC Experiment
Next article:51.Internal temperature sensor experiment

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号