Mcuzone_TKN 发表于 2020-5-14 14:50:52

SAM4E-EK开发板代码解读12——lm75

关键词:Microchip AtmelSAM4E SAM4E-EKSAM4E16E 芯片 LM75A 温度


概述:简要介绍读取实时温度和LM75A


LM75A是一个高速I2C接口的温度传感器,可以在-55℃~+125℃的温度范围内将温度直接转换为数字信号,并可实现0.125℃的精度。MCU可以通过I2C总线直接读取其内部寄存器中的数据,并可通过I2C对4个数据寄存器进行操作,以设置成不同的工作模式。
LM75A有3个可选的逻辑地址管脚,使得同一总线上可同时连接8个器件而不发生地址冲突。LM75A可配置成不同的工作模式。它可设置成在正常工作模式下周期性地对环境温度进行监控,或进入关断模式来将器件功耗降至最低。OS输出有2 种可选的工作模式:OS比较器模式和OS中断模式,OS输出可选择高电平或低电平有效。
正常工作模式下,当器件上电时,OS工作在比较器模式,温度阈值为80℃,滞后阈值为75℃。
特点:
[*]提供环境温度对应的数字信息,直接表示温度;

[*]可以对某个特定温度作出反应,可以配置成中断或者比较器模式(OS输出);

[*]高速I2C总线接口,有A2-A0地址线,一条总线上最多可同时使用8个LM75A;

[*]低功耗设计,工作电流典型值为250uA,掉电模式为3.5uA;

[*]测量的温度最大范围为-55℃~+125℃;

[*]宽工作电压范围:2.8V~5.5V;

[*]提供了良好的温度精度(0.125℃);

[*]可编程温度阈值和滞后设定点。





[*]打开产品光盘SAM4E16E-EK/SAM4E16E-EK中文资料/softpack软件包/Atmel Studio 7,打开13b_TWI_MASTER_EXAMPLE_lm75例子。

