Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

95 řádky
1.6 KiB

  1. //#include <proc/p32mx440f256h.h>
  2. #include "TC77.h"
  3. #include "BoardCfg.h"
  4. #include "timer.h"
  5. short mDeviceID;
  6. int TC77Configure()
  7. {
  8. TEMP_SENSOR_CS_PIN = 0;
  9. //Execute a 16 bits read first.
  10. SPI3BUF = 0xFF;
  11. while(!SPI3STATbits.SPIRBF);
  12. SPI3BUF = 0xFF;
  13. while(!SPI3STATbits.SPIRBF);
  14. //Now, write 0xFFFF to config register.
  15. SPI3BUF = 0xFF;
  16. while(!SPI3STATbits.SPIRBF);
  17. SPI3BUF = 0xFF;
  18. while(!SPI3STATbits.SPIRBF);
  19. //Read Device ID
  20. SPI3BUF = 0x00;
  21. while(!SPI3STATbits.SPIRBF);
  22. mDeviceID = SPI3BUF;
  23. mDeviceID <<= 8;
  24. SPI3BUF = 0xFF;
  25. while(!SPI3STATbits.SPIRBF);
  26. mDeviceID += SPI3BUF;
  27. TEMP_SENSOR_CS_PIN = 1;
  28. mDeviceID &= 0xFFFC;
  29. if(mDeviceID != 0x5400)
  30. {
  31. return RET_ERROR;
  32. }
  33. Sleep(100);
  34. //Device detected. Now, put the device in continuous read mode.
  35. TEMP_SENSOR_CS_PIN = 0;
  36. //Execute a 16 bits read first.
  37. SPI3BUF = 0xFF;
  38. while(!SPI3STATbits.SPIRBF);
  39. SPI3BUF = 0xFF;
  40. while(!SPI3STATbits.SPIRBF);
  41. //Now, write 0x0000 to config register.
  42. SPI3BUF = 0x00;
  43. while(!SPI3STATbits.SPIRBF);
  44. SPI3BUF = 0x00;
  45. while(!SPI3STATbits.SPIRBF);
  46. TEMP_SENSOR_CS_PIN = 1;
  47. return RET_OK;
  48. }
  49. float TC77GetActualTemp()
  50. {
  51. short RawTemp = 0;
  52. float Temp;
  53. TEMP_SENSOR_CS_PIN = 0;
  54. //Read 16 bits.
  55. SPI3BUF = 0x00;
  56. while(!SPI3STATbits.SPIRBF);
  57. RawTemp = SPI3BUF;
  58. RawTemp <<= 8;
  59. SPI3BUF = 0xFF;
  60. while(!SPI3STATbits.SPIRBF);
  61. RawTemp += SPI3BUF;
  62. TEMP_SENSOR_CS_PIN = 1;
  63. RawTemp &= 0xFFF8; //Get rid of useless 3 LSB
  64. //RawTemp >>= 3;
  65. Temp = ((float)0.0625 * (float)RawTemp);
  66. Temp /= 8;
  67. return Temp;
  68. }
  69. short TC77GetDeviceID()
  70. {
  71. return mDeviceID;
  72. }