大家好,欢迎来到IT知识分享网。
1、MCU使用STM32104RCT6,使用SPI2外设,ADS1220只支持SPI模式1
2、生成的SPI初始化代码
下面是ADS1220的源文件和头文件,改配置是通道0和通道1组成差分输入模式,通道3和通道4采用单端输入模式,ADC基准电压配置为采用模拟供电电压作为基准源(3.3V),除了转换速率其余都未默认配置,具体寄存器内容请自行查看相关文档。
#ifndef INC_ADS1220_H_ #define INC_ADS1220_H_ #include "main.h" #include "spi.h" #include "cmsis_os.h" #include <stdint.h> #include <stdio.h> #define ADS1220_REG_CONFIG0 0x00 #define ADS1220_REG_CONFIG1 0x01 #define ADS1220_REG_CONFIG2 0x02 #define ADS1220_REG_CONFIG3 0x03 #define ADS1220_CMD_RESET 0x06 #define ADS1220_CMD_START_SYNC 0x08 #define ADS1220_CMD_POWERDOWN 0x02 #define ADS1220_CMD_RDATA 0x10 #define ADS1220_CONFIG0_VAL 0x81 #define ADS1220_CONFIG1_VAL 0x20 #define ADS1220_CONFIG2_VAL 0xC0 #define ADS1220_CONFIG3_VAL 0x00 #define ADS1220_CS(x) HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, (x) == 0 ? GPIO_PIN_RESET : GPIO_PIN_SET) extern void ads1220_test(void); extern void ADS1220_Init(void); extern void get_device_voltage(samp_val *pdat); #endif /* INC_ADS1220_H_ */
#include "ads1220.h" const uint8_t VOLTAGE_1 = 0; const uint8_t VOLTAGE_2 = 2; void SPI_Init(void) { ADS1220_CS(1); } void SPI_Transmit(uint8_t *txData, uint16_t length) { ADS1220_CS(0); if (HAL_SPI_Transmit(&hspi2, txData, length, 10) != HAL_OK) { error_printf("%s %d %s\n", __FILE__, __LINE__, __func__); ADS1220_CS(1); return; } ADS1220_CS(1); } void SPI_TransmitReceive(uint8_t *txData, uint8_t *rxData, uint16_t length) { ADS1220_CS(0); if (HAL_SPI_TransmitReceive(&hspi2, txData, rxData, length, 10) != HAL_OK) { error_printf("%s %d %s\n", __FILE__, __LINE__, __func__); ADS1220_CS(1); return; } ADS1220_CS(1); } void ADS1220_WriteRegister(uint8_t reg, uint8_t value) { uint8_t tx_buf[2] = { (0x40 | (reg << 2)), value }; SPI_Transmit(tx_buf, 2); } void ADS1220_Reset(void) { SPI_Transmit((uint8_t[] ) { ADS1220_CMD_RESET }, 1); HAL_Delay(1); } void ADS1220_StartConversion() { SPI_Transmit((uint8_t[] ) { ADS1220_CMD_START_SYNC }, 1); } uint8_t ADS1220_ReadRegister(uint8_t reg) { uint8_t tx_buf[2] = { (0x20 | (reg << 2)), 0x00 }; uint8_t rx_buf[2] = { 0 }; SPI_TransmitReceive(tx_buf, rx_buf, 2); return rx_buf[1]; } void configure_channel(uint8_t channel) { static const uint8_t config0_vals[4] = { 0x01, 0x91, 0xA1, 0xB1 }; if (channel > 3) return; ADS1220_WriteRegister(ADS1220_REG_CONFIG0, config0_vals[channel]); ADS1220_WriteRegister(ADS1220_REG_CONFIG1, ADS1220_CONFIG1_VAL); ADS1220_WriteRegister(ADS1220_REG_CONFIG2, ADS1220_CONFIG2_VAL); ADS1220_WriteRegister(ADS1220_REG_CONFIG3, ADS1220_CONFIG3_VAL); } int32_t ADS1220_ReadData(void) { uint8_t rx_buf[3] = { 0 }; SPI_TransmitReceive((uint8_t[] ) { ADS1220_CMD_RDATA }, rx_buf, 3); int32_t data = (rx_buf[0] << 16) | (rx_buf[1] << 8) | rx_buf[2]; if (data & 0x) { data |= 0xFF000000; } return data; } int32_t read_channel(uint8_t channel) { configure_channel(channel); ADS1220_StartConversion(); osDelay(300); return ADS1220_ReadData(); } void sample_all_channels(int32_t *data) { for (uint8_t channel = 0; channel < 4; ++channel) { data[channel] = read_channel(channel); } } #define V_REF 3.3f #define GAIN 1.0f void ADS1220_ReadAllChannels(void) { int32_t data[4]; float voltage[4]; sample_all_channels(data); for (uint8_t i = 0; i < 4; ++i) { voltage[i] = (float) data[i] / (float) (1 << 23) * (V_REF / GAIN); info_printf("Channel %d: %d, Voltage: %.5f V\n", i, data[i], voltage[i]); } info_printf("\n"); } float get_voltage(const uint8_t ch, char *str) { float val = 0; int32_t dat = read_channel(ch); val = (float) dat / (float) (1 << 23) * (V_REF / GAIN); info_printf("Channel %d: %d, %s voltage: %.5f V\n", ch, dat, str, val); return val; } void get_device_voltage(samp_val *pdat) { get_voltage(VOLTAGE_1, "VOLTAGE_1"); get_voltage(VOLTAGE_2, "VOLTAGE_2"); printf("\n"); } void ADS1220_Init(void) { SPI_Init(); ADS1220_Reset(); ADS1220_WriteRegister(ADS1220_REG_CONFIG1, ADS1220_CONFIG1_VAL); ADS1220_WriteRegister(ADS1220_REG_CONFIG2, ADS1220_CONFIG2_VAL); ADS1220_WriteRegister(ADS1220_REG_CONFIG3, ADS1220_CONFIG3_VAL); }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/116749.html

