stm32 mcp3421 代码
时间: 2023-09-09 17:14:09 浏览: 311
以下是基于STM32的MCP3421驱动代码,需要先配置I2C通信:
```c
#include "stm32f10x.h"
#define MCP3421_ADDRESS (0x68 << 1) // MCP3421 I2C地址
/* MCP3421配置寄存器 */
#define MCP3421_CONFIG_RDY (1 << 7)
#define MCP3421_CONFIG_C1 (1 << 6)
#define MCP3421_CONFIG_C0 (1 << 5)
#define MCP3421_CONFIG_OC (1 << 4)
#define MCP3421_CONFIG_S1 (1 << 3)
#define MCP3421_CONFIG_S0 (1 << 2)
#define MCP3421_CONFIG_G1 (1 << 1)
#define MCP3421_CONFIG_G0 (1 << 0)
/* MCP3421采样率配置 */
#define MCP3421_SAMPLE_RATE_240 0
#define MCP3421_SAMPLE_RATE_60 MCP3421_CONFIG_S0
#define MCP3421_SAMPLE_RATE_15 MCP3421_CONFIG_S1
#define MCP3421_SAMPLE_RATE_3_75 (MCP3421_CONFIG_S0 | MCP3421_CONFIG_S1)
/* MCP3421增益配置 */
#define MCP3421_GAIN_1 0
#define MCP3421_GAIN_2 MCP3421_CONFIG_G0
#define MCP3421_GAIN_4 MCP3421_CONFIG_G1
#define MCP3421_GAIN_8 (MCP3421_CONFIG_G0 | MCP3421_CONFIG_G1)
/* MCP3421分辨率配置 */
#define MCP3421_RESOLUTION_12BIT 0
#define MCP3421_RESOLUTION_14BIT MCP3421_CONFIG_C0
#define MCP3421_RESOLUTION_16BIT MCP3421_CONFIG_C1
#define MCP3421_RESOLUTION_18BIT (MCP3421_CONFIG_C0 | MCP3421_CONFIG_C1)
/* MCP3421采样时间 */
#define MCP3421_CONVERSION_TIME_240 13
#define MCP3421_CONVERSION_TIME_60 67
#define MCP3421_CONVERSION_TIME_15 267
#define MCP3421_CONVERSION_TIME_3_75 1047
/* MCP3421读取数据的寄存器 */
#define MCP3421_DATA_REG 0x00
/* MCP3421配置寄存器 */
#define MCP3421_CONFIG_REG 0x01
/* MCP3421状态寄存器 */
#define MCP3421_STATUS_REG 0x02
/* I2C初始化函数 */
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* 使能GPIOB和I2C1时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* 配置GPIOB的I2C1引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 配置I2C1 */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
/* 使能I2C1 */
I2C_Cmd(I2C1, ENABLE);
}
/* MCP3421初始化函数 */
void MCP3421_Init(uint8_t sample_rate, uint8_t gain, uint8_t resolution)
{
uint8_t config = MCP3421_CONFIG_RDY; // 使能RDY位
/* 配置采样率 */
config |= sample_rate;
/* 配置增益 */
config |= gain;
/* 配置分辨率 */
config |= resolution;
/* 发送配置数据到MCP3421 */
I2C_SendData(I2C1, MCP3421_CONFIG_REG);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) == RESET);
I2C_SendData(I2C1, config);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) == RESET);
/* 等待MCP3421完成配置 */
while ((I2C_ReadReg(I2C1, MCP3421_STATUS_REG) & MCP3421_CONFIG_RDY) == 0);
}
/* 读取MCP3421采样数据 */
uint16_t MCP3421_Read(void)
{
uint16_t value = 0;
/* 发送读取数据命令 */
I2C_SendData(I2C1, MCP3421_DATA_REG);
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) == RESET);
/* 等待MCP3421完成采样 */
switch (I2C_ReadReg(I2C1, MCP3421_CONFIG_REG) & (MCP3421_CONFIG_S1 | MCP3421_CONFIG_S0))
{
case MCP3421_SAMPLE_RATE_240:
delay_us(MCP3421_CONVERSION_TIME_240);
break;
case MCP3421_SAMPLE_RATE_60:
delay_us(MCP3421_CONVERSION_TIME_60);
break;
case MCP3421_SAMPLE_RATE_15:
delay_us(MCP3421_CONVERSION_TIME_15);
break;
case MCP3421_SAMPLE_RATE_3_75:
delay_us(MCP3421_CONVERSION_TIME_3_75);
break;
}
/* 读取2字节的16位采样数据 */
value = I2C_ReadReg(I2C1, MCP3421_DATA_REG);
value <<= 8;
value |= I2C_ReadReg(I2C1, MCP3421_DATA_REG);
return value;
}
```
使用方法如下:
```c
int main(void)
{
uint16_t value = 0;
I2C_Configuration(); // 初始化I2C
MCP3421_Init(MCP3421_SAMPLE_RATE_240, MCP3421_GAIN_1, MCP3421_RESOLUTION_12BIT); // 初始化MCP3421
while (1)
{
value = MCP3421_Read(); // 读取MCP3421采样数据
printf("%d\n", value); // 打印采样数据
}
}
```
阅读全文