Răsfoiți Sursa

I2C dont work.

Vladimir N. Shilov 1 an în urmă
părinte
comite
59e34bb0b9
5 a modificat fișierele cu 45 adăugiri și 40 ștergeri
  1. 18 19
      lib/htu21.c
  2. 15 15
      lib/i2c.c
  3. 1 1
      lib/i2c_master_poll.c
  4. 1 1
      lib/led.c
  5. 10 4
      src/main.c

+ 18 - 19
lib/htu21.c

@@ -1,6 +1,6 @@
 #include "stm8s.h"
 #include "htu21.h"
-//#include "i2c.h"
+#include "i2c.h"
 #include "i2c_master_poll.h"
 #include "delay.h"
 
@@ -9,21 +9,20 @@
 
 static uint8_t buf[3];
 static const uint8_t addr = 0x40;
-//static t_i2c_status i2cs;
-static uint8_t i2cs;
+static t_i2c_status i2cs = I2C_SUCCESS;
 static uint32_t val;
 
 static uint8_t CalcSht21Crc(uint8_t *data, uint8_t nbrOfBytes);
 
 htu_err_t htu_GetTemperature(int16_t * temperature) {
-  i2cs = 0;//i2c_wr_reg(addr, HTU_T_MEASURE);
   buf[0] = HTU_T_MEASURE;
+  //i2cs = i2c_wr_data(addr, 1, buf);
   I2C_WriteBytes(addr, 1, buf);
   if (i2cs == 0) {
     Delay(90);
-    //i2c_rd_reg(addr, buf);
+    //i2c_rd_data(addr, 3, buf);
     I2C_ReadBytes(addr, 3, buf);
-//    if(buf[2] == CalcSht21Crc(buf,2)) {
+    if(buf[2] == CalcSht21Crc(buf,2)) {
       val = buf[0];
       val <<= 8;
       val |= (buf[1] & 0xfc);
@@ -34,25 +33,25 @@ htu_err_t htu_GetTemperature(int16_t * temperature) {
       val /= 1638400; // 2^16/4*100
       *temperature = val - 4685;
       return HTU_OK;
-//    } else {
-//      *temperature = 1;
-//      return HTU_CRC_Temp;
-//    }
+    } else {
+      *temperature = 1110;
+      return HTU_CRC_Temp;
+    }
   } else {
-    *temperature = 777;
+    *temperature = 1111;
     return HTU_I2C;
   }
 }
 
 htu_err_t htu_GetHumidity(uint16_t * humidity) {
-  i2cs = 0;//i2c_wr_reg(addr, HTU_H_MEASURE);
   buf[0] = HTU_H_MEASURE;
+  //i2cs = i2c_wr_data(addr, 1, buf);
   I2C_WriteBytes(addr, 1, buf);
   if (i2cs == 0) {
     Delay(60);
-    //i2c_rd_reg(addr, buf);
+    //i2c_rd_data(addr, 3, buf);
     I2C_ReadBytes(addr, 3, buf);
-//    if(buf[2] == CalcSht21Crc(buf,2)) {
+    if(1) { //buf[2] == CalcSht21Crc(buf,2)) {
       val = buf[0];
       val <<= 8;
       val |= (buf[1] & 0xfc);
@@ -63,12 +62,12 @@ htu_err_t htu_GetHumidity(uint16_t * humidity) {
       val /= 65536;
       *humidity = val - 60;
       return HTU_OK;
-//    } else {
-//      *humidity = 1;
-//      return HTU_CRC_Humidity;
-//    }
+    } else {
+      *humidity = 1110;
+      return HTU_CRC_Humidity;
+    }
   } else {
-    *humidity = 555;
+    *humidity = i2cs * 100;
     return HTU_I2C;
   }
 }

+ 15 - 15
lib/i2c.c

@@ -71,30 +71,30 @@ t_i2c_status i2c_wr_data(const uint8_t address, const uint8_t num_bytes_to_wr, u
   //Генерация СТАРТ-посылки
   I2C->CR2 |= I2C_CR2_START;
   //Ждем установки бита SB
-  wait_event(!(I2C->SR1 & I2C_SR1_SB), 1);
+  wait_event(!(I2C->SR1 & I2C_SR1_SB), 5);
   
   //Записываем в регистр данных адрес ведомого устройства
   I2C->DR = adr;
   //Ждем подтверждения передачи адреса
-  wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 1);
+  wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 5);
   //Очистка бита ADDR чтением регистра SR3
   I2C->SR3;
   
   //Отправка данных
   while (length--) {
     //Ждем освобождения регистра данных
-    wait_event(!(I2C->SR1 & I2C_SR1_TXE), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_TXE), 5);
     //Отправляем адрес регистра
     I2C->DR = *data++;
   }
   
   //Ловим момент, когда DR освободился и данные попали в сдвиговый регистр
-  wait_event(!((I2C->SR1 & I2C_SR1_TXE) && (I2C->SR1 & I2C_SR1_BTF)), 1);
+  wait_event(!((I2C->SR1 & I2C_SR1_TXE) && (I2C->SR1 & I2C_SR1_BTF)), 5);
   
   //Посылаем СТОП-посылку
   I2C->CR2 |= I2C_CR2_STOP;
   //Ждем выполнения условия СТОП
-  wait_event((I2C->CR2 & I2C_CR2_STOP), 1);
+  wait_event((I2C->CR2 & I2C_CR2_STOP), 5);
   
   return I2C_SUCCESS;
 }
@@ -116,7 +116,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
   //Генерация СТАРТ-посылки
   I2C->CR2 |= I2C_CR2_START;
   //Ждем установки бита SB
-  wait_event(!(I2C->SR1 & I2C_SR1_SB), 1);
+  wait_event(!(I2C->SR1 & I2C_SR1_SB), 2);
   
   //Записываем в регистр данных адрес ведомого устройства и переходим
   //в режим чтения (установкой младшего бита в 1)
@@ -128,7 +128,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     //Запрещаем подтверждение в конце посылки
     I2C->CR2 &= ~I2C_CR2_ACK;
     //Ждем подтверждения передачи адреса
-    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 2);
     
     //Заплатка из Errata
     __disable_interrupt();
@@ -141,7 +141,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     __enable_interrupt();
     
     //Ждем прихода данных в RD
-    wait_event(!(I2C->SR1 & I2C_SR1_RXNE), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_RXNE), 2);
     
     //Читаем принятый байт
     *data = I2C->DR;
@@ -151,7 +151,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     //Бит который разрешает NACK на следующем принятом байте
     I2C->CR2 |= I2C_CR2_POS;
     //Ждем подтверждения передачи адреса
-    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 2);
     //Заплатка из Errata
     __disable_interrupt();
     //Очистка бита ADDR чтением регистра SR3
@@ -162,7 +162,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     __enable_interrupt();
     //Ждем момента, когда первый байт окажется в DR,
     //а второй в сдвиговом регистре
-    wait_event(!(I2C->SR1 & I2C_SR1_BTF), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_BTF), 2);
     
     //Заплатка из Errata
     __disable_interrupt();
@@ -177,7 +177,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
   //N>2
   else if (length > 2) {
     //Ждем подтверждения передачи адреса
-    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_ADDR), 2);
     
     //Заплатка из Errata
     __disable_interrupt();
@@ -190,7 +190,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     
     while (length-- > 3 && I2C_timeout) {
       //Ожидаем появления данных в DR и сдвиговом регистре
-      wait_event(!(I2C->SR1 & I2C_SR1_BTF), 1);
+      wait_event(!(I2C->SR1 & I2C_SR1_BTF), 2);
       //Читаем принятый байт из DR
       *data++ = I2C->DR;
     }
@@ -200,7 +200,7 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     //Осталось принять 3 последних байта
     //Ждем, когда в DR окажется N-2 байт, а в сдвиговом регистре
     //окажется N-1 байт
-    wait_event(!(I2C->SR1 & I2C_SR1_BTF), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_BTF), 2);
     //Запрещаем подтверждение в конце посылки
     I2C->CR2 &= ~I2C_CR2_ACK;
     //Заплатка из Errata
@@ -215,13 +215,13 @@ t_i2c_status i2c_rd_data(const uint8_t address, const uint8_t num_bytes_to_rd, u
     //Заплатка из Errata
     __enable_interrupt();
     //Ждем, когда N-й байт попадет в DR из сдвигового регистра
-    wait_event(!(I2C->SR1 & I2C_SR1_RXNE), 1);
+    wait_event(!(I2C->SR1 & I2C_SR1_RXNE), 2);
     //Читаем N байт
     *data++ = I2C->DR;
   }
   
   //Ждем отправки СТОП посылки
-  wait_event((I2C->CR2 & I2C_CR2_STOP), 1);
+  wait_event((I2C->CR2 & I2C_CR2_STOP), 2);
   //Сбрасывает бит POS, если вдруг он был установлен
   I2C->CR2 &= ~I2C_CR2_POS;
 

+ 1 - 1
lib/i2c_master_poll.c

@@ -54,7 +54,7 @@ void I2C_Init(void) {
 #endif
   I2C->OARL = 0xA0;              // own address A0;
   I2C->OARH |= 0x40;
-  I2C->ITR = 1;                  // enable error interrupts
+  //I2C->ITR = 1;                  // enable error interrupts
   I2C->CR2 |= 0x04;              // ACK=1, Ack enable
   I2C->CR1 |= 0x01;              // PE=1
 }

+ 1 - 1
lib/led.c

@@ -75,7 +75,7 @@ void led_OutputValue(void) {
     case led_Off:
       break;
     default:
-      LED_OUT_MM;
+      GPIOD->ODR &= ~(LED_SEG_D);
   }
 
   if(LedPoint[ledn] != 0) {

+ 10 - 4
src/main.c

@@ -21,6 +21,7 @@
 #include "stm8s.h"
 #include "board.h"
 #include "i2c.h"
+#include "i2c_master_poll.h"
 #include "led.h"
 #include "delay.h"
 #include "htu21.h"
@@ -50,7 +51,8 @@ void main(void)
   LedDigits[7] = led_H;
 
   /* I2C Configuration */
-  i2c_master_init();
+  //i2c_master_init();
+  I2C_Init();
 
   /* Infinite loop */
   while (1) {
@@ -66,9 +68,12 @@ void main(void)
 
 /* Private functions ---------------------------------------------------------*/
 static void showT(void) {
-  int16_t t = (Temperature + 5) / 10;;
+  int16_t t = (Temperature + 5) / 10;
   uint8_t a;
 
+  if (t < 0) {
+    t *= -1;
+  }
   LedDigits[0] = t / 100;
   a = t % 100;
   LedDigits[1] = a / 10;
@@ -76,10 +81,11 @@ static void showT(void) {
 }
 
 static void showH(void) {
+ uint16_t h = (Humidity + 5) / 10;
  uint8_t a;
 
- LedDigits[4] = Humidity / 100;
- a = Humidity % 100;
+ LedDigits[4] = h / 100;
+ a = h % 100;
  LedDigits[5] = a / 10;
  LedDigits[6] = a % 10;
 }