新聞中心

MSP430單片機硬件IIC

作者: 時間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
//MSP430IIC接口是很方便的物件,但是還是有很多人棄之不用,當然有各種原因,但是如果你確實要用IIC接口,而且容許的話,用它還是很舒服的,可能開始不太順,但是一旦搞通了你會體會到他的好處。
我看了一些網(wǎng)上的程序,有的仍然用模擬方式的思維處理某些環(huán)節(jié),比如應(yīng)答信號。非要追蹤”ACK”不可,其實接口已經(jīng)提供了“NACK”中斷,何必非要反向處理呢?事實上,如果在正常傳送中出現(xiàn)“NACK”中斷,你要考慮更換器件或者考慮系統(tǒng)的穩(wěn)定性了。

#include <msp430x16x.h>
unsigned char RX_BUF[10];
unsigned char TX_BUF[16];
unsigned char Rece_data[16];

int RX_COUNTER;
int TX_COUNTER;

void Init_IIC(void);
void EEPROM_WriteN(unsigned char mAddr,unsigned char sAddr,unsigned char n);
unsigned char EEPROM_ByteRead(unsigned char mAddr,unsigned char sAddr);
void EEPROM_ReadN(unsigned char mAddr,unsigned char sAddr,unsigned char n);
//=======================================
//USART0中斷
//=======================================
#pragma vector = USART0TX_VECTOR
__interrupt void I2C_ISR(void)
{
switch(I2CIV)
{
case 2: I2CTCTL |= I2CSTP; I2CIFG=0; break;//無應(yīng)答
case 10: RX_BUF[RX_COUNTER] = I2CDRB;
RX_COUNTER++;
break;

case 12: I2CDRB = TX_BUF[TX_COUNTER];
TX_COUNTER++;
break;
default: break;
}
}

//====================================
//系統(tǒng)時鐘初始化
//====================================
void INIT_SYSTIMER(void)
{
unsigned char i;
BCSCTL1 &= ~XT2OFF; //選 XT2
do
{
IFG1 &= ~OFIFG; //清除震蕩器失效標志
for (i = 0xFF; i > 0; i--); //穩(wěn)定時間
}
while ((IFG1 & OFIFG)!=0); //如果震蕩器失效標志存在,等待
BCSCTL2 = SELM_2 + SELS + DIVS_3; //MCLK時鐘源=XT2,SMCLK時鐘源=XT2/8
}
//=================================
void delay(unsigned int i)
{
unsigned int j;
for(j=0;j}

//=================================================
void Init_IIC(void)
{
P3SEL |= 0x0a;
U0CTL = I2C + SYNC;
U0CTL &= ~I2CEN;
I2CTCTL |= I2CSSEL_2; //SMCLK
I2CPSC = 9; //I2C時鐘 = SMCLK / 10: 100KH;
I2CSCLH = 0x03; //SCL 高電平為:5 *I2C 時鐘
I2CSCLL = 0x03; //SCL 低電平為:5 *I2C 時鐘
I2CSA = 0X50;
I2CNDAT = 0x02;
U0CTL |= I2CEN; //I2C 模塊有效
I2CIE |= TXRDYIE + RXRDYIE; //Enable TX RX interrupt
}
//=================================================
//對于FM24L16的寫操作 發(fā)送N個數(shù)據(jù)
//16>n>0
//0x50<=mAddr<=0x57 sAddr<16;
//=================================================
void EEPROM_WriteN(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
I2CSA = mAddr; //頁地址
TX_BUF[0] = sAddr; //子地址
I2CNDAT = n; //計數(shù)器初值
TX_COUNTER = 0;
while (I2CDCTL & I2CBUSY); //檢查模塊忙
I2CIFG =0; //中斷標志清零
I2CIE |= TXRDYIE;
U0CTL |= MST;
I2CTCTL |= I2CTRX + I2CSTT + I2CSTP;
while ((~I2CIFG) & ARDYIFG); //檢查發(fā)送結(jié)束
}
//===================================================
//讀1個字節(jié)
//===================================================
unsigned char EEPROM_ByteRead(unsigned char mAddr,unsigned char sAddr)
{
I2CSA = mAddr; //頁地址
TX_BUF[0] = sAddr; //子地址
TX_COUNTER = 0; //計數(shù)器初值
RX_COUNTER = 0;

while (I2CDCTL & I2CBUSY); //檢查模塊忙
I2CNDAT = 1; //1位數(shù)據(jù)=子地址
I2CIFG = 0; //中斷標志清零
I2CIE |= TXRDYIE;
U0CTL |= MST;
I2CTCTL |= I2CTRX + I2CSTT; //置位起始位 發(fā)送子地址
while ((~I2CIFG) & ARDYIFG); //檢查發(fā)送結(jié)束

I2CIFG &= ~ARDYIFG;
I2CIE |= RXRDYIE;
I2CTCTL &= ~I2CTRX;
I2CTCTL |= I2CSTT + I2CSTP; //接收1位數(shù)據(jù)后置位停止位
while ((~I2CIFG) & ARDYIFG); //檢查接收結(jié)束

return RX_BUF[0]; //返回接收的1字節(jié)數(shù)據(jù)
}
//======================================
//n=讀N個字節(jié),mAddr頁地址;sAddr子地址
//======================================
void EEPROM_ReadN(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
unsigned char i;
unsigned char k;
unsigned char m=0;
k = sAddr + n;
for(i=sAddr;i {
Rece_data[m]=EEPROM_ByteRead(mAddr,i);
m++;
}
}



//==================================
void SAVE_DATA(unsigned char mAddr,unsigned char sAddr,unsigned char n)
{
TX_BUF[1] = 0X41;
TX_BUF[2] = 0X42;
TX_BUF[3] = 0X43;
TX_BUF[4] = 0X44;
TX_BUF[5] = 0X45;
TX_BUF[6] = 0X46;
TX_BUF[7] = 0X47;
TX_BUF[8] = 0X48;
TX_BUF[9] = 0X49;
TX_BUF[10] = 0X4A;
TX_BUF[11] = 0X4B;

EEPROM_WriteN(0x50,0x00,n+1);
}

//==================================
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
INIT_SYSTIMER();
Init_IIC();
_EINT(); //打開中斷

while(1)
{
//=================================

SAVE_DATA(0x50,0x00,11);
EEPROM_ReadN(0x50,0x00,11);

delay(10);
//=================================
}


關(guān)鍵詞: MSP430單片機硬件II

評論


技術(shù)專區(qū)

關(guān)閉