temperatur.cpp 911 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * temperatur.cpp
  3. *
  4. * Created on: Feb 23, 2018
  5. * Author: sebastian
  6. */
  7. #include <wiringPi.h>
  8. //#include <wiringPiSPI.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <inttypes.h>
  13. #include <unistd.h>
  14. #include "global.h"
  15. #include "logger.h"
  16. #include "temperatur.h"
  17. #include "spi.h"
  18. uint8_t rawData[4] = {0,0,0,0};
  19. void temperatur_init(){
  20. pinMode(TEMP_CS, OUTPUT);
  21. digitalWrite(TEMP_CS, HIGH);
  22. }
  23. int16_t get_temperatur(){
  24. //active low
  25. digitalWrite(TEMP_CS, LOW);
  26. //do one read, Temperatur will only be reevaluated on CS High
  27. for(int i = 0; i < 4; i++){
  28. rawData[i] = spi_readwrite(0);
  29. }
  30. digitalWrite(TEMP_CS, HIGH);
  31. //extracting the temperature data without the lowest two bit
  32. int16_t temp = (((uint16_t)rawData[0]) << 4) | (((uint16_t)rawData[1] >> 4) & 0x0F);
  33. logger(V_BASIC, "%x, %x, %x, %x\n", rawData[0], rawData[1], rawData[2], rawData[3]);
  34. return temp;
  35. }