⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hdi2c.c

📁 MSP430的I2C模块
💻 C
字号:
/*
ChangShu Microelectronics Technologies Co., Ltd.
Hardware I2C Communication Routines for MSP430 15,16 Series 
Copyrights Reverved, 2005
Creation:2005.4.10
Last Update:2005.4.19
Programmer: Steven Z.
*/

#include "chip.h"
#include "string.h"
#include "hdi2c.h"
#include "sport1.h"

void (*I2CISR[])(void )={
          I2CIdleISR,        //0x00
          0,
          I2CArbLosISR,      //0x02
          0,         
          I2CNAckISR,        //0x04
          0,          
          I2COwAddISR,       //0x06
          0,
          I2CRdyISR,         //0x08
          0,
          I2CRxISR,          //0x0a
          0,
          I2CTxISR,          //0x0c
          0,
          I2CGenCalISR,      //0x0e
          0,
          I2CBegDetISR};     //0x10
                   
unsigned char *ptrI2CBuf;
unsigned char I2CRxFlg,I2CRxMode;
unsigned char I2CBuf;

void InitI2C()
{
UCTL0 |=SWRST;              //Ready for configurating I2C register  
UCTL0 |=SYNC| I2C ;          //Configuration SUART to I2C mode
UCTL0 &=~I2CEN;
//================= Starting Of I2C FUNCTION CONFIGURATION========================//

I2CTCTL=I2CSSEL_2;          //Byte transferring mode and amounts controlled by I2CNGAT; SMCLK used,
I2CPSC=2;                   //The inner signal I2CIN = 2M @ 8Mhz OSC
I2CSCLH=4;                 //~2.5us,high periode of SCK at 400K transmission rate @ 8M I2CIN
I2CSCLL=4;                 //~2.5us,low periode of SCK at 400K transmission rate @ 8M I2Cin
            
/*As a single master device, the following bitS should be disabled
1.Start detect interrupt disable,Only used when MSP430 is as a slave
2.General call interrupt disable, namely disabling the broadcast calling of I2C protocol;
3.Own address interrupt disable, Only used when MSP430 is as a slave
4.Arbitration lost interrupt disable, Only used when using mulit-master application
Current, these funciton are variable 
1.Transmission Interrupt may be operated as polling when disable.
2.Receive Interrupt
3.I2C Ready Interrupt
4.NACK interrupt
*/
I2CIE=RXRDYIE;//+NACKIE;//TXRDYIE++ARDYIE;
UCTL0 |=I2CEN;               //I2C module avariable
//================= End Of I2C FUNCTION CONFIGURATION========================// 

P3SEL |=0x0A;    //P3.3(SCK),P3.1(SDA)
P3DIR &=0xFD;
I2CRxFlg=0;
I2CRxMode=0;
}

#pragma vector=UART0TX_VECTOR
__interrupt void I2C_ISR(void)
{
 I2CISR[I2CIV]();
} 

void  I2CIdleISR(){}

void  I2CArbLosISR(){}

void  I2CNAckISR()
{
     I2CIFG &=~NACKIFG;
     _DINT();
     Tx1S("\nSlave unresponse\n");
     _EINT();
}

void  I2COwAddISR(){}

void  I2CRdyISR()
{
     _DINT();
     Tx1S("\nSlave Ready\n");
     _EINT();  
} 

void  I2CRxISR()
{
    if(!I2CRxMode) I2CBuf=I2CDRB;
    else
    {
      *ptrI2CBuf=I2CDRB;
      ptrI2CBuf++;
    }
}

void  I2CTxISR()
{
}

void  I2CGenCalISR(){}

void  I2CBegDetISR(){} 


void I2CTxByte(unsigned char deviceID,char cbuf)
{
  UCTL0 |=MST ;                 //7-bit addressing; I2C master mode; Disabling DMA and Loopback enabled.
  while(CHK_I2CBUS_STAT);       //Optional,Checking I2C Bus status for mulit-master mode
  while(CHK_I2C_BUSY);          //Waiting for previous completion of Tx/Rx
  I2CTCTL |=I2CTRX;             //Tx mode
  I2CSA=deviceID;               //Transmiting target slave device of I2C
  I2CNDAT=1;                    //Setting the length of data to be sent
  I2CTCTL |=I2CSTT | I2CSTP;    //Initiation the transaction of I2C     
  while(CHK_I2C_BUSY)           //Tx data with automatically repeat mode
  {
    if(CHK_I2C_TXRDY) I2CDRB=cbuf; //Tx Data
  }
}

void I2CTxNBytes(unsigned char deviceID,unsigned char *ptrbuf,unsigned int len)
{
  UCTL0 |=MST ;                 //7-bit addressing; I2C master mode; Disabling DMA and Loopback enabled.
  while(CHK_I2CBUS_STAT);       //Optional,Checking I2C Bus status for mulit-master mode
  while(CHK_I2C_BUSY);          //Waiting for previous completion of Tx/Rx
  I2CTCTL |=I2CTRX;             //Tx mode
  I2CSA=deviceID;               //Transmiting target slave device of I2C
  I2CNDAT=len;                  //Setting the length of data to be sent
  I2CTCTL |=I2CSTT | I2CSTP;    //Initiation the transaction of I2C     
  while(CHK_I2C_BUSY)           //Tx data with automatically repeat mode
  {
    if(CHK_I2C_TXRDY)
    {
     I2CDRB=*ptrbuf;              //Tx Data
     ptrbuf++;
    }
  }
}

unsigned char I2CRxByte(unsigned char deviceID)
{
  UCTL0 |=MST ;                 //7-bit addressing; I2C master mode; Disabling DMA and Loopback enabled.
  while(CHK_I2CBUS_STAT);       //Optional,Checking I2C Bus status for mulit-master mode
  while(CHK_I2C_BUSY);          //Waiting for previous completion of Tx/Rx
  I2CSA=deviceID;               //Transmiting target slave device of I2C
  I2CNDAT=1;                    //Setting the first address length of memory to be read
  I2CTCTL |=I2CSTT | I2CSTP;    //Initiation the transaction of I2C     
  while(!CHK_I2C_RXRDY);  
  while(CHK_I2C_BUSY);
  return I2CBuf;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -