#include
#include
#include "UART.H"
#include "DHT11.H"
#include "type.h"
#include "hal_lcd.h"
typedef unsigned int uint;
#define LED1 P1_0
// P1.0口控制LED1
#define LED4 P1_4
#define KEY1 P0_1 // P0.1口控制S1
uchar temp[3];
uchar humidity[3];
void DelayMS(uint msec)
{
uint i,j;
for (i=0; i for (j=0; j<535; j++);
}
void InitLed(void)
{
P1DIR |= 0x01; //P1.0定义为输出口
P1DIR |= 0x10;
LED4= 0;
LED1 = 0; //LED1灯上电默认为熄灭
}
void InitKey()
{
P0IEN |= 0x02; // P0.1 设置为中断方式 1:中断使能
PICTL |= 0x00; //上升沿触发
IEN1 |= 0x20; //允许P0口中断;
P0IFG = 0x00; //初始化中断标志位
EA = 1; //打开中断
}
#pragma vector = P0INT_VECTOR
__interrupt void P0_ISR(void)
{
DelayMS(10); //延时去抖
LED1 = ~LED1;
DelayMS(1000); //改变LED1状态
LED4 = ~LED4; //改变LED1状态
UartSendString("外部中断!温度是",16);
UartSendString(temp, 2);
UartSendString(" ", 2);
UartSendString("湿度",5);
UartSendString(humidity,2);
UartSendString("\n", 1);
P0IFG = 0; //清中断标志
P0IF = 0; //清中断标志
}
void InitT3()
{
T3CTL |= 0x08 ; //开溢出中断
T3IE = 1; //开总中断和T3中断
T3CTL |= 0xE0; //128分频,128/16000000*N=0.5S,N=62500
T3CTL &= ~0x03; //自动重装 00->0xff 62500/255=245(次)
T3CTL |= 0x10; //启动
EA = 1; //开总中断
}
unsigned int count;
//定时器T3中断处理函数
#pragma vector = T3_VECTOR
__interrupt void T3_ISR(void)
{
IRCON = 0x00; //清中断标志, 也可由硬件自动完成
if(count++ >945) //245次中断后LED取反,闪烁一轮(约为0.5 秒时间)
{ //经过示波器测量确保精确
count = 0; //计数清零
LED1 = ~LED1;
UartSendString("温度",5);
UartSendString(temp, 2);
UartSendString("\n", 1);
UartSendString("湿度",5);
UartSendString(humidity, 2);
//HalLcd_HW_WriteLine(4,humidity);
UartSendString("\n", 1);
/* */
}
}
/****************************************************************************
* 程序入口函数
****************************************************************************/
void main(void)
{
Delay_ms(1000); //让设备稳定
InitUart(); //串口初始化
HalLcd_HW_Init(); //初始化LCD
HalLcd_HW_WriteLine(1,"nanjing gebi dianzi");
while(1)
{
memset(temp, 0, 3);
memset(humidity, 0, 3);
DHT11(); //获取温湿度
//将温湿度的转换成字符串
temp[0]=wendu_shi+0x30;
temp[1]=wendu_ge+0x30;
humidity[0]=shidu_shi+0x30;
humidity[1]=shidu_ge+0x30;
UartSendString("temp", 5);
UartSendString(temp, 2);
Delay_ms(1000); //延时,2S读取1次
UartSendString(" ", 3);
UartSendString("humidity",9);
UartSendString(humidity, 2);
UartSendString("\n", 1);
//HalLcd_HW_WriteLine(2,temp);
//HalLcd_HW_WriteLine(3,strHumidity);
//HalLcd_HW_WriteLine(4,humidity);
Delay_ms(1000); //延时,2S读取1次
InitLed();
InitKey();
InitT3();
}
}