久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2689|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

AT91SAM9G25采用控制的 i2c 驅(qū)動(dòng) ,非GPIO模式

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:314569 發(fā)表于 2018-4-23 16:49 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
9G25 采用控制的 i2c 驅(qū)動(dòng) ,非GPIO模式

單片機(jī)源程序如下:
  1. //i2c_test.c
  2. #include<stdio.h>  
  3. #include<linux/types.h>  
  4. #include<fcntl.h>  
  5. #include<unistd.h>  
  6. #include<stdlib.h>  
  7. #include<sys/types.h>  
  8. #include<sys/ioctl.h>  
  9. #include<errno.h>  
  10. #include<assert.h>  
  11. #include<string.h>  
  12. #include<linux/i2c.h>  
  13. #include<linux/i2c-dev.h>  

  14. #define CHIP_ADDR_START 0x40
  15. #define CHIP_NUM                1
  16. #define BUF_SIZE 32

  17. #define I2C_DEV "/dev/i2c-1"

  18. static int read_dth(int fd, unsigned char slave_address, unsigned char reg_addr, unsigned char buff[], int count)
  19. {
  20.         int ret;
  21.         struct i2c_rdwr_ioctl_data work_queue;
  22.        
  23.         work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
  24.         if(!work_queue.msgs)  
  25.     {  
  26.         printf("read_dth, Memery alloc error\n");  
  27.          
  28.         return -1;  
  29.     }

  30.         work_queue.nmsgs = 2;   
  31.         work_queue.msgs[0].len = 1;   
  32.         work_queue.msgs[0].addr = slave_address;   
  33.         // work_queue.msgs[0].flags = 0;     /* write */   
  34.         work_queue.msgs[0].buf= ®_addr;   

  35.         work_queue.msgs[1].len= count;   
  36.         work_queue.msgs[1].addr= slave_address;   
  37.         work_queue.msgs[1].flags= 1;     /* read */   
  38.         work_queue.msgs[1].buf= buff;   

  39.         ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);   
  40.         if(ret < 0)
  41.         {   
  42.                 printf("readerror\n");     
  43.         }   
  44.         else
  45.         {
  46.                 printf("read  data: %02x%02x from address: %02x\n", buff[0], buff[1], reg_addr);
  47.         }
  48.          
  49.         return ret;
  50. }


  51. static int write_dth(int fd, unsigned char slave_address, unsigned char addr, char buff[], int count)
  52. {
  53.         int ret;
  54.         unsigned char buffer[2];
  55.         struct i2c_rdwr_ioctl_data work_queue;
  56.        
  57.     work_queue.msgs = (struct i2c_msg *)malloc(work_queue.nmsgs *sizeof(struct i2c_msg));
  58.         if(!work_queue.msgs)  
  59.     {  
  60.         printf("write_dth, Memery alloc error\n");  
  61.          
  62.         return -1;  
  63.     }
  64.        
  65.         work_queue.nmsgs = 1;   
  66.         work_queue.msgs[0].len = 2;   
  67.         work_queue.msgs[0].addr = slave_address;   
  68.         work_queue.msgs[0].flags = 0;     /* write */   

  69.           
  70.         work_queue.msgs[0].buf = buffer;   
  71.         work_queue.msgs[0].buf[0]= addr;    /* write address */   
  72.         work_queue.msgs[0].buf[1]= buff[0];  /* write data */   

  73.         ret= ioctl(fd, I2C_RDWR, (unsigned long)&work_queue);   
  74.         if(ret < 0)
  75.         {   
  76.                 printf("writedata error\n");     
  77.         }
  78.         else
  79.         {
  80.                 printf("writedata: %02x to address: %02x\n", buff[0], addr);  
  81.         }       

  82.         return  ret;

  83. }


  84. static int init_one_dth(int fd, unsigned char u8chip_addr)
  85. {
  86.         int res;
  87.         unsigned char buf[BUF_SIZE];
  88.        
  89.         buf[0] = 0X80;
  90.         buf[1] = 0X00;
  91.         write_dth(fd, u8chip_addr, 0x02, buf, 2);
  92.         usleep(1000000);
  93.        
  94.         buf[0] = 0X10;
  95.         buf[1] = 0X00;
  96.         write_dth(fd, u8chip_addr, 0x02, buf, 2);
  97.         usleep(1000000);
  98.        
  99.         buf[0] = 0X00;
  100.         buf[1] = 0X00;
  101.         buf[2] = 0X00;
  102.         buf[3] = 0X00;
  103.         buf[4] = 0X00;
  104.         buf[5] = 0X00;
  105.         read_dth(fd, u8chip_addr, 0xfb, buf, 2);
  106.         read_dth(fd, u8chip_addr, 0xfc, buf + 2, 2);
  107.         read_dth(fd, u8chip_addr, 0xfd, buf + 4, 2);
  108.         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]);
  109. }


  110. int init_dth(void)
  111. {
  112.         int fd;
  113.         int res;
  114.         unsigned char i;
  115.        
  116.         fd = open(I2C_DEV,O_RDWR);
  117.         if(fd < 0 )
  118.         {
  119.                 printf("Can't Open %s !!!\r\n\r\n",I2C_DEV);
  120.                 return -1;
  121.         }
  122.        
  123.         res = ioctl(fd, I2C_RETRIES, 10);
  124.         res = ioctl(fd, I2C_TENBIT, 0);

  125.         for(i = 0; i < CHIP_NUM; i++)
  126.         {
  127.                 init_one_dth(fd, (CHIP_ADDR_START + i));
  128.         }
  129.        
  130.         printf("\r\n\r\n");
  131.        
  132.         return fd;
  133. }


  134. int sample_dth(int fd, float *fdata)
  135. {
  136.         unsigned char i;
  137.         int res;
  138.         int rres[2];
  139.         unsigned char buf[BUF_SIZE];
  140.        
  141.         float temper, humidity;
  142.        
  143.         for(i = 0; i < CHIP_NUM; i++)
  144.         {
  145.                 buf[0] = 0X00;
  146.                 write_dth(fd, (CHIP_ADDR_START + i), 0x00, buf, 1);
  147.         }
  148.        
  149.         usleep(500000);
  150.        
  151.         for(i = 0; i < CHIP_NUM; i++)
  152.         {
  153.                 buf[0] = 0X00;
  154.                 buf[1] = 0X00;
  155.                 buf[2] = 0X00;
  156.                 buf[3] = 0X00;
  157.                 rres[0] = read_dth(fd, (CHIP_ADDR_START + i), 0x00, buf, 2);
  158.                 rres[1] = read_dth(fd, (CHIP_ADDR_START + i), 0x01, buf + 2, 2);
  159.                
  160.                 if(1) // if(2 == rres[0])
  161.                 {
  162.                         temper = ((float)(buf[0] * 0x100 + buf[1]) / 65536.0 * 165.0 - 40.0);
  163.                 }
  164.                 else
  165.                 {
  166.                         temper = -273.0;
  167.                 }
  168.                
  169.                 if(1) // if(2 == rres[1])
  170.                 {
  171.                         humidity = ((float)(buf[2] * 0x100 + buf[3]) / 65536.0 * 100.0);
  172.                 }
  173.                 else
  174.                 {
  175.                         humidity = 125.0;
  176.                 }
  177.                
  178.                 fdata[i * 2 + 0] = temper;
  179.                 fdata[i * 2 + 1] = humidity;
  180.                
  181.                 printf("temperat[%u] = %.1f\r\n", (unsigned int)i, temper);
  182.                 printf("humidity[%u] = %.1f%%\r\n", (unsigned int)i, humidity);
  183.         }
  184.        
  185. }


  186. int main(void)
  187. {
  188.         int fd;
  189.         float fdata[4][2];
  190.        
  191.         fd = init_dth();
  192.        
  193.         while(1)
  194.         {
  195.                 sample_dth(fd, &fdata[0][0]);
  196.                 printf("\r\n");
  197.                 usleep(1000000);
  198.         }

  199.         close(fd);
  200.         return 0;
  201. }
