#ifndef _I2C_H
#define _I2C_H
#define SDA_1 P4OUT|=0x02 //SDA = 1
#define SDA_0 P4OUT&=~0x02 //SDA = 0
#define SCL_1 P4OUT|=0x01 //SCL = 1
#define SCL_0 P4OUT&=~0x01 //SCL = 0
#define SDA_IN P4DIR&=~0x02; //I/O port is input
#define SDA_OUT P4DIR|=0x02 //I/0 port is output
#define J_SDA_1_or_0 (P4IN&0x02)==0x02
//--------------------------------------------
//--------The following is the 24C64 read and write sub-function
//DELAY Function program //Function: delay subroutine
//==============================================
static void Delay(unsigned int n)
{
while(n!=0)
{n--;}
}
//===========================================
//START Function program
//Function: start flash memory operation
/==============================================
void Start(void)
{
SDA_OUT;
SDA_1;
Delay(10);
SCL_1;
Delay(10);
SDA_0;
Delay(10);
SCL_0;
Delay(10);
}
//===========================================
//STOP function program
//Function: stop flash memory operation
//===============================================
void Stop(void)
{
SDA_0;
Delay(10);
SCL_1;
Delay(10);
SDA_1;
Delay(10);
}
void TX_Byte(unsigned char WriteData)
{
unsigned char i,j;
j=WriteData;
SDA_OUT;
for (i=0; i<8; i++)
{
if ((j&0x80)==0)
{ SDA_0;}
else
{ SDA_1;}
j <<=1;
Delay(10);
SCL_1;
Delay(10);
SCL_0;
Delay(10);
}
}
unsigned char RX_Byte(void)
{
unsigned char i;
unsigned char TempData = 0;
SDA_IN;
for (i=0; i<8; i++)
{
Delay(10);
SCL_1;
Delay(10);
TempData <<= 1;
if (J_SDA_1_or_0)
{ TempData++; }
SCL_0;
Delay(10);
}
SDA_OUT;
return(TempData);
}
//===========================================
//ACK function program
//Function: high level indicates flash memory clock signal
//==============================================
void ReceiveAck(void)
{
SDA_IN;
SCL_1;
Delay(10);
while (J_SDA_1_or_0)
{ }
SCL_0;
SDA_OUT;
Delay(10);
}
unsigned char ReadWord(unsigned long int unit)
{
unsigned char LowAdd = 0;
unsigned char HighAdd = 0;
unsigned char TempData = 0;
LowAdd = (unsigned char)unit;
HighAdd = (unsigned char)(unit >> 8);
SDA_OUT;
Start();
TX_Byte(0xa0);
ReceiveAck();
TX_Byte(HighAdd);
ReceiveAck();
TX_Byte(LowAdd);
ReceiveAck();
Start();
TX_Byte(0xa1);
ReceiveAck();
TempData = RX_Byte();
Stop();
Delay(8000);
return(TempData);
}
void WriteWord(unsigned long int unit, unsigned char WriteData)
{
unsigned char LowAdd = 0;
unsigned char HighAdd = 0;
LowAdd = (unsigned char)unit;
HighAdd = (unsigned char)(unit >> 8);
Start();
TX_Byte(0xa0);
ReceiveAck();
TX_Byte(HighAdd);
ReceiveAck();
TX_Byte(LowAdd);
ReceiveAck();
TX_Byte(WriteData);
ReceiveAck();
Stop();
Delay(8000);
}
#endif
Keywords:MSP430 IIC
Reference address:MSP430 IIC bus program
#define _I2C_H
#define SDA_1
#define SDA_0
#define SCL_1
#define SCL_0
#define SDA_IN
#define SDA_OUT
#define J_SDA_1_or_0
//--------------------------------------------
//--------The following is the 24C64 read and write sub-function
//DELAY Function program //Function: delay subroutine
//==============================================
static void Delay(unsigned int n)
{
while(n!=0)
{n--;}
}
//===========================================
//START Function program
//Function: start flash memory operation
/==============================================
void Start(void)
{
}
//===========================================
//STOP function program
//Function: stop flash memory operation
//===============================================
void Stop(void)
{
}
void TX_Byte(unsigned char WriteData)
{
}
unsigned char RX_Byte(void)
{
}
//===========================================
//ACK function program
//Function: high level indicates flash memory clock signal
//==============================================
void ReceiveAck(void)
{
}
unsigned char ReadWord(unsigned long int unit)
{
}
void WriteWord(unsigned long int unit, unsigned char WriteData)
{
}
#endif
Previous article:Wireless transmission protocol based on MSP430
Next article:MSP430 Development Summary
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Ranking of installed capacity of smart driving suppliers from January to September 2024: Rise of independent manufacturers and strong growth of LiDAR market
- Industry first! Xiaopeng announces P7 car chip crowdfunding is completed: upgraded to Snapdragon 8295, fluency doubled
- P22-009_Butterfly E3106 Cord Board Solution
Guess you like
- Share CC2541 Bluetooth learning about ADC
- Summary of frequently asked questions about oscilloscopes (Part 2)
- MSP430 LCD5110 driver programming example
- NUCLEO F446RE meets X-NUCLEO-IKS01A3
- A huge reward! Looking for someone who can crack the RSA2048 and factory protocol in the ECU!
- Family Pet Health Maintenance System
- How to solve the problem that the STM32 does not run at full speed when entering the simulation debugging interface?
- HF Antenna Analyzer(3MHz to 30MHz)
- 1. Previous Power Supply Competition Topics
- Detailed Explanation of I2C Bus Protocol