|
1 |
| -#include <SPI.h> |
| 1 | +#include <ldc.h> |
2 | 2 |
|
3 |
| -#define LDC1000_CMD_REVID 0x00 |
4 |
| -#define LDC1000_CMD_RPMAX 0x01 |
5 |
| -#define LDC1000_CMD_RPMIN 0x02 |
6 |
| -#define LDC1000_CMD_SENSORFREQ 0x03 |
7 |
| -#define LDC1000_CMD_LDCCONFIG 0x04 |
8 |
| -#define LDC1000_CMD_CLKCONFIG 0x05 |
9 |
| -#define LDC1000_CMD_THRESHILSB 0x06 |
10 |
| -#define LDC1000_CMD_THRESHIMSB 0x07 |
11 |
| -#define LDC1000_CMD_THRESLOLSB 0x08 |
12 |
| -#define LDC1000_CMD_THRESLOMSB 0x09 |
13 |
| -#define LDC1000_CMD_INTCONFIG 0x0A |
14 |
| -#define LDC1000_CMD_PWRCONFIG 0x0B |
15 |
| -#define LDC1000_CMD_STATUS 0x20 |
16 |
| -#define LDC1000_CMD_PROXLSB 0x21 |
17 |
| -#define LDC1000_CMD_PROXMSB 0x22 |
18 |
| -#define LDC1000_CMD_FREQCTRLSB 0x23 |
19 |
| -#define LDC1000_CMD_FREQCTRMID 0x24 |
20 |
| -#define LDC1000_CMD_FREQCTRMSB 0x25 |
| 3 | +dht11 DHT11; |
21 | 4 |
|
22 |
| -#define RPMAX 0x13 /**< maximum calibration value for RPMAX */ |
23 |
| -#define RPMIN 0x3A /**< minimum calibration value for RPMAX */ |
24 |
| - |
25 |
| -unsigned char proximtyData[2]; |
26 |
| -unsigned char frequencyData[3]; |
27 |
| - |
28 |
| -unsigned int proximtyDataMAX; |
29 |
| -unsigned int frequencyDataMAX; |
30 |
| - |
31 |
| -unsigned int proximtyDataTEMP; |
32 |
| -unsigned int frequencyDataTEMP; |
33 |
| - |
34 |
| -const int chipSelectPin = 53; |
35 |
| - |
36 |
| -static byte txlen; |
37 |
| -static byte rxlen; |
38 |
| -static byte *txbuf; |
39 |
| -static byte *rxbuf; |
40 |
| -static byte txaddr; |
41 |
| -static byte wordbuf; |
42 |
| -void setup() { |
43 |
| - byte orgVal[20]; |
44 |
| - int i; |
45 |
| - // set the slaveSelectPin as an output: |
46 |
| - pinMode (chipSelectPin, OUTPUT); |
47 |
| - // initialize SPI: |
48 |
| - Serial.begin(9600); |
49 |
| - |
50 |
| - // start the SPI library: |
51 |
| - SPI.begin(); |
52 |
| - SPI.begin(); |
53 |
| - SPI.setDataMode(SPI_MODE0); |
54 |
| - SPI.setBitOrder(MSBFIRST); |
55 |
| - pinMode(chipSelectPin, OUTPUT); |
56 |
| - delay(100); |
57 |
| - spi_writeByte(LDC1000_CMD_RPMAX, RPMAX); |
58 |
| - spi_writeByte(LDC1000_CMD_RPMIN, RPMIN); |
59 |
| - spi_writeByte(LDC1000_CMD_SENSORFREQ, 0x94); |
60 |
| - spi_writeByte(LDC1000_CMD_LDCCONFIG, 0x17); |
61 |
| - spi_writeByte(LDC1000_CMD_CLKCONFIG, 0x02); |
62 |
| - spi_writeByte(LDC1000_CMD_INTCONFIG, 0x02); |
63 |
| - |
64 |
| - spi_writeByte(LDC1000_CMD_THRESHILSB, 0x50); |
65 |
| - spi_writeByte(LDC1000_CMD_THRESHIMSB, 0x14); |
66 |
| - spi_writeByte(LDC1000_CMD_THRESLOLSB, 0xC0); |
67 |
| - spi_writeByte(LDC1000_CMD_THRESLOMSB, 0x12); |
68 |
| - |
69 |
| - spi_writeByte(LDC1000_CMD_PWRCONFIG, 0x01); |
70 |
| - |
71 |
| - //read all registers |
72 |
| - |
73 |
| - spi_readBytes(LDC1000_CMD_REVID, &orgVal[0],12); |
74 |
| - for(i=0;i<12;i++) |
75 |
| - { |
76 |
| - Serial.print("orgVal="); |
77 |
| - Serial.print(orgVal[i],HEX); |
78 |
| - Serial.print("\n"); |
79 |
| - } |
80 |
| -} |
81 |
| - |
82 |
| -void loop() { |
83 |
| - spi_readBytes(LDC1000_CMD_PROXLSB,&proximtyData[0],2); |
84 |
| - spi_readBytes(LDC1000_CMD_FREQCTRLSB,&frequencyData[0],3); |
85 |
| - /* Serial.print("pro[0]="); |
86 |
| - Serial.print(proximtyData[0],DEC); |
87 |
| - Serial.print(" "); |
88 |
| - Serial.print("pro[1]="); |
89 |
| - Serial.print(proximtyData[1],DEC); |
90 |
| - Serial.print("\n");*/ |
91 |
| - proximtyDataTEMP = ((unsigned char)proximtyData[1]<<8) + proximtyData [0]; |
92 |
| - frequencyDataTEMP = ((unsigned char)frequencyData[1]<<8) + frequencyData[0]; |
93 |
| - Serial.print("proximty="); |
94 |
| - Serial.print(proximtyDataTEMP,DEC); |
95 |
| - Serial.print(" "); |
96 |
| - Serial.print("proximty="); |
97 |
| - Serial.print(proximtyDataTEMP,DEC); |
98 |
| - Serial.print("\n"); |
99 |
| - |
100 |
| -} |
101 |
| - |
102 |
| -byte spi_writeByte(byte addr, byte data) |
| 5 | +void setup() |
103 | 6 | {
|
104 |
| - wordbuf = data; // copy from stack to memory |
105 |
| - txbuf = &wordbuf; |
106 |
| - txaddr = addr & ~0x80; |
107 |
| - digitalWrite(chipSelectPin,LOW); //P1OUT &= ~BIT0; |
108 |
| - SPI.transfer(txaddr);//while (!(IFG2&UCA0TXIFG)); |
109 |
| - //UCA0TXBUF = txaddr; |
110 |
| - //while (!(IFG2&UCA0TXIFG)); |
111 |
| - //UCA0TXBUF = *txbuf; |
112 |
| - SPI.transfer(data); |
113 |
| - digitalWrite(chipSelectPin,HIGH); //P1OUT |= BIT0; |
114 |
| - return 0; |
| 7 | + Serial.begin(9600); |
| 8 | + Serial.println("DHT11 TEST PROGRAM "); |
| 9 | + Serial.print("LIBRARY VERSION: "); |
| 10 | + Serial.println(DHT11LIB_VERSION); |
| 11 | + Serial.println(); |
115 | 12 | }
|
116 | 13 |
|
117 |
| -byte spi_readByte( byte addr, byte data) |
| 14 | +void loop() |
118 | 15 | {
|
119 |
| - //rxbuf = data; |
120 |
| - txaddr = addr | 0x80; |
121 |
| - digitalWrite(chipSelectPin,LOW);//P1OUT &= ~BIT0; |
122 |
| - SPI.transfer(txaddr);//while (!(IFG2&UCA0TXIFG)); |
123 |
| - //UCA0TXBUF = txaddr; |
124 |
| - //while (!(IFG2&UCA0TXIFG)); |
125 |
| - //UCA0TXBUF = 0; |
126 |
| - //while (UCA0STAT & UCBUSY); |
127 |
| - data = SPI.transfer(0x00);//* rxbuf = UCA0RXBUF; |
128 |
| - //while (UCA0STAT & UCBUSY); |
129 |
| - digitalWrite(chipSelectPin,HIGH);//P1OUT |= BIT0; |
130 |
| - return 0; |
| 16 | + Serial.println("\n"); |
| 17 | + |
| 18 | + int chk = DHT11.read(DHT11PIN); |
| 19 | + |
| 20 | + Serial.print("Read sensor: "); |
| 21 | + switch (chk) |
| 22 | + { |
| 23 | + case DHTLIB_OK: |
| 24 | + Serial.println("OK"); |
| 25 | + break; |
| 26 | + case DHTLIB_ERROR_CHECKSUM: |
| 27 | + Serial.println("Checksum error"); |
| 28 | + break; |
| 29 | + case DHTLIB_ERROR_TIMEOUT: |
| 30 | + Serial.println("Time out error"); |
| 31 | + break; |
| 32 | + default: |
| 33 | + Serial.println("Unknown error"); |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + Serial.print("Humidity (%): "); |
| 38 | + Serial.println((float)DHT11.humidity, 2); |
| 39 | + |
| 40 | + Serial.print("Temperature (oC): "); |
| 41 | + Serial.println((float)DHT11.temperature, 2); |
| 42 | + |
| 43 | + Serial.print("Temperature (oF): "); |
| 44 | + Serial.println(Fahrenheit(DHT11.temperature), 2); |
| 45 | + |
| 46 | + Serial.print("Temperature (K): "); |
| 47 | + Serial.println(Kelvin(DHT11.temperature), 2); |
| 48 | + |
| 49 | + Serial.print("Dew Point (oC): "); |
| 50 | + Serial.println(dewPoint(DHT11.temperature, DHT11.humidity)); |
| 51 | + |
| 52 | + Serial.print("Dew PointFast (oC): "); |
| 53 | + Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity)); |
| 54 | + |
| 55 | + delay(2000); |
131 | 56 | }
|
132 |
| -byte spi_readBytes( byte addr, byte * buffer, byte len) |
133 |
| -{ |
134 |
| - int rxlen; |
135 |
| - rxlen = len; |
136 |
| - rxbuf = buffer; |
137 |
| - txaddr = addr | 0x80; |
138 |
| - digitalWrite(chipSelectPin,LOW);//P1OUT &= ~BIT0; |
139 |
| - //while (!(IFG2&UCA0TXIFG)); |
140 |
| - SPI.transfer(txaddr);//UCA0TXBUF = txaddr; |
141 |
| - while (rxlen > 0) { |
142 |
| - //SPI.transfer(0x00); |
143 |
| - *rxbuf = SPI.transfer(0x00); |
144 |
| - rxbuf++; |
145 |
| - rxlen--; |
146 |
| - } |
147 |
| - digitalWrite(chipSelectPin,HIGH); |
148 |
| - return 0; |
149 |
| - } |
150 |
| - |
0 commit comments