Last active
April 14, 2022 09:43
-
-
Save ksvbka/e1083bd0c1b6381bc741 to your computer and use it in GitHub Desktop.
I2C Hardware for MSP430
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************** | |
Product: I2C - Hardware - Block | |
Module: I2C | |
Created: 12/04/2015, by KIENLTB | |
Description: I2C Driver for MSP430 | |
********************************************************************************/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Header inclusions */ | |
/*-----------------------------------------------------------------------------*/ | |
#include "msp430g2553.h" | |
#include "i2c_hw.h" | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Constant definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Macro definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Data type definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
//typedef unsigned char BYTE; | |
//typedef unsigned short WORD; | |
//typedef unsigned long DWORD; | |
//typedef unsigned int UINT; | |
//typedef BYTE BOOL; | |
//typedef unsigned char CHAR; | |
//typedef void VOID; | |
// | |
//typedef BYTE* PBYTE; | |
//typedef WORD* PWORD; | |
//typedef DWORD* PDWORD; | |
//typedef UINT* PUINT; | |
//typedef CHAR* PCHAR; | |
//typedef VOID* PVOID; | |
// | |
//typedef BYTE RESULT; | |
/*-----------------------------------------------------------------------------*/ | |
/* Global variables */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Function prototypes */ | |
/*-----------------------------------------------------------------------------*/ | |
VOID I2C_Init(); | |
VOID I2C_SetSlaverAddress(BYTE bySlaverAddress); | |
VOID I2C_Stop(); | |
BYTE I2C_StartWrite(WORD wSlave, BYTE byFirstdata); | |
BYTE I2C_WriteByte(BYTE byData); | |
BYTE I2C_StartRead(WORD wSlave); | |
BYTE I2C_ReadByte(); | |
VOID I2C_WriteData(PBYTE pData, BYTE byDevideAddr, BYTE byRegister, BYTE nLength); | |
VOID I2C_ReadData(PBYTE pBuff, BYTE byDevideAddr, BYTE byRegister, BYTE nLength); | |
/*-----------------------------------------------------------------------------*/ | |
/* Function implementations */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Init | |
Purpose: Init for I2C Hardware block | |
Parameters: None | |
Return: None | |
-------------------------------------------------------------------------------*/ | |
VOID I2C_Init(VOID) | |
{ | |
//select GPIO P1.6 and P1.7 as I2C pin | |
P1SEL |= BIT6 + BIT7; | |
P1SEL2 |= BIT6 + BIT7; | |
// Enable SW set to config I2C; | |
UCB0CTL1 |= UCSWRST; | |
// Config I2C | |
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C master, synchronous mode | |
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset; | |
// Config baudrate I2C SCL = SMCLK/DIV | |
// LSB of DIV stored in UCB0BR0, MSB stored in BCB0BR1 | |
UCB0BR0 = 0x19;//0x0A; // 0x05, 0x19, 0x0A; | |
UCB0BR1 = 0; | |
IFG2 &= ~UCB0TXIFG; | |
IFG2 &= ~UCB0RXIFG; | |
UCB0CTL1 &= ~UCSWRST; // Clear SW reset. | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_SetSlaverAddr | |
Purpose : Set slaver address for I2C communication | |
Parameters : I2C Address of Slaver | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_SetSlaverAddress(BYTE bySlaverAddress) | |
{ | |
// Set slave address | |
UCB0I2CSA = bySlaverAddress; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Stop | |
Purpose: Transmit stop signal | |
Parameters: None | |
Return: None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_Stop() | |
{ | |
while((UCB0STAT & BUSY)!= 0); | |
UCB0CTL1 |= UCTXSTP; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_StartWrite | |
Purpose : Set slaver address and send a byte to slave | |
Parameters : I2C Address of Slaver | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_StartWrite(WORD wSlave, BYTE byFirstdata) | |
{ | |
while((UCB0STAT & BUSY)!= 0); | |
UCB0I2CSA = wSlave; | |
UCB0CTL1 |= UCTR; // I2C TX | |
UCB0CTL1 |= UCTXSTT; | |
UCB0TXBUF = byFirstdata; | |
while((IFG2 & UCB0TXIFG) == 0); | |
if((UCB0STAT & UCNACKIFG)!=0) | |
return 0; | |
else | |
return 1; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteByte | |
Purpose : Write single Byte from slave, call after called I2C_StartWrite | |
Parameters : byData | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteByte(BYTE byData) | |
{ | |
while((UCB0STAT & BUSY)!= 0); | |
UCB0TXBUF = byData; | |
while((IFG2 & UCB0TXIFG) == 0); | |
if((UCB0STAT & UCNACKIFG)!=0) | |
return 0; | |
else | |
return 1; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_StartRead | |
Purpose : Set slaver Adress and Start read a byte to the slave | |
Parameters : I2C Address of Slaver | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_StartRead(WORD wSlave) | |
{ | |
while((UCB0STAT & BUSY)!= 0); | |
UCB0I2CSA = wSlave; | |
UCB0CTL1 &= ~UCTR; | |
UCB0CTL1 |= UCTXSTT; | |
while((UCB0CTL1 & UCTXSTT)!= 0); | |
if((UCB0STAT & UCNACKIFG)!=0) | |
return 0; | |
else | |
return 1; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadByte | |
Purpose : Read single Byte from slave | |
Parameters : None | |
Return : byData | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadByte(VOID) | |
{ | |
BYTE byData; | |
while((UCB0STAT & BUSY)!= 0); | |
while((IFG2 & UCB0RXIFG) == 0); | |
byData = UCB0RXBUF; | |
return byData; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadData | |
Purpose : Read multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_ReadData(PBYTE pBuff, BYTE byDevideAddr, BYTE byRegister, BYTE nLength) | |
{ | |
BYTE nIndex; | |
//I2C_Init(); | |
I2C_StartWrite(byDevideAddr, byRegister); | |
I2C_StartRead(byDevideAddr); | |
for(nIndex = 0; nIndex < nLength; nIndex++) | |
{ | |
*(pBuff + nIndex) = I2C_ReadByte(); | |
} | |
UCB0CTL1 |= UCTXNACK; | |
I2C_Stop(); | |
I2C_ReadByte(); | |
__delay_cycles(15); | |
I2C_Stop(); | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteData | |
Purpose : Write Multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_WriteData(PBYTE pData, BYTE byDevideAddr, BYTE byRegister, BYTE nLength) | |
{ | |
BYTE nIndex; | |
//I2C_Init(); | |
I2C_StartWrite(byDevideAddr, byRegister); | |
for(nIndex = 0; nIndex < nLength; nIndex++) | |
{ | |
I2C_WriteByte(*(pData + nIndex)); | |
} | |
__delay_cycles(15); | |
I2C_Stop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************** | |
Product: I2C - Hardware - Block | |
Module: I2C | |
Author: 4/12/2015, by KIENLTB | |
Description: Driver I2C for MSP430 | |
********************************************************************************/ | |
#ifndef __I2C_HW__H__ | |
#define __I2C_HW__H__ | |
/*-----------------------------------------------------------------------------*/ | |
/* Header inclusions */ | |
/*-----------------------------------------------------------------------------*/ | |
#include "msp430g2553.h" | |
/*-----------------------------------------------------------------------------*/ | |
/* Data type definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
typedef unsigned char BYTE; | |
typedef unsigned short WORD; | |
typedef unsigned long DWORD; | |
typedef unsigned int UINT; | |
typedef BYTE BOOL; | |
typedef unsigned char CHAR; | |
typedef void VOID; | |
typedef BYTE* PBYTE; | |
typedef WORD* PWORD; | |
typedef DWORD* PDWORD; | |
typedef UINT* PUINT; | |
typedef CHAR* PCHAR; | |
typedef VOID* PVOID; | |
typedef BYTE RESULT; | |
/*-----------------------------------------------------------------------------*/ | |
/* Constant definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Macro definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Global variables */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Function prototypes */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Init | |
Purpose: Init for I2C Hardware block | |
Parameters: None | |
Return: None | |
-------------------------------------------------------------------------------*/ | |
VOID I2C_Init(); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_SetSlaverAddr | |
Purpose : Set slaver address for I2C communication | |
Parameters : I2C Address of Slaver | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_SetSlaverAddress(BYTE bySlaverAddress); | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Stop | |
Purpose: Transmit stop signal | |
Parameters: None | |
Return: None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_Stop(); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_StartWrite | |
Purpose : Set slaver address and send a byte to slave | |
Parameters : I2C Address of Slaver | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_StartWrite(WORD wSlave, BYTE byFirstdata); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteByte | |
Purpose : Write single Byte from slave, call after called I2C_StartWrite | |
Parameters : byData | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteByte(BYTE byData); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_StartRead | |
Purpose : Set slaver Adress and Start read a byte to the slave | |
Parameters : I2C Address of Slaver | |
Return : 0 if success, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_StartRead(WORD wSlave); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadByte | |
Purpose : Read single Byte from slave | |
Parameters : None | |
Return : byData | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadByte(VOID); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadData | |
Purpose : Read multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_WriteData(PBYTE pData, BYTE byDevideAddr, BYTE byRegister, BYTE nLength); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteData | |
Purpose : Write Multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_ReadData(PBYTE pBuff, BYTE byDevideAddr, BYTE byRegister, BYTE nLength); | |
#endif // __I2C__H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************** | |
Product: I2C - Hardware - Block | |
Module: I2C | |
Created: 12/04/2015, by KIENLTB | |
Description: I2C Driver for MSP430 | |
********************************************************************************/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Header inclusions */ | |
/*-----------------------------------------------------------------------------*/ | |
#include "msp430g2553.h" | |
#include "i2c_hw.h" | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Constant definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Macro definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Local Data type definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
//typedef BYTE BYTE; | |
//typedef unsigned short WORD; | |
//typedef unsigned long DWORD; | |
//typedef unsigned int UINT; | |
//typedef BYTE BOOL; | |
//typedef BYTE CHAR; | |
//typedef VOID VOID; | |
// | |
//typedef BYTE* PBYTE; | |
//typedef WORD* PWORD; | |
//typedef DWORD* PDWORD; | |
//typedef UINT* PUINT; | |
//typedef CHAR* PCHAR; | |
//typedef VOID* PVOID; | |
// | |
//typedef BYTE RESULT; | |
/*-----------------------------------------------------------------------------*/ | |
/* Global variables */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Function prototypes */ | |
/*-----------------------------------------------------------------------------*/ | |
VOID I2C_Init(); | |
VOID I2C_SetSlaverAddress(BYTE bySlaverAddress); | |
BYTE I2C_ReadByte(BYTE byDevideAddr, BYTE byRegister); | |
BYTE I2C_ReadData(BYTE *Data,BYTE byDevideAddr,BYTE Addr_Data, BYTE Length); | |
BYTE I2C_WriteByte(BYTE byData, BYTE byDevideAddr, BYTE byRegister); | |
BYTE I2C_WriteData(BYTE *data, BYTE byDevideAddr, BYTE address, BYTE Length); | |
/*-----------------------------------------------------------------------------*/ | |
/* Function implementations */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Init | |
Purpose: Init for I2C Hardware block | |
Parameters: None | |
Return: None | |
-------------------------------------------------------------------------------*/ | |
VOID I2C_Init(VOID) | |
{ | |
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0 | |
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0 | |
UCB0CTL1 |= UCSWRST; // Enable SW reset | |
UCB0CTL0 = UCMST+UCMODE_3+UCSYNC; // I2C Master, synchronous mode | |
UCB0CTL1 = UCSSEL_2+UCSWRST; // Use SMCLK, keep SW reset | |
UCB0BR0 = 130; // fSCL = SMCLK/40 = ~400kHz | |
UCB0BR1 = 0; | |
//UCB0I2CSA = addr; // Set slave address | |
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_SetSlaverAddr | |
Purpose : Set slaver address for I2C communication | |
Parameters : I2C Address of Slaver | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_SetSlaverAddress(BYTE addr) //Thay doi Sia chi Slave | |
{ | |
UCB0I2CSA = addr; // Set slave address | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadByte | |
Purpose : Read single Byte from slave | |
Parameters : I2C Address of Slaver and address of register | |
Return : byData | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadByte(BYTE byDevideAddr, BYTE byRegister) | |
{ | |
I2C_SetSlaverAddress(byDevideAddr); | |
while (UCB0CTL1 & UCTXSTP); // Cho tin hieu I2C STT duoc gui di | |
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX,START | |
while (!(IFG2&UCB0TXIFG)); // Cho gui xong | |
UCB0TXBUF = byRegister; // Dia chi luu gia tri Seconds | |
while (!(IFG2&UCB0TXIFG)); // Cho gui xong | |
UCB0CTL1 &= ~UCTR; // I2C RX | |
UCB0CTL1 |= UCTXSTT; // I2C RESTART | |
IFG2 &= ~UCB0TXIFG; // Xoa co ngat USCI_B0 TX | |
while (UCB0CTL1 & UCTXSTT); // Cho den khi I2C STT duoc gui di | |
UCB0CTL1 |= UCTXSTP; // Gui bit STOP | |
return UCB0RXBUF; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadData | |
Purpose : Read multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadData(BYTE *Data,BYTE byDevideAddr,BYTE Addr_Data, BYTE Length) | |
{ | |
BYTE i=0; | |
//UCB0I2CSA = byDevideAddr; // Set slave address | |
I2C_SetSlaverAddress(byDevideAddr); | |
while (UCB0CTL1 & UCTXSTP); // Loop until I2C STT is sent | |
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition | |
while (!(IFG2&UCB0TXIFG)); | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi | |
UCB0TXBUF = Addr_Data; // Dia chi luu gia tri Seconds | |
while (!(IFG2&UCB0TXIFG)); | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi | |
UCB0CTL1 &= ~UCTR; // I2C RX | |
UCB0CTL1 |= UCTXSTT; // I2C start condition | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent | |
for(i=0;i<(Length-1);i++) | |
{ | |
while (!(IFG2&UCB0RXIFG)); | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
Data[i] = UCB0RXBUF; | |
} | |
while (!(IFG2&UCB0RXIFG)); | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
UCB0CTL1 |= UCTXSTP; // I2C stop condition after 1st TX | |
Data[Length-1] = UCB0RXBUF; | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
return 0; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteByte | |
Purpose : Write 1 byte to register of slave device | |
Parameters : Data - data value | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteByte(BYTE byData, BYTE byDevideAddr, BYTE byRegister) | |
{ | |
I2C_SetSlaverAddress(byDevideAddr); | |
//UCB0I2CSA = byDevideAddr; // Set slave address | |
while (UCB0CTL1 & UCTXSTP); // Cho den khi tin hieu STT duoc gui xong | |
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, Gui bit START | |
while (!(IFG2&UCB0TXIFG)); // Cho cho bit START gui xong | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi thì thoat khoi ham | |
UCB0TXBUF = byRegister; // Gui dia chi thanh ghi can ghi | |
while (!(IFG2&UCB0TXIFG)); // Cho gui xong | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi thì thoat khoi ham | |
UCB0TXBUF = byData; // Gui du lieu | |
while (!(IFG2&UCB0TXIFG)); // Cho gui xong | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi thì thoat khoi ham | |
UCB0CTL1 |= UCTXSTP; // Gui bit STOP | |
IFG2 &= ~UCB0TXIFG; // Xoa co USCI_B0 TX | |
return 0; | |
} | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteData | |
Purpose : Write Multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteData(BYTE *data, BYTE byDevideAddr, BYTE address, BYTE Length) | |
{ | |
BYTE i=0; | |
I2C_SetSlaverAddress(byDevideAddr); | |
//UCB0I2CSA = byDevideAddr; // Set slave address | |
while (UCB0CTL1 & UCTXSTP); // Loop until I2C STT is sent | |
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition | |
while (!(IFG2&UCB0TXIFG)); | |
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi | |
UCB0TXBUF = address; // Dia chi luu gia tri Seconds | |
for(i = 0; i < (Length - 1); i++) | |
{ | |
UCB0TXBUF = data[i]; | |
} | |
while (!(IFG2&UCB0TXIFG)); // Cho gui xong | |
if(UCB0STAT & UCNACKIFG) return UCB0STAT; //Neu bao loi thì thoat khoi ham | |
UCB0CTL1 |= UCTXSTP; // Gui bit STOP | |
IFG2 &= ~UCB0TXIFG; // Xoa co USCI_B0 TX | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************** | |
Product: I2C - Hardware - Block | |
Module: I2C | |
Author: 4/12/2015, by KIENLTB | |
Description: Driver I2C for MSP430 | |
********************************************************************************/ | |
#ifndef __I2C_HW__H__ | |
#define __I2C_HW__H__ | |
/*-----------------------------------------------------------------------------*/ | |
/* Header inclusions */ | |
/*-----------------------------------------------------------------------------*/ | |
#include "msp430g2553.h" | |
/*-----------------------------------------------------------------------------*/ | |
/* Data type definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
//typedef BYTE BYTE; | |
//typedef unsigned short WORD; | |
//typedef unsigned long DWORD; | |
//typedef unsigned int UINT; | |
//typedef BYTE BOOL; | |
//typedef BYTE CHAR; | |
//typedef VOID VOID; | |
// | |
//typedef BYTE* PBYTE; | |
//typedef WORD* PWORD; | |
//typedef DWORD* PDWORD; | |
//typedef UINT* PUINT; | |
//typedef CHAR* PCHAR; | |
//typedef VOID* PVOID; | |
// | |
//typedef BYTE RESULT; | |
/*-----------------------------------------------------------------------------*/ | |
/* Constant definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Macro definitions */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Global variables */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-----------------------------------------------------------------------------*/ | |
/* Function prototypes */ | |
/*-----------------------------------------------------------------------------*/ | |
/*-------------------------------------------------------------------------------- | |
Function: I2C_Init | |
Purpose: Init for I2C Hardware block | |
Parameters: None | |
Return: None | |
-------------------------------------------------------------------------------*/ | |
VOID I2C_Init(VOID); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_SetSlaverAddr | |
Purpose : Set slaver address for I2C communication | |
Parameters : I2C Address of Slaver | |
Return : None | |
--------------------------------------------------------------------------------*/ | |
VOID I2C_SetSlaverAddress(BYTE bySlaverAddress); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadByte | |
Purpose : Read single Byte from slave | |
Parameters : I2C Address of Slaver and address of register | |
Return : byData | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadByte(BYTE byDevideAddr, BYTE byRegister); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_ReadData | |
Purpose : Read multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_ReadData(BYTE *Data,BYTE byDevideAddr,BYTE Addr_Data, BYTE Length); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteByte | |
Purpose : Write 1 byte to register of slave device | |
Parameters : Data - data value | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteByte(BYTE Data, BYTE byDevideAddr, BYTE byRegister); | |
/*-------------------------------------------------------------------------------- | |
Function : I2C_WriteData | |
Purpose : Write Multi byte to register of slave device | |
Parameters : pData - Pointer to data | |
byDevideAddr - I2C address of slave device | |
byRegister - Register address need to read data. | |
Return : return 0 if succcess, 1 if failt | |
--------------------------------------------------------------------------------*/ | |
BYTE I2C_WriteData(BYTE *data, BYTE byDevideAddr, BYTE address, BYTE Length); | |
#endif // __I2C__H__ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixd some issue went read multi byte from slave