//得到ADC采样内部温度传感器的温度值 //返回值3位温度值 XXX*0.1C int Get_Temp(void) { float temperate; temperate=(float)Get_Adc(TEMP_CH)*(3.3/4096);//得到温度传感器的电压值 temperate=(1.43-temperate)/0.0043+25;//计算出当前温度值 temperate*=10;//扩大十倍,使用小数点后一位 return (int)temperate; }
//获取通道ch的转换值,取times次,然后平均 //ch:通道编号 //times:获取次数 //返回值:通道ch的times次转换结果平均值 u16 Get_Adc_Average(u8 ch,u8 times) { u32 temp_val=0; u8 t; for(t=0;t<times;t++) { temp_val+=Get_Adc(ch); } return temp_val/times; }
//得到ADC采样内部温度传感器的值 //取10次,然后平均 u16 Get_Temp(void) { u16 temp_val=0; u8 t; for(t=0;t<10;t++) { temp_val+=Get_Adc(TEMP_CH); delay_ms(5); } return temp_val/10; }
/** * @brief 获取连续10次ADC采样的平均值 * @param ADCx: ADC1 / ADC2 / ADC3 * @param ch:通道值 ADC_Channel_0 ~ ADC_Channel_17 * @retval ADC转换的平均值 * @note */ uint16_t Get_Adc_Average(ADC_TypeDef* ADCx, uint8_t ch) { uint32_t adc_val = 0; uint8_t i; for(i = 0; i < 10; i++) { adc_val += Get_Adc(ADCx, ch); delay_ms(5); } return adc_val/10; }
//得到ADC采样内部温度传感器的温度值 //返回值3位温度值 XXX*0.1C int Get_Temp(void) { u16 temp_val=0; u8 t; float temperate; for(t=0;t<20;t++)//读20次,取平均值 { temp_val+=Get_Adc(16);//温度传感器为通道16 } temp_val/=20; temperate=(float)temp_val*(3.3/4096);//得到温度传感器的电压值 temperate=(1.43-temperate)/0.0043+25;//计算出当前温度值 temperate*=10;//扩大十倍,使用小数点后一位 return (int)temperate; }
/***************************************************************************** 函 数 名: AirCondition_Temp 功能说明: 空调外部温度更新 参 数: 无 返 回 值: 无 *****************************************************************************/ void AirCondition_Temp(void) { static u8 cnt = 0; static u16 Average = 0; u16 val; val = Get_Adc(0); // if ((Average > val ? Average - val : val - Average) > 200) //取值波动较大 // { // ADCValue = val; // Average = val; // cnt = 0; // } // else //取平均值 // { // ADCValue += val; // Average = ADCValue/(++cnt); //取每次的平均值 // } // ADCValue += val; if (++cnt >= 10) //取样十次 { cnt = 0; Average = ADCValue/10; for (val = 0; val < sizeof(RT_Tab)/sizeof(RT_Tab[0]); val++) { if (Average >= RT_Tab[val]) { gAirCondition.temp = val + gAirCondition.Correction_temperature; break; } } ADCValue = 0; } }
int main(void) { u16 adcx; float temp; SystemInit(); delay_init(72); //延时初始化 NVIC_Configuration(); uart_init(9600); LED_Init(); KEY_Init(); LCD_Init(); Adc_Init(); POINT_COLOR=RED;//设置字体为红色 LCD_ShowString(60,50,"Mini STM32"); LCD_ShowString(60,70,"ADC TEST"); LCD_ShowString(60,90,"ATOM@ALIENTEK"); LCD_ShowString(60,110,"2010/12/30"); //显示提示信息 POINT_COLOR=BLUE;//设置字体为蓝色 LCD_ShowString(60,130,"ADC_CH0_VAL:"); LCD_ShowString(60,150,"ADC_CH0_VOL:0.000V"); while(1) { adcx=Get_Adc(ADC_Channel_0); LCD_ShowNum(156,130,adcx,4,16);//显示ADC的值 temp=(float)adcx*(3.3/4096); adcx=temp; LCD_ShowNum(156,150,adcx,1,16);//显示电压值 temp-=adcx; temp*=1000; LCD_ShowNum(172,150,temp,3,16); LED0=!LED0; delay_ms(250); } }
int main(void) { // u8 key; // u16 i=0; u16 adc_91000,adc_91200,reg_read_data=0,reg_write_data=0; float LMP91000_VOUT,LMP91200_VOUT,Temp_Integer,Temp_Decimal; short temp; u8 read_val[5] = {0xff}; u8 status = TI_LMP91000_NOT_READY; SystemInit(); delay_init(72); //延时初始化 NVIC_Configuration(); uart_init(9600); LED_Init(); KEY_Init(); Adc_Init(); IIC_Init(); //IIC初始化 LMP91200_Init(); //初始化PH delay_ms(500); printf("\nHello XXL"); /************************************************************ * 溶氧电极初始化及数据采集 ************************************************************/ LMP91000_ENABLE(); // enable LM while (status == TI_LMP91000_NOT_READY) // wait while device is not ready status = LMP91000_ReadOneByte(TI_LMP91000_STATUS_REG); // Read device ready status read_val[0] = LMP91000_ReadOneByte(TI_LMP91000_LOCK_REG); // Read from lock register default value 0x01 read_val[1] = LMP91000_ReadOneByte(TI_LMP91000_TIACN_REG); // Read TIA control register default value 0x03 read_val[2] = LMP91000_ReadOneByte(TI_LMP91000_REFCN_REG); // Read Reference control register default value 0x20 read_val[3] = LMP91000_ReadOneByte(TI_LMP91000_MODECN_REG); // Read Mode control register default value 0x00 LMP91000_WriteOneByte(TI_LMP91000_LOCK_REG, TI_LMP91000_WRITE_UNLOCK); // unlock the registers for write LMP91000_WriteOneByte(TI_LMP91000_TIACN_REG, TIACN_NEW_VALUE); // Modify TIA control register LMP91000_WriteOneByte(TI_LMP91000_REFCN_REG, REFCN_NEW_VALUE); // Modify REF control register LMP91000_WriteOneByte(TI_LMP91000_MODECN_REG, MODECN_NEW_VALUE); // Modify MODE control register read_val[1] = LMP91000_ReadOneByte(TI_LMP91000_TIACN_REG); // Read to confirm register is modified read_val[2] = LMP91000_ReadOneByte(TI_LMP91000_REFCN_REG); // Read to confirm register is modified read_val[3] = LMP91000_ReadOneByte(TI_LMP91000_MODECN_REG); // Read to confirm register is modified read_val[4] = LMP91000_ReadOneByte(TI_LMP91000_STATUS_REG); // test if write/read values match if (read_val[1] == TIACN_NEW_VALUE) { // while (1) // no error: blink LED continuously // { delay_ms(500); LED1=!LED1;//绿灯闪烁 // } } else { delay_ms(500); LED0=!LED0;//红灯闪烁 // error } // 获取LMP91000输出电压 // while(1) // { adc_91000=Get_Adc(ADC_Channel_8); LMP91000_VOUT=(float)adc_91000*(3.3/4096); LMP91000_VOUT=LMP91000_VOUT; printf("\n\nDO=%fv ",LMP91000_VOUT); temp=DS18B20_Get_Temp(); if(temp<0) { temp=-temp; } Temp_Integer=temp/10; Temp_Integer=Temp_Integer; //整数 Temp_Decimal=temp%10; Temp_Decimal=Temp_Decimal; //小数 printf(" Temp=%f.%fc " __TIME__ "",Temp_Integer,Temp_Decimal); delay_ms(1000); // } LMP91000_WriteOneByte(TI_LMP91000_LOCK_REG, TI_LMP91000_WRITE_LOCK); // lock the registers LMP91000_DISABLE(); /************************************************************ * PH电极初始化及数据采集,无论在空气,碱水,酸水中vout总是1.420~1.429,可能电极坏掉 ************************************************************/ Clr_LMP91200_CSN; reg_write_data = TI_LMP91200_CONFIG_REG_TEST_VALUE; // value to write reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write to config register, read old value reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write again to read previous update reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write again to read previous update,if not read this time,bit-15 will be wrong // Set_LMP91200_CSN; while(1) // test if write/read values match { if (reg_write_data == reg_read_data) { adc_91200=Get_Adc(ADC_Channel_9); LMP91200_VOUT=(float)adc_91200*(3.3/4096); LMP91200_VOUT=LMP91200_VOUT; LED1=!LED1; //正确则绿灯亮 delay_ms(500); } else { LED0=!LED0; //错误则黄灯亮 delay_ms(300); } } /************************************************************ * 温度传感器初始化及数据采集 ************************************************************/ while(DS18B20_Init())//初始化DS18B20,兼检测18B20 { delay_ms(600); // error_report();//DS18B20 未连接 } while(1) { temp=DS18B20_Get_Temp(); if(temp<0) { temp=-temp; } Temp_Integer=temp/10; Temp_Integer=Temp_Integer; //整数 Temp_Decimal=temp%10; Temp_Decimal=Temp_Decimal; //小数 delay_ms(500); LED1=!LED1;//DS1闪烁 } /************************************************************ * 按键扫描 ************************************************************/ #if 0 while(1) { key=KEY_Scan(); if(key==1)//KEY0按下,写入24C02 { ; } if(key==3)//KEY_UP按下,读取字符串并显示 { ; } i++; delay_ms(10); } #endif }