#include
#define SDA BIT1
#define SCL BIT2
#define SEG_A 0xA0 //0x0200---0x027F
#define SEG_B 0xB0 //0x0280---0x02FF
#define SEG_C 0xC0 //0x0300---0x037F
//0x0380---0x0400
__no_init char wokao@0x243;
//=============================
char *send_ptr;
char DEVICE_ADR=0;
char WORD_ADR=0;
char REC_DATA=0;
char START_flag = 0;
char STOP_flag = 0;
char PreState = 0;
char NowState = 0;
//*********************Function declaration*****************************************
void ACK(void);
//****************************************************************
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
//======================MCLK=16MHz=====================================
DCOCTL = CALDCO_16MHZ;
BCSCTL1 = CALBC1_16MHZ; //MCLK=DCO=16MHz
P3DIR &= ~(SDA+SCL);
while(1)
{
NN=10;
PreState = READ_SDA;
while(READ_SCL && NN--)
{
NowState = READ_SDA;
if(PreState && !NowState)
{
START_flag = 1;
_DINT();
}
if(!PreState && NowState)
{
STOP_flag = 1;
_UNITE();
}
PreState = NowState;
if(START_flag)
{
START_flag=0;
while(READ_SCL); //Wait for SCL high level state at START
for(gg=8;gg>0;gg--) //Receive device address
{
while(!READ_SCL); //Wait for SCL low level state
DEVICE_ADR<<=1;
if(READ_SDA) //The first CLK high level of data comes
DEVICE_ADR |= 0x01;
while(READ_SCL); //Wait for SCL high level state
}
ACK(); //ACK response to device address
//-----------The above receives the device address and knows whether the host wants to read or write to the slave---
for(gg=8;gg>0;gg--) //Receive memory unit address
{
while(!READ_SCL);
WORD_ADR<<=1;
if(READ_SDA)
WORD_ADR |= 0x01;
while(READ_SCL);
}
//-----------The above has received the memory unit address------------
ACK(); //ACK response signal to memory unit
if(DEVICE_ADR & 0x01) //slave sends data to host R/W=1
{
if(DEVICE_ADR==SEG_A+0x01)
{
send_ptr =(char*)(0x0200 + WORD_ADR);
}
else if(DEVICE_ADR==SEG_B+0x01)
{
send_ptr =(char*)(0x0280 + WORD_ADR);
}
else if(DEVICE_ADR==SEG_C+0x01)
{
send_ptr =(char*)(0x0300 + WORD_ADR);
}
//--------The above is to determine that the host is reading the slave, and assign the address unit to be read to the pointer---
for(gg=8;gg>0;gg--)
{
while(!READ_SCL);
if( *send_ptr & 0x80)
_NOP();
else
{
P3DIR |= SDA; //output 0
}
while(READ_SCL); //SCL is 1, then keep SDA output unchanged
P3DIR &= ~SDA; //When SCL is 0, switch SDA back to receiving state
*send_ptr <<=1;
}
ACK(); //Slave data transmission completed, response signal
_NOP();
//------------------The above is the data sent from the machine to the host-----------------
}
else //Host writes to slave R/W=0
{
for(gg=8;gg>0;gg--) //Receive the data that the host wants to write to the memory unit of the device
{
while(!READ_SCL);
REC_DATA <<=1;
if(READ_SDA)
REC_DATA |= 0x01;
while(READ_SCL);
}
if(DEVICE_ADR==SEG_A)
{
send_ptr =(char*)(0x0200 + WORD_ADR);
*send_ptr = REC_DATA;
}
else if(DEVICE_ADR==SEG_B)
{
send_ptr =(char*)(0x0280 + WORD_ADR);
*send_ptr = REC_DATA;
}
else if(DEVICE_ADR==SEG_C)
{
send_ptr =(char*)(0x0300 + WORD_ADR);
*send_ptr = REC_DATA;
}
ACK();
_NOP();
while(!READ_SCL);
}
}// if(START_flag)
}//while(NN--)
_UNITE();
_NOP();
}//while(1)
}
//****************************************************
void ACK(void)
{
// while(READ_SCL);
while(!READ_SCL);
P3DIR |= SDA; //When the 9th CLK goes high, SDA outputs 0
while(READ_SCL);
P3DIR &= ~SDA; //When the 9th CLK goes low, SDA outputs 1
}
Previous article:MSP430 FAQ
Next article:Some understanding of MSP430
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- EEWORLD University ---- Linux Kernel Design
- TI eSMO library Fsmopos and Gsmopos parameter analysis
- Today's live broadcast: TE will explain the design trends of smart antennas and sensor application cases in the Internet of Things
- High frequency amplifier circuit knowledge explanation
- msp430 implements a key matrix scan and UART transmission
- MSP430 ADC acquisition filter
- 【GD32E231 DIY】USART0 uses DMA to send and receive, without using CPU
- Soil moisture measurement circuit diagram
- [2022 Digi-Key Innovation Design Competition] Material Unboxing - ESP32-S2 and ESP32-S3
- MSP430F149 Comparator