main.c
- #include "bsp_mcp3421.h"
- int main()
- {
-     float mcp3421_dat = 0;
-     while(1)
-     {
-         MCP3421_Init(); // 初始化设置
-         MCP3421_Read(&mcp3421_dat); // 获取值
-     }
- }
bsp_mcp3421.h
- #ifndef _BSP_MCP3421_H
- #define _BSP_MCP3421_H
- #include "stm32f10x.h"
- #define        SLAVE_ADDRESS    0xd0    // 设备地址
- /* 
-     寄存器配置
- */
- #define        CONTINUOUS_TRANSITION      0x10    // 启动连续转换
- #define        SINGLE_TRANSITION           0x00    // 启动单次转换
- #define        READY_FLAG                  0x80    // 就绪标志位
- // 采样率选择位
- #define        SAMPLE_RATE_SELECT_12bit    0x00    // 12位 240sps
- #define        SAMPLE_RATE_SELECT_14bit    0x04    // 14位 60sps
- #define        SAMPLE_RATE_SELECT_16bit    0x08    // 16位 15sps
- #define        SAMPLE_RATE_SELECT_18bit    0x0c    // 18位 3.75sps
- // PGA 增益选择位
- #define        PGA_SELECT_Bit_1V    0x00    // 1 V/V
- #define        PGA_SELECT_Bit_2V    0x01    // 2 V/V
- #define        PGA_SELECT_Bit_4V    0x02     // 4 V/V
- #define        PGA_SELECT_Bit_8V    0x03    // 8 V/V
- void MCP3421_Single_Transition();
- char MCP3421_Read_Data(uint8_t *buf, uint8_t sum);
- int
-  MCP3421_Read(float *dat);
- void MCP3421_Init();
- #endif //_BSP_MCP3421_H
bsp_mcp3421.c
- #include "bsp_mcp3421.h"
- #include "bsp_i2c.h"
- #include "bsp_tim2.h"
- #include "bsp_tim3.h"
- #include "stdio.h"
- #include "bsp_DMAusart2.h"
- /******************************************************
-  *MCP_3421模数转换传感器
-  *
-  *
-  *
-  *输出代码 = (指定采样速率下的最大代码+1)*PGA * (Vin+ - Vin-)/LSB
-  *LSB = 2*VREF/2^SPS=2*2.048V / 2^SPS
-  *
-  *当MSB=0(输入正电压)
-  *    输入电压=(输出代码)* LSB /PGA
-  *
-  *当MSB=1(输入负电压)
-  *    输入电压=(输出代码的二进制补码) * LSB /PGA
-  *
- *********************************************************/
- // 读数据
- char MCP3421_Read_Data(uint8_t *buf, uint8_t sum)
- {
-     if(buf == NULL || sum == 0)
-     {
-         return -1;
-     }
-     int i = 0;
-     i2c_Start();
-     i2c_SendByte(SLAVE_ADDRESS + 1);
-     i2c_WaitAck();
-     //Delay_ms(100);
-     for( ; i<(sum-1); i++)
-     {
-         *buf = i2c_ReadByte(1);
-         buf++;
-     }
-     *buf = i2c_ReadByte(0);
-     i2c_WaitAck();
-     i2c_Stop();
-     return 0;
- }
- // 18位模式下读取数据
- int MCP3421_Read(float *dat)
- {
-     if(dat == NULL)
-     {
-         return -1;
-     }
-     float ADC = 0;
-     float LSB = (float)((2.0*2.048)/65536.0); //32768.0
-     uint8_t PGA = 1;
-     uint8_t bufs[3] = {0};
-     int AD_B_Result = 0;
-     MCP3421_Read_Data(bufs, 3);
-     AD_B_Result = (int)((bufs[0] << 8) | bufs[1]);
-     ADC = (float)((LSB * AD_B_Result)/PGA);
-     //*dat = 6.2307*ADC-7.4601;
-     *dat = 6.2307*ADC-7.4600; // 将获取的电压信号转转压力值
-     /* MCP3421有正负输入电压
-      *这里只做正电压判断
-      */
- }
- // 写配置
- void MCP3421_Write_Data(uint8_t dat)
- {
-     i2c_Start();
-     i2c_SendByte(SLAVE_ADDRESS);
-     i2c_WaitAck();
-     i2c_SendByte(dat);
-     i2c_WaitAck();
-     i2c_Stop();
- }
- // 单次转换
- void MCP3421_Single_Transition()
- {
-     MCP3421_Write_Data(SINGLE_TRANSITION     // 启动单次转换
-                     | SAMPLE_RATE_SELECT_16bit // 16位
-                     | PGA_SELECT_Bit_1V);   // PGA 增益选择位
- //    MCP3421_Write_Data(0x0b);
- }
- // 连续转换
- void MCP3421_Continuous_Transition()
- {
-     MCP3421_Write_Data(CONTINUOUS_TRANSITION  // 启动连续转换
-                     | SAMPLE_RATE_SELECT_16bit // 16位 
-                     | PGA_SELECT_Bit_1V);     // PGA 增益选择位
- //    MCP3421_Write_Data(0x8b);
- }
- // 寄存器初始化
- void MCP3421_Config()
- {
-     MCP3421_Continuous_Transition();
-     //MCP3421_Single_Transition();
- }
- // 复位
- void MCP3421_Reset()
- {
-     i2c_Start();
-     i2c_SendByte(SLAVE_ADDRESS);
-     i2c_WaitAck();
- }
- // 初始化
- void MCP3421_Init()
- {
-     MCP3421_Config();
- }