This commit is contained in:
shuixx 2019-06-20 22:00:35 +08:00
parent d0c34bf05c
commit 55cc678a7c
3 changed files with 61 additions and 24 deletions

View File

@ -122,7 +122,8 @@ void readDs18b20Data(unsigned char *value)
// local_irq_enable(); // local_irq_enable();
mdelay(750); //mdelay(750);
mdelay(375);
//msleep(750); //msleep(750);
// local_irq_disable(); // local_irq_disable();
@ -145,16 +146,18 @@ void readDs18b20Data(unsigned char *value)
// mdelay(1); // mdelay(1);
// init_DS18B20(); //init_DS18B20();
//write_char(0xB4); //write_char(0xB4);
//DQ_OUT; //DQ_OUT;
//printk("%d: EC:%d", gpio_num,DQ_V); //printk("%d: EC:%d", gpio_num,DQ_V);
//write_char(0xCC);
//write_char(0x4E); //write_char(0x4E);
//write_char(0x64); //write_char(0x64);
//write_char(0x00); //write_char(0x00);
//write_char(0x7F); //write_char(0x5F);
//init_DS18B20(); //init_DS18B20();
//write_char(0xCC);
//write_char(0x48); //write_char(0x48);
} }

1
make.sh Normal file
View File

@ -0,0 +1 @@
gcc -lpthread

47
temp.c
View File

@ -2,17 +2,47 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h> #include <sys/select.h>
#include <pthread.h>
#define N 2
float getTempByDev(char *devName); float getTempByDev(char *devName);
void *run(void *args);
float results[N];
pthread_t tids[N];
char *names[] = {"/dev/ds18b20-223","/dev/ds18b20-224"};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
float t223 = getTempByDev("/dev/ds18b20-223"); int i = 0;
float t224 = getTempByDev("/dev/ds18b20-224"); for (i = 0; i < N; i++)
printf("%.4f#%.4f\n", t223, t224); {
pthread_create(&tids[i], NULL, run, (void*)i);
}
for (i = 0; i < N; i++)
{
pthread_join(tids[i], NULL);
}
for (i = 0; i < N; i++)
{
if (i > 0)
{
printf("#");
}
printf("%.3f", results[i]);
}
printf("\n");
return 0; return 0;
} }
void *run(void *args)
{
int ret=(int)args;
float res = getTempByDev(names[ret]);
results[ret] = res;
}
float getTempByDev(char *devName) float getTempByDev(char *devName)
{ {
int fd; int fd;
@ -20,22 +50,24 @@ float getTempByDev(char *devName)
unsigned int hightBitValue = 0; unsigned int hightBitValue = 0;
unsigned int lowBitValue = 0; unsigned int lowBitValue = 0;
float p = 0.0625; float p = 0.0625;
float value = 1024.0f; float value = 1024;
short count = 0;
fd = open(devName, 0); fd = open(devName, 0);
if (fd >= 0) if (fd >= 0)
{ {
do
{
count++;
int i = read(fd, &result, sizeof(&result)); int i = read(fd, &result, sizeof(&result));
if (i >= 0) if (i >= 0)
{ {
// printf("%xH-%xH\n", result[1], result[0]);
hightBitValue = result[1]; hightBitValue = result[1];
lowBitValue = result[0]; lowBitValue = result[0];
hightBitValue <<= 8; hightBitValue <<= 8;
hightBitValue = hightBitValue + lowBitValue; hightBitValue = hightBitValue + lowBitValue;
if ((result[1] & 0xf8)) if ((result[1] & 0xf8))
{ {
// printf("aaa\n");
hightBitValue = ~hightBitValue + 1; hightBitValue = ~hightBitValue + 1;
value = hightBitValue * p * -1; value = hightBitValue * p * -1;
} }
@ -43,8 +75,9 @@ float getTempByDev(char *devName)
{ {
value = hightBitValue * p; value = hightBitValue * p;
} }
} }
}
while ((value < -55 || value > 125) && count < 5);
close(fd); close(fd);
} }
return value; return value;