Freescale 16-bit MCU (Thirteen) - SPI module test

Publisher:pcwgLatest update time:2021-07-19 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. SPI module introduction

         The Serial Peripheral Interface (SPI) module provides full-duplex, synchronous, serial communication between the microcontroller and peripheral devices. These peripheral devices can include other microcontrollers, analog-to-digital shifters, shift registers, sensors, and memories. The SPI can run at a baud rate of up to 2 divided by the bus clock in master mode and up to 4 divided by the bus clock in slave mode.


        The central element of SPI is the SPI shift register. Data is written to the double-buffered transmitter (written to SPIDR) and then transferred to the SPI shift register at the start of the data transfer. After conversion in the data byte, the data is transferred to the double-buffered receiver, where the data can be read (read from SPIDR). The pin multiplexing logic controls the connection between the microcontroller pins and the SPI module. The functional block diagram of SPI is shown in the figure below.

        When the SPI is configured as a master SPI, the clock output is routed to the SCK pin, the shifter output is routed to MOSI, and the shifter input is routed out of the MISO pin. When the SPI is configured as a slave SPI, the SCK pin is the clock output, MOSI is the shifter output, and the MISO pin is the shifter input.


       In an external SPI system, just connect all the SCK pins to each other, all the MISO pins to each other, and all the MOSI pins to each other. Peripheral devices often use slightly different names for these pins.


       AT25040N is an EEPROM memory with a storage capacity of 4K. In this experiment, the SPI interface is used to read and write data. The schematic diagram of the hardware circuit is shown in the figure below.

        The SPI interface consists of 4 pins, namely SCK, MOSI, MISO, and CS.


SCK — SPI serial clock


When the SPI is enabled as a slave SPI, this pin is the serial clock input. When the SPI is enabled as a master SPI, this pin is the serial clock output.


MOSI — Master Data Out, Slave Data In


When the SPI is enabled as a master SPI, this pin is the serial data output. When the SPI is enabled as a slave SPI, this pin is the serial data input. When the single-wire bidirectional mode is selected and the master mode is selected, this pin becomes a bidirectional data I/O pin (MOMI).


MISO — Master Data In, Slave Data Out


When the SPI is enabled as a master SPI, this pin is the serial data input. When the SPI is enabled as a slave SPI, this pin is the serial data output. If the single-wire bidirectional mode is selected and the slave mode is selected, this pin becomes a bidirectional data I/O pin (SISO).


CS — Select from


During an SPI transfer, data is sampled (read) and transferred from the MOSI pin on an SCK edge, and the bit value on the MOSI pin is changed after half an SCK cycle. After 8 SCK cycles, the data in the master SPI's shift register has been transferred from the MOSI pin to the slave SPI, and the 8-bit data in the MISO pin is transferred to the master SPI's shift register. At the end of the transfer, the received data byte is transferred from the shifter to the receive data buffer, and SPRF is set, and the display data can be read by reading SPIDR. If there is another data byte waiting in the transmit buffer at the end of the transfer, it is moved to the shifter, SPTEF is set, and a new transfer is started.


Here we introduce the AT25040N communication mode, which is as follows:

Before the microcontroller writes data to AT25040N, it must first write the "Write Enable" command to AT25040N. The command format is as follows:

The format of writing data from the microcontroller to the AT25040N is as follows. The written data consists of at least 3 bytes of commands. The first byte is the "op-code" command. The "A" bit of this byte is the A8 bit in the AT25040N address. Because the AT25040N is an EEPROM with a storage space of 4Kb, it can store 512 bytes of data. Each byte corresponds to an address, which needs to be represented by a 9-bit address, namely "A8-A0". After the "op-code" is "A7-A0", followed by the data to be transmitted.

The format of the data read by the AT25040N microcontroller is as follows: First, the "op-code" command, followed by the lower 8 bits of the read data address, and then the data to be read.

2. Routine Test

In this experiment, we use SPI to write a byte of data into the AT25040 chip, and then read the data out to verify it. The SPI initialization code is shown below.


void INIT_SPI(void) 

{

  HOLD_dir = 1;

  CS_dir = 1;

 

  SPI0CR1 = 0b01010000; // Enable SPI, disable interrupts, clock high valid, phase 0;

  SPI0CR2 = 0x00; //SS pin is normal I/O, bidirectional mode;

  SPI0BR = 0x70; //Set the SPI clock frequency to 2MHz;

  CS = 1;

}


The initialization code sets the SPI to the same mode as the AT25040 chip mode, sets the CS pin to a normal pin, and finally sets the communication frequency through program control.


