色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          MSP430單片機(jī)硬件IIC

          作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
          //MSP430IIC接口是很方便的物件,但是還是有很多人棄之不用,當(dāng)然有各種原因,但是如果你確實(shí)要用IIC接口,而且容許的話,用它還是很舒服的,可能開始不太順,但是一旦搞通了你會(huì)體會(huì)到他的好處。
          我看了一些網(wǎng)上的程序,有的仍然用模擬方式的思維處理某些環(huán)節(jié),比如應(yīng)答信號(hào)。非要追蹤”ACK”不可,其實(shí)接口已經(jīng)提供了“NACK”中斷,何必非要反向處理呢?事實(shí)上,如果在正常傳送中出現(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;//無(wú)應(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)時(shí)鐘初始化
          //====================================
          void INIT_SYSTIMER(void)
          {
          unsigned char i;
          BCSCTL1 &= ~XT2OFF; //選 XT2
          do
          {
          IFG1 &= ~OFIFG; //清除震蕩器失效標(biāo)志
          for (i = 0xFF; i > 0; i--); //穩(wěn)定時(shí)間
          }
          while ((IFG1 & OFIFG)!=0); //如果震蕩器失效標(biāo)志存在,等待
          BCSCTL2 = SELM_2 + SELS + DIVS_3; //MCLK時(shí)鐘源=XT2,SMCLK時(shí)鐘源=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時(shí)鐘 = SMCLK / 10: 100KH;
          I2CSCLH = 0x03; //SCL 高電平為:5 *I2C 時(shí)鐘
          I2CSCLL = 0x03; //SCL 低電平為:5 *I2C 時(shí)鐘
          I2CSA = 0X50;
          I2CNDAT = 0x02;
          U0CTL |= I2CEN; //I2C 模塊有效
          I2CIE |= TXRDYIE + RXRDYIE; //Enable TX RX interrupt
          }
          //=================================================
          //對(duì)于FM24L16的寫操作 發(fā)送N個(gè)數(shù)據(jù)
          //16>n>0
          //0x50<=mAddr<=0x57 sAddr<16;
          //=================================================
          void EEPROM_WriteN(unsigned char mAddr,unsigned char sAddr,unsigned char n)
          {
          I2CSA = mAddr; //頁(yè)地址
          TX_BUF[0] = sAddr; //子地址
          I2CNDAT = n; //計(jì)數(shù)器初值
          TX_COUNTER = 0;
          while (I2CDCTL & I2CBUSY); //檢查模塊忙
          I2CIFG =0; //中斷標(biāo)志清零
          I2CIE |= TXRDYIE;
          U0CTL |= MST;
          I2CTCTL |= I2CTRX + I2CSTT + I2CSTP;
          while ((~I2CIFG) & ARDYIFG); //檢查發(fā)送結(jié)束
          }
          //===================================================
          //讀1個(gè)字節(jié)
          //===================================================
          unsigned char EEPROM_ByteRead(unsigned char mAddr,unsigned char sAddr)
          {
          I2CSA = mAddr; //頁(yè)地址
          TX_BUF[0] = sAddr; //子地址
          TX_COUNTER = 0; //計(jì)數(shù)器初值
          RX_COUNTER = 0;

          while (I2CDCTL & I2CBUSY); //檢查模塊忙
          I2CNDAT = 1; //1位數(shù)據(jù)=子地址
          I2CIFG = 0; //中斷標(biāo)志清零
          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個(gè)字節(jié),mAddr頁(yè)地址;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單片機(jī)硬件II

          評(píng)論


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

          關(guān)閉