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

          新聞中心

          msp430讀寫24c512程序

          作者: 時(shí)間:2016-11-22 來源:網(wǎng)絡(luò) 收藏
          *文件名:msp430f169i2c.c

          *整體描述:MSP430F169單片機(jī)硬件IIC軟件,字節(jié)方式,主方式
          * IIC接口:P3.3=SCL,P3.1=SDA;(開漏輸出)
          * 相應(yīng)寄存器:地址寄存器 I2COA 用于存放自身從地址(從方式時(shí)才有用)
          * 地址寄存器 I2CSA 用于存放外圍的從機(jī)地址(主方式時(shí)才有用)
          * 控制寄存器 U0CTL 硬件I2C的設(shè)置、使能、模式等。
          發(fā)送控制寄存器 I2CTCTL
          * 數(shù)據(jù)控制寄存器 I2CDCTL 指示I2C總線的狀態(tài)
          *
          * U0CTL -- RXDMAEN,TXDMAEN,I2C, XA, LISREN,SYNC,MST,I2CEN
          * 0 0 1 0 0 1 1 1 (0x17)
          * I2CTCTL --I2CWORD,I2CRM,I2CSSEL1,I2XSSEL0, I2CTRX,I2CSTB, I2CSTP, I2CSTT
          * 0 1 1 0 * 0 * *
          ************************************************************/
          #include
          #include "fpgacode.h"

          本文引用地址:http://www.ex-cimer.com/article/201611/320062.htm

          #define WR24C512
          #define LED1_1 (0x20)
          #define LED1_0 (0xdf)

          #define SDA_1 P3OUT |= BIT1 //串行數(shù)據(jù)線,SDA = 1
          #define SDA_0 P3OUT &=~ BIT1 //SDA = 0
          #define SCL_1 P3OUT |= BIT3 //串行時(shí)鐘線,SCL = 1
          #define SCL_0 P3OUT &=~ BIT3 //SCL = 0

          #define SDADIR_IN P3DIR &=~ BIT1 //SDA,I/O口為輸入
          #define SDADIR_OUT P3DIR |= BIT1 //I/0口為輸出
          #define SDA_IN ((P3IN >> 1) & 0x01) //Read SDA

          #define SCLDIR_IN P3DIR &=~ BIT3 //SCL,I/O口為輸入
          #define SCLDIR_OUT P3DIR |= BIT3 //I/0口為輸出
          #define SCL_IN ((P3IN >> 3) & 0x01) //Read SCL

          #ifdef WR24C512
          static int numi = 0; //Data Pointer
          #else
          static int numj = 0; //Data Pointer
          #endif

          #define I2CSLA 0x50


          void I2c_Idle_Check(void)
          {
          while (I2CBUSY & I2CDCTL); // I2C ready? 在空閑狀態(tài):0,空閑;1:忙
          }

          void DelayTime10us(unsigned char n)
          {
          unsigned char i;
          while(n--) // 5 cycles
          for(i=0;i<10;i++); // 8mhz 110:771 + 4 to while
          }


          void I2cBusSendByte(unsigned char c)
          {
          while((I2CIFG & TXRDYIFG) != TXRDYIFG); // 檢測(cè)發(fā)送準(zhǔn)備
          I2CDRB = c; // 寫發(fā)送寄存器
          }



          void I2C_Send(unsigned char ndatNum)
          {
          // 注意:通訊結(jié)束,I2CMST 自動(dòng)清零,再次通訊必須重新置位
          P3OUT = 0x00; // clear P3 output register
          P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
          U0CTL |= (I2C + SYNC); // select I2C mode;XA=0,7bit_addresing;
          U0CTL &= ~I2CEN; // i2c功能使能無效
          // I2CTCTL = I2CRM + I2CSSEL_2; // x(x>256)字節(jié)模式 ,new start測(cè)試使用

          // 選擇方式 I2CRM=0,最終用戶使用
          I2CTCTL = I2CSSEL_2;
          I2CNDAT = 2 + ndatNum; // 最終用戶使用,2byte地址 + 128byte數(shù)據(jù)

          I2CPSC = 2; // set scl
          I2CIFG = 0;
          I2CSA = 0x50; // Slave address of At24c512
          U0CTL |= I2CEN; // enable I2C module, 7 bit addr, master mode 08-26
          U0CTL |= MST;


          while (I2CBUSY & I2CDCTL); // I2C ready? 在空閑狀態(tài):0,空閑;1:忙
          I2CTCTL |= I2CTRX + I2CSTT + I2CSTP ; // I2CRM =0,啟動(dòng)總線,發(fā)送從器件地址
          while((I2CIFG & NACKIFG) == 0x02); // ack 為低電平,等待地址應(yīng)答位 ,判斷 無應(yīng)答NACKIFG = 1
          }

          unsigned char I2cSendSubAddr(unsigned int suba,unsigned char ndatNum)
          {
          unsigned char Hi_suba,Lo_suba;

          I2C_Send(ndatNum); // 啟動(dòng)總線,等待地址應(yīng)答位

          Hi_suba = ( unsigned char )( suba >> 8 );
          Lo_suba = ( unsigned char )( suba & 0x00ff );
          I2cBusSendByte( Hi_suba ); //發(fā)送器件子地址
          while((I2CIFG & 0x02) == NACKIFG ) // 等待數(shù)據(jù)的應(yīng)答
          {
          I2CTCTL |= I2CSTP; // 無應(yīng)答,結(jié)束總線
          return(0);
          }
          I2cBusSendByte( Lo_suba ); //發(fā)送器件子地址
          while((I2CIFG & 0x02 )==0x02 ) // 等待數(shù)據(jù)的應(yīng)答
          {
          I2CTCTL |= I2CSTP; // 無應(yīng)答,結(jié)束總線
          return(0);
          }
          return(1);
          }


          unsigned char I2cSendStr(const unsigned char * ps,unsigned char num)
          {
          // unsigned char i;
          unsigned char tempnum;
          tempnum = num-1;
          for(numi=0;numi {
          I2cBusSendByte(*ps);
          while((I2CIFG & 0x02) == NACKIFG) // 等待數(shù)據(jù)的應(yīng)答
          {
          I2CTCTL |= I2CSTP; // 無應(yīng)答,結(jié)束總線
          return(0);
          }
          ps++;
          }

          // I2CTCTL |= I2CSTP; //I2CRM =1模式中,在發(fā)送最后一個(gè)數(shù)據(jù)之前將停止位置位,*特別注意,必須
          I2cBusSendByte(*ps);
          while((I2CIFG & 0x02) == NACKIFG) // 等待數(shù)據(jù)的應(yīng)答
          {
          I2CTCTL |= I2CSTP; // 無應(yīng)答,結(jié)束總線
          return(0);
          }
          return(1);
          }

          //------------------寫at24c512----
          void I2cWrAt24c512(void)
          {
          unsigned int j=0;
          unsigned char m,n;
          unsigned int tempAddr;

          for (j = 0;j <462;j++) // page write
          {
          tempAddr = 128*j;
          while (I2CBUSY & I2CDCTL); // I2C ready?
          I2cSendSubAddr(tempAddr,0x80);
          I2cSendStr(FPGACfg+tempAddr,0x80);
          P4OUT ^= 0x80;
          for (m=0;m<125;m++) {for (n=0;n<110;n++);}
          P4OUT ^= 0x80;
          }

          tempAddr = 128*j;
          while (I2CBUSY & I2CDCTL); // I2C ready?
          I2cSendSubAddr(tempAddr,0x4f);
          I2cSendStr(FPGACfg+tempAddr,0x4f);
          P4OUT ^= 0x80;
          for (m=0;m<125;m++) {for (n=0;n<110;n++);}
          P4OUT ^= 0x80;
          }




          void I2cReceSubAddr(unsigned int suba) // test 09-25 讀之前先寫地址
          {
          unsigned char Hi_suba,Lo_suba;
          P3OUT = 0x00; // clear P3 output register
          P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
          U0CTL |= I2C + SYNC; // select I2C mode;XA=0,7bit_addresing;
          U0CTL &= ~I2CEN; // i2c功能使能無效
          // I2CTCTL =I2CRM + I2CSSEL_2;//I2CRM = 1,自己設(shè)定字節(jié)個(gè)數(shù)

          I2CTCTL =I2CSSEL_2; // I2CRM = 0,
          I2CNDAT = 2; // I2CRM = 0,2byte地址 + 128byte數(shù)據(jù),固定字節(jié)
          I2CIFG = 0;
          I2CPSC = 1; // set scl
          // I2CSCLH = 0x02; // High period of SCL
          //I2CSCLL = 0x02; // Low period of SCL
          I2CSA = 0x50; // Slave address of At24c512
          U0CTL |= I2CEN; // enable I2C module, 7 bit addr,master mode 08-26
          U0CTL |= MST;


          Hi_suba = ( unsigned char )( suba >> 8 );
          Lo_suba = ( unsigned char )( suba & 0x00ff );

          while (I2CBUSY & I2CDCTL); // I2C ready? 在空閑狀態(tài):0,空閑;1:忙
          I2CTCTL |= I2CTRX + I2CSTT + I2CSTP ;// I2CRM =0,啟動(dòng)總線,發(fā)送從器件地址
          while((I2CIFG & NACKIFG) == 0x02) // ack 為低電平,等待地址應(yīng)答位 ,判斷無應(yīng)答NACKIFG = 1
          {
          I2CTCTL |= I2CSTP; // 結(jié)束總線
          }

          I2cBusSendByte( Hi_suba ); //發(fā)送器件子地址
          while((I2CIFG & NACKIFG) == 0x02) // ack 為低電平,等待地址應(yīng)答位 ,判斷無應(yīng)答NACKIFG = 1
          {
          I2CTCTL |= I2CSTP; // 結(jié)束總線
          }
          I2cBusSendByte( Lo_suba ); //發(fā)送器件子地址
          while((I2CIFG & NACKIFG) == 0x02) // ack 為低電平,等待地址應(yīng)答位 ,判斷無應(yīng)答NACKIFG = 1
          {
          I2CTCTL |= I2CSTP; // 結(jié)束總線
          }
          DelayTime10us(250);
          DelayTime10us(250);
          DelayTime10us(250);
          DelayTime10us(250);
          }


          void InitI2cRece(unsigned char num)
          {
          P3OUT = 0x00; // clear P3 output register
          P3SEL = 0x0A; // P3.1=SDA, P3.3=SCL,Select I2C pins, Setup I2C module
          U0CTL |= I2C + SYNC; // select I2C mode;XA=0,7bit_addresing;
          U0CTL &= ~I2CEN; // i2c功能使能無效
          // I2CTCTL = I2CRM + I2CSSEL_2;// I2CRM=1 x(x>256)字節(jié)模式

          I2CTCTL =I2CSSEL_2; // I2CRM=0
          I2CNDAT = num; // 1個(gè)字節(jié)
          I2CPSC = 1; // set scl
          //I2CSCLH = 0x01; // High period of SCL
          //I2CSCLL = 0x01; // Low period of SCL
          I2CIFG = 0;
          I2CSA = 0x50; // Slave address of At24c512
          I2CTCTL &= ~I2CTRX;
          U0CTL |= I2CEN; // enable I2C module, 7 bit addr,master mode 08-26
          }


          unsigned char I2cReceSlaByte(void)
          {
          unsigned char ch;
          DelayTime10us(15);
          U0CTL |=MST; //主模式
          while (I2CBUSY & I2CDCTL); // I2C ready? 在空閑狀態(tài):0,空閑;1:忙
          I2CTCTL |= I2CSTT + I2CSTP; // I2CRM =0; Receive, ST, SP (clears MST)
          while((I2CIFG & RXRDYIFG) != RXRDYIFG);// 等待地址應(yīng)答位 ,判斷
          ch = I2CDRB;
          return(ch);
          }


          void I2cReceSlaStr(unsigned char num)
          {
          unsigned char i;
          DelayTime10us(15);
          U0CTL |=MST; //主模式
          while (I2CBUSY & I2CDCTL); // I2C ready? 在空閑狀態(tài):0,空閑;1:忙
          I2CTCTL |= I2CSTT + I2CSTP; // I2CRM =0; Receive, ST, SP (clears MST)
          for (i=0;i
          {
          while((I2CIFG & RXRDYIFG) != RXRDYIFG);// 等待地址應(yīng)答位 ,判斷
          rxDataArray = I2CDRB;
          }
          }



          關(guān)鍵詞: msp430讀寫24c51

          評(píng)論


          技術(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); })();