51 MCU Introduction - SPI Bus

Publisher:bemaiiLatest update time:2022-04-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

UART, I2C and SPI are the three most commonly used communication protocols in microcontroller systems.

1. Introduction

         

SPI is a high-speed, full-duplex, synchronous communication bus. The standard SPI uses only 4 pins and is commonly used for communication between microcontrollers and EEPROM, FLASH, real-time clocks, digital signal processors and other devices. The SPI communication principle is simpler than I2C. It is mainly a master-slave communication mode. This mode usually has only one host and one or more slaves. The standard SPI has 4 wires, namely SSEL (chip select, also written as SCS), SCLK (clock, also written as SCK), MOSI (master output slave input Master

 

Output/Slave Input) and MISO (Master Input/Slave Output).

SSEL: Slave chip select enable signal. If the slave device is low level enabled, when this pin is pulled low, the slave device will be selected, and the host will communicate with the selected slave.


SCLK: Clock signal, generated by the host, somewhat similar to the SCL of I2C communication.


MOSI: The channel through which the host sends instructions or data to the slave. MISO: The channel through which the host reads the status or data of the slave.


2. Working mode

       

The host of SPI communication is also our microcontroller. There are four modes in the process of reading and writing data timing;

       

CPOL: Clock Polarity, which is the polarity of the clock. The entire communication process is divided into idle time and communication time. If the idle state of SCLK before and after data transmission is high, then CPOL=1, if the idle state SCLK is low, then CPOL=0.

       

CPHA: Clock Phase, which is the phase of the clock.


#include

 

typedef unsigned char uchar;

 

sbit DS1302_CE = P1^7;

 

sbit DS1302_CK = P3^5;

 

sbit DS1302_IO = P3^4;


struct sTime //Date and time structure definition

 

{

 

  unsigned int year; //year

 

  unsigned char mon; //month

 

  unsigned char day; //day

 

  unsigned char hour; //hour

 

  unsigned char min; // points

 

  unsigned char sec; //seconds

 

  unsigned char week; //week

 

};

 

 

 

/* Send a byte to the DS1302 communication bus */

 

void DS1302ByteWrite(uchar dat)

 

{

 

  uchar mask;

 

 

 

  for (mask = 0x01; mask != 0; mask <<= 1) // Low bit first, shift out one by one

 

  {

 

    if ((mask & dat) != 0) // first output the data bit

 

    {

 

      DS1302_IO = 1;

 

    }

 

    else

 

    {

 

      DS1302_IO = 0;

 

    }

 

    DS1302_CK = 1; //Then pull the clock high

 

    DS1302_CK = 0; //Pull the clock down again to complete a bit operation

 

  }

 

  DS1302_IO = 1; //Finally make sure to release the IO pin

 

}

 

/* Read a byte from the DS1302 communication bus */

 

uchar DS1302ByteRead()

 

{

 

  uchar mask;

 

  uchar dat = 0;

 

 

 

  for (mask = 0x01; mask != 0; mask <<= 1) //low bit first, read bit by bit

 

  {

 

    if (DS1302_IO != 0) //First read the current IO pin and set the corresponding bit in dat

 

    {

 

      dat |= mask;

 

    }

 

    DS1302_CK = 1; //Then pull the clock high

 

    DS1302_CK = 0; //Pull the clock down again to complete a bit operation

 

  }

 

  return dat; //Finally return the read byte data

 

}

 

/* Use a single write operation to write a byte to a register, reg-register address, dat-byte to be written*/

 

void DS1302SingleWrite(uchar reg, uchar dat)

 

{

 

  DS1302_CE = 1; // Enable chip select signal

 

  DS1302ByteWrite((reg << 1) | 0x80); //Send write register instruction

 

  DS1302ByteWrite(dat); //Write byte data

 

  DS1302_CE = 0; //Disable chip select signal

 

}

 

/* Use a single read operation to read a byte from a register, reg-register address, return value-read byte*/

 

uchar DS1302SingleRead(uchar reg)

 

{

 

  uchar dat;

 

 

 

  DS1302_CE = 1; // Enable chip select signal

 

  DS1302ByteWrite((reg << 1) | 0x81); //Send the read register instruction

 

  dat = DS1302ByteRead(); //Read byte data

 

  DS1302_CE = 0; //Disable chip select signal

 

 

 

  return dat;

 

}

 

