AMukhida
Newbie level 2
Hello all. I just joined the forum and was hoping you all could help me with some problems I've been having. I'm trying to interface a DHT11 with a PIC16f690 and display the results on an LCD. I'm not fully done with the code, but I think I was far enough to display the temperature on the LCD. Anyway, I tested my program out today and I didn't get anything. I'm pretty sure the problem has to do something with the start signal since I tracked RA0 (the pin that is connected to the data line of the DHT11) using an oscilloscope and just got a constant high voltage. The start signal is supposed to make RA0 low for 20ms, then high for 30us, then it gets configured as an input and waits for the response signal from the DHT11. I'm pretty sure I'm looking over something fairly obvious in my code, but I just can't figure it out. If you could help me out with problem, or anything else you see in my code that would be awesome .
Code:
processor 16f690
include <p16f690.inc>
;Codeprotect off, watchdog off, internal high speed osc, powerup timer on, clear reset off, brown out off
__CONFIG _CP_OFF & _WDT_OFF & _INTOSCIO & _PWRTE_ON & _MCLRE_OFF & _BOR_OFF
CBLOCK 0x20
BYTE
Counter
Integer_RH
Decimal_RH
Integer_T
Decimal_T
Checksum
D1
D2
Dec_Hun
Dec_Ten
Dec_One
RH_Hun
RH_Ten
RH_One
T_Hun
T_Ten
T_One
ENDC
#define LCD_RS PORTB,5
#define LCD_RW PORTB,6
#define LCD_E PORTB,7
#define LED PORTB,4
org 0
Main
;Initialize Oscillator, 4MHz internal
BANKSEL OSCCON ;Bank 1
MOVLW b'01100000' ;Set internal oscillator frequency to 4 MHz
MOVWF OSCCON
CLRF TRISB ;port B as output
CLRF TRISC ;port C as output
BANKSEL ANSEL
CLRF ANSEL ;configure pins as digital
CLRF ANSELH
Start
BANKSEL TRISA
BCF TRISA,0 ;RA0 as output
BANKSEL PORTA
BCF PORTA,0 ;RA0 low
CALL Delay_20ms ;RA0 low for 20ms
BSF PORTA,0 ;RA0 high
CALL Delay_30us ;RA0 high for 30us
BANKSEL TRISA
BSF TRISA,0 ;RA0 as input
BANKSEL PORTA
CALL Delay_160us ;Wait for DHT11 response signal
Read_Integer_RH
CALL Read_BYTE
MOVF BYTE,W
MOVWF Integer_RH ;Move BYTE to Integer_RH
Read_Decimal_RH
CALL Read_BYTE
MOVF BYTE,W
MOVWF Decimal_RH ;Move BYTE to Decimal_RH
Read_Integer_T
CALL Read_BYTE
MOVF BYTE,W
MOVWF Integer_T ;Move BYTE to Integer_T
Read_Decimal_T
CALL Read_BYTE
MOVF BYTE,W
MOVWF Decimal_T ;Move BYTE to Decimal_T
Read_Checksum
CALL Read_BYTE
MOVF BYTE,W
MOVWF Checksum ;Move BYTE to Checksum
;CHECKSUM
;CONVERT BINARY VALUES TO ASCII CHARACTERS TO SEND TO LCD
Integer_RH_to_ASCII
MOVF Integer_RH,W
CALL Convert_to_ASCII
MOVF Dec_Hun,W
MOVWF RH_Hun
MOVF Dec_Ten,W
MOVWF RH_Ten
MOVF Dec_One,W
MOVWF RH_One
Integer_T_to_ASCII
MOVF Integer_T,W
CALL Convert_to_ASCII
MOVF Dec_Hun,W
MOVWF T_Hun
MOVF Dec_Ten,W
MOVWF T_Ten
MOVF Dec_One,W
MOVWF T_One
;SEND VALUES TO LCD
;set interface
MOVLW b'00111000' ;8-bit interface, 2 lines, 5x8 dots
CALL SendCommand
;enable display, cursor
MOVLW b'00001100' ;display on, cursor off, blinking off
CALL SendCommand
;clear and home
MOVLW b'00000001'
CALL SendCommand
;set cursor move direction
MOVLW b'00000110'
CALL SendCommand
MOVF T_Hun,W
CALL SendCharacter
MOVF T_Ten,W
CALL SendCharacter
MOVF T_One,W
CALL SendCharacter
CALL Delay_20ms
GOTO Start
;********************SUBROUTINES*************************************
Read_BYTE
CLRF BYTE ;BYTE=00000000
MOVLW d'7' ;Check_bit 8 times, each time rotating left and updating the LSB
MOVWF Counter
RLF BYTE,f
CALL Check_bit
DECFSZ Counter,f
GOTO $-3
RETURN
Check_bit
CLRF TMR2
BTFSS PORTA,0 ;Tests until RA0 is pulled high
GOTO $-1
BSF T2CON,2 ;Starts TMR2
BTFSC PORTA,0 ;Tests until RA0 is pulled low
GOTO $-1
BCF T2CON,2 ;Stops TMR2
MOVF TMR2,W ;if TMR2>40, bit=1
SUBLW d'40'
BTFSC STATUS,C ;if TMR2>40, STATUS,C will be set
BSF BYTE,0 ;sets BYTE,0 to 1 if STATUS,C=1
BCF STATUS,C ;clears STATUS,C
RETURN
Convert_to_ASCII
MOVWF BYTE
CLRF Dec_Hun
CLRF Dec_Ten
CLRF Dec_One
B2D1
MOVLW d'100' ;move 100 to work
SUBWF BYTE,f ;BYTE-100
BTFSS STATUS,C ;check if negative value
goto B2D2 ;goto next tens place if value was negative
INCF Dec_Hun,f ;add 1 to hundreds
GOTO B2D1 ;repeat until negative
B2D2
MOVLW d'100'
ADDWF BYTE,f ;add 100 back to make it positive
B2D3
MOVLW d'10' ;move 10 to work
SUBWF BYTE,f ;BYTE-10
BTFSS STATUS,C ;check if negative value
goto B2D4 ;goto next tens place if value was negative
INCF Dec_Ten,f ;add 1 to tens
GOTO B2D3 ;repeat until negative
B2D4
MOVLW d'10'
ADDWF BYTE,W ;add 10 back to make it positive and store in work
MOVWF Dec_One ;remainder is the ones place
MOVLW h'30' ;convert all digits to ASCII
ADDWF Dec_Hun,f
MOVLW h'30'
ADDWF Dec_Ten,f
MOVLW h'30'
ADDWF Dec_One,f
RETURN
SendCommand
BANKSEL PORTC
MOVWF PORTC
BCF LCD_RS
BCF LCD_RW
BSF LCD_E
CALL Delay_20ms
BCF LCD_E
return
SendCharacter
BANKSEL PORTC
MOVWF PORTC
BSF LCD_RS
BCF LCD_RW
BSF LCD_E
CALL Delay_20ms
BCF LCD_E
return
Delay_20ms
;19993 cycles
MOVLW 0x9E
MOVWF D1
MOVLW 0x10
MOVWF D2
L0
DECFSZ D1, f
GOTO $+2
DECFSZ D2, f
GOTO L0
;3 cycles
GOTO $+1
NOP
;4 cycles (including call)
RETURN
Delay_30us
MOVLW 0x09
MOVWF D1
L1
DECFSZ D1, f
GOTO L1
RETURN
Delay_160us
MOVLW 0x34
MOVWF D1
L2
DECFSZ D1, f
GOTO L2
NOP
RETURN
END