• Announcements

    • bluehash

      Forum Upgrade   03/11/2017

      Hello Everyone, Thanks for being patient while the forums were being fixed and upgraded. Please see details and report issues in this thread. Thanks!
Sign in to follow this  
Followers 0
albertRM

Readding DHT11

6 posts in this topic

Hi all i'm programming by MSP430fr5739 a temperature and humidity reading by the DHT11 sensor.
?The wire are correct, and so the problem is'nt in the link, it is in the code.

This is my code, usind the dht11 library, find on internet.
?The main problem is that i don't know how to read the value of T and H, and maybe there is some problems in the reading (probably in the interrupt)
?Thanks for the advice.
?MAIN CODE:

 

#include <msp430.h>
#include <DHT11_LIB.h>
 
unsigned char RH_byte1;
unsigned char RH_byte2;
unsigned char T_byte1;
unsigned char T_byte2;
unsigned char checksum;
unsigned char Packet[5];
unsigned char volatile TOUT;
unsigned char volatile SECOND_TIMER=0;
void init(void);
 
#pragma vector = TIMER0_A0_VECTOR
__interrupt void CCR0_ISR(void){
  SECOND_TIMER++;
  TOUT=1;
  //TOG (P1OUT,0x01); per il led
  CLR (TA0CCTL0, CCIFG);
}
 
void init(){
 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

 PJDIR  = BIT1+BIT2+BIT3;    // Set LED to output direction
 PJOUT  = BIT1;              //high the  LED
 PJOUT  = BIT2;
 //Set clock and timer
 CSCTL1 = BIT1+BIT2 ;    //  DCO a 8MHz
 CSCTL2= SELS_3;              //  SMCLK take DCOCLK
 TA0CCR0 = 50000;    // Initialize the timer to count at 20Hz
  TA0CTL = TAIE_1;    // Enable interrupt
 TA0CTL = TASSEL_2 + ID_3 + MC_1+ TACLR ; // SMCLK, div 8, up mode, e fa il clear
 _enable_interrupt();      //Enable global interrupt

}

void main(void) {
 init();
 while(1){
  //Must wait 1 second initially and between all reads
  if(SECOND_TIMER >= 5){
   TOG (PJOUT,BIT1);
   // Simple way to gather all data with one instruction
   read_Packet(Packet);
   RH_byte1 = Packet[0];
   RH_byte2 = Packet[1];
   T_byte1 = Packet[2];
   T_byte2 = Packet[3];
   checksum = Packet[4];
   if (check_Checksum(Packet))
    SET (PJOUT, BIT1);    //Se il checksum giusto si riaccende
   SET (TA0CTL, TACLR);
   SET (TA0CTL, 0x10);      //Up mode
   TA0CCR0 = 50000;  //Initialize the timer to count at 5Hz
   SECOND_TIMER = 0; //Clear counter
  }
 }
}
 
DHT11 LIBRARY:
#ifndef DHT11_LIB_H_
#define DHT11_LIB_H_
#define TST(x,y) (x & (y))
#define SET(x,y) (x|= (y))
#define CLR(x,y) (x &= ~(y))
#define TOG(x,y) (x ^= (y))
#define DPIN BIT0
extern unsigned char volatile TOUT;
void start_Signal(void);

void start_Signal(){
 SET(P2DIR, DPIN);  // Set Data pin to output direction
 CLR(P2OUT,DPIN);   // Set output to low
 
 __delay_cycles(25000);  // Low for at least 18ms
 
  SET(P2OUT,DPIN);       //HIGH
  
    __delay_cycles(30);  // High for at 20us-40us
 
 CLR(P2DIR,DPIN);  // Mette il pin in ricezione (Input)
}
 

unsigned char check_Response(void);
unsigned char check_Response(){
   
 TOUT=0;
 SET(TA0CTL,TACLR);  //Reset timer to 0;
 TA0CCR0 = 100;  //Set timer to overflow in 100uS.
 SET(TA0CTL,CCIE);  //And enable timer interrupt
 while(!(TST(P2IN,DPIN)) && !TOUT); //while if IN=0
 if (TOUT)
  return 0;
 else {
  SET(TA0CTL,TACLR);
  SET(TA0CTL,CCIE);
  while((TST(P2IN,DPIN)) && !TOUT);
                      
  if(TOUT)
   return 0;
  else{
   CLR(TA0CTL,CCIE); // Disable timer interrupt
   return 1;
  }
 }
}
 

unsigned char read_Byte(void);
unsigned char read_Byte(){
 TOUT = 0;
 unsigned char num = 0;
 unsigned char i;
 CLR(TA0CTL,CCIE);
 for (i=8; i>0; i--){
  while(!(TST(P2IN,DPIN))); //wait the hing signal
  SET(TA0CTL,TACLR); // clear
  SET(TA0CTL,0x10); //Up mode (Mode control 01b)
  SET(TA0CTL,CCIE); //enable interrupt
  while(TST(P2IN,DPIN)); //wait the low signal
  CLR(TA0CTL,0x30); //held counter
  if (TA0R > 0x28)  //TA0R

Share this post


Link to post
Share on other sites

@@albertRM What is your specific problem? We can't help unless you let us know which pieces of your program work and what the symptoms of your issue are.

 

This is the thread on this forum that our ever helpful @@Rei Vilo probably is referring to:

http://forum.43oh.com/topic/1825-interfacing-with-dht11-humidty-temp-sensor/

 

At a quick glance, I'd first try to get the sensor reading to work without involving a timer interrupt for timeouts.

Share this post


Link to post
Share on other sites

@@chicken hi!! i don't know the exatly problem but can you tell me is the clock's setting are right?
CSCTL1 = BIT1+BIT2 ;    //  DCO a 8MHz
 CSCTL2= SELS_3;              //  SMCLK take DCOCLK
 TA0CCR0 = 50000;    // Initialize the timer to count at 20Hz
  TA0CTL = TAIE_1;    // Enable interrupt
 TA0CTL = TASSEL_2 + ID_3 + MC_1+ TACLR ; // SMCLK, div 8, up mode, e fa il clear
?
Another problem is how can i read the value (to check )?

Share this post


Link to post
Share on other sites

As mentioned above, I'd focus first on communicating with the sensor before going into interrupts and timers.

 

You can verify the clock settings by looking up the registers in the Family User Guide of your MCU.

http://www.ti.com/product/MSP430FR5739/technicaldocuments#doctype6

Looking at the intro of chapter 3.4, it looks like you may need to write a password into CSCTL0 before you can write to the other registers of the clock system.

 

You should also learn to use the debugger in CCS. It allows to execute your program step by step and looking at register values etc.

http://processors.wiki.ti.com/index.php/GSG:Debugging_projects_v5#Launching_the_debugger

Share this post


Link to post
Share on other sites

PS: Setting up clock systems can be quite complex, especially with the newer MCUs. Take a look into MSP430Ware which should come installed with CCS. It contains convenient functions to setup your clocks without having to worry too much about registers.

 

Looks like this tutorial on getting started with CCS and MSP430 Launchpads uses MSP430Ware.

http://processors.wiki.ti.com/index.php/Getting_Started_with_the_MSP430_LaunchPad_Workshop

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0