The circuit remains unchanged. The following is a simulation circuit, except that the digital tube of port P2 is changed from observing which step the program is executed to checking the data in the receiving buffer.
The procedure is as follows:
#include
#define unit unsigned int
#define uchar unsigned char
uchar num=4;
uchar idata sendbuf[4]={0x96,0x84,0xd5,0x63};
uchar idata recivebuf[4];
sbit scl=P0^0;
sbit sda=P0^1;
sbit led0=P2^0;
sbit led1=P2^1;
sbit led2=P2^2;
sbit led3=P2^3;
sbit led4=P2^4;
sbit led5=P2^5;
sbit led6=P2^6;
sbit led7=P2^7;
delay(void) //delay
{
int i;
for(i=0;i<1;i++);
}
start(void) //start
{
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
stop(void) //stop
{
sda=0;
scl=1;
delay();
sda=1;
delay();
scl=0;
}
answer(void) //answer
{
sda=1;
scl=1;
delay();
sda=0;
scl=0;
}
noanswer(void) //no answer
{
sda=1;
scl=1;
delay();
sda=1;
scl=0;
}
checkanswer(void) //check answer
{
sda=1;
scl=1;
F0=0;
if(sda==1) F0=1;
scl=0;
}
sendabyte(uchar idata *saddress) //send a byte
{
uchar n=8,temp=*saddress;
while(n--)
{
if((temp&0x80)==0x80) sda=1;
else sda=0;
delay( );
scl=1;
delay();
scl=0;
temp=temp<<1;
}
checkanswer();
if(F0==1) return;
}
reciveabyte(uchar idata *raddress) //recive a byte
{
uchar n=8,temp;
while(n--)
{
scl=1;
temp=temp<<1;
if(sda==1)
temp=temp|0x01 ;
else
temp=temp&0xfe;
scl=0;
}
*raddress=temp;
}
sendnbyte(uchar n) //send n byte
{
uchar idata *ps;
ps=&sendbuf[0];
while(n--)
{
sendabyte(ps);
ps++;
}
stop();
}
recivenbyte(uchar n) //recive n byte
{
uchar idata *pr;
pr=&recivebuf[0];
while(n--)
{
reciveabyte(pr);
answer();
pr++;
}
noanswer();
stop();
}
main(void) //MAIN
{
start();
sendabyte(0xa0);
sendabyte(0x00);
sendnbyte(num);
/*-----------------------*/
start();
sendabyte(0xa1);
recivenbyte(num);
P2 = recivebuf[7];
}
Program Description:
The num at the beginning of the program defines the number of data to be transmitted, as well as the send buffer and the receive buffer.
The operation of the main function is clear at a glance and will not be introduced in detail.
Problem: In this program, I defined the data in the receive buffer as 4, and indeed 4 data were received. However, the received data starts from recivebuf[4]. In this example, recivebuf[4] stores 0X96, recivebuf[5] stores 0X84, recivebuf[6] stores 0XD5, and recivebuf[7] stores 0X63.
Previous article:Simple keyboard reading and writing program
Next article:MCU simulates I2C bus to read and write EEPROM (24CXX) program 1
- Popular Resources
- Popular amplifiers
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- CircuitPython Day
- How big is our misunderstanding of green oil? Let’s analyze it with real cases!
- How much do you know about impedance matching?
- I saw a post next door and it seems okay since I work there every day.
- EEWORLD University ---- PCB Design Video - Learn PADS PCB Design in 1 Day
- 【NUCLEO-L552ZE Review】-3: Arduino vs. Mbed?
- What is the reason why IIC fails when the MSP430FR2311 power supply voltage rises to 3.5V? How to solve it?
- Signal linear transformation problem
- [GD32E231 DIY Contest] Part 4: Summary
- 【Gravity:AS7341 Review】+ Sensor Color Value Detection