1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近在研究IIC通讯,做IIC读写AT24C256的程序,但是单片机用1M晶振可以正常运行,换成12M就出错,这应该就是时序的问题了,哪位大侠能帮我看看12M频率下应该把时序调整成扫描样呢??[C] 纯文本查看 复制代码
#ifndef AT24CXX_H_#define AT24CXX_H_#include #ifndef AT24CXXX_C_#define AT24CXXX_C_#include "at24cxx.h"***it bSda =P0^0;***it bScl =P0^1; bit bScan_Err;unsigned char idata cShare_Buf[8]={'P',1,2,3,4,5,6,7}; //I2c read and write buffervoid Start_I2c(void){ // unsigned char /*idata*/ i; bSda = 1;// Wait_1us; bScl = 1;// for(i=0;i<32;i++); //wait 7us Wait_1us; bSda = 0;// for(i=0;i<32;i++); //wait 7us Wait_1us; bScl = 0; Wait_1us;}void End_I2c(void){// unsigned char /*idata*/ i; bSda=0; Wait_1us; bScl=1;// for(i=0;i<16;i++); //wait 7us Wait_1us; bSda = 1; Wait_1us;}unsigned char Scan_I2c(){ unsigned char /*idata*/ i,j;//IIC data bSda = 1; j = 0; for(i=0;i<8;i++) { bScl = 1; j <<= 1; Wait_1us; j |= bSda; //Scan 0 Scan 1 bScl=0; Wait_1us; } return j;}void Send_I2c(unsigned char cData){ unsigned char /*idata*/ i; for(i=0;i<8;i++) { bSda=(bit)(cData&0x80); Wait_1us; bScl=1; cData<<=1; Wait_1us; bScl=0; Wait_1us; } bSda=1; Wait_1us; bScl=1; //检测ACK for(i=0;(bSda==1)&&(bScan_Err==0);i++) { if(i==90) bScan_Err=1; //如果101us没有响应就错误 } bScl=0; Wait_1us;}void Send_Ack(void){//unsigned char /*idata*/ i; bSda=0; Wait_1us; bScl=1;// for(i=0;i<2;i++); //wait 8us Wait_1us; bScl=0; Wait_1us;}void Send_Not_Ack(void){// unsigned char /*idata*/ i; bSda = 1; Wait_1us; bScl = 1;// for(i=0;i<2;i++); //wait 8us Wait_1us; bScl=0; Wait_1us;} void Write_Mem(unsigned char cMem_Size,cChips_Address,cData_Size,unsigned int cStart_Address){ unsigned char /*idata*/ i,cAddress[2]; //0-L;1-H bScan_Err=0; cAddress[0]=cStart_Address; cStart_Address>>=8; cAddress[1]=cStart_Address; i=Sla_Wr; i|=cChips_Address; if(cMem_Size==AT24C256) i&=0xf7; Start_I2c(); Wait_1us; Send_I2c(i); //SLA:1010 xxx0 Wait_1us; if(cMem_Size==AT24C256) //1-16用1个字节地址,32-512用2个字节地址,匹配地址AT24C32、64 Send_I2c(cAddress[1]); //先发送高字节地址 Send_I2c(cAddress[0]); //如果是双字节地址则为低地址否则为高地址 for(i=0;(bScan_Err==0)&&(i!=cData_Size);i++) Send_I2c(cShare_Buf); //写一个字节数据 End_I2c(); }void Read_Mem(unsigned char cMem_Size,cChips_Address,cData_Size,unsigned int cStart_Address){ unsigned char /*idata*/ i,cAddress[2]; bScan_Err=0; cAddress[0]=cStart_Address; //将16位地址分解为高低字节 cStart_Address>>=8; cAddress[1]=cStart_Address; i = Sla_Wr; i |= cChips_Address; if(cMem_Size==AT24C256) i&=0xf7; Start_I2c(); Send_I2c(i);//SLA:1010 xxx0 if(cMem_Size==AT24C256) //1-16用1个字节地址,32-512用2个字节地址,匹配地址AT24C32、64 Send_I2c(cAddress[1]); //先发送高字节地址 Send_I2c(cAddress[0]); //address and write i|=0x01; //Read Start_I2c(); Send_I2c(i); //address and read for(i=0;(bScan_Err==0)&&(i!=cData_Size);i++) { cShare_Buf=Scan_I2c(); //读入第一个字节 if(i==(cData_Size-1)) Send_Not_Ack(); else Send_Ack(); } End_I2c();}#endif |
|
相关推荐
2个回答
|
|
|
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
3310 浏览 9 评论
2991 浏览 16 评论
3492 浏览 1 评论
9055 浏览 16 评论
4086 浏览 18 评论
1174浏览 3评论
603浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
596浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2333浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1894浏览 2评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-21 14:43 , Processed in 1.252175 second(s), Total 77, Slave 58 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号