51 MCU simulation SPI bus communication program and proteus simulation

Publisher:电竞狂人Latest update time:2019-11-15 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Share a 51 microcontroller simulation SPI bus to communicate program and simulation
schematic diagram as follows
0.png

The microcontroller source program is as follows:

#include //Header file containing microcontroller registers

#include //Header file containing _nop_() function definition

//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    delay1ms();

}

/************************************************************

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

……………………


Reference address:51 MCU simulation SPI bus communication program and proteus simulation

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号