ADC-DHT11 C
ADC-DHT11 C
ADC-DHT11 C
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config OSCS = ON // Oscillator System Clock Switch Enable bit
(Oscillator system clock switch option is enabled (oscillator switching is
enabled))
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bit (Brown-out Reset
enabled)
#pragma config BORV = 20 // Brown-out Reset Voltage bits (VBOR set to 2.0V)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control
is placed on the SWDTEN bit))
#pragma config WDTPS = 128 // Watchdog Timer Postscale Select bits (1:128)
// CONFIG3H
#pragma config CCP2MUX = OFF // CCP2 Mux bit (CCP2 input/output is multiplexed
with RC1)
// CONFIG4L
#pragma config STVR = ON // Stack Full/Underflow Reset Enable bit (Stack
Full/Underflow will cause RESET)
#pragma config LVP = OFF // Low Voltage ICSP Enable bit (Low Voltage ICSP
enabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000200-001FFFh)
not code protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh)
not code protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block
(000000-0001FFh) not code protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not
code protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000200-001FFFh)
not write protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh)
not write protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit
(Configuration registers (300000-3000FFh) not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block
(000000-0001FFh) not write protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM
not write protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000200-
001FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-
003FFFh) not protected from Table Reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block
(000000-0001FFh) not protected from Table Reads executed in other blocks)
#include <xc.h>
#include <stdio.h>
#include <pic18f442.h>
#include <xlcd.h>
//#include <adc.h>
void highbit(void)
{ PORTCbits.RC6 = 1;
__delay_us(2000);
PORTCbits.RC6 = 0;
__delay_us(2000);
}
void lowbit(void)
{ PORTCbits.RC6 = 1;
__delay_us(500);
PORTCbits.RC6 = 0;
__delay_us(3500);
}
void DHT11_Start();
void DHT11_CheckResponse();
char DHT11_ReadData();
char RH_Decimal;
char RH_Integral;
char T_Decimal;
char T_Integral;
char Checksum;
char value[10];
//DELAYS FUNCTIONS
void DelayFor18TCY(void)
{
__delay_us(35);
}
void DelayPORXLCD(void)
{
__delay_ms(15);
}
void DelayXLCD(void)
{
__delay_ms(5);
}
char DHT11_ReadData()
{
char i,data = 0;
for(i=0;i<8;i++)
{
while(!(Data_In & 1)); /* wait till 0 pulse, this is start of data pulse
*/
__delay_us(30);
if(Data_In & 1) /* check whether data is 1 or 0 */
data = ((data<<1) | 1);
else
data = (data<<1);
while(Data_In & 1);
}
return data;
}
void DHT11_Start()
{
Data_Dir = 0; /* set as output port */
Data_Out = 0; /* send low pulse of min. 18 ms width */
__delay_ms(18);
Data_Out = 1; /* pull data bus high */
__delay_us(20);
Data_Dir = 1; /* set as input port */
}
void DHT11_CheckResponse()
{
while(Data_In & 1); /* wait till bus is High */
while(!(Data_In & 1)); /* wait till bus is Low */
while(Data_In & 1); /* wait till bus is High */
}
void main()
{
unsigned char bit1;
unsigned char bit2;
unsigned char bit3;
unsigned char bit4;
unsigned char bit5;
unsigned char bit6;
unsigned char bit7;
unsigned char bit8;
//LEDS
TRISD1 = 0; //RD1 as Output PIN for LED RX
TRISD0 = 0; //RD0 as Output PIN for LED ERROR
TRISC3 = 0; //RC3 as Output PIN for LED STATUS
TRISC0 = 0; //RC0 as Output PIN for LED TX
//Desactivando el cursor.
WriteCmdXLCD(0x0C);
__delay_ms(50);
while(1)
{
DHT11_Start(); /* send start pulse to DHT11 module */
DHT11_CheckResponse(); /* wait for response from DHT11 module */
//sprintf(value,".%d ",RH_Decimal);
//LCD_String(value);
//LCD_Char('%');
//sprintf(value,"%d ",Checksum);
//LCD_String_xy(1,8,value);
//__delay_ms(35);
__delay_ms(500);
//ADIF = 0;
//==========================================
/*Configuraci�n del m�dulo AD
* Fosc = 64
* Alineaci�n = derecha
* 16 TAD
* Canal AN0
* Interrupci�n habilitada
* VREF+ y VREF- conectados a VDD y VSS respectivamente
* Valor de ADCON1 = 14 (Canal AN0 anal�gico, el resto digitales)
*/
//OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_16_TAD, ADC_CH0 & ADC_INT_ON &
ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS, 14);
// ADCON0 = 0x81; //Turn ON ADC and Clock Selection
// ADCON1 = 0x00; //All pins as Analog Input and setting Reference
Voltages
//Retardo de 50 Tcy
//Delay10TCYx(5);
//__delay_us(300);
//Iniciar la conversi�n
ConvertADC();
*/
}
}
/*
void interrupt ADCInterrupt()
{
if(ADIF)
{
//Capturando el resultado
resultado = ReadADC();
vAnalogico = (long)resultado;
//Algoritmo para descomponer el resultado en digitos enteros...
vAnalogico = vAnalogico * 5139; //Voltaje de referencia VDD 5.139V
vAnalogico = vAnalogico / 65534;
//vAnalogico = vAnalogico * 1.034; //Ajuste por calibraci�n
digito = vAnalogico / 1000;
SetDDRamAddr(0x4C);
putcXLCD(digito + 48);
SetDDRamAddr(0x4D);
putrsXLCD(".");
digito = (vAnalogico / 100)%10;
SetDDRamAddr(0x4E);
putcXLCD(digito + 48);
digito = (vAnalogico / 10)%10;
SetDDRamAddr(0x4F);
putcXLCD(digito + 48);
// SetDDRamAddr(0x0D);
// putrsXLCD("V");
//ConvertADC();
}
}
*/