DAC是一个将数字量转换为模拟量的器件,简单来说就是我们可以通过编程来输出某个特定的电压值(电压值就是模拟量)。
也就是stm32G4内部集成了一个DAC模块,然后我们通过编程,可以实现在stm32的特定的管脚上输出一个电压。或者输出一个变化的电压波形(比如正弦波,三角波等等)。
可以打开stm32G4的数据手册的Pin Defination找到stm32的哪个管脚有DAC功能。
可以看到本开发板一共有一个DAC输出,就是DAC1,而这个输出有两个通道,分别对应PA4和PA5两个引脚。
stm32G4芯片的DAC特性有:
目的就是编程使得PA4和PA5管教上可以输出电压,并且这个电压可以由程序控制。
首先先要在模板工程设置PA4和PA5为DAC输出模式:
设置完之后这两个引脚还是黄色的,是因为我们还没有配置他的输出模式。在右边"Analog"选项里面找到DAC1,然后配置两个引脚的输出模式:
一共有三种模式:
第一种是将DAC产生的电压通过引脚连接到外部,这个是我们需要的。
第二种是将DAC的电压作为一个电压基准输出到其他外设上面去。
第三种是又输出到外部引脚,又输出到片内外设上面。
我们只需要设置为第一种就可以了。完整的配置如下图:
然后点击生成代码即可。然后将dac.c和dac.h文件移植到我们的编程文件里面去。
移植程序最重要的几点就是看库函数有没有加,时钟有没有对应好,main函数有没有include 这个头文件,对应的config有没有去掉注释。
做好移植程序的工作之后
由于我们用的模式都是12为数据,所以在定义关于存储dac的变量的时候定义的变量都必须是2个字节的数据也就是16位数据。(因为1个字节只有8位数据,不够用)
最后的main.c如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* © Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "led.h"
#include "key.h"
#include "i2c.h"
#include "dac.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//Led执行程序
__IO uint32_t ledTick =0,keyTick=0;
u8 led_ctrl=0xff;
void LED_Process(void)
{
if(uwTick-ledTick<500)return;
ledTick=uwTick;
LED_Control(led_ctrl);
led_ctrl=~led_ctrl;
}
void KEY_Process(void)
{
if(uwTick-keyTick<10)return;
keyTick=uwTick;
Key_Read();
// if(Trg&0x01)
// {
// LED_Control(0x01);
// }
if(Trg)
{
LED_Control(Trg);
}
}
void LCD_Process(void)
{
u8 display_buf[20];
//[问题]长数据对端数据的覆盖问题
sprintf((char*)display_buf,"%d",4000);
LCD_DisplayStringLine(Line0,display_buf);
sprintf((char*)display_buf,"%d",10);
LCD_DisplayStringLine(Line0,display_buf);
//解决方案:加空格,针对字符串
LCD_DisplayStringLine(Line2,"hello");
LCD_DisplayStringLine(Line2,"h ");
//解决方案:格式化输出,针对数据
sprintf((char*)display_buf,"%5d",5000);//默认5位,显示右对齐
LCD_DisplayStringLine(Line3,display_buf);
sprintf((char*)display_buf,"%5d",10);
LCD_DisplayStringLine(Line3,display_buf);
sprintf((char*)display_buf,"%-5d",10);//左对齐
LCD_DisplayStringLine(Line4,display_buf);
sprintf((char*)display_buf,"%05d",500);//前面补0
LCD_DisplayStringLine(Line5,display_buf);
sprintf((char*)display_buf,"%5.2f",3.1415926);//显示小鼠,总长是5位,小数点算一位
LCD_DisplayStringLine(Line6,display_buf);
sprintf((char*)display_buf,"%x",15);
LCD_DisplayStringLine(Line7,display_buf);//%x显示16进制,%o显示8进制
sprintf((char*)display_buf,"%c",'a');
LCD_DisplayStringLine(Line8,display_buf);//%s字符串,%c是字符
sprintf((char*)display_buf,"%d %%",10);
LCD_DisplayStringLine(Line9,display_buf);//输出百分号:%
}
u8 val_24c02=0;
u16 dac_ch1_val,dac_ch2_val;
void DAC_Process(void)
{
dac_ch1_val=(1.1f/3.3f*4095);//输出1.1V
dac_ch2_val=(2.1f/3.3f*4095);//输出2.2V
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,dac_ch1_val);//0->0V;4095->3.3V
HAL_DAC_Start(&hdac1,DAC_CHANNEL_1);
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_2,DAC_ALIGN_12B_R,dac_ch2_val);//0->0V;4095->3.3V
HAL_DAC_Start(&hdac1,DAC_CHANNEL_2);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LED_Control(0x00);
MX_DAC1_Init();
//LCD_Process();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_Process();
I2CInit();
EEPROM_Write(0x10,0x55);
val_24c02=EEPROM_Read(0x10);
u8 display_buf[20];
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//LED_Process();
sprintf((char*)display_buf,"EEPROM:%d",val_24c02);
LCD_DisplayStringLine(Line1,display_buf);//输出百分号:%
KEY_Process();
DAC_Process();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
要注意的一点是:
DAC最好是先赋值然后再启动(就是下面这个顺序)
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,dac_ch1_val);//0->0V;4095->3.3V
HAL_DAC_Start(&hdac1,DAC_CHANNEL_1);
ADC最好是先启动然后再赋值。