基于stm32的智能插座设计,其中包含hlw8012和DHT11和iaq等传感器设计代码,用于测量电流电压功率和温度湿度和空气中的TVOC值。同时也用到了i2c。
单片机源程序如下:
- #include "pbdata.h"
- extern u8 P_mode_flag;
- extern u8 I_mode_flag;
- extern u8 V_mode_flag;
- extern u8 close_IT_flag;
- u8 select_mode(u8 Mode)//默认为电流模式,返回值为当前的模式
- {
- if(Mode ==1)
- {
- HLW8012_SEL_L;
- }
- else
- {
- HLW8012_SEL_H;
- }
- return Mode ;
-
- }
- float HLW_avg(float *PULSE_T)
- {
- u16 k;
- float avg_8012=0;
- for(k=0;k<10;k++)
- {
- avg_8012= avg_8012+*(PULSE_T+k);
-
- }
- avg_8012=avg_8012/10;
- return avg_8012 ;
- }
- float HLW_V_DATA_CAL(float mea_V,float avg_8012)
- {
- mea_V=(cal_V*cal_V_T)/avg_8012;
- return mea_V;
- }
- float HLW_I_DATA_CAL(float mea_I,float avg_8012)
- {
- mea_I=(cal_I*cal_I_T)/avg_8012;
- return mea_I;
- }
- float HLW_P_DATA_CAL(float mea_P,float avg_8012)
- {
- mea_P=(cal_P*cal_P_T)/avg_8012;
- return mea_P;
- }
- void HLW_close_IT(void)
- {
- TIM3->CR1 &= ~(0x01);
- EXTI->IMR&=~(1<<1);//屏蔽line11上的中断
- EXTI->IMR&=~(1<<2);//屏蔽line11上的中断
- P_mode_flag=0;
- I_mode_flag=0;
- V_mode_flag=0;
- }
- float HLW_P_factor(float mea_P,float mea_V,float mea_I)
- {
- float P_factor;
- P_factor=mea_P/(mea_V *mea_I );
- return P_factor;
- }
- void HLW_8012_data(float mea_P,float mea_V,float mea_I,float P_factor)
- {
- printf("有效电压为: %.3f V\r\n",mea_V);
- printf("有效电流为: %.3f A\r\n",mea_I);
- printf("有功功率为: %.3f W\r\n",mea_P);
- P_factor =HLW_P_factor(mea_P,mea_V,mea_I);
- printf("功率因数为: %3f \r\n", P_factor);
- }
复制代码- #include "pbdata.h"
- float mea_P; //测量的有效功率
- float mea_apparent_P_; //测量的视在功率
- float P_factor ; //测量的功率因数
- float mea_V; //测量的电压
- float mea_I; //测量的电流
- u8 shift_Mode=0;
- u8 P_mode_flag=0;
- u8 I_mode_flag=0;
- u8 V_mode_flag=0;
- u8 close_IT_flag=0;
- u8 DHT11_sd;
- u8 DHT11_wd;
- u8 iaq_Read_buf[9]; //读取的数据存放的数组
- u16 iaq_pred;
- u8 iaq_status;
- s32 iaq_res;
- u16 iaq_tvoc;
- u32 ad=0; //存放ADC1读取的数值
- u32 ad1=0;//存放ADC2读取的数值
- u8 i=0;
- u8 j=0;
- double gl=0.5;
- double dy=1,dl=0.3;
- int main(void)
- {
-
- RCC_Configuration(); //系统时钟初始化
- GPIO_Configuration();//端口初始化
- USART_Configuration();//串口配置
- NVIC_Configuration(); //中断优先级配置
- ADC_Configuration(); //ADC
- ADC2_Configuration();
- I2C_Self_Init();//软件模拟I2C
- EXTI_Configuration();
- // TIM3_Configuration();//定时中断配置
- LCD_Init(); //液晶屏初始化
- LCD_Show();
-
- while(1)
- {
- }
- }
复制代码
所有资料51hei提供下载:
程序.zip
(3.26 KB, 下载次数: 78)
|