探索版
在做DAC实验的时候,用PA5测得的DAC输出电压和理论上的DAC电压不一致,按下按键之后,有时会两个电压值一样,有时候又差一些(相当于按下后测得的电压没有变化)。希望大神解答。
代码如下:
int main(void)
{
u16 adcx;
u16 dacx=0;
double temp;
double dacx2;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200); //初始化串口波特率为115200
LED_Init(); //初始化LED
LCD_Init();
My_DAC_Init();
POINT_COLOR=RED; //画笔颜 {MOD}:红 {MOD}
My_ADC_Init();
LCD_ShowString(30,40,210,24,24,"ADC MODE");
LCD_ShowString(30,70,200,16,16,"ADC1");
LCD_ShowString(30,90,200,16,16,"CHANNEL 5");
LCD_ShowString(30,120,300,24,24,"CHANNEL:0.000V");
LCD_ShowString(30,180,300,24,24,"DAC INPUT NUM:");
LCD_ShowString(30,210,300,24,24,"Should Be:0.000V");
while(1)
{
if(KEY1==0)
{
dacx+=200;
while(!KEY1);
}
if(KEY2==0)
{
dacx-=200;
while(!KEY2);
}
dacx2=(float)dacx*(3.3/4096);
DAC_Set_vol(dacx);
adcx=My_Get_ADC_Average_Value(20);
LCD_ShowxNum(30,150,adcx,4,24,0);
temp=(float)adcx*(3.3/4096);
adcx=temp;
LCD_ShowxNum(30+8*12,120,adcx,1,24,0);
LCD_ShowxNum(30+12*14,180,dacx,4,24,0);
LCD_ShowxNum(30+10*12,120,1000*(temp-adcx),3,24,0X80);
LCD_ShowxNum(30+10*12,210,(u16)dacx2,1,24,0X80);
LCD_ShowxNum(30+12*12,210,1000*(dacx2-(u16)dacx2),3,24,0X80);
}
}
初始化函数:
#include "dac.h"
void My_DAC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
DAC_InitTypeDef DAC_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitStruct.GPIO_OType=GPIO_OType_OD;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
DAC_InitStruct.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;
DAC_InitStruct.DAC_OutputBuffer=DAC_OutputBuffer_Disable;
DAC_InitStruct.DAC_Trigger=DAC_Trigger_None;
DAC_InitStruct.DAC_WaveGeneration=DAC_WaveGeneration_None;
DAC_Init(DAC_Channel_1,&DAC_InitStruct);
DAC_Cmd(DAC_Channel_1,ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R,0);
}
void DAC_Set_vol(u16 vol)
{
double temp=vol/1000;
temp=temp*4096/3.3;
DAC_SetChannel1Data(DAC_Align_12b_R,temp);
}
参考我们的按键例程,写一个专门的按键扫描函数就可以解决了。
好的,谢谢原子哥!
一周热门 更多>