这个月实在有点忙,智能家居这块有很多要做的东西,晃荡晃荡就到月底了。晚上回去把原来拿来练手的传感器和板卡一一找出来,放到调试盒里,这次使用LPC54114板卡,准备测试的线和工具,想想,有遗漏的再补充。当时选题目,比较近的就是宿舍环境了,就想到宿舍改造还比较接地气。按照实现框图,先把环境监测这部分功能做了,以前做过,也比较顺手。 组装图如图1所示: 温度和气压这块使用BMP180模块,同时使用OLED来做显示。BMP180的驱动代码及显示代码如下所示: - void BMP085Convert(void)
- {
- ut = BMP085ReadTemp();
- up = BMP085ReadPressure();
-
- x1 = (((long)ut-(long)ac6)*(long)ac5)>>15;
- x2 = ((long)mc<<11)/(x1+md);
- b5 = x1+x2;
- temper = ((b5+8)>>4);
- b6 = b5-4000;
-
- x1 = (b2 * (b6 * b6)>>12)>>11;
- x2 = (ac2 * b6)>>11;
- x3 = x1 + x2;
- b3 = ((((long)ac1*4 + x3)<<OSS) + 2)>>2;
-
- // Calculate B4
- x1 = (ac3 * b6)>>13;
- x2 = (b1 * ((b6 * b6)>>12))>>16;
- x3 = ((x1 + x2) + 2)>>2;
- b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
- b7 = ((unsigned long)(up - b3) * (50000>>OSS));
- if (b7 < 0x80000000)
- p = (b7<<1)/b4;
- else
- p = (b7/b4)<<1;
-
- x1 = (p>>8) * (p>>8);
- x1 = (x1 * 3038)>>16;
- x2 = (-7357 * p)>>16;
- pressure = p+((x1 + x2 + 3791)>>4);
- altitude=44330.0*(1-pow((double)pressure/101325,1/5.255));
复制代码- BMP085Convert();
- for(t=0;t<10;t++)
- Delay_nms(0xAFFFF);
- lcdshow[0] =(u8) (temper/100);
- lcdshow[1] =(u8) ((temper-(temper/100)*100)/10);
- lcdshow[2] =(u8) (temper%10);
- sprintf(lcdtest,"%d",lcdshow[0]);
- sprintf(lcdtest2,"%d",lcdshow[1]);
- sprintf(lcdtest3,"%d",lcdshow[2]);
- LCD_Print(40,0,lcdtest,TYPE16X16,TYPE8X16);
- LCD_Print(48,0,lcdtest2,TYPE16X16,TYPE8X16);
- LCD_Print(56,0,lcdpoint,TYPE16X16,TYPE8X16);
- LCD_Print(64,0,lcdtest3,TYPE16X16,TYPE8X16);
- LCD_Print(72,0,lcdword4,TYPE16X16,TYPE8X16);
复制代码
按照实现框图,使用风扇通过反馈的气压和温度来调节室内空气流量和室温,就拿直流电机小风叶和L298模块来完成这部分功能。组装如图2所示:
实现代码如下: - /*
- *********************************************************************************************************
- * 函数名称: Motor_PWMConfig
- * 功能说明: 电机PWM控制初始化
- * 形参:
- * 返回值:
- *********************************************************************************************************
- */
- void Motor_PWMConfig(void)
- {
- sctimer_config_t sconfig;
- sctimer_pwm_signal_param_t pwmParam;
- uint32_t srctClock_Hz;
-
- srctClock_Hz = CLOCK_GetFreq(kCLOCK_BusClk);
- SCTIMER_GetDefaultConfig(&sconfig);
- SCTIMER_Init(SCT0, &sconfig);
- /* Configure PWM params with frequency 24kHZ from output 4 */
- pwmParam.output = kSCTIMER_Out_5;
- pwmParam.level = kSCTIMER_HighTrue;
- pwmParam.dutyCyclePercent = 18u;//updatedDutycycle;
- SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_CenterAlignedPwm, 1000U, srctClock_Hz, &eventNumberOutput5);
- }
复制代码
设想是温度升到一定程度后,风扇通过PWM驱动调节风速,排热和增加空气流动。效果如图3所示:
|