• <label id="gmp5l"></label>

    <li id="gmp5l"></li>
      • 您好,歡迎訪問上海意泓電子科技有限責(zé)任公司網(wǎng)站!
        4新聞資訊
        您的位置: 首頁 ->  新聞資訊 -> 單片機(jī)

        ?MSP430學(xué)習(xí)的第一個(gè)模擬I2C程序

        文章出處:單片機(jī) 責(zé)任編輯:上海意泓電子科技有限責(zé)任公司 發(fā)表時(shí)間:
        2018
        05-20

        #include  
        unsigned char I2CBUF;
        unsigned char checkbit;
        #define SCL_H   (P10OUT |= BIT2)      // P10.2為SCL
        #define SCL_L   (P10OUT &= ~BIT2)
        #define SDA_H   (P10OUT |= BIT1)     //p10.1為SDA
        #define SDA_L   (P10OUT &= ~BIT1)
        #define SDA_OUT  (P10DIR |= BIT1)
        #define SDA_IN  (P10DIR &= ~ BIT1)
        void I2C_Start(void)    //I2C開始信號
        {
         SDA_OUT;
         SCL_L;
         SDA_H;
         __delay_cycles(5);
         SCL_H;
         __delay_cycles(5);
         SDA_L;
         __delay_cycles(5);
         SCL_L;
         __delay_cycles(5);
        }
        unsigned char testACK(void) {
         SDA_IN;
         __delay_cycles(5);
         SCL_H;
         __delay_cycles(5);
         checkbit = (P10IN & BIT1) >> 4;
         __delay_cycles(5);
         SCL_L;
         __delay_cycles(5);
         return (checkbit);
        }
        void I2C_ACK(void)                //應(yīng)答信號
        {
         SDA_OUT;
         SCL_L;
         SDA_L;
         __delay_cycles(5);
         SCL_H;
         __delay_cycles(5);
         SCL_L;
         __delay_cycles(5);
         SDA_H;
        }
        void I2C_NACK(void) {
         SDA_OUT;
         SDA_H;
         __delay_cycles(5);
         SCL_H;
         __delay_cycles(5);
         SCL_L;
         __delay_cycles(5);
        }
        void I2C_STOP(void)             //產(chǎn)生結(jié)束信號
        {
         SCL_L;
         __delay_cycles(5);
         SDA_L;
         __delay_cycles(5);
         SCL_H;
         __delay_cycles(5);
         SDA_H;
         __delay_cycles(5);
        }
        void I2C_WRITE(unsigned char data)
        {
         SDA_OUT;
         unsigned char i;
         for (i = 8; i > 0; i--)      //高位循環(huán)8次,發(fā)送1個(gè)字節(jié)數(shù)據(jù)
            {
          if (data & 0x80)   //判斷數(shù)據(jù)高位是0還是1
             {
           SDA_H;           //高位數(shù)據(jù)寫1
          }
          else
          {
           SDA_L;
          }
          SCL_H;
          __delay_cycles(5);  //發(fā)送數(shù)據(jù)
          SCL_L;
          __delay_cycles(5);
          data <<= 1;

         }
        }
        unsigned char I2C_READ(void)
        {
         unsigned char j;
         unsigned char BUFFER = 0X00;
         SDA_IN;
         for (j = 8; j >0; j--)      //高位循環(huán)8次,發(fā)送1個(gè)字節(jié)數(shù)據(jù)
            {
          SCL_H;
          __delay_cycles(5);
          BUFFER <<= 1;
          if ((P10IN & BIT1) == BIT1)   //判斷數(shù)據(jù)高位是0還是1
          {
           BUFFER |= 0X01;         //高位數(shù)據(jù)寫1
          }
          else
          {
           BUFFER |= 0XFE;
          }
          __delay_cycles(5);  //發(fā)送數(shù)據(jù)
          SCL_L;
          __delay_cycles(5);
         }
         return BUFFER;
        }
        void C16_WRITEDATA(unsigned char address, unsigned char DATA)
        {
         I2C_Start();
         I2C_WRITE(address);
         testACK();
         I2C_ACK();
         I2C_WRITE(0x00);
         testACK();
         I2C_ACK();
         I2C_WRITE(DATA);
         I2C_STOP();
        }
        unsigned char C16_READEDATA(unsigned char address)
        {
         unsigned char READDATA;
         I2C_Start();
         I2C_WRITE(0XA1);
         testACK();
         I2C_ACK();
         I2C_WRITE(address);
         I2C_Start();
         testACK();
         I2C_ACK();
         I2C_WRITE(0XA1);
         READDATA = I2C_READ();
         I2C_STOP();
         return READDATA;
        }
        int main(void)
        {
            const int DATA = 0X88;
         unsigned char READBUFFER;
         WDTCTL = WDTPW + WDTHOLD;
         P10DIR |= BIT1 + BIT2;
         P2DIR |= BIT0;
         P2OUT &= ~BIT0;
         C16_WRITEDATA(0X00, DATA);
         __delay_cycles(5);
         READBUFFER = C16_READEDATA(0X00);
         while (1)
         {
          if (READBUFFER == 0X88)
          {
           P2OUT |= BIT0;
          } else {
           P2OUT &= ~ BIT0;
          }
          __delay_cycles(15);
         }
        }


        上海意泓電子科技有限責(zé)任公司 版權(quán)所有 未經(jīng)授權(quán)禁止復(fù)制或鏡像

        CopyRight 2020-2025 www.jlodreman.com All rights reserved   滬ICP備2021005866號

        亚洲人成影院在线观看,91综合永久在线观看,欧洲亚洲日韩国产AV一级,中文字幕在线视频播放
      • <label id="gmp5l"></label>

        <li id="gmp5l"></li>