8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI 25AA040A read and write E2PROM 4

Publisher:omega34Latest update time:2022-05-19 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Description


PART 3 SPI (SCLK, SDO, SDI) full duplex  

SCLK clock, SDO (MOSI) data output, SDI (MISO) data input

8051SPI and I2C also need to use IO simulation

There are 4 types of SPI bus transmission:

CPOL bit, high level: 1, low level: 0

CPHA bit, edge, read first then write: 0, write first then read: 1

When transmitting, transmit the high bit first and then the low bit

 

The experimental program uses SPI serial bus EEPROM, 25AA040A 

process

Read data: Write the read command first, then write the read address

Write data: first latch the write address, then use the write command, write the address, and then write the data

A8 If it is a 9-bit register, valid 1, 8-bit is 0


2. Demonstration

3. Timing


Command word

Read and write timing

Write Enable

Code:


/**

* 8051/2 DEMO 3

* Commonly used local communication methods

* 1. Serial port UART, baud rate: 9600

* When connecting a device, generally only GND RX TX is connected, and Vcc is not connected to avoid power supply conflicts with the target device.

* 1.1 RS485 standard (+2V ~ +6V: 1 / -6V ~ -2V: 0)

* 1.2 RS232 standard (-15V ~ -3V: 1 / +3V ~ +15V: 0), MAX232 needs to perform level inversion before communication with MCU

* 1.3 TTL standard (2.4V--5V:1 / 0V--0.5V:0)

* 2.I2C(SCL,SDA)

* 3. SPI (SCLK, SDO, SDI) full duplex

*/

 

#include "REG52.H"

#include

 

typedef unsigned char U8;

 

/** 

* PART 3 SPI (SCLK, SDO, SDI) full duplex  

* SCLK clock

* SDO (MOSI) data output

* SDI (MISO) data input

* 8051SPI and I2C also need to use IO simulation

* There are 4 types of SPI bus transmission:

* CPOL bit, high level: 1, low level: 0

* CPHA bit, edge, read first then write: 0, write first then read: 1

* When transmitting, transmit the high bit first and then the low bit

* The experimental program uses SPI serial bus EEPROM, 25AA040A 

* process

* Read data: Write the read command first, then write the read address

* Write data: first latch the write address, then use the write command, write the address, and then write the data

* A8 If it is a 9-bit register, valid 1, 8-bit is 0

**/

 

#define READ 0x03

#define WRITE 0x02

#define WREN 0x06

 

 

sbit SCK = P2^4;

sbit SI = P2^5;

sbit SO = P2^6;

sbit CS = P2^7;

 

 

//Serial port initialization - for printing data

void uart_init()

   SCON = 0x50;

   TMOD = 0x20; //Select timer 1

   //Install the high 8 bits and low 8 bits of the baud rate, 9600

   TH1 = 0xFD;

   TL1 = 0xFD;

   ES = 1; //Open serial port interrupt

   TR1 = 1; //Turn on timer 1

   EA = 1; // Enable general interrupt

}

 

//Send a character

void uart_send_char(char str)

{

   SBUF = str; //Put the string to be sent into the buffer

   while(TI==0); //Wait for sending to complete

   TI=0; // Clear the send interrupt flag

}

 

//Serial port interrupt service processing 

void uart_handle() interrupt 4

}

 

 

//Write 1 byte

void spi_write(U8 dat)

{

  U8 i=0;

  for(i=0;i<8;i++)

  {

     SCK = 0; //Read SI data when a rising edge occurs

 

     // Take the data bits in sequence and give them to SI

     SI=given>>7;

  that=that<<1;

 

     SCK = 1;

  }

}

 

//After reading the rising edge, read the data

U8 spi_read()

{

  U8 i=0;

  U8 that=0;

  for(i=0;i<8;i++)

  {

      SCK = 0;

      SCK = 1;

      //Put the data bits into SO in sequence

      dat <<= 1;

that|=SO;

  }

  return that;

}

 

//Read eeprom operation

U8 spi_device_read(U8 addr)

   U8 that=0;

   CS = 0;

   spi_write(READ);

   spi_write(addr);

   that = spi_read();

   CS = 1; // Pull CS back to high level after reading

   return that;

}

 

void delay5ms() //Error -0.000000000001us

{

    unsigned char a,b;

    for(b=15;b>0;b--)

        for(a=152;a>0;a--);

}

 

 

//Write eeprom operation