int main(void)
{

int8_t temperature_int = 0;
uint32_t temperature_dec = 0;
sysclk_init();

board_init();

configure_console();

puts(STRING_HEADER);

printf("Configure system tick to get 1ms tick period.\n\r");
if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {
printf("-E- Systick configuration error\n\r");
while (1) {
}
}

/*设置LM75配置。* 2个故障队列周期;*极性高戒备;*中断模式下报警输出;*禁用关机模式。* / lm75_init();//初始化LM75驱动lm75_set_configuration(LM75_CONFIG_FAULT_QUEUE_2            |LM75_CONFIG_ALERT_POLARITY_ACTIVE_HIGH |LM75_CONFIG_INTERRUPT_MODE);
//设置温度限制,然后得到它
lm75_set_temperature_limit(TEMP_LIMIT_MAX, TEMP_LIMIT_DEC);
lm75_get_temperature_limit(&temperature_int, &temperature_dec);
printf("Temperature Limit:      %d.%04lu\r\n", temperature_int,
   (unsigned long)temperature_dec);
//设置温度滞后,然后得到它
lm75_set_temperature_hysteresis(TEMP_LIMIT_MIN, TEMP_LIMIT_DEC);
lm75_get_temperature_hysteresis(&temperature_int, &temperature_dec);
printf("Temperature Hysteresis: %d.%04lu\r\n", temperature_int,
   (unsigned long)temperature_dec);
while (1) {
//每秒钟检查温度并且打印出来
mdelay(1000);
if (TWI_SUCCESS !=
lm75_get_temperature(&temperature_int,
&temperature_dec)) {
break;
}
printf("Ambient Temperature:    %d.%04lu\r\n", temperature_int,
(unsigned long)temperature_dec);
}
printf("TWI bus operation error!\r\n");
while (1) {
}
}

















Mcuzone_TKN 发表于 2020-5-14 15:00:15

//读取ul_reg_index指定的LM75寄存器
static uint32_t lm75_read_register(uint32_t ul_reg_index,
                uint8_t *p_reg_value)
{
        twi_package_t rx = {
                .chip = BOARD_LM75_ADDR,
                .addr = {ul_reg_index},
                .addr_length = 1,
                .buffer = p_reg_value
        };

        if (ul_reg_index == LM75_CONF_REG) {
                rx.length = 1;
        } else {
                rx.length = 2;
        }

        return twi_master_read(BOARD_LM75_TWI_INSTANCE, &rx);
}

//写ul_reg_index指定的theLM75寄存器。
static uint32_t lm75_write_register(uint32_t ul_reg_index,
                uint8_t *p_reg_value)
{
        twi_package_t tx = {
                .chip = BOARD_LM75_ADDR,
                .addr = {ul_reg_index},
                .addr_length = 1,
                .buffer = p_reg_value
        };

        if (ul_reg_index == LM75_CONF_REG) {
                tx.length = 1;
        } else {
                tx.length = 2;
        }

        return twi_master_write(BOARD_LM75_TWI_INSTANCE, &tx);
}

//将16进制值转换为温度的整数部分。
static void lm75_hex_to_temperature_int(uint8_t uc_hex, int8_t *p_integer)
{
        *p_integer = (int8_t) uc_hex;
}

//将16进制值转换为温度的小数部分
static void lm75_hex_to_temperature_dec(uint8_t uc_hex, uint32_t *p_decimal)
{
        *p_decimal = ((uint32_t) uc_hex >> 4) * LM75_DEC_UNIT;
}

//将温度的整数部分转换为十六进制值。
static void lm75_temperature_to_hex_int(int8_t c_integer, uint8_t *p_hex)
{
        *p_hex = (uint8_t) c_integer;
}

//将温度的小数部分转换为十六进制值。
static void lm75_temperature_to_hex_dec(uint32_t ul_decimal, uint8_t *p_hex)
{
        *p_hex = (uint8_t) ((ul_decimal / LM75_DEC_UNIT) << 4);
}

//初始化控制器的硬件接口
static uint32_t lm75_interface_init(void)
{
        //TWI主初始化。
        twi_master_options_t opt = {
                .speed = TWI_SPEED,
                .chip = BOARD_LM75_ADDR
        };

        //初始化TWI主驱动程序。       
return twi_master_setup(BOARD_LM75_TWI_INSTANCE, &opt);
}

//获取环境温度
static uint32_t lm75_get_temperature(int8_t *p_integer,
                uint32_t *p_decimal)
{
        uint8_t uc_ta = { 0 };
        uint32_t ul_retval = lm75_read_register(LM75_TEMP_REG, uc_ta);

        if (ul_retval == TWI_SUCCESS) {
                lm75_hex_to_temperature_int(uc_ta, p_integer);
                lm75_hex_to_temperature_dec(uc_ta, p_decimal);
        }

        return ul_retval;
}

//设置温度限制
static uint32_t lm75_set_temperature_limit(int8_t c_integer, uint32_t ul_decimal)
{
        uint8_t uc_tset = { 0 };
        lm75_temperature_to_hex_int(c_integer, &uc_tset);
        lm75_temperature_to_hex_dec(ul_decimal, &uc_tset);

        return lm75_write_register(LM75_LIMT_REG, uc_tset);
}

//获取温度限制
static uint32_tlm75_get_temperature_limit(int8_t *p_integer, uint32_t *p_decimal)
{
        uint8_t uc_tset = { 0 };
        uint32_t ul_retval = lm75_read_register(LM75_LIMT_REG, uc_tset);

        if (ul_retval == TWI_SUCCESS) {
                lm75_hex_to_temperature_int(uc_tset, p_integer);
                lm75_hex_to_temperature_dec(uc_tset, p_decimal);
        }

        return ul_retval;
}

//设置温度滞后
static uint32_t lm75_set_temperature_hysteresis(int8_t c_integer,
                uint32_t ul_decimal)
{
        uint8_t uc_thyst = { 0 };
        lm75_temperature_to_hex_int(c_integer, &uc_thyst);
        lm75_temperature_to_hex_dec(ul_decimal, &uc_thyst);

        return lm75_write_register(LM75_HYST_REG, uc_thyst);
}

//得到温度滞后
static uint32_t lm75_get_temperature_hysteresis(int8_t *p_integer,
                uint32_t *p_decimal)
{
        uint8_t uc_thyst = { 0 };
        uint32_t ul_retval = lm75_read_register(LM75_HYST_REG, uc_thyst);

        if (ul_retval == TWI_SUCCESS) {
                lm75_hex_to_temperature_int(uc_thyst, p_integer);
                lm75_hex_to_temperature_dec(uc_thyst, p_decimal);
        }

        return ul_retval;
}

//设置LM75配置。
static uint32_t lm75_set_configuration(uint8_t uc_config)
{
        return lm75_write_register(LM75_CONF_REG, &uc_config);
}

//获取LM75配置。
static uint32_t lm75_get_configuration(uint8_t *p_config)
{
        return lm75_read_register(LM75_CONF_REG, p_config);
}

//初始化传感器。
static uint32_t lm75_init(void)
{
        return lm75_interface_init();
}

//启动传感器。
static uint32_t lm75_enable(void)
{
        uint8_t uc_config_reg = 0;
        uint32_t ul_retval = 0;

        ul_retval = lm75_read_register(LM75_CONF_REG, &uc_config_reg);
        if (ul_retval != TWI_SUCCESS) {
                return ul_retval;
        }

        uc_config_reg &= (uint8_t)(~LM75_CONFIG_SHUTDOWN_ENABLE);

        return lm75_write_register(LM75_CONF_REG, &uc_config_reg);
}

//短暂关闭感应器。
static uint32_t lm75_disable(void)
{
        uint8_t uc_config_reg = 0;
        uint32_t ul_retval = 0;

        ul_retval = lm75_read_register(LM75_CONF_REG, &uc_config_reg);
        if (ul_retval != TWI_SUCCESS) {
                return ul_retval;
        }

        uc_config_reg &= (uint8_t)(LM75_CONFIG_SHUTDOWN_ENABLE);

        return lm75_write_register(LM75_CONF_REG, &uc_config_reg);
}


页: [1]
查看完整版本: SAM4E-EK开发板代码解读12——lm75