max6675.c 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "global.h"
  2. /**
  3. * 读MAX6675原始16位数据
  4. *
  5. * @author yyq (2024/9/11/周三)
  6. *
  7. * @param port spi设备号 0、1、2
  8. *
  9. * @return value 原始16位数据
  10. */
  11. static unsigned short max6675_ReadReg(int port)
  12. {
  13. unsigned short value;
  14. if(port < HW_SPI_DEVICE_NUMBER)
  15. {
  16. hw_spi_device_take(port);
  17. hw_spi_readwrite(port, 0, (unsigned char*)&value, 2);
  18. hw_spi_device_release(port);
  19. return value;
  20. }
  21. return 0xffff;
  22. }
  23. /**
  24. * 读MAX6675温度值
  25. *
  26. * @author yyq (2024/9/11/周三)
  27. *
  28. * @param port spi设备号 0、1、2
  29. * @param value 读到的温度值
  30. *
  31. * @return int 数据有效性:1有效 0无效
  32. */
  33. int max6675_ReadTemp(int port,float* value)
  34. {
  35. unsigned short temp;
  36. int data_valid;
  37. temp = max6675_ReadReg(port);
  38. temp <<= 1;
  39. if(temp&0x08)
  40. {
  41. data_valid=0;
  42. }
  43. else
  44. {
  45. temp >>= 4;
  46. *value = (temp & 0x0fff)*0.25;
  47. data_valid=1;
  48. }
  49. return data_valid;
  50. }