📄 sht11_test.c
字号:
/*
* sht11_test.c
*
* Copyright(C) 2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; with out even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include "regs-gpio.h" // kernel/include/asm/arch/regs-gpio.h
#define LOW 0
#define HIGH 1
#define ADC_READ 0
#define ADC_INIT 1
#define SET_SCK 2 // LOW HIGH
#define SET_SDA 3 // LOW HIGH
#define SET_SDA_DIR 4 // IN OUT
#define GET_SDA 5 // LOW HIGH
#define SHT11_INIT 6
#define INPUT 0
#define OUTPUT 1
#define ACK 0
#define NOACK 1
#define MEASURE_TEMPERATURE 3
#define MEASURE_HUMIDITY 5
#define SHT11_SCK S3C2410_GPE14
#define SHT11_SCK_INP S3C2410_GPE14_INP
#define SHT11_SCK_OUTP S3C2410_GPE14_OUTP
#define SHT11_SDA S3C2410_GPE15
#define SHT11_SDA_INP S3C2410_GPE15_INP
#define SHT11_SDA_OUTP S3C2410_GPE15_OUTP
#define GPIODRV_IOC_MAGIC 'g'
#define GPIO_SET_CFG _IOW(GPIODRV_IOC_MAGIC,0,struct gpio)
#define GPIO_SET_PIN _IOW(GPIODRV_IOC_MAGIC,1,struct gpio)
#define GPIO_GET_PIN _IOW(GPIODRV_IOC_MAGIC,2,struct gpio)
struct gpio {
unsigned int v1;
unsigned int v2;
};
struct gpio GPIO;
int fd;
void gpio_set_cfg (unsigned int pin, unsigned int mode)
{
GPIO.v1 = pin;
GPIO.v2 = mode;
ioctl(fd, GPIO_SET_CFG, &GPIO);
}
void gpio_set_pin (unsigned int pin, unsigned int value)
{
GPIO.v1 = pin;
GPIO.v2 = value;
ioctl(fd, GPIO_SET_PIN, &GPIO);
}
unsigned int gpio_get_pin (unsigned int pin)
{
GPIO.v1 = pin;
GPIO.v2 = 0;
ioctl(fd, GPIO_GET_PIN, &GPIO);
return (GPIO.v2);
}
/* ioctl for sht11 */
void init_sht11()
{
// ioctl(fd, SHT11_INIT, NULL);
gpio_set_cfg(SHT11_SDA, SHT11_SDA_INP);
gpio_set_cfg(SHT11_SCK, HIGH);
gpio_set_cfg(SHT11_SDA, HIGH);
}
void set_sck(int val)
{
int i = 0x10;
//ioctl(fd, SET_SCK, &val);
gpio_set_cfg(SHT11_SCK, (unsigned int)val);
while(i--);
}
void set_sda_dir(int val)
{
//ioctl(fd, SET_SDA_DIR, &val);
if( val == INPUT )
gpio_set_cfg(SHT11_SDA, SHT11_SDA_INP);
else
gpio_set_cfg(SHT11_SDA, SHT11_SDA_OUTP);
}
void set_sda(int val)
{
//ioctl(fd, SET_SDA, &val);
gpio_set_cfg(SHT11_SDA, (unsigned int)val);
}
int get_sda(void)
{
//return ioctl(fd,GET_SDA,NULL);
return (int)gpio_get_pin(SHT11_SDA);
}
void sht11_start()
{
set_sda_dir(OUTPUT);
set_sda(HIGH);
set_sck(LOW);
set_sck(HIGH);
set_sda(LOW);
set_sck(LOW);
set_sck(HIGH);
set_sda(HIGH);
set_sck(LOW);
}
unsigned char set_cmd(unsigned char cmd)
{
unsigned char i, ret;
unsigned char buff = cmd;
// send command
set_sda_dir(OUTPUT);
for(i = 0; i < 8; i++)
{
if(buff & 0x80)
{
set_sda(HIGH);
set_sck(HIGH);
set_sck(LOW);
}
else
{
set_sda(LOW);
set_sck(HIGH);
set_sck(LOW);
}
buff <<= 1 ;
}
set_sda(HIGH);
set_sck(HIGH);
set_sda_dir(INPUT);
ret = get_sda();
set_sck(LOW);
return ret;
}
unsigned char get_msg(int ack)
{
int i;
unsigned char buff = 0;
set_sda(HIGH);
set_sda_dir(INPUT);
for(i = 0; i < 8; i++)
{
buff <<= 1;
set_sck(HIGH);
buff |= get_sda();
set_sck(LOW);
}
set_sda_dir(OUTPUT);
if ( ack == ACK )
set_sda(LOW);
set_sck(HIGH);
set_sck(LOW);
set_sda(HIGH);
return buff;
}
unsigned int sht11_command(unsigned char cmd)
{
int i;
unsigned char ret, msb, lsb, crc;
sht11_start();
ret = set_cmd(cmd);
if( ret != 0 )
{
printf("! command send fail!\n");
return 0;
}
for(i = 0; i < 100000; i++)
{
if( get_sda() == 0 )
{
msb = get_msg(ACK);
lsb = get_msg(ACK);
//crc = get_msg(NOACK);
return (msb << 8) | lsb;
}
}
return 0; // may be timed out
}
unsigned int sht11_temp()
{
return sht11_command(MEASURE_TEMPERATURE);
}
unsigned int sht11_humidity()
{
return sht11_command(MEASURE_HUMIDITY);
}
int main()
{
unsigned rh;
fd = open( "/dev/gpio", O_RDWR);
if(fd < 0 )
{
printf("! /dev/gpio open error\n");
return -1;
}
init_sht11();
printf("- %d, %d\n", sht11_temp(), sht11_humidity());
#if 0
printf("Temperature: %u degrees Celsius\n",
(unsigned)(-39.60 + 0.01 * sht11_temp()));
rh = sht11_humidity();
printf("Rel. humidity: %u%%\n",
(unsigned) (-4 + 0.0405*rh - 2.8e-6*(rh*rh)));
#endif
close(fd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -