어딜 좀 더 수정해야하려나..
1. 거리가 짧아지면 응답이 빨리 되니 짧을때는 더 빠르게 거리 측정
2. openMP나 thread이용 병렬처리
3. 클래스화?
4. 인터럽트 사용?
$ cat temp_ss.c /* * dht11.c: * Simple test program to test the wiringPi functions * DHT11 test */ #include <wiringpi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/time.h> #define TRIG_PIN 4 #define ECHO_PIN 5 float mach = 34300.0; void read_sr04_dat() { int counter = 0; struct timeval start_point, end_point; double operating_time; double len; pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); digitalWrite(TRIG_PIN, LOW); // delay(500); // 500 msec digitalWrite(TRIG_PIN, HIGH); delayMicroseconds( 10 ); digitalWrite(TRIG_PIN, LOW); while ( digitalRead(ECHO_PIN) == LOW) { counter++; delayMicroseconds( 1 ); if ( counter == 38000 ) { return; } } gettimeofday(&start_point, NULL); while ( digitalRead(ECHO_PIN) == HIGH) { counter++; delayMicroseconds( 1 ); if ( counter == 38000 ) { return; } } gettimeofday(&end_point, NULL); operating_time = (double)(end_point.tv_sec)+(double)(end_point.tv_usec)/1000000.0-(double)(start_point.tv_sec)-(double)(start_point.tv_usec)/1000000.0; len = operating_time * mach / 2; printf("%f sec %4.2f cm %d\n",operating_time, len, counter); } #define MAXTIMINGS 85 #define DHTPIN 7 int dht11_dat[5] = { 0, 0, 0, 0, 0 }; void read_dht11_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; float f; /* fahrenheit */ dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0; /* pull pin down for 18 milliseconds */ pinMode( DHTPIN, OUTPUT ); digitalWrite( DHTPIN, LOW ); delay( 18 ); /* then pull it up for 40 microseconds */ digitalWrite( DHTPIN, HIGH ); delayMicroseconds( 40 ); /* prepare to read the pin */ pinMode( DHTPIN, INPUT ); /* detect change and read data */ for ( i = 0; i < MAXTIMINGS; i++ ) { counter = 0; while ( digitalRead( DHTPIN ) == laststate ) { counter++; delayMicroseconds( 1 ); if ( counter == 255 ) { break; } } laststate = digitalRead( DHTPIN ); if ( counter == 255 ) break; /* ignore first 3 transitions */ if ( (i >= 4) && (i % 2 == 0) ) { /* shove each bit into the storage bytes */ dht11_dat[j / 8] <<= 1; if ( counter > 16 ) dht11_dat[j / 8] |= 1; j++; } } /* * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte * print it out if data is good */ if ( (j >= 40) && (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) ) { f = dht11_dat[2] * 9. / 5. + 32; printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f ); printf("%f -> ",mach); mach = 33150 + (60 * dht11_dat[2]); printf("%f\n",mach); }else { f = dht11_dat[2] * 9. / 5. + 32; printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F) ", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f ); printf( "- Data not good, skip j[%d] par[%d] calc[%d]\n", j, dht11_dat[4], (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF ); } } int main( void ) { unsigned char tempcount = 0; if ( wiringPiSetup() == -1 ) exit( 1 ); while ( 1 ) { if(tempcount % 10 == 0) read_dht11_dat(); tempcount++; read_sr04_dat(); delay( 1000 ); /* wait 1sec to refresh */ } return(0); } |
'embeded > raspberry pi' 카테고리의 다른 글
freeRTOS on rpi (0) | 2016.07.18 |
---|---|
redmine 백업 (0) | 2016.07.14 |
초음파 센서 wiring pi 버전 (0) | 2016.07.10 |
초음파/온습도 센서 데이터 시트 복사 (0) | 2016.07.10 |
rpi gstreamer mux (0) | 2016.06.28 |