完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
//==============================================================================
// Example of IIC communication between 9S12XEP100 and EEPROM FM24C256 // BUSCLK = 8MHz // SCL = 91kHz // I2C1 is used for commnication //============================================================================== #include #include "derivative.h" /* derivative-specific definitions */ //---------------------------- #define MaxLen 128 // #define slaveread 0xA1 #define slavewrite 0xA0 unsigned char RD_data; //-------------------------- byte WrData[8]; byte RdData[8]; byte RdBuf; word length; unsigned char x; //--- typedef union { word EE_Address; struct { byte Address_H; byte Address_L; }Bytes; }tAddr; tAddr ADDR; #define Address ADDR.EE_Address #define Addr_L ADDR.Bytes.Address_L #define Addr_H ADDR.Bytes.Address_H //****** Init routines ******************* //-- void IIC0_init(void) { IIC0_IBFD = 0x4C; IIC0_IBAD = 0x00; IIC0_IBCR = 0x80; //IIC0_IBSR_IBAL = 1; } //-- //--------------------------------------------------------- void IIC_write_nbytes(word addr, byte len) { byte i; Address = addr; // load address; IIC0_IBCR_TXAK = 0; ; IIC0_IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0; // And generate START condition; //-------start of transmit first byte to IIC bus----- IIC0_IBDR = slavewrite; // Address the slave and set up for master transmit; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBDR = Addr_H; // Send high byte of the word address; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBDR = Addr_L; // Send low byte of the word address; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ for(i=0;i IIC0_IBDR = WrData;//data; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; } //-----Slave ACK occurred------------ IIC0_IBCR_MS_SL = 0; // generate STOP condition; } //------------------------------------------------------------------------- void IIC_read_nbytes(word addr,byte len) { byte dummy; byte i; Address = addr; length=len; if(length>=MaxLen) { length=MaxLen; } IIC0_IBCR_TXAK = 0; // TXAK = 0; IIC0_IBCR |= 0x30; // RX/TX = 1; MS/SL = 1; TXAK = 0; // And generate START condition; IIC0_IBDR = slavewrite; // Address the slave and set up for master transmit; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBDR = Addr_H; // Send high byte of word address; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBDR = Addr_L; // Send low byte of word address; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while(IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBCR_RSTA = 1; // set up repeated start; IIC0_IBDR = slaveread; // (slave_address) | (RW = 1); while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; while (IIC0_IBSR_RXAK); // check for RXAK; //-----Slave ACK occurred------------ IIC0_IBCR_TX_RX = 0; // set up to receive; dummy=IIC0_IBDR;//RD_data = IIC0_IBDR; // dummy read; for(i=0;i if(i==7){ while(!IIC0_IBSR_IBIF); IIC0_IBSR_IBIF = 1; RdData[7]=IIC0_IBDR; } else{ while(!IIC0_IBSR_IBIF); IIC0_IBSR_IBIF=1; while (IIC0_IBSR_RXAK); RdData=IIC0_IBDR; } } IIC0_IBCR_TXAK = 1; // acknowledge disable; while (!IIC0_IBSR_IBIF); // wait until IBIF; IIC0_IBSR_IBIF=1; // clear the interrupt event flag; IIC0_IBCR_MS_SL = 0; // generate stop signal; RD_data = IIC0_IBDR; // read right data; } //****** main routines ******************* void main(void) { unsigned int i; unsigned int j,k; //---- IIC ----- IIC0_init(); WrData[0]=1; WrData[1]=0; WrData[2]=0; WrData[3]=8; WrData[4]=6; WrData[5]=1; WrData[6]=1; WrData[7]=255; //---- main cycle --- //LedPad22Dir=1; //LedCpu=1; do{ IIC_write_nbytes(0x0102,8); for(j=0;j<0x1000;j++); IIC_read_nbytes(0x0102,8); //-- for(j=0;j<50000;j++); //LedCpu=~LedCpu; } while(1); i+=8; /**/ } //============================================================================== 这是我用的程序问题就是,
|
|
相关推荐 |
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
644 浏览 0 评论
求助一下关于51系列单片机的Timer0的计时问题,TH0、TL0+1的时间是怎么算的?
1819 浏览 1 评论
【RA-Eco-RA4E2-64PIN-V1.0开发板试用】开箱+Keil环境搭建+点灯+点亮OLED
1262 浏览 0 评论
【敏矽微ME32G070开发板免费体验】使用coremark测试敏矽微ME32G070 跑分
1136 浏览 0 评论
【敏矽微ME32G070开发板免费体验】开箱+点灯+点亮OLED
1357 浏览 2 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
12026 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 09:12 , Processed in 0.472399 second(s), Total 36, Slave 30 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号