#include <16F18313.h>
//#device ADC=10
//#FUSES WDT //Watch Dog Timer
#use delay(internal=2MHz)
#use FAST_IO( A )
#define DHT22 PIN_A4
#define led PIN_A5
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,stream=PORT1,FORCE_SW)#include <pic16f18313.h>
#BIT Data_Pin = 0x00C.4 // Pin mapped to PORTA.0
#BIT Data_Pin_Direction = 0x08C.4 // Pin direction mapped to TRISA.0
char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
short Time_out ;
unsigned int8 T_byte1, T_byte2, RH_byte1, RH_byte2, CheckSum ;
unsigned int16 Temp, RH;
void start_signal(){
Data_Pin_Direction = 0; // Configure connection pin as output
Data_Pin = 0; // Connection pin output low
delay_ms(25);
Data_Pin = 1; // Connection pin output high
delay_us(30);
Data_Pin_Direction = 1; // Configure connection pin as input
}
short check_response(){
delay_us(40);
if(!Data_Pin){ // Read and test if connection pin is low
delay_us(80);
if(Data_Pin){ // Read and test if connection pin is high
delay_us(50);
return 1;}
}
}
unsigned int8 Read_Data(){
unsigned int8 i, k, _data = 0; // k is used to count 1 bit reading duration
if(Time_out)
break;
for(i = 0; i < 8; i++){ k = 0; while(!Data_Pin){ // Wait until pin goes high k++; if (k > 100) {Time_out = 1; break;}
delay_us(1);}
delay_us(30);
if(!Data_Pin)
bit_clear(_data, (7 - i)); // Clear bit (7 - i)
else{
bit_set(_data, (7 - i)); // Set bit (7 - i)
while(Data_Pin){ // Wait until pin goes low
k++;
if (k > 100) {Time_out = 1; break;}
delay_us(1);}
}
}
return _data;
}
void main(){
// set_tris_a(0x0E);
/*
0 salida
1 entrada
A5_led_ salida valor 0
A4_DHT22 a veces entrada a veces salida
A3_nada_ entrada
A2_nada_ entrada
A1_RX_RS232 valor 1
A0_TX_RS232 valor 0
Los dos codigos son 0x0E Para A4 como entrada y 0x1E para A4 como salida.
*/
set_tris_a(0x0E);
while(TRUE){
delay_ms(1000);
Time_out = 0;
Start_signal();
if(check_response()){ // If there is response from sensor
RH_byte1 = Read_Data(); // read RH byte1
RH_byte2 = Read_Data(); // read RH byte2
T_byte1 = Read_Data(); // read T byte1
T_byte2 = Read_Data(); // read T byte2
Checksum = Read_Data(); // read checksum
if(Time_out){ // If reading takes long time
printf("timeout\r\n");
}
else{
if(CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF)){
RH = RH_byte1;
RH = (RH << 8) | RH_byte2;
Temp = T_byte1;
Temp = (Temp << 8) | T_byte2; if (Temp > 0x8000){
message1[6] = '-';
Temp = Temp & 0x7FFF; }
else
message1[6] = ' ';
message1[7] = (Temp / 100) % 10 + 48;
message1[8] = (Temp / 10) % 10 + 48;
message1[10] = Temp % 10 + 48;
message2[7] = (RH / 100) % 10 + 48;
message2[8] = (RH / 10) % 10 + 48;
message2[10] = RH % 10 + 48;
message1[11] = 223; // Degree symbol
printf("correcto\r\n");
}
else {
printf("checsum error\r\n");
}
}
}
else {
printf("Sin respuesta\r\n");
}
delay_ms(2500);
output_high(led);
printf("encendido\r\n");
delay_ms(1000);
output_low(led);
printf("apagado\r\n");
delay_ms(1000);
}
}T_byte2 = Read_Data(); la cual activa el timeout.
The ANSELA bits default to the Analog mode after Reset. To use any pins as digital general purpose or peripheral inputs, the corresponding ANSEL bits must be initialized to ‘0’ by user software
Del datasheet:CitarThe ANSELA bits default to the Analog mode after Reset. To use any pins as digital general purpose or peripheral inputs, the corresponding ANSEL bits must be initialized to ‘0’ by user software