<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > MSP430單片機(jī)模式IIC協(xié)議C語言程序

          MSP430單片機(jī)模式IIC協(xié)議C語言程序

          作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
          #include "msp430x22x4.h"
          #define I2C_SDA(operation) operation(3,1)
          #define I2C_SCL(operation) operation(3,2)
          #define PORT_SET(port,bit) P##port##OUT|=(1<
          #define PORT_CLR(port,bit) P##port##OUT&=~(1<
          #define SET_GPIO_MODE(port,bit) P##port##SEL&=~(1<
          #define SET_OUTPUT_MODE(port,bit) P##port##DIR|=(1<
          #define SET_INPUT_MODE(port,bit) P##port##DIR&=~(1<
          #define DISABLE_PULL_RESISTOR(port,bit) P##port##REN&=~(1<
          #define READ_PORT(port,bit) P##port##IN&(1<
          void I2C_init()
          {
          I2C_SDA(SET_GPIO_MODE);I2C_SCL(SET_GPIO_MODE);
          I2C_SDA(DISABLE_PULL_RESISTOR);I2C_SCL(DISABLE_PULL_RESISTOR);
          I2C_SDA(SET_INPUT_MODE);I2C_SCL(SET_INPUT_MODE);
          I2C_SDA(PORT_CLR);I2C_SCL(PORT_CLR);
          }
          void Start(void)
          {
          I2C_SCL(SET_INPUT_MODE); //SCL=1
          _NOP();
          I2C_SDA(SET_INPUT_MODE); //SDA=1
          _NOP();
          I2C_SDA(SET_OUTPUT_MODE); //SDA=0
          }
          void Stop(void)
          {
          I2C_SCL(SET_OUTPUT_MODE); //SCL=0
          _NOP();
          I2C_SDA(SET_OUTPUT_MODE); //SDA=0
          _NOP();
          I2C_SCL(SET_INPUT_MODE); //SCL=1
          _NOP();
          I2C_SDA(SET_INPUT_MODE); //SDA=1
          }
          void Transfer(unsigned char data)
          {
          unsigned char i;
          for(i=0;i<8;i++)
          {
          I2C_SCL(SET_OUTPUT_MODE); //SCL=0
          if(data&0x80)I2C_SDA(SET_INPUT_MODE); //SDA=1
          else I2C_SDA(SET_OUTPUT_MODE); //SDA=0
          data=data<<1;
          I2C_SCL(SET_INPUT_MODE); //SCL=1
          }
          }
          unsigned char Acknowledge(void)
          {
          I2C_SCL(SET_OUTPUT_MODE); //SCL=0
          _NOP();
          I2C_SDA(SET_INPUT_MODE); //release SDA
          _NOP();
          I2C_SCL(SET_INPUT_MODE); //SCL=1
          if(I2C_SDA(READ_PORT)) return 0xff;
          else return 0;
          }
          unsigned char I2C_Write_Byte(unsigned char device_address, unsigned char data)
          {
          Start();
          Transfer(device_address&0xfe);
          if(Acknowledge()!=0)return 0x00;
          Transfer(data);
          if(Acknowledge()!=0)return 0x00;
          Stop();
          return 0xff;
          }
          unsigned char I2C_Write_Word(unsigned char device_address, unsigned int data)
          {
          unsigned char LowByte, HighByte;
          LowByte=(unsigned char)data;
          HighByte=(unsigned char)(data >> 8);
          Start();
          Transfer(device_address&0xfe);
          if(Acknowledge()!=0)return 0x00;
          Transfer(HighByte);
          if(Acknowledge()!=0)return 0x00;
          Transfer(LowByte);
          if(Acknowledge()!=0)return 0x00;
          Stop();
          return 0xff;
          }


          評論


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

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();