找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1883|回复: 0
打印 上一主题 下一主题
收起左侧

AT91SAM9G25采用控制的 i2c 驱动 ,非GPIO模式

[复制链接]
跳转到指定楼层
楼主
ID:314569 发表于 2018-4-23 16:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
9G25 采用控制的 i2c 驱动 ,非GPIO模式

单片机源程序如下:
  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. }
复制代码

所有资料51hei提供下载:
i2c.zip (7.56 KB, 下载次数: 4)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表