I have a problem with a conversion of a program written c in a program written in BASIC for display(posting) of the values of a sensor of temperature and humidity DTH22.
The program writes it C work correctly, while that writes in BASIC do not work correctly. I have a doubt on the conversion of the functions(offices) CheckRepsonse and ReadByte.
I use PIC 18F252 with PLL and Quartz 8 MHZ : 32 MHZ.
Thank you for your help(assistant)
Bernard Mairot
program basic :
Code: Select all
program DTHxx
' Declarations section
dim
LCD_RS as sbit at RB0_bit
LCD_EN as sbit at RB1_bit
LCD_D7 as sbit at RB7_bit
LCD_D6 as sbit at RB6_bit
LCD_D5 as sbit at RB5_bit
LCD_D4 as sbit at RB4_bit
dim
LCD_RS_Direction as sbit at TRISB0_bit
LCD_EN_Direction as sbit at TRISB1_bit
LCD_D7_Direction as sbit at TRISB7_bit
LCD_D6_Direction as sbit at TRISB6_bit
LCD_D5_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB4_bit
' End Lcd module connections
Dim
message1 as string[12]
message2 as string[12]
check as byte
TOUT,CheckSum,i,j,k,num as byte
Dat as sbit at RB3_bit
DataDir as sbit at TRISB3_bit
message as string[6]
T_Byte1,T_Byte2,RH_Byte1,RH_Byte2 as byte
T, RH as word
sub procedure StartSignal()
DataDir = 0 ' Data port is output
Dat = 0
Delay_ms(20) ' Low for at least 18us
Dat = 1
Delay_us(25) ' High for 20-40 us
DataDir = 1 ' Data port is input
end sub
sub procedure interrupt
if(PIR1.TMR2IF) then
TOUT = 1
T2CON.TMR2ON = 0 ' stop timer
PIR1.TMR2IF = 0 ' Clear TMR0 interrupt flag
end if
end sub
sub function CheckResponse() as Byte
TOUT = 0
TMR2 = 0
T2CON.TMR2ON = 1 ' Start TMR2 while waiting for sensor response
while((not Dat) and (not TOUT)) ' If there's no response within 256us, the Timer2 overflows
if (TOUT) then
CheckResponse=0
Return
else
TMR2 = 0
while(Dat and (not TOUT))
if (TOUT) then
CheckResponse =0
return
else
T2CON.TMR2ON = 0
checkResponse =1
return
end if
wend
end if
wend
end sub
sub function ReadByte() as Byte
num = 0
DataDir = 1
for i = 0 to 7
while(not Dat)
TMR2 = 0
T2CON.TMR2ON = 1 ' Start TMR2 from 0 when a low to high data pulse
while(Dat) ' is detected, and wait until it falls low again.
T2CON.TMR2ON = 0 ' Stop the TMR2 when the data pulse falls low.
if(TMR2 > 60)then
num = 1<<(7-i) ' If time > 60us, Data is 1
end if
ReadByte=num
return
wend
wend
next i
readByte=num
end sub
main:
' Main program
TRISB=%00001000 ' PORTB3 en Entrée CCP1
PORTB=0
LCD_Init() ' Initialisation LCD
Lcd_Out(1,1,"TEST DTHxx")
delay_ms(500)
Lcd_Cmd(_LCD_CLEAR) ' Effacement LCD
delay_ms(200)
J=0
K=0
INTCON.GIE = 1 ' Enable global interrupt
INTCON.PEIE = 1 ' Enable peripheral interrupt
' Configure Timer2 module
PIE1.TMR2IE = 1 ' Enable Timer2 interrupt
T2CON = 1 ' Prescaler 1:4, and Timer2 is off initially
PIR1.TMR2IF =0 ' Clear TMR INT Flag bit
TMR2 = 3
Lcd_Init()
Lcd_Cmd(_Lcd_Clear)
Lcd_Cmd(_LCD_CURSOR_OFF)
message1 = "Temp = 00.0 C"
message2 = "RH = 00.0 %"
while TRUE
Delay_ms(2000)
StartSignal
check = CheckResponse
if (not check) then
Lcd_Cmd(_Lcd_Clear)
Lcd_Out(3, 1, "No response")
Lcd_Out(4, 1, "from the sensor")
J=J+1
else
RH_Byte1 = ReadByte()
RH_Byte2 = ReadByte()
T_Byte1 = ReadByte()
T_Byte2 = ReadByte()
CheckSum = ReadByte()
' Check for error in Data reception
if (CheckSum = ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) and 255)) then
Lcd_Cmd(_Lcd_Clear)
delay_ms(50)
RH=(RH_Byte1*256)+RH_Byte2
T=(T_Byte1*256)+T_Byte2
message1[12] = 223 ' Degree symbol
message1[8]=T/100 +48
T=T mod 100
message1[9]= T/10 +48
message1[11]=T mod 10 +48
message2[8]=RH/100 +48
RH=RH mod 100
message2[9]= RH/10 +48
message2[11]= RH mod 10 +48
delay_ms(10)
Lcd_Out(1, 1, message1)
Lcd_Out(2, 1, message2)
K=K+1
ByteToStr(K,message)
Lcd_Out(1,16,message)
ByteToStr(J,message)
Lcd_Out(4,16,message)
else
Lcd_Cmd(_Lcd_Clear)
Lcd_Out(3, 1, "Checksum Error!")
Lcd_Out(4, 1, "Trying Again ...")
end if
end if
delay_ms(500)
wend
end.
Code: Select all
/*
Project: Temperature and humidity measurements using DHT11
MCU: PIC16F887
Clock: 10.0MHz external crystal
Board: UNI-DS6 board
Date: Jan 10, 2012
Written by: Rajendra Bhatt (http://www.embedded-lab.com)
*/
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
sbit Data at RB3_bit;
sbit DataDir at TRISB3_bit;
char message1[] = "Temp = 00.0 C";
char message2[] = "RH = 00.0 %";
char message[6];
unsigned short TOUT = 0, CheckSum, i,j,k;
unsigned short T_Byte1,T_Byte2,RH_Byte1,RH_Byte2;
int T,RH;
float Te, Hu;
void StartSignal(){
DataDir = 0; // Data port is output
Data = 0;
Delay_ms(20); // Low for at least 18us
Data = 1;
Delay_us(25); // High for 20-40 us
DataDir = 1; // Data port is input
}
unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 while waiting for sensor response
while(!Data && !TOUT); // If there's no response within 256us, the Timer2 overflows
if (TOUT) return 0; // and exit
else {
TMR2 = 0;
while(Data && !TOUT);
if (TOUT) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}
unsigned short ReadByte(){
unsigned short num = 0;
DataDir = 1;
for (i=0; i<8; i++){
while(!Data);
TMR2 = 0;
T2CON.TMR2ON = 1; // Start TMR2 from 0 when a low to high data pulse
while(Data); // is detected, and wait until it falls low again.
T2CON.TMR2ON = 0; // Stop the TMR2 when the data pulse falls low.
if(TMR2 > 60) num |= 1<<(7-i); // If time > 60us, Data is 1
}
return num;
}
void interrupt(){
if(PIR1.TMR2IF){
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF = 0; // Clear TMR0 interrupt flag
}
}
void main() {
unsigned short check;
TRISB = 0b00001000;
PORTB = 0;
J=0;
K=0;
INTCON.GIE = 1; //Enable global interrupt
INTCON.PEIE = 1; //Enable peripheral interrupt
// Configure Timer2 module
PIE1.TMR2IE = 1; // Enable Timer2 interrupt
T2CON = 1; // Prescaler 1:4, and Timer2 is off initially
PIR1.TMR2IF =0; // Clear TMR INT Flag bit
TMR2 = 3;
Lcd_Init();
Lcd_Cmd(_Lcd_Clear);
Lcd_Cmd(_LCD_CURSOR_OFF);
do{
Delay_ms(2000);
StartSignal();
check = CheckResponse();
if (!check) {
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(3, 1, "No response");
Lcd_Out(4, 1, "from the sensor");
J=J++ ;
}
else{
RH_Byte1 = ReadByte();
RH_Byte2 = ReadByte();
T_Byte1 = ReadByte();
T_Byte2 = ReadByte();
CheckSum = ReadByte();
// Check for error in Data reception
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
Lcd_Cmd(_Lcd_Clear);
delay_ms(50);
RH=(RH_Byte1*256)+RH_Byte2;
T=(T_Byte1*256)+T_Byte2;
message1[12] = 223; // Degree symbol
message1[8]=T/100 +48;
T=T%100;
message1[9]= T/10 +48,
message1[11]=T%10 +48;
message2[8]=RH/100 +48;
RH=RH%100;
message2[9]= RH/10 +48,
message2[11]= RH%10 +48;
delay_ms(10);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
K=K++;
}
else{
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(3, 1, "Checksum Error!");
Lcd_Out(4, 1, "Trying Again ...");
}
}
}while(1);
}