Below is the code for SPI sending.


void SPI_send(unsigned char data) 

{

  while(!SPI0SR_SPTEF);

  SPI0DRL = data; 

}

The code is relatively simple, showing whether the buffer is empty and writing the data to be sent when it is empty.


Below is the code for SPI receiving.


unsigned char SPI_receive(void) 

{

  unsigned char temp,data;

  while(!SPI0SR_SPIF);

  temp = SPI0SR;

  data = SPI0DRL;

  return(data);


The receiving code is also relatively simple. It waits for the flag to be set, then clears the flag by reading the flag register, and finally reads the data and returns it.


Below is the code of the main function.


void main(void) 

{

  DisableInterrupts; 

  INIT_PLL();

  LEDCPU_dir = 1;

  INIT_SPI();

  INIT_FM25040A();

  EnableInterrupts; 

  LEDCPU = 1;

 

  CS = 0;

  SPI_send(0x06);

  delay();

  CS = 1;

 

  delay();

  CS = 0;

  SPI_send(0x02);

  SPI_send(0x00);

  SPI_send('E'); //Store one byte of data

  delay();

  CS = 1;

 

  longdelay();

  CS = 0;

  SPI_send(0x03);

  SPI_send(0x00);

  delay();

 

  SPI_send(0x00);

  delay();

 

  temp_SPI = SPI_receive(); //Read data so that the data in the shift register can be stored in SPI0DRL

  

  data_receive = SPI_receive(); //Read data

 

  CS = 1;

  if(data_receive == 'E') //Judge whether the data is correct

    LEDCPU = 0;

  

  for(;;);

}


In the main function, the program sends a byte of data to AT25040, reads the data, and then determines whether the data is correct. If correct, the LED light is turned on.


You can analyze the code according to the communication protocol of AT25040 above.


Download the program to the microcontroller and run it, and the LED light will turn on.

Keywords:Freescale Reference address:Freescale 16-bit MCU (Thirteen) - SPI module test

Previous article:Freescale 16-bit MCU (XIV) - CAN bus module test
Next article:Freescale 16-bit MCU (XII) - IIC module test

Recommended ReadingLatest update time:2024-11-16 16:40

Freescale MC9S12XS TIME input capture
Today I will show you how to learn input capture in the Freescale MC9S12X TIME module. The TIME module has been introduced before and will not be repeated here. First, let me introduce what input capture is. Input capture Input Capture (IC): Detects external events and records the time of selected input signal tra
[Microcontroller]
Freescale MC9S12XS TIME input capture
Freescale MC9S12X Time Output Comparison
Today I will show you the output comparison of Freescale MC9S12 Time module. The specific code is as follows /****************************************************************/ /* Initialize the phase-locked loop */ /* Use external crystal: 16MHz */ /* Set bus frequency: 16MHz */ /**************************************
[Microcontroller]
freescale MC9S12G128 SCI, printf usage
Serial Communication Interface (SCI) SCI asynchronous serial communication: Glossary of terms: IR: InfraRed IrDA: Infrared Design Associate IRQ: Interrupt Request LIN: Local Interconnect Network LSB: Least Significant Bit MSB: Most Significant Bit NRZ: Non-Return-to-Zero RZI: Return-to-Zero-Inverted RXD: Receive Pin S
[Microcontroller]
freescale MC9S12G128 SCI, printf usage
Freescale MCU automotive infotainment system solutions
Description of the program: Keeping pace with the rapid development of portable electronic products while focusing on their own stringent quality standards and vehicle life cycle requirements is a challenge faced by automakers and in-vehicle infotainment system suppliers. Freescale provides mature automotive in
[Embedded]
Freescale MCU automotive infotainment system solutions
Freescale S12x series PLL settings
pll :Phase Locking Loop  In the MC9S12XDP512 microcontroller I use Setting up the pll is very simple: CLKSEL = 0x00; //Set the current fBUS to use an external crystal PLLCTL = 0xe1; //Set to enter PLL setting mode //PLL calculation: //fPLL= 2*fOSC*(SYNDIV + 1)/(REFDIV + 1), // fBUS = fPLL/2 //where fOSC is th
[Microcontroller]
Freescale synchronous serial transmission SPI optimization design
Most of the Freescale series MCUs have an SPI module, which is a synchronous serial peripheral interface that allows the MCU to communicate with various peripheral devices in a serial manner.  At present, most Freescale series microcontroller buses cannot be expanded externally. When the on-chip I/O or memory cannot
[Microcontroller]
Freescale synchronous serial transmission SPI optimization 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号