保险中断会有什么影响会出发I2C中断?

硬件中断方式I2C(CV***R)
> 硬件中断方式I2C(CV***R)
硬件中断方式I2C(CV***R)
//i2c.h#defineTWPS00#defineTWPS11#defineTWEN2#defineTWIE0#defineTWEA6#defineTWINT7#defineTWSTA5#defineTWSTO4//TWSRvalues(notbits)//Master#defineTW_START0x08#defineTW_REP_START0x10//MasterTransmitter#defineTW_MT_SLA_ACK0x18#defineTW_MT_SLA_NACK0x20#defineTW_MT_DATA_ACK0x28#defineTW_MT_DATA_NACK0x30#defineTW_MT_ARB_LOST0x38//MasterReceiver#defineTW_MR_ARB_LOST0x38#defineTW_MR_SLA_ACK0x40#defineTW_MR_SLA_NACK0x48#defineTW_MR_DATA_ACK0x50#defineTW_MR_DATA_NACK0x58//SlaveTransmitter#defineTW_ST_SLA_ACK0xA8#defineTW_ST_ARB_LOST_SLA_ACK0xB0#defineTW_ST_DATA_ACK0xB8#defineTW_ST_DATA_NACK0xC0#defineTW_ST_LAST_DATA0xC8//SlaveReceiver#defineTW_SR_SLA_ACK0x60#defineTW_SR_ARB_LOST_SLA_ACK0x68#defineTW_SR_GCALL_ACK0x70#defineTW_SR_ARB_LOST_GCALL_ACK0x78#defineTW_SR_DATA_ACK0x80#defineTW_SR_DATA_NACK0x88#defineTW_SR_GCALL_DATA_ACK0x90#defineTW_SR_GCALL_DATA_NACK0x98#defineTW_SR_STOP0xA0//Misc#defineTW_NO_INFO0xF8#defineTW_BUS_ERROR0x00//definesandconstants#defineTWCR_CMD_MASK0x0F#defineTWSR_STATUS_MASK0xF8//returnvalues#defineI2C_OK0x00#defineI2C_ERROR_NODEV0x01#defineI2C_SEND_DATA_BUFFER_SIZE0x20#defineI2C_RECEIVE_DATA_BUFFER_SIZE0x20#defineF_CPU8000000#defineTRUE1#defineFALSE0//typestypedefenum{I2C_IDLE=0,I2C_BUSY=1,I2C_MASTER_TX=2,I2C_MASTER_RX=3,I2C_SL***E_TX=4,I2C_SL***E_RX=5}eI2cStateT//i2c.c#include#include"i2c.h"//I2C标准波特率://低速100KHz//高速400KHz//I2C状态和地址变量staticvolatileeI2cStateTypeI2cSstaticunsignedcharI2cDeviceAddrRW;//发送缓冲区staticunsignedcharI2cSendData[I2C_SEND_DATA_BUFFER_SIZE];staticunsignedcharI2cSendDataIstaticunsignedcharI2cSendDataL//接收缓冲区staticunsignedcharI2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];staticunsignedcharI2cReceiveDataIstaticunsignedcharI2cReceiveDataLunsignedcharlocalBuffer[]="!!";unsignedcharlocalBufferLength=0x20;//指向接收处理函数的指针,当本机被选中从接收时调用函数:I2cSlaveReceivestaticvoid(*i2cSlaveReceive)(unsignedcharreceiveDataLength,unsignedchar*recieveData);//指向发送处理函数的指针,当本机被选中从发送时调用函数:II2cSlaveTransmitstaticunsignedchar(*i2cSlaveTransmit)(unsignedchartransmitDataLengthMax,unsignedchar*transmitData);//设置总线速率voidi2cSetBitrate(unsignedintbitrateKHz){unsignedcharbitrate_//SCLfreq=F_CPU/(16+2*TWBR))#ifdefTWPS0//对于用速率分频的***R(mega128)//SCLfreq=F_CPU/(16+2*TWBR*4^TWPS)//setTWPStozeroTWSR&=~(1<=16)bitrate_div=(bitrate_div-16)/2;TWBR=bitrate_}//总线初始化voidi2cInit(void){//设置总线上拉#ifdef_MEGA128_INCLUDED_//#ifdef_MEGA64_INCLUDED_PORTD.0=1;//i2cSCLonATmega128,64PORTD.1=1;//i2cSDAonATmega128,64#elsePORTC.0=1;//i2cSCLonATmega163,323,16,32,等PORTC.1=1;//i2cSDAonATmega163,323,16,32,等#endif//清空从发送和从接受i2cSlaveReceive=0;i2cSlaveTransmit=0;//设置i2c波特率为100KHzi2cSetBitrate(100);//I2C总线使能TWCR|=1<1){i2cReceiveByte(TRUE);i2cWaitForComplete();*data++=i2cGetReceivedByte();length--;}//接收数据无回应(末位信号)i2cReceiveByte(FALSE);i2cWaitForComplete();*data++=i2cGetReceivedByte();}else{//如未回应器件地址,停止发送,返回错误retval=I2C_ERROR_NODEV;}//发送停止条件,保持TWEA以便从接收i2cSendStop();//开I2C中断TWCR|=TWIE;}eI2cStateTypei2cGetState(void){returnI2cS}//I2C(TWI)中断服务程序interrupt[TWI]voidtwi_isr(void){//读状态位status=TWSR&TWSR_STATUS_MASK;switch(status){//主方式caseTW_START://0x08:START已发送caseTW_REP_START://0x10:重复START已发送//发送器件地址i2cSendByte(I2cDeviceAddrRW);//主发送,主接收状态码caseTW_MT_SLA_ACK://0x18:SLA+W已发送;接收到ACKcaseTW_MT_DATA_ACK://0x28:数据已发送;接收到ACKif(I2cSendDataIndex 分享给小伙伴们:
我来说两句……
最新技术贴
微信公众号二
微信公众号一

参考资料

 

随机推荐