Jump to content

Recommended Posts

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.


#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){
  //TOG (P1OUT,0x01); per il led
void init(){
 WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

 PJDIR  = BIT1+BIT2+BIT3;    // Set LED to output direction
 PJOUT  = BIT1;              //high the  LED
 //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) {
  //Must wait 1 second initially and between all reads
  if(SECOND_TIMER >= 5){
   // Simple way to gather all data with one instruction
   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, 0x10);      //Up mode
   TA0CCR0 = 50000;  //Initialize the timer to count at 5Hz
   SECOND_TIMER = 0; //Clear counter
#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(){
 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 {
  while((TST(P2IN,DPIN)) && !TOUT);
   return 0;
   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;
 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
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:



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

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 )?

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.


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.


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.


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...