前言
( T9 t+ V) d$ }5 M$ F z- d1 E& y前面我们配置了ADC模块,本节我们学习配置DAC模块。+ _8 i) [& W' z2 n) j
* l) P) X4 u; b1 @
1.CubeMx的配置步骤
; _! x1 y9 Q. c4 A; N修改之前的ADC的ioc文件来生成工程。
3 r, f4 ?1 e: }( ^& }1 ^" X! `; w4 D0 @ N5 A+ u# q/ n
3 N5 b8 }% F$ r! ^$ c4 }& U3 U2 q7 B e5 v
2.生成工程
/ O' Q- P8 \0 ?7 o8 r [. m
- e" Q5 \* p, P
# T# X* c& M3 w- f" V' D
8 H5 g- z7 M' A% X0 z( b' a3 N) x0 m点击GENERATE CODE生成代码,再在bsp文件夹建立bdac.h和bdac.c,并加入工程中。
9 b# O, o6 A# B- r
$ b$ M R) d- h( o6 {
; l1 J% ?, q: e- h1 x
7 T6 @ k8 n( V
, Q$ {/ q: q7 R& o* z6 k
- t, c: g5 i) @ X6 g4 v
至此工程就建立完毕了。
3 M8 a, F. v' A5 q' z4 f. Y* C& c" r% y: p" q. _" N' w4 @, U& o" O
2 n. H: {0 m8 D N
3.测试代码- l1 ]& k/ v: ^9 G, O. ]8 k7 p
bdac.h:- #ifndef __BDAC_H__. U" S1 b" g( Y4 C
- #define __BDAC_H__8 h( u3 T6 x8 q1 Y% r/ D
- ) J5 z: C2 j8 A" X$ A# p5 ]) r7 O
- #include "main.h"
$ }( x" |# B, g- J, w - 1 R7 h- g( Y9 W
- void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol);
0 a9 [& U; M% @! `! x2 D0 }+ i - - ^* h, _, B& d7 M3 q: f4 ^
- #endif) }& D9 G5 H& J- e3 x
复制代码
# o7 \$ R3 N" b: m! E7 {' T! t0 X5 c8 D2 V- k
bdac.c:
' ]1 M3 L( p# d2 h. l- #include "bdac.h", z) U# u# ~8 y& z- f( t J, x. A
- 9 f s4 K# T# O$ u! a5 E2 {
- void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol)6 Q" R: ]& |2 f/ H. A0 {
- {
) A7 c' ~( W4 J' G1 c! c2 z; n - unsigned int temp;8 t: \' f+ o: |( u
- temp = (unsigned int)(Vol / 3.3f * 4096);% P; A! V7 E) T% q6 [" O9 J4 P B$ G0 j
- HAL_DAC_SetValue(hdac, Channel, DAC_ALIGN_12B_R, temp);) N& H! y: r) |/ E \
- }& |% w: J+ m5 J }
复制代码
4 a/ M- _. q' X1 Cmain.c:
% Q" s6 E' C" d: i1 A8 f1 Q1 L- /* USER CODE BEGIN Header */* ~# S" J! _% x
- /**
. g% \: q* m6 j8 M5 R/ e - ******************************************************************************
* d3 I; Y+ L9 a: ^( Q - * @file : main.c
' ^. S" F2 S" k W, J - * @brief : Main program body
& Z% K1 K+ v# @0 z - ******************************************************************************3 a+ w4 P) U# a7 G8 r' p, I; m
- * @attention7 u) h& Z/ G4 P% Z* N8 R
- *0 q; W& S2 [* _0 X
- * <h2><center>© Copyright (c) 2023 STMicroelectronics. ?6 V' _2 [) L' s. Y4 r9 z
- * All rights reserved.</center></h2>& T9 }! c% B1 a% }
- *) b1 O T) |) K D) c6 m: o
- * This software component is licensed by ST under BSD 3-Clause license,2 F& s) I3 ?' t( i% ^6 K
- * the "License"; You may not use this file except in compliance with the5 @7 z# K/ a- Z# T
- * License. You may obtain a copy of the License at:
% o O4 Q7 b. j7 J1 b' a - * opensource.org/licenses/BSD-3-Clause
8 w, C r. u* u7 M - *' Z4 c% R% X3 p: r+ h2 M
- ******************************************************************************
6 B+ a$ b' S% I8 ] - */, B: w5 l! j; A3 m. n, ^# u
- /* USER CODE END Header */
1 ?7 V7 W: X* L - /* Includes ------------------------------------------------------------------*/
1 v9 `) J {0 M% p- ?; A* ` - #include "main.h"
6 L; h7 W7 Z+ h5 g2 B* S% c - #include "adc.h"
/ [) v% b! y6 C9 g$ p D- x - #include "dac.h"
6 q$ K! s: q5 o; R0 h - #include "tim.h"
8 V# P# s. @; f0 u+ U: j - #include "gpio.h"- @) T: J+ [) s0 V) K
. ]6 ^! f5 H" ]! y; @4 p/ L- /* Private includes ----------------------------------------------------------*/
2 w2 E; \2 e* J4 o; M3 o - /* USER CODE BEGIN Includes */
% u# `& d- c+ [1 s - #include "lcd.h"1 `. M$ T" C8 r
- #include "badc.h"
' u* ~/ E( y( W& q# Q. n - #include "stdio.h"3 u, H) c& s7 W8 q9 @9 L- c4 `
- #include "bdac.h" n0 ?# N1 @% S! H) {( `' O
- /* USER CODE END Includes */5 ~8 n+ T. p1 G" |( r( O! P! d4 f
: Z4 r" h% W0 u( t; o- /* Private typedef -----------------------------------------------------------*/
; }/ k8 c4 O! o0 N3 [) N1 d - /* USER CODE BEGIN PTD */2 g5 f) N7 ^! c
3 d8 v1 n! W# z* q& H f- /* USER CODE END PTD */& Q( @" U: K% j, W1 ^7 v
- ' G/ O. ~3 t. D6 A: h: A5 o
- /* Private define ------------------------------------------------------------*/
0 N& }) c3 K: o- y3 R5 Q6 V - /* USER CODE BEGIN PD */
2 O) R h; z& Y+ Z4 p( a! G - /* USER CODE END PD */6 O. G* n: u, u; V9 y5 t. F# ?& J
/ e( M) Z' i) N& Z# @" H- /* Private macro -------------------------------------------------------------*/2 f2 ~) _4 M; j" D; t) o% E- N
- /* USER CODE BEGIN PM */+ [, Z6 t- k; [9 }0 Z
% c! t: R2 H2 i! _/ @5 C1 c- /* USER CODE END PM */* E' N6 Q# k% z
& Q- h# f5 |/ E( ^! ^( f7 r4 L7 D- /* Private variables ---------------------------------------------------------*/
$ s( L9 s4 I* ~" k+ _) T - 5 M1 }4 H- u1 T6 e" h1 m
- /* USER CODE BEGIN PV */
% V& O% ^% R# ~' d5 w - float Vol1 = 0;& A4 }: Y8 G6 ^% G
- /* USER CODE END PV */, ^# \6 N: i! a- z) n
0 f( \5 ~8 V- H9 c9 w& y6 u" C- /* Private function prototypes -----------------------------------------------*/
) \" T; ]0 h+ R2 H' d# @$ m, c6 ] - void SystemClock_Config(void);
+ w4 u i# h! Z) v - /* USER CODE BEGIN PFP */
! d2 q. q4 _* }, d& K! D - ' F1 w, G8 C9 X$ |5 D5 O- @
- /* USER CODE END PFP */3 @! _# X% n# F) C, Z: s% _" F2 b1 K
/ ?! c% n& B7 W" `* g( h- /* Private user code ---------------------------------------------------------*/2 v( j! r) q' U1 g* t5 n
- /* USER CODE BEGIN 0 */
, J+ G% F! r, o) l, y
o4 z% n# b0 H: M* ]2 \* ]- /* USER CODE END 0 */
' H& q" F4 W5 b K) E
4 z( g) [- E8 f" k: N7 S- F- /**
% m1 @: q3 c9 T e& W - * @brief The application entry point.8 Q3 h; b) {* d; L# E( T
- * @retval int$ D7 J3 X1 V/ R6 H& \
- */. {: R2 n3 b- Q' C6 {4 }
- int main(void)* E N' Y* W; y. ]' v8 P4 c" D3 `
- {) X- k5 ^, l4 e% o9 @
- /* USER CODE BEGIN 1 */ o2 S* P' G( f: O5 |' ?% O- c. }+ t
- / U2 n' j6 p9 V* u$ {( E6 P
- /* USER CODE END 1 */( v% E6 p3 g- g. D- Q3 }
- 8 r0 T( n" y% O
- /* MCU Configuration--------------------------------------------------------*/
: w# c0 }( r1 N# e) F
- ]) P! Q+ E0 W9 m3 b; O# ]- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
% T9 E2 N, T- W$ o& L% p+ G - HAL_Init();* c I* f; e+ s7 p/ j) O4 @& a
1 S2 X( u1 S1 y& `" z- /* USER CODE BEGIN Init */
9 T0 k- {* U0 t& ?3 D5 @; S0 } - ( _6 L. X7 z: H7 Y i' j
- /* USER CODE END Init */
0 \- F' v' G9 F - # ?0 X) V( L, q$ l5 o
- /* Configure the system clock */# g/ k. \4 V' v9 d, w! M F
- SystemClock_Config();. N% C6 ~" @/ W2 o" ?7 n1 G
1 l8 Y( }0 c! ^2 K( A0 H- z- /* USER CODE BEGIN SysInit */
6 }5 b9 h* _' ]4 z8 o2 ]' `3 R4 ?' p - * {% r7 Q7 C7 o2 U+ F
- /* USER CODE END SysInit */
2 G$ s! @2 x* F @5 f - ; L) L: Y* s+ u$ ]& }5 r" Q
- /* Initialize all configured peripherals */
! c' D2 D! B L9 h7 S - MX_GPIO_Init();
; X$ Q. d5 O5 d. u, D8 G! [ k - MX_TIM3_Init();
0 ]! i Z" s( W7 F5 R+ [ - MX_TIM16_Init();' L$ P' \' ~0 L* ]0 g6 C% `4 x
- MX_TIM17_Init();6 ~( \4 Y, E# Y! H/ u% ]6 [
- MX_ADC1_Init();4 u8 S M# c# C5 d1 J. v! m6 T
- MX_ADC2_Init();
" E" s" j: _- s7 _2 N - MX_DAC1_Init();' v/ i- w# _ E6 _6 ]$ G7 z$ y
- /* USER CODE BEGIN 2 */5 s/ |) r" x3 G; K/ s" U7 c. ?! u
- LCD_Init();
/ Z# a' j& `1 s2 ? - LCD_Clear(Black);! k3 z, @6 \4 B' M7 ^: s
- LCD_SetBackColor(Black);
3 L& d8 h5 w# `! k: w - LCD_SetTextColor(White);# E1 O7 K0 k: A6 ]2 v! D
- HAL_DAC_Start(&hdac1 , DAC_CHANNEL_1);
& N/ U, U. Y# z, J8 s - HAL_DAC_Start(&hdac1 , DAC_CHANNEL_2);: S+ T4 X k1 c! n. ~$ q3 d
& p% y3 q! S& V7 \- setDAC(&hdac1, DAC_CHANNEL_2, 3.0);
* _" s$ t5 a; A0 x. n* e+ g1 ? - /* USER CODE END 2 */3 z# C. U9 p& ~0 J* T
- / J; f- M, n" K r! |
- /* Infinite loop */
% t! W* a% e/ V) w& _5 N1 |3 G - /* USER CODE BEGIN WHILE */9 p* L' e7 I2 P3 N. [
- while (1)3 l D9 C* H6 ]) ~" o4 v
- {
5 D; v( p9 \% t. B4 M1 |3 e! y - /* USER CODE END WHILE */
/ `, v- \/ f6 `5 r+ Y s% v' ?* }4 s
% \) ~( @$ x2 o# x: ?% }: e- /* USER CODE BEGIN 3 */
& q9 }$ n& T, ?/ H - setDAC(&hdac1, DAC_CHANNEL_1, Vol1);' S9 C& c U% \. d' H# o3 c
- char text[30];5 P, n# w4 \! p0 Y0 m; ?
- sprintf(text, " V1:%.2f", getADC(&hadc1));
) D+ p; x( k, G6 `/ S2 l - LCD_DisplayStringLine(Line4, (unsigned char *)text);6 t" ]2 k. H/ }" `
- sprintf(text, " V2:%.2f", getADC(&hadc2));
' Z8 \3 y) ^+ s6 B7 Y+ D7 p - LCD_DisplayStringLine(Line5, (unsigned char *)text);
2 v ^( p- X) s) a( { s/ V0 d - Vol1 += 0.1;9 f6 R- }9 t4 r6 e+ O) a+ [* J
- if(Vol1 == 3.4)
* ]4 w( I- T$ O$ g - {& f1 s( _: D( I
- Vol1 = 0;" h% q. E6 [7 ?+ Q" @" `
- }% C* {2 y# h8 ]5 W) _, \% k
- HAL_Delay(500);/ u8 M5 J, d; d5 e( e
- }5 A( \$ J( P% \# N5 q8 V z7 R' k( X
- /* USER CODE END 3 */
; S, L1 z' y/ i9 A: W - }4 C+ {5 \: S% ^5 h1 t. y
- 2 B4 \$ Q4 F2 [0 G& A
- /**! ^! _5 w9 E7 ~) `* o9 x
- * @brief System Clock Configuration
! D6 ]0 D/ T/ t5 c# \2 Y - * @retval None
- L3 f- X1 D& ]; h$ _6 v' x - */5 @+ N- v. v0 X8 ]: Z3 |, D
- void SystemClock_Config(void)
1 B# V, C! y* y4 F& s2 n' Q. { m - {
$ D4 `3 l" S0 G) y0 X - RCC_OscInitTypeDef RCC_OscInitStruct = {0};4 j! S( d0 n1 k
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
+ r% g9 {5 L! q - RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
* e5 [/ N! s. M8 L" i* N3 }
# H' H3 v7 A: t/ H- }/ x- /** Configure the main internal regulator output voltage/ K; {3 e0 M7 x' `% E o( Y
- */
5 @% @* V0 w6 d3 S) C - HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
+ K |+ K( J- q* {8 b$ P - /** Initializes the RCC Oscillators according to the specified parameters, I3 w+ P0 d* \/ f' r* q
- * in the RCC_OscInitTypeDef structure.
, K- r/ h& b1 C( X' d9 _" h - */- s {, {- g5 c# z' i" D1 l* S
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
0 \4 _7 T! [& D* [ - RCC_OscInitStruct.HSEState = RCC_HSE_ON;# h1 C# E3 W: w4 L0 k! N% l) S
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
, w2 l3 [2 E4 ]7 C - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
/ D* T/ r' s) l# |1 ~ - RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;# }9 ^6 q0 X# l' Q" }
- RCC_OscInitStruct.PLL.PLLN = 20;; j; f1 C+ w3 N
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;: n8 x. |: c; A# e( [/ [" o+ Z" z
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;- u8 s! w2 x9 B
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;8 B# b" c8 k0 Z) k8 r
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)6 w' \3 T( V0 W5 g+ E* O; M
- {! s) e# k" ]% C
- Error_Handler();1 L! w2 l! b" M% v* W
- }
% X: r& j8 u' ~9 d5 \! t - /** Initializes the CPU, AHB and APB buses clocks, |% h" t* w0 {( f
- */
% N$ ?" T( Q3 \ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK0 W R% o7 g" e9 W
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;3 D) b: |4 F7 L/ z
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;( E d" L+ R+ S! H
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+ b" |1 X* _: f9 ^1 f# H/ T - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
0 `) ~8 ^* G% \4 B - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
7 }6 U6 C# v t2 s/ _+ U
# N, b, d* J" }% O! K$ B- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)# k* `1 [; x7 A! ~, P* h
- {2 L) V/ S5 G. ?# r0 I
- Error_Handler();" e6 i4 K, f$ M( x2 c$ V
- }
; M( l8 p0 h9 [1 o# \! X3 l/ | - /** Initializes the peripherals clocks
; A/ Q# u' [: W, J1 L - */
' s+ f' v, Q) h$ j! X - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;
8 F- j6 P* Q: H4 C$ k* J - PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;
6 c# L/ M2 [3 p) m/ ?0 n5 n - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
8 v# }7 B" T9 V' } - {) ]- I1 T, U! Q* u; |$ U, }# {
- Error_Handler();: ?/ w# H m) I/ S1 t% p$ |9 v
- }
3 s& A7 Y! t; W - }
% H* w+ N3 p- H6 p% s, \3 m - |$ x0 F; ]1 U! d: c$ y. k
- /* USER CODE BEGIN 4 */ e4 \" h2 p. D9 Y( Z) H
- 7 a" s! y9 Y B& @
- /* USER CODE END 4 */! C) I/ Z) H. O0 T d5 B/ R* s5 [
% X. I: v$ b% o# t1 h" m. D9 G- /**
8 ^7 C( f' W% R' m - * @brief This function is executed in case of error occurrence.3 ?' h( G+ V! `, J3 Z9 ?, U
- * @retval None
8 V9 o: k1 {- _) V9 k - */
- {2 R$ H% t% c. C1 }% H; [) p- L - void Error_Handler(void), |) U8 b4 U9 R# P, Y
- {3 R4 b* O" ]' H" _( }5 b
- /* USER CODE BEGIN Error_Handler_Debug */
' _5 Q6 o- ]' `7 O, H F- z2 @" u! F - /* User can add his own implementation to report the HAL error return state */
4 r; D u6 p" k8 N$ B - __disable_irq();
* W" i: U# V* g/ ` V5 Z, L& |& b* c% H - while (1)
5 M5 z7 t- H, K; `2 F8 G6 n& Y# @ - {' V$ s# ]9 C0 ~$ L2 [
- }
2 q8 ]0 [) y2 z5 D! y( ]: j - /* USER CODE END Error_Handler_Debug */
) P, {- ]' t# B# E0 U6 i - }
7 f6 f# k$ {8 |( }5 _ - # D: y. N: u* @0 e+ L! w0 `0 u
- #ifdef USE_FULL_ASSERT
( p5 ^' \( [# g0 d; D" b - /**; l" J7 t' V- k) w- O+ H
- * @brief Reports the name of the source file and the source line number& w) C d" Q4 \( _2 @/ ?
- * where the assert_param error has occurred.
* i7 y( M+ o. f; P4 g M - * @param file: pointer to the source file name6 o9 s2 ]+ p$ e; t
- * @param line: assert_param error line source number
+ i. `3 p0 N; {3 H - * @retval None: O" ^4 ]( H; A5 r% U4 y, e$ u8 _: E D
- */7 a( c9 e2 `: E& X; @
- void assert_failed(uint8_t *file, uint32_t line)
3 k& H1 o& i$ ^5 y3 ^" i+ w: R# J - {
5 B' K% Q' _$ v - /* USER CODE BEGIN 6 */
, f% T% p. s" W - /* User can add his own implementation to report the file name and line number,* X6 U+ x: o+ M
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */+ p% M& c: s. S4 i* l2 p
- /* USER CODE END 6 */ C& H% x- D. j9 Q3 f1 J
- }
) m: u! {) _; x# t; y9 `0 H# L: J: ?7 `5 ] - #endif /* USE_FULL_ASSERT */5 R i- ]' p" \! |7 v3 \ o, w) i
- " Q. b5 |6 M5 \* [% S2 F0 k2 V' A
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/5 B: u1 y% T9 n
复制代码
' l3 c- U0 }9 A, h3 c \4.演示效果
4 _7 X d% _& x' l, }3 o( J
; _. B: T" j0 R% y
7 V. o/ @0 |: A. }5 K0 j$ L; H' V' ?. d
————————————————6 C$ Q' E; l% d
版权声明:火花页.
C1 C3 d. p- B( {7 K0 j2 l8 j
; ?* b! l0 r1 ^9 P4 b" y$ { {0 @3 s/ ]/ M) C" t8 i
|