oneWire.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "stm32f1xx_hal.h"
  2. #include "oneWireDriver.h"
  3. #include<stdio.h>
  4. uint8_t check = 2, temp_l = 0, temp_h = 0;
  5. int16_t temp = 0;
  6. float temperature;
  7. int tempInt;
  8. void testOneWireTimingFunc() {
  9. HAL_Delay(100);
  10. gpio_set_input(); // set as output
  11. while(1) {
  12. writeByte(0xAA);
  13. us_delay(100);
  14. /*writeBitOn();
  15. us_delay(40);
  16. gpio_set_input(); // set as output
  17. us_delay(10);
  18. gpio_set_output(); // set as output
  19. writeBitOff();
  20. us_delay(20);
  21. */
  22. }
  23. }
  24. void initReadTemp()
  25. {
  26. uint32_t noOfSlots = 0;
  27. oneWire_init();
  28. writeByte(0xCC); // skip ROM
  29. writeByte(0x44); // convert t
  30. while( readSlot() == 0 ) {
  31. noOfSlots++;
  32. us_delay(10);
  33. }
  34. printf("Sl:%u\n",noOfSlots);
  35. }
  36. float readTemperature()
  37. {
  38. uint8_t retVal = oneWire_init();
  39. writeByte(0xCC); // skip ROM
  40. writeByte(0xBE); // Read Scratchpad
  41. temp_l = readByte();
  42. temp_h = readByte();
  43. uint8_t b3 = readByte();
  44. uint8_t b4 = readByte();
  45. uint8_t b5 = readByte();
  46. uint8_t b6 = readByte();
  47. uint8_t b7 = readByte();
  48. uint8_t b8 = readByte();
  49. temp = (int16_t)((temp_h << 8) | temp_l);
  50. temperature = (float)temp / 16;
  51. printf("retVal=%u %u %u %u %u %u ",retVal,temp_l,temp_h,b3,b4,b5);
  52. printf(" %u %u %u\n",b6,b7,b8);
  53. return temperature;
  54. }