文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>AT91RM9200在linux2.6.20下TWI测试程序

AT91RM9200在linux2.6.20下TWI测试程序

时间:2010-09-02  来源:yaoqigui

经过几天的瞎折腾,终于把AT91RM9200在linux2.6.20下TWI测试程序调试好了,总结如下 1.上拉电阻10K估计不合适,我换成2K的,SCL和SDA上就不断的有数据了,先前只有第一次有数据. 2.速度不能太快,由于我做的是两层板,干扰比较大,我I2C时钟跑在200K的时候,就不正常啦.我调成100K后,情况大为好转   测试程序如下:   #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
          
int main(void)
{
    int i;
    int fd;
    int res;
    unsigned char buf[8];
    printf("Start to test the driver for I2C\n");
    printf("Get a char\n");
    getchar();
    fd= open("/dev/i2c",  O_RDWR);
    if( fd < 0 )
    {
        printf( "Fail to open AT24C32!\n" );
        exit(1);
    }  
    res=ioctl(fd,I2C_TENBIT,0);
// printf("res=%d\n",res);
    res=ioctl(fd,I2C_SLAVE,0x50);
// printf("res=%d\n",res);
    res=ioctl(fd,I2C_TIMEOUT,10);
// printf("res=%d\n",res);
    res=ioctl(fd,I2C_RETRIES,1);
// printf("res=%d\n",res);
   
     
     
   buf[0]=0x00;
   buf[1]=0x11;
   buf[2]=0x22;
   buf[3]=0x33;
   buf[4]=0x44;
   if ( write(fd,buf,5) != 5) {
     printf("write iic error1... ... \n");
    }
    else
   {
     printf("write iic datas  \n");
         for(i= 1;i<5;i++)
       { 
           printf("0x%x ",buf[i]);
       }
       printf("\n");
   }    buf[0]=0x00;
   if ( write(fd,buf,1) != 1) {
    printf("write iic error2... ... \n");
   }
   if ( read(fd,buf,4) != 4) {
        printf("read iic data1 error... ... \n");
    } else {
     printf("read iic datas  \n");
         for(i= 0;i<4;i++)
       { 
           printf("0x%x ",buf[i]);
       }
       printf("\n");
   
    }
  
   buf[0]=0x00;
   buf[1]=0x55;
   buf[2]=0x66;
   buf[3]=0x77;
   buf[4]=0x88;
   if ( write(fd,buf,5) != 5) {
    /* ERROR HANDLING: i2c transaction failed */
    printf("write iic error3... ... \n");
 }
   else{
     printf("write iic datas  \n");
         for(i= 1;i<5;i++)
       { 
           printf("0x%x ",buf[i]);
       }
       printf("\n");
      }
   buf[0]=0x00;
   if ( write(fd,buf,1) != 1) {
    /* ERROR HANDLING: i2c transaction failed */
    printf("write iic error4... ... \n");
   }
   if (read(fd,buf,4) != 4) {
        printf("read iic data2 error... ... \n");
    } else {
        printf("read iic datas   \n");
         for(i= 0;i<4;i++)
       { 
           printf("0x%x ",buf[i]);
       }
       printf("\n");
   
    }
    printf("\n finished!\n"); 
    close(fd);
   
    return 0;
}
  测试结果: Start to test the driver for I2C
Get a char
write iic datas
0x11 0x22 0x33 0x44
read iic datas
0x11 0x22 0x33 0x44
write iic datas
0x55 0x66 0x77 0x88
read iic datas
0x55 0x66 0x77 0x88
 finished
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载