復(fù)制代碼

所有資料51hei提供下載:
i2c.zip (7.56 KB, 下載次數(shù): 4)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 色狠狠一区 | 欧美一级片在线播放 | 午夜一级大片 | 一级国产精品一级国产精品片 | 亚洲精品无 | 男女国产视频 | 亚洲国产精品日本 | 成人免费网站www网站高清 | 国产成人网 | 日本黄色不卡视频 | 成人av片在线观看 | 影音av | 久久久www成人免费无遮挡大片 | 国产高清在线 | 久久中文字幕一区 | 99精品观看 | 97国产精品| 欧美激情精品久久久久久 | 羞羞视频免费观看 | 免费一级大片 | 国产美女福利在线观看 | 最新黄色毛片 | 国产日韩中文字幕 | 在线欧美视频 | 日本成人中文字幕在线观看 | 久久精品国产一区二区电影 | 在线日韩中文字幕 | 另类亚洲视频 | 狠狠色狠狠色综合日日92 | 精品综合网 | 日韩中文在线视频 | 久久亚洲欧美日韩精品专区 | caoporn国产精品免费公开 | 日本一区不卡 | 亚洲精选久久 | 午夜寂寞影院列表 | 日韩2020狼一二三 | 亚洲毛片在线观看 | 久久久久久免费毛片精品 | 日韩欧美国产精品 | 成人小视频在线 |