0% found this document useful (0 votes)
60 views7 pages

ADC-DHT11 C

The document contains configuration settings for a PIC18F442 microcontroller. It sets the oscillator to 8MHz and enables various features like the brown-out reset and watchdog timer. It also defines I/O pins for communicating with sensors to read temperature and humidity from a DHT11 sensor and display the results on a LCD screen. Functions are included to start the sensor reading, check responses, and read the data values.

Uploaded by

Giancarlo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
60 views7 pages

ADC-DHT11 C

The document contains configuration settings for a PIC18F442 microcontroller. It sets the oscillator to 8MHz and enables various features like the brown-out reset and watchdog timer. It also defines I/O pins for communicating with sensors to read temperature and humidity from a DHT11 sensor and display the results on a LCD screen. Functions are included to start the sensor reading, check responses, and read the data values.

Uploaded by

Giancarlo
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 7

// PIC18F442 Configuration Bit Settings

// 'C' source line config statements

// 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)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 8000000 //Define Clock to 8MHz for delay calculations

#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);
}

#define Data_Out LATC2 /* assign Port pin for data*/


#define Data_In PORTCbits.RC2 /* read data from Port pin*/
#define Data_Dir TRISCbits.RC2 /* Port direction */

//Retardos requeridos por la librer�a XLCD


void DelayFor18TCY(void);
void DelayPORXLCD(void);
void DelayXLCD(void);

void DHT11_Start();
void DHT11_CheckResponse();
char DHT11_ReadData();

unsigned int resultado;


//long int vAnalogico;
char digito;

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);
}

//FUNCIONES PARA LEER SENSOR DE HUMEDAD Y TEMPERATURA DHT11

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;

unsigned char address1;


unsigned char address10;
unsigned char address100;
unsigned char AddressByte;

// State of each switch (0 or 1)


unsigned char s1_state;
unsigned char s2_state;
unsigned char s3_state;
unsigned char s4_state;
unsigned char s5_state;
unsigned char s6_state;
unsigned char s7_state;
unsigned char s8_state;
unsigned char s9_state;
unsigned char s10_state;

// Microchip pins used for the rotary switches


#define S1 PORTBbits.RB4
#define S2 PORTBbits.RB5
#define S3 PORTBbits.RB6
#define S4 PORTBbits.RB7
#define S5 PORTBbits.RB0
#define S6 PORTBbits.RB1
#define S7 PORTBbits.RB2
#define S8 PORTBbits.RB3
#define S9 PORTDbits.RD4
#define S10 PORTDbits.RD5

//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

RBPU = 0; //Enables PULL-UP on Input Port B

//Rotary switch for Units


TRISB4 = 1; //RB4 as Input PIN for RSW1-1
TRISB5 = 1; //RB5 as Input PIN for RSW1-2
TRISB6 = 1; //RB6 as Input PIN for RSW1-4
TRISB7 = 1; //RB7 as Input PIN for RSW1-8

//Rotary switch for Tens


TRISB0 = 1; //RB0 as Input PIN for RSW2-1
TRISB1 = 1; //RB1 as Input PIN for RSW2-2
TRISB2 = 1; //RB2 as Input PIN for RSW2-4
TRISB3 = 1; //RB3 as Input PIN for RSW2-8

//Rotary switch for Hundreds


TRISD4 = 1; //RD4 as Input PIN for RSW3-1
TRISD5 = 1; //RD5 as Input PIN for RSW3-2
TRISD6 = 1; //RD6 as Input PIN for RSW3-4
TRISD7 = 1; //RD7 as Input PIN for RSW3-8

//Output RS-422 Driver


TRISC5 = 1; //RC5 as Input PIN not used
TRISC4 = 0; //RC4 as Output PIN Set to HIGH for driver enable
PORTCbits.RC4 = 1; //RC4 set to HIGH
TRISC6 = 0; //RC6 as Output PIN for DI

//Input RS-422 Receiver


TRISC7 = 1; //RC7 as Input PIN for RO
TRISD3 = 0; //RD3 as Output PIN Set to LOW for receiver enable
PORTDbits.RD3 = 0; //RD3 set to LOW
TRISD2 = 0; //RD2 as Output PIN not used

//Configurando LCD 4 bits mutil�nea


OpenXLCD(FOUR_BIT & LINES_5X7);
//Esperar hasta que el display est� disponible.
while(BusyXLCD());
//Mover cursor a la derecha...
WriteCmdXLCD(0x06);
//Desactivando el cursor.
WriteCmdXLCD(0x0C);
__delay_ms(100);
SetDDRamAddr(0x00); //Ubica el cursor en la 1era l�nea
putrsXLCD("TEMPERATURA: C");

SetDDRamAddr(0x40); //Ubica el cursor en la 2da l�nea


putrsXLCD("HUMEDAD % ");

//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 */

/* read 40-bit data from DHT11 module */


RH_Integral = DHT11_ReadData(); /* read Relative Humidity's integral value */
RH_Decimal = DHT11_ReadData(); /* read Relative Humidity's decimal value */
T_Integral = DHT11_ReadData(); /* read Temperature's integral value */
T_Decimal = DHT11_ReadData(); /* read Relative Temperature's decimal value
*/
Checksum = DHT11_ReadData(); /* read 8-bit checksum value */

/* convert humidity value to ascii and send it to display*/


//sprintf(value,"%d",RH_Integral);

// Check addition of humidity and temperature value equals to checksum


if(Checksum == (RH_Integral + RH_Decimal + T_Integral + T_Decimal))
{
digito = RH_Integral%10;
SetDDRamAddr(0x49);
putcXLCD(digito + 48);
digito = (RH_Integral / 10)%10;
SetDDRamAddr(0x48);
putcXLCD(digito + 48);

//sprintf(value,".%d ",RH_Decimal);
//LCD_String(value);
//LCD_Char('%');

/* convert temperature value to ascii and send it to display*/


//sprintf(value,"%d",T_Integral);
SetDDRamAddr(0x0E);
digito = T_Integral%10;
putcXLCD(digito + 48);
digito = (T_Integral / 10)%10;
SetDDRamAddr(0x0D);
putcXLCD(digito + 48);
//sprintf(value,".%d",T_Decimal);
//LCD_String(value);
//LCD_Char(0xdf);
//LCD_Char('C');

//sprintf(value,"%d ",Checksum);
//LCD_String_xy(1,8,value);

/* check addition of humidity and temperature value equals to checksum


if(Checksum != (RH_Integral + RH_Decimal + T_Integral + T_Decimal))
LCD_String_xy(0,8,"Error");
else
LCD_String_xy(0,8,"No Error");
*/
// __delay_ms(500);
}

//PRINT ADDRESS ON LCD DISPLAY


digito = Checksum%10;
SetDDRamAddr(0x4F);
putcXLCD(digito + 48);
digito = (Checksum / 10)%10;
SetDDRamAddr(0x4E);
putcXLCD(digito + 48);
digito = (Checksum / 100)%10;
SetDDRamAddr(0x4D);
putcXLCD(digito + 48);

//__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);

/*ADC_INT_ENABLE(); //Habilitaci�n de la interrupci�n AD


ei();

//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();
}

}
*/

You might also like