-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.c
More file actions
152 lines (133 loc) · 3.83 KB
/
answer.c
File metadata and controls
152 lines (133 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* 包含头文件 */
#include "ioCC2530.h"
#include <stdio.h>
#include <stdlib.h>
#include "hal_i2c.h"
#include "sht.h"
#include "sht1x.h"
#include "sht3x.h"
#define LED1 P1_0 // P1_0定义为P1_0 led灯端口
#define uint16 unsigned short
#define uint unsigned int //4Bytes
//uint tempereture,huimidity;
int8 sensor_tem;
uint8 sensor_humi;
uint counter=0; //统计中断次数
unsigned char tempHuim[2];//存放温湿值
char switchFlag;
void InitLED()
{
P1SEL&=~0X01; //设置 P1_0为 GPIO
P1DIR |= 0x01; //设置 p1_0 为输出口
LED1=0; //设置 p1_0 输出低电平
}
/**********串口通信初始化************************/
void initUART0(void)
{
PERCFG &= ~0x01; //外设控制寄存器,启用位置1
//=====》空白处开始
P0SEL = 0x3c; //设置 p0's 2,3,4,5 为1,作为外设口,其它位为0
U0CSR |= 0x80; //设置uart异步模式
U0BAUD = 216; //115200
U0GCR = 11;
//《=====空白处结束
U0UCR |= 0x80; //停止其它操作,就绪
UTX0IF = 0; // 清零UART0 TX中断标志
IEN0 |= 0X04; //UART0 接收中断使能
EA = 1; //使能全局中断
}
/*************************************************
* 函数名称:initTimer1
* 功 能:初始化定时器T1控制状态寄存器
******************定时器初始化*****************************/
void initTimer1()
{
//=====》空白处开始
CLKCONCMD &= 0x80; //时钟速度设置为32MHz
T1CTL = 0x0E; // 配置128分频,模比较计数工作模式,并开始运行
T1CCTL0 |= 0x04; //设定timer1通道0比较模式
T1CC0L =50000 & 0xFF; // 0.2S,把50000的低8位写入T1CC0L
T1CC0H = (50000 & 0xFF00) >> 8; //把50000的高8位写入T1CC0H
//《=====空白处结束
T1IF=0; //清除timer1中断标志(同IRCON &= ~0x02)
T1STAT &= ~0x01; //清除通道0中断标志
TIMIF &= ~0x40; //不产生定时器1的溢出中断
IEN1 |= 0x02; //使能定时器1的中断
EA = 1; //使能全局中断
}
/**************单片机发送数据到串口******************/
void UART0SendData(unsigned char *str,int len )
{
for(int i=0;i<len;i++)
{
U0DBUF = str[i]; // 将要发送的1字节数据写入U0DBUF
while (!UTX0IF) ; // 等待TX中断标志,即U0DBUF就绪
UTX0IF = 0; // 清零TX中断标志UART0SendByte(*str++);
}
}
/******************************************
* 功 能:定时器T1中断服务子程序
************************************/
#pragma vector = T1_VECTOR //中断服务子程序
__interrupt void T1_ISR(void)
{
EA = 0; //禁止全局中断
counter++;
T1STAT &= ~0x01; //清除通道0中断标志
EA = 1; //使能全局中断
}
#pragma vector = URX0_VECTOR
__interrupt void UART0_RX_ISR(void)
{
URX0IF = 0;
switchFlag = U0DBUF;
}
/******************************************
* 函数名称:main
* 功 能:main函数入口
* 入口参数:无
* 出口参数:无
* 返 回 值:无
**************************************************/
void main(void)
{
InitLED();
initTimer1(); //初始化Timer1
initUART0(); // UART0初始化
SHT_Init();//初始化温湿度
while(1)
{
//采集温湿度
//=====》空白处开始
SHT_SmpSnValue((int8 *)(&sensor_tem), (uint8 *)(&sensor_humi));
tempHuim[0]= sensor_tem;
tempHuim[1]= sensor_humi;
//《=====空白处结束
/*
//只支持旧温湿度
call_sht11(&tempereture, &huimidity);
tempHuim[0]=tempereture&0xff00>>8;
tempHuim[1]=huimidity&0x00ff;
if(tempereture>=28)
*/
if(sensor_tem>=0x16)// 0x16是22度
LED1 = 1;
if(counter>=10) //发送
{
U0CSR &=~0X40; //禁止接收
counter=0; //清标志位
//发送温湿度数据到串口
//=====》空白处开始
UART0SendData(tempHuim,2);
//《=====空白处结束
}
else//接收
{
U0CSR |=0X40; //允许接收,每次接收都使能接收器
if(switchFlag==1)
LED1 = 1; //指示灯熄开
else
LED1 = 0; //指示灯熄灭
}
}
}