STM32F407单片机24bit_ADC_AD7190称重模块+16bit_DAC_电压输出实验KEIL工程源码
int main(void)
{
uint16_t data=0xFFFF/2;
double temp,opa;
float data_temp;
int32_t weight_count;
uint8_t cali_flag=0;
/* 复位所有外设,初始化Flash接口和系统滴答定时器 */
HAL_Init();
/* 配置系统时钟 */
SystemClock_Config();
/* 初始化串口并配置串口中断优先级 */
MX_DEBUG_USART_Init();
/* 初始化LED */
LED_GPIO_Init();
KEY_GPIO_Init();
/* 初始化BEEP */
BEEP_GPIO_Init();
if(AD7190_Init()==0)
{
printf("获取不到 AD7190 !\n");
while(1)
{
HAL_Delay(1000);
if(AD7190_Init())
break;
}
}
printf("检测到 AD7190 !\n");
weight_ad7190_conf();
HAL_Delay(500);
weight_Zero_Data = weight_ad7190_ReadAvg(6);
printf("zero:%d\n",weight_Zero_Data);
printf("硬石DAC(AD5689)模块模拟量电压输出\n");
AD5689_Init();
AD5689_WriteUpdate_DACREG(DAC_A,data);
AD5689_WriteUpdate_DACREG(DAC_B,0xFFFF-data);
printf("data:%d\n",data);
opa=OPA_RES_R2/OPA_RES_R1;
while(1)
{
weight_count=weight_ad7190_ReadAvg(3);
data_temp=weight_count-weight_Zero_Data;
weight=data_temp*1000/weight_proportion;
printf("重量:%d->%.2f\n",weight_count,weight);
HAL_Delay(200);
if(KEY1_StateRead()==KEY_DOWN) // 清零
{
weight_Zero_Data = weight_ad7190_ReadAvg(6);
printf("zero:%d\n",weight_Zero_Data);
cali_flag=1;
}
if(KEY2_StateRead()==KEY_DOWN) // 校准:必须先按“清零”键,然后把20g砝码放在称上,按下校准键
{
if(cali_flag)
{
weight_count = weight_ad7190_ReadAvg(6);
weight_proportion=(weight_count-weight_Zero_Data)*1000/100;
printf("weight_proportion:%d\n",weight_proportion);
}
cali_flag=0;
}
if(KEY3_StateRead()==KEY_DOWN)
{
if(data>(0xFFFF-1000))
data=(0xFFFF-1000);
data +=1000;
AD5689_WriteUpdate_DACREG(DAC_A,data);
AD5689_WriteUpdate_DACREG(DAC_B,0xFFFF-data);
temp=(double)(data*2-0xFFFF)*2500*opa/0xFFFF;
//temp为目标电源值,这里先放大1000倍(方便计算而已),等后面显示再还原
//data是数字量DA值,当data取值