新手上路
- 积分
- 31
- 金钱
- 31
- 注册时间
- 2017-5-29
- 在线时间
- 7 小时
|
1金钱
如题,我配置了两个DAC通道,分别出3.3V和0,通道一可以正常出电压,但通道二始终为一个固定电压2.33.
代码如下:
DAC:
DAC_HandleTypeDef DAC1_Handler;//DAC句柄
DAC_HandleTypeDef DAC2_Handler;
//初始化DAC
void DAC1_Init(void)
{
DAC_ChannelConfTypeDef DACCH1_Config;
DAC1_Handler.Instance=DAC;
HAL_DAC_Init(&DAC1_Handler); //初始化DAC
DACCH1_Config.DAC_Trigger=DAC_TRIGGER_NONE; //不使用触发功能
DACCH1_Config.DAC_OutputBuffer=DAC_OUTPUTBUFFER_DISABLE;//DAC1输出缓冲关闭
HAL_DAC_ConfigChannel(&DAC1_Handler,&DACCH1_Config,DAC_CHANNEL_1);//DAC通道1配置
HAL_DAC_Start(&DAC1_Handler,DAC_CHANNEL_1); //开启DAC通道1,通道2
}
//初始化DAC
void DAC2_Init(void)
{
DAC_ChannelConfTypeDef DACCH2_Config;
DAC1_Handler.Instance=DAC;
HAL_DAC_Init(&DAC2_Handler); //初始化DAC
DACCH2_Config.DAC_Trigger=DAC_TRIGGER_NONE; //不使用触发功能
DACCH2_Config.DAC_OutputBuffer=DAC_OUTPUTBUFFER_DISABLE;//DAC1输出缓冲关闭
HAL_DAC_ConfigChannel(&DAC1_Handler,&DACCH2_Config,DAC_CHANNEL_2);//DAC通道1配置
HAL_DAC_Start(&DAC1_Handler,DAC_CHANNEL_2); //开启DAC通道1,通道2
}
//DAC底层驱动,时钟配置,引脚 配置
//此函数会被HAL_DAC_Init()调用
//hdacAC句柄
void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_DAC_CLK_ENABLE(); //使能DAC时钟
__HAL_RCC_GPIOA_CLK_ENABLE(); //开启GPIOA时钟
GPIO_Initure.Pin=GPIO_PIN_4|GPIO_PIN_5; //PA4,PA5
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //模拟
GPIO_Initure.Pull=GPIO_NOPULL; //不带上下拉
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
}
主程序:
int main(void)
{
u8 key;
u8 led0sta=1,led1sta=1; //LED0,LED1的当前状态
u16 High=4095,Low=0;
Cache_Enable(); //打开L1-Cache
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
LED_Init(); //初始化LED
KEY_Init(); //按键初始化
DAC1_Init(); //DAC通道1初始化
DAC2_Init(); //DAC通道2初始化
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,Low);//设置DAC初始值
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_2,DAC_ALIGN_12B_R,Low);//设置DAC初始值
while(1)
{
key=KEY_Scan(0); //得到键值
if(key)
{
switch(key)
{
case WKUP_PRES: //
led1sta=1;led0sta=1;//A0,A1同时灭
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,Low);//设置DAC值
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_2,DAC_ALIGN_12B_R,Low);//设置DAC值
break;
case KEY2_PRES: //
led0sta=0;led1sta=1;//A0亮,A1灭
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,High);//设置DAC值
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_2,DAC_ALIGN_12B_R,Low);//设置DAC值
break;
case KEY1_PRES: //
led0sta=1;led1sta=0;//A0灭,A1亮
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,Low);//设置DAC值
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_2,DAC_ALIGN_12B_R,High);//设置DAC值
break;
case KEY0_PRES: //
led0sta=0;led1sta=0;//A0亮,A1亮
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_1,DAC_ALIGN_12B_R,High);//设置DAC值
HAL_DAC_SetValue(&DAC1_Handler,DAC_CHANNEL_2,DAC_ALIGN_12B_R,High);//设置DAC值
break;
}
LED0(led0sta); //控制LED0状态
LED1(led1sta); //控制LED1状态
}else delay_ms(10);
}
}
|
|