当前位置:   article > 正文

stm32cubemx使用硬件spi和外部中断来驱动MAX31685来获取PT100测量到的温度

max31685

目录

1.cubemx外设选择驱动

1.单片机时钟设置

 2.硬件spi设置

3.外部中断使能

4.NVIC的设置

2.Max31685如何接线

 3.具体的代码实现


1.cubemx外设选择驱动

1.单片机时钟设置

        根据自己的硬件晶振来设置,我的单片机是高速外部晶振和低速外部晶振。

 时钟设置

 2.硬件spi设置

         没用使用硬件的片选信号引脚,我们这里是使用任意一个引脚来作为硬件SPI的片选引脚。(具体的看代码实现)

3.外部中断使能

       

         外部中断引脚设置为PA3,下降沿触发。硬件SPI的片选引脚为PA4。

外部中断:外部中断是指由CPU之外的外部设备或信号产生的中断请求,如键盘输入、鼠标移动、定时器到期、硬件故障等,它们会打断正在执行的程序或操作系统内核的运行,引起CPU的注意,进而进行相应的中断处理。外部中断可以使系统实现实时响应和事件处理,提高了系统的可靠性和稳定性。

4.NVIC的设置

2.Max31685如何接线

Max31685三线接法

3.具体的代码实现

  1. #include "PT100.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. /* MAX31865参考电阻 */
  5. #define RREF 430 //430Ω
  6. void MAX31865_Init()
  7. {
  8. //MAX31865_SB_Write(0x80,0xC1);//二线、四线配置
  9. MAX31865_SB_Write(0x80,0xD1);//三线配置
  10. HAL_Delay(10);
  11. }
  12. float Get_tempture(void)//PT100
  13. {
  14. unsigned int data;
  15. float Rt;
  16. float Rt0 = 100; //PT100
  17. float Z1,Z2,Z3,Z4,temp;
  18. float a = 3.9083e-3;
  19. float b = -5.775e-7;
  20. float rpoly;
  21. // MAX31865_Write(0x80, 0xD3);
  22. data=MAX31865_SB_Read(0x01)<<8;
  23. data|=MAX31865_SB_Read(0x02);
  24. data>>=1; //去掉Fault位
  25. //printf("Read=0x%02X\r\n",data);
  26. Rt=(float)data/32768.0f*RREF;
  27. //printf("Rt=0x%.1f\r\n",Rt);
  28. /*************根据数据手册编写**********/
  29. Z1 = -a;
  30. Z2 = a*a-4*b;
  31. Z3 = 4*b/Rt0;
  32. Z4 = 2*b;
  33. temp = Z2+Z3*Rt;
  34. temp = (sqrt(temp)+Z1)/Z4;
  35. if(temp>=0) return temp;
  36. rpoly = Rt;
  37. temp = -242.02;
  38. temp += 2.2228 * rpoly;
  39. rpoly *= Rt; // square
  40. temp += 2.5859e-3 * rpoly;
  41. rpoly *= Rt; // ^3
  42. temp -= 4.8260e-6 * rpoly;
  43. rpoly *= Rt; // ^4
  44. temp -= 2.8183e-8 * rpoly;
  45. rpoly *= Rt; // ^5
  46. temp += 1.5243e-10 * rpoly;
  47. return temp;
  48. }
  49. uint8_t MAX31865_SB_Read(uint8_t addr)//SPI Single-Byte Read
  50. {
  51. uint8_t read;
  52. MAX31685_CS_LOW();
  53. HAL_SPI_Transmit(&hspi1, &addr, 1, 60);
  54. HAL_SPI_Receive(&hspi1, &read, 1, 60);
  55. MAX31685_CS_HIGH();
  56. return read;
  57. }
  58. void MAX31865_SB_Write(uint8_t addr,uint8_t wdata)//SPI Single-Byte Write
  59. {
  60. uint8_t dat[2];
  61. dat[0] = addr;
  62. dat[1] = wdata;
  63. MAX31685_CS_LOW();
  64. HAL_SPI_Transmit(&hspi1,dat,2,60);
  65. MAX31685_CS_HIGH();
  66. }
  1. #ifndef __PT100_H__
  2. #define __PT100_H__
  3. #include "spi.h"
  4. #include "gpio.h"
  5. //#define MAX31685_RDY() HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) //iso module is not connect
  6. #define MAX31685_CS_HIGH() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET)
  7. #define MAX31685_CS_LOW() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET)
  8. void MAX31865_Init(void);
  9. uint8_t MAX31865_SB_Read(uint8_t addr);
  10. void MAX31865_SB_Write(uint8_t addr,uint8_t wdata);
  11. float Get_tempture(void);
  12. #endif
  1. /*********主函数可以添加*********/
  2. if(s_ucTemperFlag == 1)
  3. {
  4. s_ucTemperFlag = 0; //温度标准位置零
  5. s_fNowTemper = Get_tempture();
  6. snprintf(Temperabuf,16,"T:%.3f C",s_fNowTemper);
  7. OLED_ShowString(2,1,Temperabuf); //显示温度
  8. }
  9. /***************/
  10. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  11. {
  12. if(GPIO_Pin == GPIO_PIN_3)
  13. {
  14. if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_3) == GPIO_PIN_RESET)
  15. {
  16. if(s_ucTemperFlag == 0)
  17. s_ucTemperFlag = 1; //温度转换标准位
  18. }
  19. }
  20. }

使用外部中断来接收温度转换完成标准。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/219972
推荐阅读
相关标签
  

闽ICP备14008679号