#include <stdio.h>
#include <stdlib.h>
#include "hw_i2c.h"
#include "hw_adc.h"
#include "hw_gpio.h"
#include "hw_timer.h"
#include "hw_tft.h"
//传感器定义
#define I2C_ADDR_SENSOR 0x01
#define SENSOR_TEMP 0x01
#define SENSOR_HUM 0x02
#define SENSOR_POLLUTION 0x03
//TFT显示屏定义
#define LCD_WIDTH 320
#define LCD_HEIGH 240
//函数声明
void readLDSensorValue(int* temp, int* hum, int* pol);
void drawSensorInformation(int temp, int hum, int pol);
int main()
{
int temp, hum, pol;
//设置GPIO
HW_GPIO_init();
//读取传感器值
readLDSensorValue(&temp, &hum, &pol);
//设置TFT显示屏
HW_TFT_Init();
//在TFT上绘制传感器信息
drawSensorInformation(temp, hum, pol);
return 0;
}
//读取传感器值
void readLDSensorValue(int* temp, int* hum, int* pol)
{
unsigned char data[2];
//读取温度
hwI2CReadBytes(I2C_ADDR_SENSOR, SENSOR_TEMP, data, 2);
*temp = (data[0]<<8)|data[1];
//读取湿度
hwI2CReadBytes(I2C_ADDR_SENSOR, SENSOR_HUM, data, 2);
*hum = (data[0]<<8)|data[1];
//读取环境污染值
hwI2CReadBytes(I2C_ADDR_SENSOR, SENSOR_POLLUTION, data, 2);
*pol = (data[0]<<8)|data[1];
}
//绘制传感器信息
void drawTemperatureHumidityPollution(int temp,int hum,int pol)
{
char buff[20];
//绘制温度
sprintf(buff,"Temp. %dC",temp);
HW_TFT_UsrTextDraw(0,LCD_HEIGH-3*20, buff ,20 ,White, Black );
//绘制湿度
sprintf(buff,"Hum. %d%%",hum);
HW_TFT_UsrTextDraw(0,LCD_HEIGH-2*20, buff ,20 ,White, Black );
//绘制环境污染度
sprintf(buff,"Polution: %dmg/m^3",pol);
HW_TFT_UsrTextDraw(0,LCD_HEIGH-1*20, buff ,20 ,White, Black );
}
写代码的门槛越来越低了