问题描述
- CC2530-SHT1X温湿度报警器的设计
-
#include
#include "ugOled9616.h"
#include "LcdDisp.h"
#include#define uint unsigned int
#define uchar unsigned char#define SHT1X_DAT P1_4 //定义数据线
#define SHT1X_DAT_DIR P1DIR //定义数据线输出寄存器
#define SHT1X_DAT_BV BV(4) //数据线位定义
#define SHT1X_SCK P1_5 //定义时钟线
#define SHT1X_SCK_DIR P1DIR //定义时钟线输出寄存器
#define SHT1X_SCK_BV BV(5) //时钟线位定义//定义数据线输入/输出
#define SHT1X_DATA_IN st(SHT1X_DAT_DIR &= ~(SHT1X_DAT_BV);)
#define SHT1X_DATA_OUT st(SHT1X_DAT_DIR |= SHT1X_DAT_BV;)//定义控制灯的端口
#define RLED P1_0 //定义LED1为P10口控制
#define YLED P1_1 //定义LED2为P11口控制
#define K1 P0_6 //P0_6#define ON 0
#define OFF 1uint16 sht;//读取的温湿度,浮点值
float tempsht;//转换的温度,浮点值
float humsht;//转换的湿度,浮点值extern void SHT1X_PORT_INT(void); //SHT10 引脚初始化
extern uint8 WaitForSHT1XAck(void); //等待SHT10 确认函数
extern void SHT1X_REST(void); //SHT10 复位函数
extern void SHT1X_STAT(void); //SHT10 启动函数
extern uint8 SendToSHT1X(uint8 data); //发送数据到SHT10 函数
extern void WaitForAminit(uint16 delay); //等待转换函数
extern void SHT1X_INT(void); //SHT10 初始化函数
extern uint16 Read_SHT1X(uint8 option); //读SHT10 传感器函数void delay(uint n);
void InitKey(void);//按键初始化程序
uchar KeyScan(void);
void Initial(void);//Led程序初始化
void Lcddisplay(void);void Lcddisplay(void)
{
char tempBuf[4];
sht = Read_SHT1X(3);
sht&=0x3fff;
tempsht = (float)(sht*0.01) - 39.60;
sprintf( (void )tempBuf,"%f",tempsht);
LcdPutString16_8( 8,0,(uint8)tempBuf,4,1);
delay(10);
char humBuf[6];
sht = Read_SHT1X(5);
sht&=0x0fff;
humsht = (float)(sht*0.0405) - 4 - (sht * sht * 0.000028);
sprintf( (void )humBuf,"%f",humsht);
humBuf[5] = 37;//%
LcdPutString16_8( 48,0,(uint8)humBuf,6,1);
delay(10);
}/****************************
//按键初始化程序
*****************************/
void InitKey(void)
{
P0SEL &= ~0X40;
P0INP |= 0x40; //上拉
P0DIR &= ~(0x01<<(6)); //按键在P06 ADC采集
}/*****************************************
//读键值
*****************************************/
uchar KeyScan(void)
{
if(K1 == 0) //低电平有效
{
delay(100); //检测到按键
if(K1 == 0)
{
while(!K1); //直到松开按键
return(1);
}
}
}/*****************************************
//led程序初始化
*****************************************/
void Initial(void)
{
P1DIR |= 0x03; //P10、P11定义为输出
RLED = 1;
YLED = 1; //LED
}//*************************************************************************
//函数原型:void Delay(uint n)
//功 能:延时
//输 入:uint n
//输 出:NULL
//*************************************************************************
void delay(uint n)
{
uint tt;
for(tt = 0;tt<n;tt++);
for(tt = 0;tt<n;tt++);
for(tt = 0;tt<n;tt++);
for(tt = 0;tt<n;tt++);
for(tt = 0;tt<n;tt++);
}//*************************************************************************
//函数原型:uint16 Read_SHT1X(uint8 option)
//功 能:读取温湿度
//输 入:3-温度 5-湿度
//输 出:读取的温湿度,浮点值
//*************************************************************************
uint16 Read_SHT1X(uint8 option)
{
uint16 temp = 0;
uint8 i;
SHT1X_STAT();//启动传感器
if(SendToSHT1X(option&0x1f) == 0)
return 1;//芯片没有应答
SHT1X_DATA_IN;
for(i = 0;i<10;i++)//等转换完成
WaitForAminit(30000);
SHT1X_SCK = 0;
NOP();
SHT1X_SCK = 1;
NOP();
if(SHT1X_DAT != 0)
{
return 2;//芯片没有响应 转换完成
}
SHT1X_SCK = 0;
NOP();
for(i = 0;i<7;i++)
{
SHT1X_SCK = 1;
NOP();
temp <<= 1;
if(SHT1X_DAT)
{
temp |= 1;
}
SHT1X_SCK = 0;
NOP();
}
SHT1X_DATA_OUT;
NOP();
SHT1X_DAT = 0;
NOP();
SHT1X_SCK = 1;
NOP();NOP();
SHT1X_SCK = 0;
NOP();
NOP();
SHT1X_DATA_IN;
for(i = 0;i<8;i++)
{
SHT1X_SCK = 1;
NOP();
temp <<= 1;
if(SHT1X_DAT)
{
temp |= 1;
}
SHT1X_SCK = 0;
NOP();
}
SHT1X_DATA_OUT;
NOP();
SHT1X_DAT = 1;
SHT1X_SCK = 1;
NOP();
SHT1X_SCK = 0;
return temp;
}void main(void)
{
char Status = 1;//报警器状态,1为开启
RLED = OFF; //关灯
uchar Keyvalue = 0 ;
Initial();
//intClock(); ??函数干嘛用的
P2DIR |= 0x01; //打开电源
P2 &=~ 0x01;
SHT1X_INT();
ugOled9616int(); //初始化OLed 配置
delay(10);
while(1)
{Lcddisplay();//显示温湿度传感器信号
Keyvalue = KeyScan(); //扫键
while(Status == 1)
{
if(tempsht>=28.00&&Keyvalue==0)
{
RLED = !RLED;
delay(500);
}
if(tempsht>=28.00&&Keyvalue==1)
{
RLED = OFF; //关灯Status = 0 ;
}
}
}
}编译的时候老是在SHT1X_DATA_IN;SHT1X_DATA_OUT出错,求大神解答