|
|
View previous topic :: View next topic |
Author |
Message |
D_Fisher
Joined: 24 Jul 2023 Posts: 3
|
4 DHT22 code |
Posted: Mon Jul 24, 2023 8:39 pm |
|
|
Hi, Im trying to make a programm with 4 DHT22 working together and that it throws in an lcd 1602 the average of the 4 sensors, but I have some issues with the trisb and portb sentence. Im new in css, I dont know how it works, i guess is something for the pin connection. Im using B0, B1, B2 and B3 for the data pin of the 4 dht22.
Code: |
#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
#define Boton pin_A2 // Conexion pin entre hc boton y pic
///HC-05
#use delay(internal=48MHz) // Tipo de oscilador y frecuencia dependiendo del microcontrolador
#build(reset=0x02000,interrupt=0x02008) // Asigna los vectores de reset e interrupciĆ³n para la versiĆ³n con bootloader
#org 0x0000,0x1FFF {} // Reserva espacio en memoria para el bootloader
#USE RS232(stream=SERIE, BAUD=9600, PARITY=N, XMIT=PIN_C6, RCV=PIN_C7,BITS=8)
//-------------------------------------------------------------------------------
//LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections
#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#include <lcd.c>
#use fast_io(B)
// Connection pin between PIC18F4550 and DHT22 sensor
#BIT Data_Pin = 0xF81.0 // Pin mapped to PORTB.0
#BIT Data_Pin_Direction = 0xF93.0 // Pin direction mapped to TRISB.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(){
setup_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz
setup_adc_ports(NO_ANALOGS); // Configure AN pins as digital
lcd_init(); // Initialize LCD module
lcd_putc('\f'); // LCD clear
printf(lcd_putc,"INICIO OK");
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
lcd_putc('\f'); // LCD clear
lcd_gotoxy(5, 1); // Go to column 5 row 1
lcd_putc("Time out!");
}
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
lcd_putc('\f'); // LCD clear
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, message1); // Display message1
lcd_gotoxy(1, 2); // Go to column 1 row 2
printf(lcd_putc, message2); // Display message2
}
else {
lcd_putc('\f'); // LCD clear
lcd_gotoxy(1, 1); // Go to column 1 row 1
lcd_putc("Checksum Error!");
}
}
}
else {
lcd_putc('\f'); // LCD clear
lcd_gotoxy(3, 1); // Go to column 3 row 1
lcd_putc("No response");
lcd_gotoxy(1, 2); // Go to column 1 row 2
lcd_putc("from the sensor");
}
}
}
|
This code works for the B0 pin but i need something for B1,B2 and B3, something that make it work together. Im also tryng to send all the temperatures and rhs to the serial terminal bluetooth mobile app with a HC-05. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Tue Jul 25, 2023 9:35 am |
|
|
Why don't you average 4 readings from one sensor? I don't see the point of using four sensors measuring the same thing, if they are close together. Which they must be, if you are looking for an average that has any meaning. I'd understand two if getting a valid result every second is something vital and you need redundancy. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jul 25, 2023 9:50 am |
|
|
I'd guess it is probably just something like incubator boxes, each with
a separate sensor.
Given the way the timing is done, just duplicate the code for the other pins.
Create new bit definitions like:
Code: |
#BIT Data_PinB0 = 0xF81.0 // Pin mapped to PORTB.0
#BIT Data_Pin_DirectionB0 = 0xF93.0 // Pin direction mapped to TRISB.0
#BIT Data_PinB1 = 0xF81.1 // Pin mapped to PORTB.1
#BIT Data_Pin_DirectionB1 = 0xF93.1 // Pin direction mapped to TRISB.1
#BIT Data_PinB2 = 0xF81.2 // Pin mapped to PORTB.2
#BIT Data_Pin_DirectionB2 = 0xF93.2 // Pin direction mapped to TRISB.2
#BIT Data_PinB3 = 0xF81.3 // Pin mapped to PORTB.3
#BIT Data_Pin_DirectionB3 = 0xF93.3 // Pin direction mapped to TRISB.3
|
Then just have multiple copies of the routines with the names having
B0 B1, B2 & B3 added.
As a comment, the initial start pulse, can be massively shortened, The
older 11 chip needed the long pulse here, but the 22, only needs
1mSec. I'd say use 2mSec, instead of the 25mSec. Will make the
overall time needed for the reading a lot less. |
|
|
D_Fisher
Joined: 24 Jul 2023 Posts: 3
|
|
Posted: Tue Jul 25, 2023 9:51 am |
|
|
Well, the 4 sensors are not together, is just measuring an area, and the program uses an lcd. So I'm using 4 but 2 or 3 it's a good idea too. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|