Last active
July 17, 2024 14:00
-
-
Save battlecoder/29fea31fda3042554fc62c2b959ced06 to your computer and use it in GitHub Desktop.
MAX32650 ADT7420 Temperature sensor demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* Quick test of the ADT7420 temperature PMOD eval board | |
******************************************************************************/ | |
/***** Includes *****/ | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include "i2c.h" | |
#include "mxc_device.h" | |
#include "mxc_delay.h" | |
/***** Definitions *****/ | |
#define I2C_PORT MXC_I2C1 ///< I2C instance | |
#define I2C_FREQ 100000 ///< I2C clock frequency | |
#define TEMP_DEV_ADDR 0x48 | |
#define TEMP_REG_MSB 0x00 | |
#define TEMP_REG_LSB 0x01 | |
#define TEMP_REG_CONFIG 0x03 | |
// ***************************************************************************** | |
void i2c_clear(){ | |
MXC_I2C_ClearRXFIFO(I2C_PORT); | |
MXC_I2C_ClearTXFIFO(I2C_PORT); | |
} | |
int read_reg(uint8_t addr, uint8_t *recv_buffer, uint8_t len) { | |
uint8_t write_data = addr; | |
mxc_i2c_req_t reqMaster; | |
i2c_clear(); | |
reqMaster.i2c = I2C_PORT; | |
reqMaster.addr = TEMP_DEV_ADDR; | |
reqMaster.tx_buf = &write_data; | |
reqMaster.tx_len = 1; | |
reqMaster.rx_buf = recv_buffer; | |
reqMaster.rx_len = len; | |
reqMaster.restart = 0; | |
reqMaster.callback = NULL; | |
int error = MXC_I2C_MasterTransaction(&reqMaster); | |
if (error != E_NO_ERROR) printf("Couldn't read from the bus!\n"); | |
return error; | |
} | |
int write_byte_reg(uint8_t addr, uint8_t data){ | |
uint8_t write_data[2]; | |
mxc_i2c_req_t reqMaster; | |
write_data[0] = addr; | |
write_data[1] = data; | |
i2c_clear(); | |
reqMaster.i2c = I2C_PORT; | |
reqMaster.addr = TEMP_DEV_ADDR; | |
reqMaster.tx_buf = write_data; | |
reqMaster.tx_len = 2; | |
reqMaster.rx_buf = NULL; | |
reqMaster.rx_len = 0; | |
reqMaster.restart = 0; | |
reqMaster.callback = NULL; | |
int error = MXC_I2C_MasterTransaction(&reqMaster); | |
if (error != E_NO_ERROR) printf("Couldn't write to the bus!\n"); | |
return error; | |
} | |
void init_temp_sensor(){ | |
uint8_t rxbuff; | |
// Set data format to 16 bits format | |
printf("Configuring ADT7420 for 16 bit conversions\n"); | |
read_reg(TEMP_REG_CONFIG, &rxbuff, 1); | |
printf("Config before: %02x\n", rxbuff); | |
// Set 7th bit | |
rxbuff |= 0b10000000; | |
write_byte_reg(TEMP_REG_CONFIG, rxbuff); | |
read_reg(TEMP_REG_CONFIG, &rxbuff, 1); | |
printf("Config after : %02x\n", rxbuff); | |
} | |
uint16_t read_temperature_raw(){ | |
uint8_t tempbuffer[2]; | |
read_reg(TEMP_REG_MSB, tempbuffer, 2); | |
return (tempbuffer[0]<<8) | (tempbuffer[1]&0xff); | |
} | |
float read_temperature(){ | |
// We assume that we successfully enabled 16-bit mode | |
uint16_t tempRaw = read_temperature_raw(); | |
if (((tempRaw>>15) & 0x1) == 1){ | |
// Negative | |
return (long)(tempRaw - 65536)/128.0; | |
} | |
return (long)tempRaw / 128.0; | |
} | |
int main(void) { | |
int error = E_NO_ERROR; | |
float temperature; | |
error = MXC_I2C_Init(I2C_PORT, 1, 0); | |
if (error != E_NO_ERROR) { | |
printf("I2C master configure failed with error %i\n", error); | |
return error; | |
} | |
MXC_I2C_SetFrequency(I2C_PORT, I2C_FREQ); | |
init_temp_sensor(); | |
while (1) { | |
temperature = read_temperature(); | |
printf("Current temperature is %0.2f C\n", (double)temperature); | |
MXC_Delay(MXC_DELAY_SEC(1)); | |
} | |
return E_NO_ERROR; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment