This document discusses various temperature and humidity sensors that can be connected to a PIC16F887 microcontroller including the LM35 analog temperature sensor, DS18B20 digital temperature sensor, and DHT11 digital humidity sensor. It provides code for reading temperature values from the DS18B20 sensor, converting it to Celsius, and displaying it on an LCD screen. The code also implements an alarm function using the temperature readings to control an LED indicator light.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
49 views2 pages
Components
This document discusses various temperature and humidity sensors that can be connected to a PIC16F887 microcontroller including the LM35 analog temperature sensor, DS18B20 digital temperature sensor, and DHT11 digital humidity sensor. It provides code for reading temperature values from the DS18B20 sensor, converting it to Celsius, and displaying it on an LCD screen. The code also implements an alarm function using the temperature readings to control an LED indicator light.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
PIC16F887
LCD LM044L 20x4
Senzor analogic temperatura [LM35] Senzor digital temperatura [DS18B20] Senzor digital umiditate [DHT11] Senzor analogic umiditate [HR-202]
LM35 - > Data pin to PIC A0 Pin
PIN1 -> 5v PIN2 -> PIC (A0) PIN3 -> GND Reading voltage quantity using the ADC gives us a number between 0 and 1023 (10-bit resolution), 0V is represented by 0 and 5V is represented by 1023. Converting back the ADC digital value is easy and we can use the following equation for that conversion: Voltage (in Volts) = ADC reading * 5 / 1023 Multiplying the previous result by 100 (LM35 scale factor is 10mV/°C = 0.01V/°C) will gives the actual temperature: Temperature(°C) = ADC reading * 0.489 where 0.489 = 500 / 1023
// Set TEMP_RESOLUTION to the corresponding resolution of your DS18x20 sensor:
// 18S20: 9 // 18B20: 12 (default setting; can be 9,10,11,or 12) const unsigned short TEMP_RESOLUTION = 12; const int RES_FACTOR_1[4] = {5000, 2500, 1250, 625}; const unsigned int RES_FACTOR_2[4] = {0x0001, 0x0003, 0x0007, 0x000F}; const unsigned int RES_FACTOR_3[4] = {0x8000, 0xC000, 0xE000, 0xF000}; float alarma; unsigned temp,temp2,new_temp; unsigned short j, RES_SHIFT,j2; void Display_Temperature(unsigned int temp) { const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8; unsigned int temp_whole, temp_fraction; unsigned short i; char text[8]; // Isolate the fraction and make it a 4-digit decimal integer (for display) temp_fraction = temp & RES_FACTOR_2[RES_SHIFT - 1]; temp_fraction = temp_fraction * RES_FACTOR_1[RES_SHIFT - 1]; //portc = temp_fraction; // Handle the whole part of temperature value temp_whole = temp; // Is temperature negative? if ((temp_whole & 0x8000) != 0u) i = 1; // Yes, i = 1 else i = 0; // No, i = 0 // PORTC = i; // Remove the fractional part temp_whole >>= RES_SHIFT; // Correct the sign if necessary if (i) temp_whole |= RES_FACTOR_3[RES_SHIFT - 1]; //portd = temp_whole; IntToStr(temp_whole, text); // Convert whole part to string Lcd_Out(2, 6, text); // Print whole part on LCD Lcd_Chr_Cp('.'); // Print dot to separate fractional part IntToStr(temp_fraction, text); // Convert fractional part to string // Add leading zeroes (we display 4 digits fractional part) if (temp_fraction < 1000u) Lcd_Chr_Cp('0'); if (temp_fraction < 100u) Lcd_Chr_Cp('0'); if (temp_fraction < 10u) Lcd_Chr_Cp('0'); Lcd_Out_Cp(text); // Print fractional part on LCD Lcd_Chr_Cp(223); // Print degree character Lcd_Chr_Cp('C'); // Print 'C' for Centigrades }//~ void main() { ADCON1 = 0xFF; // Configure RA5 pin as digital I/O PORTE = 0xFF; TRISE = 0x0F; // PORTE is input PORTB = 0; TRISB = 0; // PORTB is output TRISD=0; PORTD=0; TRISC=0; PORTC=0; // Initialize LCD on PORTB and prepare for output Lcd_Init(&PORTB); Lcd_Cmd(Lcd_CURSOR_OFF); Lcd_Out(1, 1, "Sicaklik: "); do { // main loop Ow_Reset(&PORTE,2); // Onewire reset signal Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM Ow_Write(&PORTE,2,0x44); // Issue command CONVERT_T Delay_us(120); Ow_Reset(&PORTE,2); Ow_Write(&PORTE,2,0xCC); // Issue command SKIP_ROM Ow_Write(&PORTE,2,0xBE); // Issue command READ_SCRATCHPAD Delay_ms(400); j = Ow_Read(&PORTE,2); // Get temperature LSB j2=j; temp = Ow_Read(&PORTE,2); // Get temperature MSB temp2=temp; temp <<= 8; temp += j; // Form the result temp2<<=5; j2>>=3; new_temp=temp2^j2; portd=new_temp; // alarma=39; alarma=((new_temp*127.5)/255); if(((alarma>=36.5)&&(alarma<=38))) { //YESİL LED AKTIF PORTC.F0=1; } else { portc.f0=0;} Display_Temperature(temp); // Format and display result on LCD Delay_ms(500); } while (1); }//~!