Share a 51 microcontroller simulation SPI bus to communicate program and simulation
schematic diagram as follows
The microcontroller source program is as follows:
#include #include //x5045 pin definition sbit SCK=P3^4; //Define the SCK bit as P3.4 pin sbit SI=P3^5; //Define the SI bit as P3.5 pin sbit SO=P3^6; //Define the SO bit as P3.6 pin sbit CS=P3^7; //Define the SCK bit as P3.7 pin //Function variable definition #define WREN 0x06 //Write enable latch allows #define WRDI 0x04 //Write enable latch disabled #define WRSR 0x01 //Write status register #define READ 0x03 //Read #define WRITE 0x02 //Write /************************************************************ Function: Delay 1ms (3j+2)*i=(3×33+2)×10=1010(microseconds), which can be considered as 1 millisecond *******************************************************/ void delay1ms() { unsigned char i,j; for(i=0;i<10;i++) for(j=0;j<33;j++) ; } /************************************************************ Function: Delay for several milliseconds Entry parameter: n *******************************************************/ void delaynms(unsigned char n) { unsigned char i; for(i=0;i } /************************************************************ Function: Read data from the current address of X5045 Export parameter: x *******************************************************/ unsigned char ReadCurrent(void) { unsigned char i; unsigned char x=0x00; //Store the data read from X5045 SCK=1; //Put SCK in a known high state for(i = 0; i < 8; i++) { SCK=1; //Pull SCK high SCK=0; //Output data on the falling edge of SCK x<<=1; //Shift each binary bit in x one position to the left, because the highest bit of the byte is read first x|=(unsigned char)SO; //Store the data on SO into x through bitwise "OR" operation } return(x); //Return the read data } /************************************************************ Function: Write data to the current address of X5045 Entry parameter: dat *******************************************************/ void WriteCurrent(unsigned char dat) { unsigned char i; SCK=0; //Put SCK in a known low state for(i = 0; i < 8; i++) // Loop through 8 bits { SI=(bit)(dat&0x80); //Send the highest bit data to S through bitwise "AND" operation //Because the high bit is in front and the low bit is in the back when transmitting SCK=0; SCK=1; //Write data on the rising edge of SCK dat<<=1; //Shift each binary bit in y one position to the left, because the highest bit of the byte is written first } } /************************************************************ Function: Write status register, can set watchdog overflow time and data protection Entry parameter: rs; //Store register status value *******************************************************/ void WriteSR(unsigned char rs) { CS=0; //Pull CS low and select X5045 WriteCurrent(WREN); //Write enable latch allows CS=1; //Pull CS high CS=0; //Pull CS low again, otherwise the following write register status instruction will be discarded WriteCurrent(WRSR); //Write status register WriteCurrent(rs); //Write the newly set register status value CS=1; //Pull CS high } /************************************************************ Function: Write data to the specified address of X5045 Entry parameter: addr *******************************************************/ void WriteSet(unsigned char dat,unsigned char addr) { SCK=0; //Put SCK in a known state CS=0; //Pull CS low and select X5045 WriteCurrent(WREN); //Write enable latch allows CS=1; //Pull CS high CS=0; //Pull CS low again, otherwise the following write instruction will be discarded WriteCurrent(WRITE); //Write instruction WriteCurrent(addr); //Write to the specified address WriteCurrent(dat); //Write data CS=1; //Pull CS high SCK=0; //Put SCK in a known state } /************************************************************ Function: Read data from the specified address of X5045 Entry parameter: addr Export parameter: dat *******************************************************/ unsigned char ReadSet(unsigned char addr) { unsigned char dat; SCK=0; //Put SCK in a known state CS=0; //Pull CS low and select X5045 WriteCurrent(READ); //Start reading WriteCurrent(addr); //Write to the specified address dat=ReadCurrent(); //Read data CS=1; //Pull CS high SCK=0; //Put SCK in a known state return dat; //Return the read data } /************************************************************ Function: Watchdog reset program *******************************************************/ void WatchDog(void) { CS=1; //Pull CS high CS = 0; //A falling edge on the CS pin resets the watchdog timer ……………………
Previous article:Source program circuit diagram of optical drive laser engraving machine based on 51 single chip microcomputer + host computer
Next article:ESP8266+51 MCU WIFI control multi-channel remote control switch program design
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- 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
- Electrochemical sensor signal processing circuit issues
- EEWORLD University Hall----Live Replay: TI Ultrasonic Gas Flow Measurement Innovation Solution
- [NXP Rapid IoT Review] +5. Write a simple project
- C2000 MCU, Vienna Rectifier-Based Three-Phase Power Factor Correction Reference Design
- STC89C52RC MCU realizes serial port printing function
- ADI Flow Cytometer Solutions
- Simple comparison of Cortex series M0-4
- Ultra-low power Bluetooth controlled, cost-effective, dimmable smart lighting solution
- December 26, let’s watch the sun together
- Implementing the Internet of Things with Bluetooth 4.2