/* Use burst mode to continuously write 8 register data, dat-data pointer to be written*/

 

void DS1302BurstWrite(uchar *dat)

 

{

 

  uchar i;

 

 

 

  DS1302_CE = 1;

 

  DS1302ByteWrite(0xBE); //Send burst write register instruction

 

  for (i = 0; i < 8; i++) //Continuously write 8 bytes of data

 

  {

 

    DS1302ByteWrite(dat[i]);

 

  }

 

  DS1302_CE = 0;

 

}

 

/* Use burst mode to continuously read the data of 8 registers, dat-read data receiving pointer*/

 

void DS1302BurstRead(uchar *dat)

 

{

 

  uchar i;

 

 

 

  DS1302_CE = 1;

 

  DS1302ByteWrite(0xBF); //Send burst read register instruction

 

  for (i = 0; i < 8; i++) //Read 8 bytes continuously

 

  {

 

    dat[i] = DS1302ByteRead();

 

  }

 

  DS1302_CE = 0;

 

}

 

/* Get real-time time, that is, read the current time of DS1302 and convert it into time structure format*/

 

void GetRealTime(struct sTime *time)

 

{

 

  uchar buf[8];

 

 

 

  DS1302BurstRead(buf);

 

  time->year = buf[6] + 0x2000;

 

  time->mon = buf[4];

 

  time->day = buf[3];

 

  time->hour = buf[2];

 

  time->min = buf[1];

 

  time->sec = buf[0];

 

  time->week = buf[5];

 

}

 

/* Set the real time, convert the set time in the time structure format into an array and write it into DS1302*/

 

void SetRealTime(struct sTime *time)

 

{

 

  uchar buf[8];

 

 

 

  buf[7] = 0;

 

  buf[6] = time->year;

 

  buf[5] = time->week;

 

  buf[4] = time->mon;

 

  buf[3] = time->day;

 

  buf[2] = time->hour;

 

  buf[1] = time->min;

 

  buf[0] = time->sec;

 

  DS1302BurstWrite(buf);

 

}

 

/* DS1302 initialization, if power failure occurs, reset the initial time*/

 

void InitDS1302()

 

{

 

  uchar dat;

 

  struct sTime code InitTime[] = //Tuesday, May 18, 2016, 9:00:00

 

  {

 

    0x2016, 0x05, 0x18, 0x09, 0x00, 0x00, 0x02

 

  };

 

 

 

  DS1302_CE = 0; //Initialize DS1302 communication pin

 

  DS1302_CK = 0;

 

  dat = DS1302SingleRead(0); //Read the seconds register

 

  if ((dat & 0x80) != 0) //Judge whether DS1302 has stopped by the value of the highest bit CH of the seconds register

 

  {

 

    DS1302SingleWrite(7, 0x00); //Remove write protection to allow data to be written

 

    SetRealTime(&InitTime); //Set DS1302 to the default initial time

 

  }

 

}


Reference address:51 MCU Introduction - SPI Bus

Previous article:51 MCU Introduction - DS18B20 Temperature Sensor
Next article:51 MCU Introduction - UART Serial Port

Recommended ReadingLatest update time:2024-11-24 19:20

51 MCU (AT89C52) timer
 #include #define uchar unsigned char #define uint unsigned int flying i,temp; void init() { TMOD=0x01; TH0=(65536-46080)/256; TL0=(65536-46080)%256; ET0=1; EA=1; TR0=1; } void main() { i=0; temp=0x01; heat(); while(1); } void timer0() interrupt 1 { TH0=(65536-46080)/256;
