#define I2EN 6 #define STA 5 #define STO 4 #define SI 3 #define AA 2
__inline void IIC_Start(void) { I2C0CONCLR = (1 << STA) | (1 << SI) | (1 << AA); I2C0CONSET = (1 << STA) | (1 << I2EN); } __inline void IIC_RepStart(void) { I2C0CONCLR = (1 << STA) | (1 << SI) | (1 << AA); I2C0CONSET = (1 << STA) | (1 << I2EN); } __inline void IIC_Stop(void) { I2C0CONCLR = (1 << STA)| (1 << SI); I2C0CONSET = (1 << STO); } __inline void IIC_Wait() { while(!(I2C0CONSET & (1 << SI))); } __inline void IIC_WriteData(void) { I2C0CONCLR = (1 << STA)| (1 << SI)| (1 << STO); } __inline void IIC_ReadAck(void) { I2C0CONCLR = (1 << STA)| (1 << SI)| (1 << STO); I2C0CONSET = (1 << AA); } __inline void IIC_ReadNack(void) { I2C0CONCLR = (1 << STA)| (1 << SI)| (1 << STO)| (1 << AA); } /********************************************* *Function :Iic0非嵌套IRQ中断服务程序 *Create By:shannon **********************************************/ void IRQIic0(void) __irq { VICVectAddr = 0;//向量中断结束 }
/********************************************* *Function :iic0初始化程序 * 通讯频率固定在80K *Create By:shannon **********************************************/ uint8 Iic0Init(void) { uint32 bak; uint32 addrOffset; pinmap.pin.p02 = GP002SCL0; pinmap.pin.p03 = GP003SDA0; PINSEL0 = pinmap.PinSel[0]; #if (I2C0_PRI > 0 && I2C0_PRI < 17)//1到16,0未用 VICIntSelect = VICIntSelect & ~(1 << (I2C0_PRI - 1));//设置相应的中断为IRQ中断 addrOffset = (I2C0_PRI - 1) * 4; *(volatile uint32 *)(VICVectAddrBase + addrOffset) = (uint32)IRQIic0;//设置中断服务程序地址 *(volatile uint32 *)(VICVectCntlBase + addrOffset) = 0x20 | I2C0_INT;//分配中断 VICIntEnable = 1 << I2C0_INT; //使能中断 #endif
I2C0SCLH = Fpclk / 160000; I2C0SCLL = Fpclk / 160000; return (1); }
void RTCWrite(uint8 ad,uint8 da) { IIC_Start(); IIC_Wait(); I2C0DAT = 0xA0; IIC_WriteData(); IIC_Wait(); I2C0DAT = ad; IIC_WriteData(); IIC_Wait(); I2C0DAT = da; IIC_WriteData(); IIC_Wait(); IIC_Stop(); }
uint8 RTCRead(uint8 ad) { uint8 data; IIC_Start(); IIC_Wait(); I2C0DAT = 0xA0; IIC_WriteData(); IIC_Wait(); I2C0DAT = ad; IIC_WriteData(); IIC_Wait(); IIC_RepStart(); IIC_Wait(); I2C0DAT = 0xA1; IIC_WriteData(); IIC_Wait(); IIC_ReadNack(); IIC_Wait(); data = I2C0DAT; IIC_Stop(); return data; } |