linux at24测试程序,AT91RM9200在linux2.6.20下TWI测试程序

本文介绍了在 Linux 2.6.20 下使用 AT91RM9200 进行 TWI (I²C) 接口调试的经验。调试过程中发现上拉电阻大小及通讯速率对数据传输稳定性的影响,并给出具体测试程序实例及运行结果。
AI助手已提取文章相关产品:

经过几天的瞎折腾,终于把AT91RM9200在linux2.6.20下TWI测试程序调试好了,总结如下

1.上拉电阻10K估计不合适,我换成2K的,SCL和SDA上就不断的有数据了,先前只有第一次有数据.

2.速度不能太快,由于我做的是两层板,干扰比较大,我I2C时钟跑在200K的时候,就不正常啦.我调成100K后,情况大为好转

测试程序如下:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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!

您可能感兴趣的与本文相关内容

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值