|
- //读取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[2] = { 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[0], p_integer);
- lm75_hex_to_temperature_dec(uc_ta[1], p_decimal);
- }
- return ul_retval;
- }
- //设置温度限制
- static uint32_t lm75_set_temperature_limit(int8_t c_integer, uint32_t ul_decimal)
- {
- uint8_t uc_tset[2] = { 0 };
- lm75_temperature_to_hex_int(c_integer, &uc_tset[0]);
- lm75_temperature_to_hex_dec(ul_decimal, &uc_tset[1]);
- return lm75_write_register(LM75_LIMT_REG, uc_tset);
- }
- //获取温度限制
- static uint32_t lm75_get_temperature_limit(int8_t *p_integer, uint32_t *p_decimal)
- {
- uint8_t uc_tset[2] = { 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[0], p_integer);
- lm75_hex_to_temperature_dec(uc_tset[1], p_decimal);
- }
- return ul_retval;
- }
- //设置温度滞后
- static uint32_t lm75_set_temperature_hysteresis(int8_t c_integer,
- uint32_t ul_decimal)
- {
- uint8_t uc_thyst[2] = { 0 };
- lm75_temperature_to_hex_int(c_integer, &uc_thyst[0]);
- lm75_temperature_to_hex_dec(ul_decimal, &uc_thyst[1]);
- 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[2] = { 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[0], p_integer);
- lm75_hex_to_temperature_dec(uc_thyst[1], 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);
- }
复制代码 |
|