【STC8】LM75AD

本文详细介绍了STC8单片机如何与LM75AD进行通信,包括通信的时序图,具体程序编写,寄存器设置以及完整的程序示例,为单片机控制温度传感器提供了清晰的操作流程。
摘要由CSDN通过智能技术生成

时序图

image

image

  1. 写地址
  2. 写寄存器指针
  3. 重新开始
  4. 写地址
  5. 读数据

程序


#define LM75A_ADDRESS_READ 0b10010001
#define LM75A_ADDRESS_WRITE 0b10010000
#define LM75A_REGISTER_TEMPERATURE 0

/* starts i2c */
void initLM75A(); 
/* gets the full temperature register info and returns it to master */
uint16_t getTemperature();
/*
   allows the lm75a temperature register to be read
   Returns: unsigned 16bit int containing the temperature. The top 8 bits represent the whole number part of the temperature, bits 9, 10 and 11 represent the fractional part of the temperature (0.5, 0.25 and 0.125 degrees) and the remaining bits are always 0
*/
uint16_t getTemperature()
{
   
	i2cStart();
	i2cSend(LM75A_ADDRESS_WRITE);
	i2cSend(LM75A_REGISTER_TEMPERATURE); // set register pointer to temperature register

	i2cStart(); // restart, now reading
	i2cSend(LM75A_ADDRESS_READ);

	uint8_t upperByte = i2cRead(ACK);
	uint8_t lowerByte = i2cRead(NACK);

	i2cStop();

	return (upperByte << 8) | lowerByte;
}

寄存器

image

image

完整程序

#ifndef _LM75AD_H_
#define _LM75AD_H_

#include    "config.h"
#include	"STC8A_GPIO.h"
#include	"STC8A_Delay.h"


#define 	LM75A_ADDR		0X48   	//i2c slave address
#define		REG_TEMP		0X00	//温度,只读
#define		REG_CONF		0X01	//配置
#define		REG_THYST		0X02	//滞后控制
#define		REG_TOS			0X03	//超温关闭寄存器

#define IIC_WRITE 0	//数据方向,写入
#define IIC_READ  1  //数据方向,接收

#define LM75AD_SDA_IN()  P5_MODE_IO_PU(GPIO_Pin_2);
#define LM75AD_SDA_OUT(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值