Repository navigation
Expand file tree
/
Copy pathMSP430_DHT11_Sensor
More file actions
103 lines (102 loc) · 3.08 KB
/
Copy pathMSP430_DHT11_Sensor
File metadata and controls
103 lines (102 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
volatile int temp[50];
volatile int diff[50];
volatile unsigned int i=0;
volatile unsigned int j=0;
char th_char[5];
char tl_char[5];
char hh_char[5];
char hl_char[5];
volatile int hh = 0;
volatile int hl = 0;
volatile int th = 0;
volatile int tl = 0;
volatile int check = 0;
volatile int checksum = 0;
volatile int dataok;
char temperature[] = "Temperature is: ";
char dot[] = ".";
char celcius[] = " degrees Celcius ";
char humidity[] = "Humidity is: ";
char percent[] = " %\r\n";
void ser_output(char *str);
void main(void)
{
WDTCTL = WDTPW | WDTHOLD;
BCSCTL1= CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1|BIT2;
P1SEL2 = BIT1|BIT2;
UCA0CTL1 |= UCSWRST+UCSSEL_2;
UCA0BR0 = 52;
UCA0BR1 = 0;
UCA0MCTL = UCBRS_0;
UCA0CTL1 &= ~UCSWRST;
__delay_cycles(2000000);
P2DIR |= BIT4;
P2OUT &= ~BIT4;
__delay_cycles(20000);
P2OUT |= BIT4;
__delay_cycles(20);
P2DIR &= ~BIT4;
P2SEL |= BIT4;
TA1CTL = TASSEL_2|MC_2 ;
TA1CCTL2 = CAP | CCIE | CCIS_0 | CM_2 | SCS ;
_enable_interrupts();
while (1){
if (i>=40){
for (j = 1; j <= 8; j++){
if (diff[j] >= 110)
hh |= (0x01 << (8-j));
}
for (j = 9; j <= 16; j++){
if (diff[j] >= 110)
hl |= (0x01 << (16-j));
}
for (j = 17; j <= 24; j++){
if (diff[j] >= 110)
th |= (0x01 << (24-j));
}
for (j = 25; j <= 32; j++){
if (diff[j] >= 110)
tl |= (0x01 << (32-j));
}
for (j = 33; j<=40; j++){
if (diff[j] >= 110)
checksum |= (0x01 << (40-j));
}
check=hh+hl+th+tl;
if (check == checksum)
dataok = 1;
else
dataok = 0;
//if you get an error with ltoa, try: 'ltoa(th,th_char,10)'
//check article:
//https://software-dl.ti.com/ccs/esd/documents/sdto_cgt_handling_changes_in_ltoa.html
ltoa(th,th_char);
ltoa(tl,tl_char);
ltoa(hh,hh_char);
ltoa(hl,hl_char);
ser_output(temperature); ser_output(th_char); ser_output(dot); ser_output(tl_char); ser_output(celcius);
ser_output(humidity); ser_output(hh_char); ser_output(dot); ser_output(hl_char); ser_output(percent);
__delay_cycles(1000000);
WDTCTL = WDT_MRST_0_064;
}
}
}
#pragma vector = TIMER1_A1_VECTOR
__interrupt void Timer_A1(void){
temp[i] = TA1CCR2;
i += 1;
TA1CCTL2 &= ~CCIFG ;
if (i>=2) diff[i-1]=temp[i-1]-temp[i-2];
}
void ser_output(char *str){
while(*str != 0){
while (!(IFG2&UCA0TXIFG));
UCA0TXBUF = *str++;
}
}