9G25 采用控制的 i2c 驱动 ,非GPIO模式
单片机源程序如下:
- //i2c_test.c
- #include<stdio.h>
- #include<linux/types.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<sys/ioctl.h>
- #include<errno.h>
- #include<assert.h>
- #include<string.h>
- #include<linux/i2c.h>
- #include<linux/i2c-dev.h>
- #define CHIP_ADDR_START 0x40
- #define CHIP_NUM 1
- #define BUF_SIZE 32
- #define I2C_DEV "/dev/i2c-1"
- static int read_dth(int fd, unsigned char slave_address, unsigned char reg_addr, unsigned char buff[], int count)
- {
- int ret;
- struct i2c_rdwr_ioctl_data work_queue;
-
- work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
- if(!work_queue.msgs)
- {
- printf("read_dth, Memery alloc error\n");
-
- return -1;
- }
-
- work_queue.nmsgs = 2;
- work_queue.msgs[0].len = 1;
- work_queue.msgs[0].addr = slave_address;
- // work_queue.msgs[0].flags = 0; /* write */
- work_queue.msgs[0].buf= ®_addr;
- work_queue.msgs[1].len= count;
- work_queue.msgs[1].addr= slave_address;
- work_queue.msgs[1].flags= 1; /* read */
- work_queue.msgs[1].buf= buff;
- ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
- if(ret < 0)
- {
- printf("readerror\n");
- }
- else
- {
- printf("read data: %02x%02x from address: %02x\n", buff[0], buff[1], reg_addr);
- }
-
- return ret;
- }
- static int write_dth(int fd, unsigned char slave_address, unsigned char addr, char buff[], int count)
- {
- int ret;
- unsigned char buffer[2];
- struct i2c_rdwr_ioctl_data work_queue;
-
- work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
- if(!work_queue.msgs)
- {
- printf("write_dth, Memery alloc error\n");
-
- return -1;
- }
-
- work_queue.nmsgs = 1;
- work_queue.msgs[0].len = 2;
- work_queue.msgs[0].addr = slave_address;
- work_queue.msgs[0].flags = 0; /* write */
-
- work_queue.msgs[0].buf = buffer;
- work_queue.msgs[0].buf[0]= addr; /* write address */
- work_queue.msgs[0].buf[1]= buff[0]; /* write data */
- ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);
- if(ret < 0)
- {
- printf("writedata error\n");
- }
- else
- {
- printf("writedata: %02x to address: %02x\n", buff[0], addr);
- }
- return ret;
- }
- static int init_one_dth(int fd, unsigned char u8chip_addr)
- {
- int res;
- unsigned char buf[BUF_SIZE];
-
- buf[0] = 0X80;
- buf[1] = 0X00;
- write_dth(fd, u8chip_addr, 0x02, buf, 2);
- usleep(1000000);
-
- buf[0] = 0X10;
- buf[1] = 0X00;
- write_dth(fd, u8chip_addr, 0x02, buf, 2);
- usleep(1000000);
-
- buf[0] = 0X00;
- buf[1] = 0X00;
- buf[2] = 0X00;
- buf[3] = 0X00;
- buf[4] = 0X00;
- buf[5] = 0X00;
- read_dth(fd, u8chip_addr, 0xfb, buf, 2);
- read_dth(fd, u8chip_addr, 0xfc, buf + 2, 2);
- read_dth(fd, u8chip_addr, 0xfd, buf + 4, 2);
- printf("DTH%u sensor ID = 0x%02x%02x%02x%02x%02x%02x\r\n", (unsigned int)(u8chip_addr - CHIP_ADDR_START), buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
- }
- int init_dth(void)
- {
- int fd;
- int res;
- unsigned char i;
-
- fd = open(I2C_DEV,O_RDWR);
- if(fd < 0 )
- {
- printf("Can't Open %s !!!\r\n\r\n",I2C_DEV);
- return -1;
- }
-
- res = ioctl(fd, I2C_RETRIES, 10);
- res = ioctl(fd, I2C_TENBIT, 0);
- for(i = 0; i < CHIP_NUM; i++)
- {
- init_one_dth(fd, (CHIP_ADDR_START + i));
- }
-
- printf("\r\n\r\n");
-
- return fd;
- }
- int sample_dth(int fd, float *fdata)
- {
- unsigned char i;
- int res;
- int rres[2];
- unsigned char buf[BUF_SIZE];
-
- float temper, humidity;
-
- for(i = 0; i < CHIP_NUM; i++)
- {
- buf[0] = 0X00;
- write_dth(fd, (CHIP_ADDR_START + i), 0x00, buf, 1);
- }
-
- usleep(500000);
-
- for(i = 0; i < CHIP_NUM; i++)
- {
- buf[0] = 0X00;
- buf[1] = 0X00;
- buf[2] = 0X00;
- buf[3] = 0X00;
- rres[0] = read_dth(fd, (CHIP_ADDR_START + i), 0x00, buf, 2);
- rres[1] = read_dth(fd, (CHIP_ADDR_START + i), 0x01, buf + 2, 2);
-
- if(1) // if(2 == rres[0])
- {
- temper = ((float)(buf[0] * 0x100 + buf[1]) / 65536.0 * 165.0 - 40.0);
- }
- else
- {
- temper = -273.0;
- }
-
- if(1) // if(2 == rres[1])
- {
- humidity = ((float)(buf[2] * 0x100 + buf[3]) / 65536.0 * 100.0);
- }
- else
- {
- humidity = 125.0;
- }
-
- fdata[i * 2 + 0] = temper;
- fdata[i * 2 + 1] = humidity;
-
- printf("temperat[%u] = %.1f\r\n", (unsigned int)i, temper);
- printf("humidity[%u] = %.1f%%\r\n", (unsigned int)i, humidity);
- }
-
- }
- int main(void)
- {
- int fd;
- float fdata[4][2];
-
- fd = init_dth();
-
- while(1)
- {
- sample_dth(fd, &fdata[0][0]);
- printf("\r\n");
- usleep(1000000);
- }
- close(fd);
- return 0;
- }
复制代码
所有资料51hei提供下载:
i2c.zip
(7.56 KB, 下载次数: 4)
|