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
The DAC module has 2 converters, one channel each.
2. Main features of the STM32DAC module
3. DAC module block diagram
(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.
To use DAC to output to PA4 or PA5, you need to set PA4 or PA5 to analog input.
4. DAC conversion
(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
6. Select the trigger mode of DAC
You can select an external trigger event:
7. Calculation of DAC output voltage
DAC output = Vref * (DOR / 4095)
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
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
2. DAC configuration steps
3. Experimental Procedure
1. Hardware Connection
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.
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);
}
}
Previous article:53.PWM DAC Experiment
Next article:51.Internal temperature sensor experiment
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- [Atria AT32WB415 Series Bluetooth BLE 5.0 MCU] Infrared Detection
- Live broadcast has ended [Microchip Embedded Security Solution | Accessory Authentication Using Secure Element]
- After reading these two books, you will never be embarrassed to talk about the Internet of Things again!
- Verilog007
- The future belongs to ultra-wideband (UWB)
- Zhongke Bluesun (AB32VG1) development board (based on RT-Thread system) --- Music player -- Sharing
- What exactly is a sine filter?
- Pingtouge RVB2601 board-GPIO
- [TI recommended course] #DC/DC switching regulator packaging innovation#
- ST60 short-distance, contactless connector evaluation 1: application environment and principle