前言1 z) n4 n# ^# h% M7 ?+ N& v
前面我们配置了ADC模块,本节我们学习配置DAC模块。
! R/ o0 H) W1 L6 e0 e9 Q) ]* w! f B) s( C' ` F5 p
1.CubeMx的配置步骤5 D- D' }2 {* B2 f
修改之前的ADC的ioc文件来生成工程。- m! |* t2 s5 y' \# O4 s
# L6 a$ j3 ~$ i6 E
; c& o( K4 n; v. Z5 N6 K `9 c
9 w% K. ~& N* {7 i& q2.生成工程
2 `7 S5 p+ i1 Y- ]5 ~9 z! u! W1 O% R. L
& L! Q* O. ~# _0 d+ A n
: Q1 q4 y& i2 X9 X* Y点击GENERATE CODE生成代码,再在bsp文件夹建立bdac.h和bdac.c,并加入工程中。
2 X; T0 O& @& C5 z' `$ M. O& W) X9 d" c& |8 R
4 X5 q, b* ~7 I7 O6 f
1 }" W8 B5 o: E8 O9 ]5 e
' n1 a# N; t7 q+ r9 O- Z$ w9 M& z5 z) \! r* c
至此工程就建立完毕了。
/ h7 o+ K* i8 _$ O$ J Z
$ w' t6 G( o% `) F0 Q
: i; x- W. D, T! O3.测试代码% P- x, _" y' t
bdac.h:- #ifndef __BDAC_H__
2 `! x0 k* z8 O; ^ - #define __BDAC_H__- C7 J; Y9 P7 v7 {/ y* k4 P* G
- - K4 _ W" ~$ p) R; P
- #include "main.h"
: Y5 U$ R* B. C* J' ~6 o/ _6 e. W
8 C/ U) ~! A' {/ T- void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol);
/ T z$ M2 J) ]1 d# D$ D - + H% z- z2 D/ c" m" X
- #endif
3 ~& Z, s3 q# ]( E9 Z; v0 n- I* _: i7 ^
复制代码
: c: x( G; d: Q; H5 ^1 U3 S8 |% Y1 e+ N9 Y @' D
bdac.c:
8 g+ k. f" f2 [" f! `+ D- #include "bdac.h": ]- @% Y- O& n3 i9 ]8 l9 U
. q% |4 d( M3 d. J6 u6 O1 `- void setDAC(DAC_HandleTypeDef *hdac, unsigned int Channel, float Vol)
/ W2 e; N) h1 J- o7 ?, | - {
+ ]$ ?$ h, _$ I+ G, y5 f+ o, Z% F - unsigned int temp;! i; `3 @* |" u$ X1 c( R
- temp = (unsigned int)(Vol / 3.3f * 4096);
% C' @6 E/ m1 T" { - HAL_DAC_SetValue(hdac, Channel, DAC_ALIGN_12B_R, temp);
8 i j5 Q8 Q; G - }' O; d& |7 k( A9 @1 C7 l/ Z @) c9 V
复制代码 : m6 P/ s+ o/ [1 E- o: Z
main.c:. l8 m4 i# r" [! I" r J
- /* USER CODE BEGIN Header */) f/ `% G/ M& Z- k* D2 P
- /**1 T+ s& W- ^% h% \: q6 A9 Z+ f
- ******************************************************************************
, H* v& `) j; C k) K - * @file : main.c
& \, I9 F" a' U( \' e& V; j- c - * @brief : Main program body: ?0 A$ V: U9 T6 S
- ******************************************************************************
4 D4 ^, B% c1 ^- [8 [0 i* w* Y - * @attention# j0 n( b0 J( Q7 ?' t% n% ]
- *
: [. k. p' a K1 l/ Z. K# A - * <h2><center>© Copyright (c) 2023 STMicroelectronics.- g: d* `7 [- S: i0 a% T
- * All rights reserved.</center></h2>
+ L% \9 b5 [1 s6 R! n - *, l) Z! _. M7 ^ @1 ?/ }# }- D X
- * This software component is licensed by ST under BSD 3-Clause license,
# f9 k G2 z7 I/ O5 } - * the "License"; You may not use this file except in compliance with the9 W/ ?" a9 O3 x% P! T2 i$ ]
- * License. You may obtain a copy of the License at:
5 v, M. N6 S, E1 ?( R7 C; q* z - * opensource.org/licenses/BSD-3-Clause q: p, ]0 C6 W$ Y# ~
- * t/ o$ \/ _7 N9 c. Q. `* H8 p
- ******************************************************************************, c7 ? `0 B# ~& Q8 Y' T( M+ g) O+ M1 x
- */
: g, x6 a e9 _' t* X, q - /* USER CODE END Header */
, u6 k4 X2 B6 H9 K - /* Includes ------------------------------------------------------------------*/$ W/ ~6 c; U; u- _) K8 H
- #include "main.h"
& G0 b$ q' s9 y8 r9 m8 e& O: S5 S+ Q - #include "adc.h"1 b# J. `1 L2 [' P9 K& b
- #include "dac.h"& ?9 P, C4 `' [
- #include "tim.h"
" s& m& ]# b5 j; B. N2 X - #include "gpio.h"
! S7 F3 @$ j: g/ F' i) v. d0 T - ! d9 c- o/ i5 ?: E- V# |8 |
- /* Private includes ----------------------------------------------------------*/
$ F! {% g& ^8 b - /* USER CODE BEGIN Includes */
. O+ M' V3 S( R% c - #include "lcd.h"
& o3 ] J! e# ^) U0 i - #include "badc.h"
0 P2 q& C5 x$ ]1 i/ m - #include "stdio.h"
0 P% ~2 n4 n# B; D1 q. k9 V) B" c) ] - #include "bdac.h"0 h4 o. l3 T- A" ]! y5 m
- /* USER CODE END Includes */" P/ l, b; I) `
- - k: X* N( z; q/ m
- /* Private typedef -----------------------------------------------------------*/
- t4 l7 u5 e' p7 R - /* USER CODE BEGIN PTD */$ ~: R2 A6 @/ m2 R+ G
1 l( b( B) q, c) H8 L, ?3 B# }% p, X' y- /* USER CODE END PTD */
, E+ H5 J! A S: Y" O* p/ u - & [+ B) u) |( b$ [5 X' b/ b
- /* Private define ------------------------------------------------------------*/; f/ N& ~ V9 g$ ~" A, f
- /* USER CODE BEGIN PD *// u3 n6 v! e6 E0 R
- /* USER CODE END PD */
6 s% Y: U/ P+ z3 u9 N2 ^/ p, [ - % R2 Y& s& [/ J. J! K5 R
- /* Private macro -------------------------------------------------------------*/% [9 a" i8 Z# V& I3 c( r6 C0 d
- /* USER CODE BEGIN PM */
* |$ _& [! _5 g9 [- w2 y0 b - % I( T3 a9 r; ?. A& V: o1 W! K! o
- /* USER CODE END PM */% D# q7 J$ x" Y# f2 _& g, @
& {" T" B6 R* i8 }- /* Private variables ---------------------------------------------------------*/
2 S% \# O! M7 V1 ~: u
2 _2 ?2 F' ?5 Y, r+ U- /* USER CODE BEGIN PV */4 a3 l8 ?0 Y, n+ x1 [; z1 @' l
- float Vol1 = 0;+ \% M5 _) s; y" t; g
- /* USER CODE END PV */
$ {/ h' a' ~% l* N6 i - & i& X* W! r5 [& C) a
- /* Private function prototypes -----------------------------------------------*/: R7 @( a! ^9 y5 E$ v3 h# s
- void SystemClock_Config(void);4 G0 a! k0 g8 B6 O }# x7 m7 G
- /* USER CODE BEGIN PFP */
4 e8 v8 h5 A% Z5 G2 ^( R
. I4 K+ _: U5 {( B. C) B3 y) e {3 M- /* USER CODE END PFP */
+ i% M8 c7 L. j0 A8 [
/ S& o9 g! o6 A- /* Private user code ---------------------------------------------------------*/
. N! Y7 T# E f/ p - /* USER CODE BEGIN 0 *// s3 k( {$ R, ?" J
, Z# t9 n5 R+ J9 n7 _- /* USER CODE END 0 *// T, H' F8 F5 v/ P$ o$ {
, d b3 ?$ n& c. `" i0 ^- /**
& C4 D" k+ T, h0 j% V - * @brief The application entry point., b4 d; P. u) F$ I
- * @retval int
' I" T; k+ C. X9 a9 c2 c6 _3 l5 J - */
9 t' \4 {% g: `, z7 E8 O( Q - int main(void)
6 t- V) `2 P4 a* U - {! h u- }- b6 X& A t) J& K
- /* USER CODE BEGIN 1 */" ]" L/ p. a5 Y
- ) m7 F. ^7 A) L( F, j6 u( a- ]
- /* USER CODE END 1 */8 L: m, F/ U; Q* F% n, I$ u- h
- 2 M4 Q+ u: F. d. P' ]5 l) n; ?
- /* MCU Configuration--------------------------------------------------------*/+ l& a) n4 ~4 k7 ^
- 6 n/ S1 a) O& {3 S: S
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */( z5 Q- H: m! M! E% G4 t z
- HAL_Init();
. g, n1 J w9 m4 }2 P
{. G# h5 ~$ b8 k# W- /* USER CODE BEGIN Init */
% f; q( g% E! s5 h$ ~; P - ; A- l; J9 Y; Z. B
- /* USER CODE END Init */
& o. K4 A0 a7 l. b. \$ u) u, m
) d1 _& I$ u/ u: c- /* Configure the system clock */
* \; G! \- V/ f! Y z1 x* Q - SystemClock_Config();
9 b2 n/ u; Q$ @) U2 a: j# M - 3 l' H7 l/ e# A0 X' o2 m* D
- /* USER CODE BEGIN SysInit */1 E }1 P; t" @
+ o$ g8 u8 t' p& @- /* USER CODE END SysInit *// r8 D" q4 h( x* ^1 V+ t. N
+ H. D/ o& V9 m& I' o. _- /* Initialize all configured peripherals */
- B/ l* s- g7 n% b - MX_GPIO_Init();
% M4 i. u# M% w: l8 j. `" k - MX_TIM3_Init();
9 i" k8 h# R- p! }' L1 `& M - MX_TIM16_Init();
/ T/ w7 {, d5 @& W% D5 y" R3 h% Y3 e - MX_TIM17_Init();
_1 J& b% O O2 M) o$ o' p$ j - MX_ADC1_Init();0 c0 f0 m% b R! ~# l5 b' _
- MX_ADC2_Init();6 w4 W5 I- }5 H1 R k. [8 ?
- MX_DAC1_Init();
& ?) [% f. f: v7 E - /* USER CODE BEGIN 2 */
# f8 g$ V7 `* l: d - LCD_Init();
$ }( b5 c1 H$ X( `9 @( ] j5 a - LCD_Clear(Black);
7 j0 Y4 K/ w& ~, x7 @( e - LCD_SetBackColor(Black);7 Q6 O) w9 T2 Z8 i& i
- LCD_SetTextColor(White);
- I( O9 j( }7 H2 H* ~" a; ^$ ` - HAL_DAC_Start(&hdac1 , DAC_CHANNEL_1);0 ~ I, [/ K- a3 m8 }6 X
- HAL_DAC_Start(&hdac1 , DAC_CHANNEL_2);
* K* ]( S% v$ M. T+ y( _ - , M: c. @% ?: t8 R
- setDAC(&hdac1, DAC_CHANNEL_2, 3.0);& X% ?' ]" B9 C' |0 L
- /* USER CODE END 2 */" w6 Y! s- ?" p0 s. r# W D4 E
- : L1 L7 _$ ]# f* d; ~" x
- /* Infinite loop */
& c5 H7 m3 x# h6 H; d6 `# M - /* USER CODE BEGIN WHILE */. a; P: }$ O7 q. V
- while (1)$ r B0 b# X; A9 Y2 n4 A* X
- {
" R: X% M2 x3 L; ~5 H, n - /* USER CODE END WHILE */
" w! J! E2 |7 e6 j+ y - , y% a7 e7 u2 p4 T D
- /* USER CODE BEGIN 3 */, i: Z) L& u2 `5 {6 ]
- setDAC(&hdac1, DAC_CHANNEL_1, Vol1);
- S' J9 h% Y: N6 Q - char text[30];
1 i: M: H( R: e2 Y/ ^ - sprintf(text, " V1:%.2f", getADC(&hadc1));6 O/ X1 c6 v# t6 M$ \6 v9 y2 a
- LCD_DisplayStringLine(Line4, (unsigned char *)text);
* J! i. F5 ^9 b; `2 ] - sprintf(text, " V2:%.2f", getADC(&hadc2));2 G9 y" X+ a. D( Z
- LCD_DisplayStringLine(Line5, (unsigned char *)text);2 e2 b/ f! G, U! Q h9 ?# \. x
- Vol1 += 0.1;: z6 @" v" k2 `4 k, O6 O |
- if(Vol1 == 3.4)
0 Z7 Y. Y6 E8 u) D4 x( @$ |/ m: p - {
' C3 N/ R: Q; d4 p - Vol1 = 0;9 N* X! f8 {+ d9 H: K
- }
' e& |8 \3 ?8 r( a1 Z# n; W4 @( c# o; M - HAL_Delay(500);
. z* @7 G' R9 a& v" q* j. k - }
7 `' w( ]& t# Y+ \3 U; G8 \* [ - /* USER CODE END 3 */7 R! v4 v" z9 _2 J4 G9 x
- }# n5 |1 o1 f+ N$ t! B1 y0 P
" ?- m, X9 w: o6 M- /** ?: j' ?/ g- w8 S
- * @brief System Clock Configuration8 s3 p6 j' Y$ y# M9 Z/ s- w9 o
- * @retval None
4 L. y9 F3 S" x - */% \4 \% A5 d& u# t; [# \$ |
- void SystemClock_Config(void)
" ?) O V# L" g8 p - {. }+ y9 ~. A' p1 o
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};' e/ a* }3 b: r! ~, X, G
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};$ L. j9 f; z6 h3 l: `* N
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};4 E' a4 c& |+ }: h# S' B2 N
- ' r0 Z. w& `0 d6 c$ m4 y
- /** Configure the main internal regulator output voltage
% W% C& H! g g2 {# [; v - */, o7 n/ \: Z) A. y# |1 u
- HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);/ y& [# m+ x _ \" y0 E9 l
- /** Initializes the RCC Oscillators according to the specified parameters, ]/ w8 b4 S1 @) n
- * in the RCC_OscInitTypeDef structure.
/ S v" S& G, V/ m1 D - */
) x# W6 U5 b2 P - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;6 g2 j# a7 b d! t) S4 z: W
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;! {4 H3 M4 [7 Z9 R3 Z! f
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
9 v/ M* E/ L' q! d S - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;2 z9 E! N& {/ E6 O& G
- RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;% V; r0 u* L( a
- RCC_OscInitStruct.PLL.PLLN = 20;+ e$ W/ D1 [/ D9 a. A( g: ~% b6 w8 h# {
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
) `1 |# c& g' m! v& _ - RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;$ c0 B& [# J ^ j% {
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;- k; w% c2 g" l7 Y- l6 G8 i
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
+ s- f# j+ t* x0 Q - {" C% G$ z( T+ n7 h9 f
- Error_Handler();2 X; w7 \4 m3 } H- w
- }- G; H% R \) t1 }8 ^ `6 o# |. j
- /** Initializes the CPU, AHB and APB buses clocks3 [# r0 I" ^2 x$ G: ~
- */
4 g% I4 n( r0 z- G* ]/ G. w1 A - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
1 U% s2 O* U" j& K/ w w( A, { - |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
. }% _& z2 A! H: o1 Y$ e - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;! N) L5 ]6 ~' V$ Z! O: i: g; }
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;" q$ }) _2 T4 C7 `+ |5 j
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
B. ^: z) S1 B" w9 P2 D. \0 \7 q2 @ - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
* i2 u6 `; }: ?* g: G4 M - 9 A& h1 |2 \% t& `% r
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)6 q$ D9 l# h2 B. z
- {
; J9 {' U6 [, j - Error_Handler();5 c, y: a9 F% |( R* R0 T' F
- }
$ Z7 K8 \8 V' y# J) F* W; C. B - /** Initializes the peripherals clocks/ P& G0 ~5 H; X4 I; J4 C) O R% I
- */
6 R4 O6 k* I8 w1 F% i) t) M, U - PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC12;, c- [1 o7 c# J
- PeriphClkInit.Adc12ClockSelection = RCC_ADC12CLKSOURCE_SYSCLK;5 I/ U$ N0 m. \4 @# C
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
~2 \- M7 o* Z - {; U3 M; R. I4 |% l! b* c9 R
- Error_Handler();
! p: Z- g& ~+ l; j - }
& @2 O2 k( ?! h. n - }
8 ]( s& o* o$ b
: N. P/ U+ h4 |2 b- /* USER CODE BEGIN 4 */. b# _4 V" Z* F, c: p0 F3 J" c+ V
& [. b9 \7 @6 F9 a, |2 K2 w) t; K/ T/ U- /* USER CODE END 4 */
* B, S5 q6 [' b% p* |, j' l) v2 C
5 h* v+ a% {& D; j# M8 l- /**/ |# T0 u; G; U9 E X G. r6 A
- * @brief This function is executed in case of error occurrence.
: L: y: F7 j" ?/ ~- k8 p7 x - * @retval None/ g8 e- B0 y( h3 a: i# @4 q) {
- */1 P% |! b" A( g/ }, V$ u1 @1 j
- void Error_Handler(void)
}+ ?! V @% I+ ?: }0 ^ - {
! C3 x @1 @, ` - /* USER CODE BEGIN Error_Handler_Debug */
5 A& l$ H" I; G: ~7 W - /* User can add his own implementation to report the HAL error return state */
3 P1 t' f: _/ Z! s" F - __disable_irq();2 B m% _+ x$ |; Z$ ?# ~
- while (1)
* m( o g/ w$ `- s1 P2 O - {
( [4 b* P2 q* Y - }
: K9 G7 r' |" R: x$ U - /* USER CODE END Error_Handler_Debug */
) d7 Y; b8 l& ^0 s& o& {5 x - }
* b1 f$ A: r4 a7 t1 k, V
& {3 m; l/ Y. r* _. c l- #ifdef USE_FULL_ASSERT2 H5 O7 l/ U0 d/ [5 q: w
- /**
5 D! f' S- ?' S \/ D1 Y9 N - * @brief Reports the name of the source file and the source line number6 }! H. N3 `' W
- * where the assert_param error has occurred.3 U, _ ^4 x% a- t5 N; B H
- * @param file: pointer to the source file name3 M" J4 k `8 r- z& r
- * @param line: assert_param error line source number, [% x4 E: K7 B' t% h; ?6 {- D; D
- * @retval None& I8 V$ n" e0 P
- */
- e2 {8 j0 W! E - void assert_failed(uint8_t *file, uint32_t line)
0 ~' ?5 n% x0 ^; y& }) J" L. K- @ - {+ B5 }" R% @0 q* ?6 C7 r9 V
- /* USER CODE BEGIN 6 */: g% I. a6 n. \$ T% c) ?0 I
- /* User can add his own implementation to report the file name and line number,* I; b. K$ Z$ M% k
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/ S0 K1 Y# x' o% G; P- ^' Z - /* USER CODE END 6 */
$ M. }4 o, E J L# r" \; }: i' H - }/ L# ^! R* N! R. l/ ]8 U: N
- #endif /* USE_FULL_ASSERT */
% C# r$ }: j0 o: T. | - ( Z, ]- ]# F7 V1 x2 ]$ @
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
9 g; z+ v8 v( H$ h) O. M. N6 J
复制代码
; _7 W+ X% C, l8 e8 h0 x/ }6 h4.演示效果
1 a5 A; N# c1 g# O, a. T% v4 x% y$ J! i, u' U5 y3 k0 ]2 D n
( W* b, N; m% ~5 j" d. W
3 Z4 c" N( i; |" a
————————————————( u' f3 D" L, ^
版权声明:火花页.4 @: C$ I& o* u8 d1 U% {# x- D
, C3 z! `) M3 y0 f9 F+ t
/ p) t7 e, Q$ M2 n% s$ R, T
|