📄 sht7i.c
字号:
#include <reg931.h> //Microcontroller specific library, e.g. port definitions
#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library
//********************************************************************************
#define uchar unsigned char //数据类型定义
#define uint unsigned int
sbit DATA= P0^1;
sbit SCK= P0^0;
#define _Nop() _nop_(),_nop_(),_nop_() //定义空指令
#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0 //读状态寄存器
#define STATUS_REG_R 0x07 //000 0011 1 //写状态寄存器
#define MEASURE_TEMP 0x03 //000 0001 1 //测温命令
#define MEASURE_HUMI 0x05 //000 0010 1 //测湿度命令
#define RESET 0x1e //000 1111 0 //软复位命令
typedef union
{ unsigned int i;
float f;
} value;
//********************************************************************************
//tools(串口输出)
//********************************************************************************
void Prints(uchar sMessage[26]);
void Putchar(uchar byte);
void Puthexbyte(uchar ch);
//char s_write_byte(unsigned char value);
//char s_write_byte(unsigned char tvalue);
/**********************************************************************
* 名称 : Puthexbyte
* 功能描述: 将数据用16进制的格式表示
* 输入参量: unsigned char ch
* 输出参量: 无
* 调用子程: Putchar
* 使用方法: 例如数据puthexbyte(0x16)将向串口送出0x31 0x36,如果用串口工具的文本模式将看到"16"
--------------------------------*/
void Puthexbyte(unsigned char ch)
{
unsigned char i;
i=(ch>>4);
if (i<=9) Putchar(0x30+i);
else Putchar (0x37+i);
i=(ch&0x0F);
if (i<=9) Putchar(0x30+i);
else Putchar (0x37+i);
}
/**********************************************************************
* 名称 : prints
* 功能描述: 打印一串字符,不超过50bytes
* 输入参量: unsigned char sMessage[50]
* 输出参量: 无
* 调用子程: Putchar
* 使用方法: prints("hello");
-------------------------------------*/
void Prints(unsigned char sMessage[26])
{
uchar k=0;
while(sMessage[k] != 0)
{
Putchar(sMessage[k]);
k++;
}
}
/**********************************************************************
* 名称 : Putchar
* 功能描述: 输出一个字节函数
* 输入参量: unsigned char byte
* 输出参量: 无
* 调用子程: 无
* 使用方法: 直接调用,例如如果要向串口发送方波即0x55,则调用 Putchar(0x55);
-------------------------------*/
void Putchar(unsigned char byte)
{
SBUF=byte;
while(TI==0);
TI=0;
}
/****************************************************
初始化串口
****************************************************/
void InitialUart()
{
SCON=0x50; //SM0=0,SM1=1,模式1
//REN=1,RI=0,允许接收
SSTAT=0x20; //使用独立中断
BRGCON=0; //先设置成0后,才可以置位下两位
BRGR0=0xF0; //7.373MHz内部RC振荡,波特率9600
BRGR1=0x02;
BRGCON=0x03; // enable BRG
IEN0=0X10; //开串口接收中断
IEN1=0x40; //开串口发送中断
EA=1;
}
/****************************************************
初始化IO口
****************************************************/
void InitialIO()
{
// P2M1=0xd3; //SPI总线IO口
// P2M2=0xd3;
P1M1=0x40; //串口和P1.7为准双向口
P1M2=0x40;
P0M1=0x00; //置P0.0,P1.1为准双向口
P0M2=0x00;
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char tvalue)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{
if (i & tvalue)
DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
_Nop();_Nop();_Nop(); //pulswith approx. 5 us
SCK=0;
_Nop();
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
// Prints("DATA=");
// Puthexbyte(DATA);
// Prints("\r\n");
SCK=0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
uchar i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{
SCK=1; //clk for SENSI-BUS
_Nop(); //后加
if (DATA) val=(val | i); //read bit
SCK=0; //后加
_Nop();
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_Nop();//_Nop();_Nop(); //pulswith approx. 5 us
SCK=0;
_Nop();//_Nop(); //后加
DATA=1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //Initial state
_Nop();
SCK=1;
_Nop();
DATA=0;
_Nop();
SCK=0;
_Nop();//_Nop();_Nop(); //修改
SCK=1;
_Nop();
DATA=1;
_Nop();
SCK=0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{
SCK=1;
_Nop();//_Nop();
SCK=0;
_Nop();//_Nop();
}
s_transstart(); //transmission start
}
/*
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
Prints("status=");
Puthexbyte(*p_value);
Prints("\r\n");
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -