IO模拟IIC读取TMP275AIDR器件的读取函数怎么写,请写出代码
时间: 2024-02-27 17:55:20 浏览: 28
好的,以下是模拟 IIC 读取 TMP275AIDR 器件的读取函数的示例代码:
```c
#include "stm32f10x.h"
#define SDA_OUT() GPIO_InitTypeDef GPIO_InitStructure; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; \
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; \
GPIO_Init(GPIOB, &GPIO_InitStructure)
#define SDA_IN() GPIO_InitTypeDef GPIO_InitStructure; \
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; \
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; \
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; \
GPIO_Init(GPIOB, &GPIO_InitStructure)
#define SDA_H() GPIO_SetBits(GPIOB, GPIO_Pin_7)
#define SDA_L() GPIO_ResetBits(GPIOB, GPIO_Pin_7)
#define SDA_READ() GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)
#define SCL_H() GPIO_SetBits(GPIOB, GPIO_Pin_6)
#define SCL_L() GPIO_ResetBits(GPIOB, GPIO_Pin_6)
void IIC_delay(uint16_t cnt)
{
uint16_t i;
while(cnt--)
{
i = 10;
while(i--);
}
}
void IIC_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SDA_H();
SCL_H();
}
void IIC_start(void)
{
SDA_OUT();
SDA_H();
SCL_H();
IIC_delay(5);
SDA_L();
IIC_delay(5);
SCL_L();
}
void IIC_stop(void)
{
SDA_OUT();
SCL_L();
SDA_L();
IIC_delay(5);
SCL_H();
SDA_H();
IIC_delay(5);
}
uint8_t IIC_wait_ack(void)
{
uint8_t i;
SDA_IN();
SDA_H();
IIC_delay(5);
SCL_H();
IIC_delay(5);
i = SDA_READ();
SCL_L();
return i;
}
void IIC_ack(void)
{
SDA_OUT();
SDA_L();
IIC_delay(5);
SCL_H();
IIC_delay(5);
SCL_L();
}
void IIC_nack(void)
{
SDA_OUT();
SDA_H();
IIC_delay(5);
SCL_H();
IIC_delay(5);
SCL_L();
}
void IIC_write_byte(uint8_t data)
{
uint8_t i;
SDA_OUT();
SCL_L();
for(i = 0; i < 8; i++)
{
if((data & 0x80) == 0x80)
SDA_H();
else
SDA_L();
data <<= 1;
IIC_delay(5);
SCL_H();
IIC_delay(5);
SCL_L();
}
}
uint8_t IIC_read_byte(uint8_t ack)
{
uint8_t i, data = 0;
SDA_IN();
for(i = 0; i < 8; i++)
{
data <<= 1;
SCL_L();
IIC_delay(5);
SCL_H();
IIC_delay(5);
if(SDA_READ())
data |= 0x01;
}
if(ack)
IIC_ack();
else
IIC_nack();
return data;
}
void TMP275AIDR_read_temp(float *temp)
{
uint8_t temp_data[2] = {0};
float t;
IIC_start();
IIC_write_byte(0x90); // 发送器件地址,写模式
IIC_wait_ack();
IIC_write_byte(0x00); // 发送子地址,温度数据寄存器
IIC_wait_ack();
IIC_start();
IIC_write_byte(0x91); // 发送器件地址,读模式
IIC_wait_ack();
temp_data[0] = IIC_read_byte(1);
temp_data[1] = IIC_read_byte(0);
IIC_stop();
t = (temp_data[0] << 4) | (temp_data[1] >> 4);
if(t & 0x800)
t = (0xFFFFF000 | t) / 16.0;
else
t /= 16.0;
*temp = t;
}
```
需要注意的是,以上代码是针对基于 STM32F10x 的开发板编写的,如果你的开发板不是 STM32F10x,可能需要修改相关的 GPIO 初始化和操作函数。另外,TMP275AIDR 的 I2C 地址是 0x48,因此在发送器件地址时需要将其左移一位,即发送 0x90(写模式)或 0x91(读模式)。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)