void spi_device_write(U8 addr,U8 dat){

   //Write ready

   CS = 0;

   spi_write(WREN); 

   CS = 1; //pull high

 

   CS = 0;

   spi_write(WRITE); //Use write command

   spi_write(addr);

   spi_write(dat);

   //Whether the write is successful or not, delay 5ms

   delay5ms();

   CS = 1;

}

 

void delay1s() //error 0us

{

    unsigned char a,b,c;

    for(c=167;c>0;c--)

        for(b=171;b>0;b--)

            for(a=16;a>0;a--);

}

 

 

void main()

{

   U8 res1;

   //Initialize the serial port to display the data to the serial port

   uart_init();

   

   //Write data, address 1, write value 2

   delay1s();

   spi_device_write(1,0x11);

   delay1s();

   res1 = spi_device_read(1);

 

   //Send to the serial port

   uart_send_char(res1);

 

   while (1){

   }

}

Keywords:MCU Reference address:8051/2 microcontroller commonly used local communication methods UART, RS485, I2C, SPI 25AA040A read and write E2PROM 4

Previous article:8051/2 single chip microcomputer DHT11 air temperature and humidity sensor value
Next article:Common local communication methods for 8051/2 microcontrollers: UART, RS485, I2C, SPI, I2C E2PROM 3

Recommended ReadingLatest update time:2024-11-22 20:05

AsiaInfo Electronics Launches Latest EtherCAT Slave Dual-core Microcontroller Solution
AsiaInfo's AX58400 EtherCAT slave dual-core microcontroller is equipped with the highest performance 480MHz ARM® Cortex®-M7 core in the ARM® Cortex®-M series and a 240MHz ARM® Cortex®-M4 core that can operate in parallel; the EtherCAT slave controller integrates two 100M Ethernet PHYs that can support both fib
[Embedded]
AsiaInfo Electronics Launches Latest EtherCAT Slave Dual-core Microcontroller Solution
PIC18F452 MCU Matrix Keyboard
#include       //Call the header file, you can go to the PICC18 software to find PIC18FXX2.H __CONFIG(1,XT);         //Crystal oscillator is external 4M __CONFIG(2,WDTDIS);     //Watchdog off __CONFIG(4,LVPDIS);     //Disable low voltage programming #define B7 RB7 #define B6 RB6 #define B5 RB5 #define
[Microcontroller]
Using the port address of the microcontroller to control the LCD display
In many occasions where single-chip microcomputers are used for control, in order to improve the human-machine interface, liquid crystal displays are often used to display the working status of the control mechanism and various parameter information for operators to make decisions. Since the liquid crystal display i
[Microcontroller]
Using the port address of the microcontroller to control the LCD display
Nokia 7.2 renderings exposed again: Oreo triple rear camera + ultra-thin design
 It was previously reported that the new Nokia 6.2/7.2 mobile phones may be unveiled at IFA 2019, and recently foreign media once again released a set of renderings of Nokia 7.2.       From the leaked images, it is not difficult to see that Nokia 7.2 will adopt an ultra-thin design, a rear Oreo triple-camera module
[Mobile phone portable]
STC15F2K60S2 reads DS18B20 temperature serial port display
//************************** //Program description: stc15f2k60s2 collects ds18b20 temperature data and outputs it from the serial port //To facilitate debugging, here is the project download address of the program: //The crystal oscillator used is 22.1184M //************************** #include stc15fxxxx.h //Call s
[Microcontroller]
STC15F2K60S2 reads DS18B20 temperature serial port display
Application of C8051F020 in LED display control system
0 Preface In the design of single-chip microcomputer system, the design of LCM (liquid crystal display module) human-computer interaction interface is often a very important link. LCM can be used to display setting parameters, status prompts, test results and parameters to be input on intelligent instruments. In com
[Microcontroller]
Application of C8051F020 in LED display control system
The third assignment of single chip microcomputer
Homework Question 7: Compare whether the number in R0 is greater than 10H. If it is equal, assign 0 to R1; if it is less than 10H, assign FF to R1; if it is greater than 10H, assign AAH to R1. 1. First  create the asm file 7.asm using fileànew MOV A,R0 CJNE A,#10H,L1 MOV R1,#0 AJMP L3 L1:JC L2     MOV R1,#0AAH
[Microcontroller]
Application of PIC16LF874 microcontroller in capacitance measurement module
Capacitive sensors have been widely used in industry, medicine, military and other fields. However, most of the current capacitance measurement methods have low integration level and low precision, so accurate measurement of capacitance, especially small capacitance, has always been a very important content. The osc
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号