[Microcontroller]
51 MCU (AT89C52) timer
51 single chip microcomputer-----------four-way traffic light
1. Experimental purpose: Experiment 4: Using AT89C51 chip to realize four-way traffic lights: (1) Master the method of programming microcontroller control programs using C language. (2) Master the methods of writing, compiling and debugging programs using Keil4 software. (3) Master the use of Proteus software to draw
[Microcontroller]
51 single chip microcomputer-----------four-way traffic light
51 MCU UART communication program
Special registers used:   SM0 SM1: Operation mode control 01 Operation mode 1 1 start bit 8 data bits 1 stop bit Variable baud rate TI: Transmit interrupt bit TI=1 Transmitting is completed, software sets to 0 RI: Receive interrupt bit RI=1 Receiving is completed, software sets to 0 REN: Receive enable SBUF: B
[Microcontroller]
51 MCU UART communication program
51 single chip digital frequency meter - with simulation file
The digital frequency meter made of 51 single chip microcomputer, source code and simulation diagram can be downloaded from the link below: http://www.51hei.com/f/51 Digital Frequency Meter.rar #include reg52.h #include intrins.h #define uint unsigned int  #define uchar unsigned char unsigned char code dispbit =
[Microcontroller]
Proportional Electromagnet Control Technology Based on 51 Single Chip Microcomputer
introduction As an actuator, the proportional solenoid is one of the key products of mechatronics and is widely used in various automatic control systems. The proportional solenoid has a large thrust, simple structure, convenient maintenance, and low cost. It is a widely used electro-mechanical converter .
[Microcontroller]
Proportional Electromagnet Control Technology Based on 51 Single Chip Microcomputer
51 MCU T0 timer application 1
1. Experimental task:   Use the timer/counter T0 of the AT89S51 single-chip microcomputer to generate a one-second timing time as the second counting time. When one second is generated, the second count increases by 1. When the second count reaches 60, it automatically starts from 0. The hardware circuit is shown in t
[Microcontroller]
51 MCU T0 timer application 1
51 single chip microcomputer learning - 5-- independent buttons
principle Button Introduction A touch switch is an electronic switch. When in use, lightly press the switch button to turn on the switch. When you release your hand, the switch is turned off. The switch we use is shown in the figure below: Independent button principle When the key is closed and opened, the contact
[Microcontroller]
51 single chip microcomputer learning - 5-- independent buttons
Small RTOS51 realizes the design of temperature controller based on single chip microcomputer
At present, 8-bit single-chip microcomputers still occupy an important position in the field of measurement and control and the application of intelligent electronic products. The application of embedded real-time operating system (ERTOS) will greatly facilitate the software development of 8-bit single-chip microco
[Microcontroller]
Small RTOS51 realizes the design of temperature controller based on single chip microcomputer

Recommended posts

Several questions about MmwaveStudio
Question1:WhataretheparametersfortheSensorconfigurationoptioninMmwaveStudioandwhatareitsmeanings? Thereissomeconfusionwhenconfiguringthemmwavestudiosensorconfigurationoptions.Thecompletesensorconfigurationtabissho
qwqwqw2088 Analogue and Mixed Signal
C6678 on-chip storage space allocation mechanism
ThestackspaceonembeddeddevicessuchasDSPisattheKblevel.Whendefininganarrayorapplyingforspacewithinafunction,youcannotdefineandapplyforitdirectlyasinLinux.Youmusteitherdefineitgloballyorpointtoawell-allo
Jacktang Microcontroller MCU
A question about understanding electric current again
Ionceansweredaquestionfromanetizen:"Ifaninsulatingobjectwithastaticelectricitycoremovesathighspeed,eitherinastraightlineorinacircle,willamagneticfieldbeformedaroundit?" Myansweratthetimewas:"Itshouldbe
bigbat Power technology
DIY Wireless Charging Cool Light
Whileonvacationathome,IusedoldcomponentsandmaterialstoDIYasimpleandcoollampwithwirelesscharging. Materialsused: Fakefruitearphoneswirelesschargingbox 3VLEDLightBoard 20ohmresistor Toggle
dcexpert MicroPython Open Source section
Selection Tips | How to choose a suitable power amplifier for the experiment? What parameters should be paid attention to?
SelectionTips|Howtochooseasuitablepoweramplifierfortheexperiment?Whatparametersshouldbepaidattentionto? Whatindicatorparametersshouldwepayattentionto? Didn'tsayanything Haha~Sincethevideoisshort,Imay
aigtekatdz Test/Measurement
How to prevent the phase-shifted full-bridge DCDC from crashing when starting or increasing or decreasing power?
Purpose:Duetomanufacturingprocessandotherreasons,wewanttoreducethepossibilityofpowersupplycrashingtozerobytestingandaddingprotection.Evenifthepowersupplyitselfhasabnormalcomponents,itwillnotcrash.Currently,only
DKandDK Switching Power Supply Study Group
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号