Quellcode durchsuchen

Показывает температуру в HEX

Vladimir N. Shilov vor 7 Jahren
Ursprung
Commit
06a4de20b4
2 geänderte Dateien mit 15 neuen und 11 gelöschten Zeilen
  1. 14 10
      main.c
  2. 1 1
      main.h

+ 14 - 10
main.c

@@ -71,11 +71,7 @@ __C_task void main(void)
     if (1 == Flag.newTemp) {
       Flag.newTemp = 0;
 
-      if (Temperature < 0) {
-        Display[0] = Sym_minus;
-      } else {
-        Display[0] = Sym_blank;
-      }
+      Display[0] = Sym_blank;
       Display[1] = IndctrNums[(0x0F & (Temperature>>4))];
       Display[2] = IndctrNums[(0x0F & Temperature)];
       Display[3] = Sym_gradus;
@@ -296,7 +292,6 @@ static void ds18b20_StartMeasure(void) {
 
 static void ds18b20_ReadTemperature(void) {
   uint8_t temperature[2];
-  int8_t digit;
 
   /* Reset, skip ROM and send command to read Scratchpad */
   if (ds18b20_Reset() != 0) {
@@ -312,13 +307,22 @@ static void ds18b20_ReadTemperature(void) {
     //ds18b20_reset();
 
     /* Store temperature integer digits */
-    digit=temperature[0]>>4;
-    digit|=(temperature[1]&0x7)<<4;
+    Temperature = ((temperature[1]&0x7)<<4) | (temperature[0]>>4);
 
     /* Get only integer part of temperature */
-    Temperature = digit / 16;
+//    Temperature = (int8_t)digit;
+
+    //выделяем с помощью битовой маски дробную часть
+    tempDecimals = (temperature[0] & 15);
+    
+    //преобразуем в целое число
+    tempDecimals *= 10; //(tempDecimals << 1) + (tempDecimals << 3);// Умножаем на 10
+    tempDecimals >>= 4; //(tempDecimals >> 4);//делим на 16 или умножаем на 0.0625
+    
+    if (tempDecimals > 4) {
+      Temperature ++;
+    }
   }
-
   Flag.newTemp = 1;
 }
 

+ 1 - 1
main.h

@@ -113,7 +113,7 @@ const indctr_sym_t IndctrNums[16] = {
 #define DS18B20_HIGH            DS18B20_PORT|=(1<<DS18B20_PAD)
 #define DS18B20_VALUE           (DS18B20_PIN & (1<<DS18B20_PAD))
 
-static int8_t Temperature;
+static int8_t Temperature, tempDecimals;
 
 static uint8_t ds18b20_Reset(void);
 static void ds18b20_WriteBit(uint8_t bit);