main.c

  1. #include "bsp_mcp3421.h"
  2. int main()
  3. {
  4. float mcp3421_dat = 0;
  5. while(1)
  6. {
  7. MCP3421_Init(); // 初始化设置
  8. MCP3421_Read(&mcp3421_dat); // 获取值
  9. }
  10. }

bsp_mcp3421.h

  1. #ifndef _BSP_MCP3421_H
  2. #define _BSP_MCP3421_H
  3. #include "stm32f10x.h"
  4. #define SLAVE_ADDRESS 0xd0 // 设备地址
  5. /*
  6. 寄存器配置
  7. */
  8. #define CONTINUOUS_TRANSITION 0x10 // 启动连续转换
  9. #define SINGLE_TRANSITION 0x00 // 启动单次转换
  10. #define READY_FLAG 0x80 // 就绪标志位
  11. // 采样率选择位
  12. #define SAMPLE_RATE_SELECT_12bit 0x00 // 12位 240sps
  13. #define SAMPLE_RATE_SELECT_14bit 0x04 // 14位 60sps
  14. #define SAMPLE_RATE_SELECT_16bit 0x08 // 16位 15sps
  15. #define SAMPLE_RATE_SELECT_18bit 0x0c // 18位 3.75sps
  16. // PGA 增益选择位
  17. #define PGA_SELECT_Bit_1V 0x00 // 1 V/V
  18. #define PGA_SELECT_Bit_2V 0x01 // 2 V/V
  19. #define PGA_SELECT_Bit_4V 0x02 // 4 V/V
  20. #define PGA_SELECT_Bit_8V 0x03 // 8 V/V
  21. void MCP3421_Single_Transition();
  22. char MCP3421_Read_Data(uint8_t *buf, uint8_t sum);
  23. int
  24. MCP3421_Read(float *dat);
  25. void MCP3421_Init();
  26. #endif //_BSP_MCP3421_H

bsp_mcp3421.c

  1. #include "bsp_mcp3421.h"
  2. #include "bsp_i2c.h"
  3. #include "bsp_tim2.h"
  4. #include "bsp_tim3.h"
  5. #include "stdio.h"
  6. #include "bsp_DMAusart2.h"
  7. /******************************************************
  8. *MCP_3421模数转换传感器
  9. *
  10. *
  11. *
  12. *输出代码 = (指定采样速率下的最大代码+1)*PGA * (Vin+ - Vin-)/LSB
  13. *LSB = 2*VREF/2^SPS=2*2.048V / 2^SPS
  14. *
  15. *当MSB=0(输入正电压)
  16. * 输入电压=(输出代码)* LSB /PGA
  17. *
  18. *当MSB=1(输入负电压)
  19. * 输入电压=(输出代码的二进制补码) * LSB /PGA
  20. *
  21. *********************************************************/
  22. // 读数据
  23. char MCP3421_Read_Data(uint8_t *buf, uint8_t sum)
  24. {
  25. if(buf == NULL || sum == 0)
  26. {
  27. return -1;
  28. }
  29. int i = 0;
  30. i2c_Start();
  31. i2c_SendByte(SLAVE_ADDRESS + 1);
  32. i2c_WaitAck();
  33. //Delay_ms(100);
  34. for( ; i<(sum-1); i++)
  35. {
  36. *buf = i2c_ReadByte(1);
  37. buf++;
  38. }
  39. *buf = i2c_ReadByte(0);
  40. i2c_WaitAck();
  41. i2c_Stop();
  42. return 0;
  43. }
  44. // 18位模式下读取数据
  45. int MCP3421_Read(float *dat)
  46. {
  47. if(dat == NULL)
  48. {
  49. return -1;
  50. }
  51. float ADC = 0;
  52. float LSB = (float)((2.0*2.048)/65536.0); //32768.0
  53. uint8_t PGA = 1;
  54. uint8_t bufs[3] = {0};
  55. int AD_B_Result = 0;
  56. MCP3421_Read_Data(bufs, 3);
  57. AD_B_Result = (int)((bufs[0] << 8) | bufs[1]);
  58. ADC = (float)((LSB * AD_B_Result)/PGA);
  59. //*dat = 6.2307*ADC-7.4601;
  60. *dat = 6.2307*ADC-7.4600; // 将获取的电压信号转转压力值
  61. /* MCP3421有正负输入电压
  62. *这里只做正电压判断
  63. */
  64. }
  65. // 写配置
  66. void MCP3421_Write_Data(uint8_t dat)
  67. {
  68. i2c_Start();
  69. i2c_SendByte(SLAVE_ADDRESS);
  70. i2c_WaitAck();
  71. i2c_SendByte(dat);
  72. i2c_WaitAck();
  73. i2c_Stop();
  74. }
  75. // 单次转换
  76. void MCP3421_Single_Transition()
  77. {
  78. MCP3421_Write_Data(SINGLE_TRANSITION // 启动单次转换
  79. | SAMPLE_RATE_SELECT_16bit // 16位
  80. | PGA_SELECT_Bit_1V); // PGA 增益选择位
  81. // MCP3421_Write_Data(0x0b);
  82. }
  83. // 连续转换
  84. void MCP3421_Continuous_Transition()
  85. {
  86. MCP3421_Write_Data(CONTINUOUS_TRANSITION // 启动连续转换
  87. | SAMPLE_RATE_SELECT_16bit // 16位
  88. | PGA_SELECT_Bit_1V); // PGA 增益选择位
  89. // MCP3421_Write_Data(0x8b);
  90. }
  91. // 寄存器初始化
  92. void MCP3421_Config()
  93. {
  94. MCP3421_Continuous_Transition();
  95. //MCP3421_Single_Transition();
  96. }
  97. // 复位
  98. void MCP3421_Reset()
  99. {
  100. i2c_Start();
  101. i2c_SendByte(SLAVE_ADDRESS);
  102. i2c_WaitAck();
  103. }
  104. // 初始化
  105. void MCP3421_Init()
  106. {
  107. MCP3421_Config();
  108. }