久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
AT91SAM9G25采用控制的 i2c 驅(qū)動 ,非GPIO模式
[打印本頁]
作者:
hezhanghong
時間:
2018-4-23 16:49
標(biāo)題:
AT91SAM9G25采用控制的 i2c 驅(qū)動 ,非GPIO模式
9G25 采用控制的 i2c 驅(qū)動 ,非GPIO模式
單片機(jī)源程序如下:
//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;
}
復(fù)制代碼
所有資料51hei提供下載:
i2c.zip
(7.56 KB, 下載次數(shù): 4)
2018-4-23 16:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
欧美成人a∨高清免费观看 欧美日韩中
|
一区二区三区免费网站
|
国产在线观看一区二区
|
国产视频一区二区
|
日韩伦理一区二区
|
国产精品久久久久久一区二区三区
|
亚洲精品国产一区
|
日本在线看片
|
色香婷婷
|
97精品国产一区二区三区
|
h漫在线观看
|
国外成人在线视频网站
|
中文字幕一级毛片
|
四虎永久免费黄色影片
|
精品精品视频
|
成人三级av
|
国产乡下妇女做爰
|
综合久久综合久久
|
国产精品久久久99
|
大伊人久久
|
中文成人在线
|
成人在线一级片
|
成人小视频在线观看
|
91精品国产91久久综合桃花
|
日本h片在线观看
|
五月天综合网
|
亚洲精品日韩综合观看成人91
|
日本a∨精品中文字幕在线 亚洲91视频
|
久久精品国产免费高清
|
日韩欧美国产一区二区
|
免费在线观看一区二区
|
欧美456
|
欧美成人精品一区二区男人看
|
国产免费一区二区三区
|
黄色片视频网站
|
黄色在线免费网站
|
毛片在线视频
|
国产一区二区三区欧美
|
国产日韩欧美一区二区在线播放
|
99精品久久
|
天堂在线1
|