Conectare senzor umiditate si temperatura SHT11 la Arduino

deAdrian G.

Conectare senzor umiditate si temperatura SHT11 la Arduino

sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections

//Conexiuni SHT 11
sbit SDA at RC4_bit;                     // Serial data pin
sbit SCL at RC3_bit;                     // Serial clock pin
sbit SDA_Direction at TRISC4_bit;        // Serial data direction pin
sbit SCL_Direction at TRISC3_bit;        // Serial clock direction pin
// Constante pentru calcularea temperaturii si a umiditatii
const unsigned int C1 = 400;
const unsigned int C2 = 405;
const unsigned short C3 = 28;
const unsigned int D1 = 4000;
const unsigned short D2 = 1;

unsigned short i, j;
long int temp, k, SOt, SOrh, Ta_res, Rh_res;
char Ta[9]  = “000.0  “;         //Caracterele pentru grade celsius
char Rh[12] = “H00.0”;           //Caractere pentru umiditate
int val;

void SHT_Reset() {
  SCL = 0;
  SDA_Direction = 1;                     // definesc SDA ca input
  for (i = 1; i <= 18; i++)              // repet de 18 ori
    SCL = ~SCL;                          // inversul SCL
}

void Transmission_Start() {
  SDA_Direction = 1;                     // definesc SDA ca input
  SCL = 1;                               // SCL high
  Delay_1us();                           // intarziere de 1us
  SDA_Direction = 0;                     // definesc SDA ca output
  SDA = 0;                               // SDA low
  Delay_1us();                           // intarziere de 1us
  SCL = 0;                               // SCL low
  Delay_1us();                           // intarziere de 1us
  SCL = 1;                               // SCL high
  Delay_1us();                           // intarziere de 1us
  SDA_Direction = 1;                     // definesc SDA ca input
  Delay_1us();                           // intarziere de 1us
  SCL = 0;                               // SCL low
}

// MCU ACK
void MCU_ACK() {
  SDA_Direction = 0;     // definesc SDA ca output
  SDA = 0;               // SDA low
  SCL = 1;               // SCL high
  Delay_1us();           // intarziere de 1us
  SCL = 0;               // SCL low
  Delay_1us();           // intarziere de 1us
  SDA_Direction = 1;     // definesc SDA ca input
}

// Aceasta functie va returna valorile temperaturii si umiditatii in functie de comanda
long int Measure(short num) {
  j = num;                           // j = comanda (0x03 or 0x05)
  SHT_Reset();                       // resetam SHT11
  Transmission_Start();              // pornim transmisia de date
  k = 0;                             // k = 0
  SDA_Direction = 0;                 // definesc SDA ca output
  SCL = 0;                           // SCL low
  for(i = 1; i <= 8; i++) {          // repet de 8 ori
    if (j.F7 == 1)                   // if bit 7 = 1
     SDA_Direction = 1;              // definesc SDA ca input
    else {                           // else (if bit 7 = 0)
     SDA_Direction = 0;              // definesc SDA ca output
     SDA = 0;                        // SDA low
   }
    Delay_1us();                     // intarziere de 1us
    SCL = 1;                         // SCL high
    Delay_1us();                     // intarziere de 1us
    SCL = 0;                         // SCL low
    j <<= 1;
  }

  SDA_Direction = 1;                 // definesc SDA ca input
  SCL = 1;                           // SCL high
  Delay_1us();                       // intarziere de 1us
  SCL = 0;                           // SCL low
  Delay_1us();                       // intarziere de 1us
  while (SDA == 1)                   // while SDA is high, do nothing
    Delay_1us();                     // intarziere de 1us

  for (i = 1; i <=16; i++) {         // repet de 16 ori
    k <<= 1;
    SCL = 1;                         // SCL high
    if (SDA == 1)                    // if SDA is high
    k = k | 0x0001;
    SCL = 0;
    if (i == 8)                      // daca counterul i = 8 atunci
      MCU_ACK();                     // MCU acknowledge
  }
  return k;                          // returneaza valoarea lui k
}

int sht(){
    // masurarea temperaturii
    SOt = Measure(0x03);             // functie pentru masurarea temperaturii

     // masurarea umiditatii
    SOrh = Measure(0x05);            // functie pentru masurarea umiditatitii

// Calculating temperature
// Ta_res = D1 + D2 * SOt
    if(SOt > D1)                     // daca temperatura este pozitiva
      Ta_res = SOt * D2 – D1;        // calculez temperatura
    else                             // else (daca temperatura ii pozitiva)
      Ta_res = D1 – SOt * D2;        // calculez temperatura

// Calculez umiditatea
    temp = SOrh * SOrh * C3 / 100000;             // calculez umiditatea
    Rh_res = SOrh * C2 / 100 – temp – C1;         // calculez umiditatea
    Ta[0] = 0b00010010;//Ta_res / 10000 + 48;     // examplu: Ta[5] = 12345 / 10000 = 1, 1 + 48 = ‘1’ – ASCII
    Ta[1] = Ta_res % 10000 / 1000 + 48;           // examplu: Ta[6] = 12345 % 10000 = 2345, Ta[6] = 2345 / 1000 = 2, 2 + 48 = ‘2’ – ASCII
    Ta[2] = Ta_res % 1000 / 100 + 48;             // examplu: Ta[7] = 12345 % 1000 = 345, Ta[7] = 345 / 100 = 3, 3 + 48 = ‘3’ – ASCII
    Ta[4] = Ta_res % 100 / 10 + 48;               // examplu: Ta[9] = 12345 % 100 = 45, Ta[9] = 45 / 10 = 4, 4 + 48 = ‘4’ – ASCII
    //Ta[7] = Ta_res % 10 + 48;                   // examplu: Ta[10] = 12345 % 10 = 5, 5 + 48 = ‘5’ – ASCII

    Rh[1] = Rh_res / 10000 + 48;                  // example: Rh[5] = 12345 / 10000 = 1, 1 + 48 = ‘1’ – ASCII
    Rh[2] = Rh_res % 10000 / 1000 + 48;           // example: Rh[6] = 12345 % 10000 = 2345, Rh[6] = 2345 / 1000 = 2, 2 + 48 = ‘2’ – ASCII
    Rh[3] = Rh_res % 1000 / 100 + 48;             // example: Rh[7] = 12345 % 1000 = 345, Rh[7] = 345 / 100 = 3, 3 + 48 = ‘3’ – ASCII
    Rh[5] = Rh_res % 100 / 10 + 48;               // example: Rh[9] = 12345 % 100 = 45, Rh[9] = 45 / 10 = 4, 4 + 48 = ‘4’ – ASCII
    Rh[6] = Rh_res % 10 + 48;                     // example: Rh[10] = 12345 % 10 = 5, 5 + 48 = ‘5’ – ASCII
    Rh[7] = ‘%’;                      // ‘%’ character

// delete unnecessary digits (zeros)
    if (Ta[0] == ‘0’)                             // daca Ta[5] = ‘0’ atunci
      Ta[0] = ‘ ‘;                                // inserez caracterul gol la Ta[5]
    if (Ta[0] == ‘ ‘ && Ta[1] == ‘0’)             // daca Ta[5] ii gol si Ta[6] = ‘0’ atunci
      Ta[1] = ‘ ‘;                                // insereaza caracterul gol la Ta[6]

    if (Rh[1] == ‘0’)                             // daca Ta[5] = ‘0’ atunci
      Rh[1] = ‘ ‘;                                // inserez caracterul gol la Ta[5]
    if (Rh[1] == ‘ ‘ && Rh[2] == ‘0’)             // daca Ta[5] ii gol si Ta[6] = ‘0’ atunci
      Rh[2] = ‘ ‘;                                // insereaza caracterul gol la Ta[6]
      return  Ta_res;
}

void main()
{
  ANSEL  = 0;                        // Configurez pinii analogici in digitali
  INTCON = 0;
  TRISC  = 0;
  SCL_Direction = 0;                 // SCL ii output
  LCD_Init();                        // initializare LCD on PORTB
  LCD_Cmd(_LCD_CURSOR_OFF);          // opreste cursorul la LCD
  LCD_Out(1, 1, “Starting …”);     // Scrie pe LCD
  Delay_ms(1000);                    // Intarzie de 2 sec
  LCD_Cmd(_LCD_CLEAR);               // sterge LCD
while (1) {
  val = sht();

    Lcd_Out(1, 2,Ta);                 // Afiseaza temperatura pe primul rand
// Display humidity on LCD
  Lcd_Out(1, 9, Rh);
  Lcd_Out(1, 13, “.”);                // Afiseaza umiditatea pe randul doi
    Delay_ms(500);                    // Intarzie de 2 sec
  }
}

Despre autor

Adrian G. administrator