Serial Interface SPI interface application design

Source: Internet
Author: User

Author: Ma Chao
The Synchronous Serial three-line SPI interface can be easily connected to the peripheral or another piece of avr mcu using the SPI communication protocol, achieving high-speed synchronous communication within a short distance. The full-duplex, three-line synchronous byte-oriented data transmission (SPI) communication is implemented by using hardware. It supports the master, slave, and two kinds of SPI time series with different polarity. There are 7 kinds of communication rate options, the maximum speed of the host mode is 1/2 system clock, and the maximum speed of the slave mode is 1/4 system clock.

The internal SPI interface of the ATmega128 microcontroller is also used for programming download and upload of program memory and data E2PROM. Note that, in this case, the MoSi and miso interfaces of SPI are no longer mapped to the PBS and PB3 pins, but instead to the pe0 and pe1 pins (PDI and PdO ), for details, see the serial programming and verification of program memory in Chapter 2.

The SPI of ATmega128 is used for hardware interface and transmission to complete the interrupt request. Therefore, an effective method to transmit data using SPI is to use the interrupt mode + data cache. Note the following points during SPI initialization:

Correctly select and set the host or slave, as well as the working mode (polarity) and data transmission rate;

. Pay attention to the sequence of transmitted bytes, whether it is LSB first or MSB frist );

Correctly set the input and output directions of MoSi and miso interfaces. The input pin uses the pull-up resistor, which can save the high resistance on the bus.

The following is a routine for sending (receiving) bytes in the SPI Host Mode:

 

# Define size 100
Unsigned char spi_rx_buff [size];
Unsigned char spi_tx_buff [size];
Unsigned char rx_wr_index, rx_rd_index, rx_counter, rx_buffer_overflow;
Unsigned char tx_wr_index, tx_rd_index, tx_counter;

# Pragma interrupt_handler spi_stc_isr: 18
Void spi_stc_isr (void)
......{
Spi_rx_buff [rx_wr_index] = SPDR; // read the received bytes from the ISP port.
If (++ rx_wr_index = size) rx_wr_index = 0; // put it into the receiving buffer and adjust the queue pointer
If (++ rx_counter = size)
......{
Rx_counter = 0;
Rx_buffer_overflow = 1;
}
If (tx_counter) // if the sending buffer contains data to be sent
......{
-- Tx_counter;
SPDR = spi_tx_buff [tx_rd_index]; // send a byte of data and adjust the pointer
If (++ tx_rd_index = size) tx_rd_index = 0;
}
}

Unsigned char getspichar (void)
......{
Unsigned char data;
While (rx_counter = 0); // No data is received, wait
Data = spi_rx_buff [rx_rd_index]; // retrieve the data received by an SPI from the receiving buffer
If (++ rx_rd_index = size) rx_rd_index = 0; // adjust the pointer
CLI ();
-- Rx_counter;
SEI ();
Return data;
}

Void putspichar (char C)
......{
While (tx_counter = size); // The sending buffer is full, waiting
CLI ();
If (tx_counter | (spsr & 0x80) = 0) // The sending buffer contains data to be sent.
... {// Or when SPI is sending data
Spi_tx_buffer [tx_wr_index] = C; // put data into the sending buffer to queue
If (++ tx_wr_index = size) tx_wr_index = 0; // adjust the pointer
++ Tx_counter;
}
Else
SPDR = C; // The sending buffer is hollow and the SPI port is idle. It is directly placed into the SPDR and sent by the SIP port.
SEI ();
}

Void spi_init (void)
......{
Unsigned chat temp;
Ddrb | = 0x080; // miso = input and MoSi, sck, SS = Output
Portb | = 0x80; // valid miso pull-up Resistance
Spcr = 0xd5; // SPI enabled, host mode, MSB enabled, SPI interrupted, polar mode 01,1/16 system clock rate
Spsr = 0x00;
Temp = spsr;
Temp = SPDR; // clear the SPI and interrupt flag to make the SPI idle
}

Void main (void)
......{
Unsigned char I;
CLI (); // disconnected
Spi_init (); // initialize the SPI interface
SEI (); // interrupt
While ()
......{
Putspichat (I); // sends a byte
I ++;
Getspichar (); // receives one byte (the first byte is null)
.........
}
}

 

This typical SPI routine is relatively simple. The main program first initializes the hardware SPI of the ATmega128. During initialization, Mosi, sclk, and SS pins of portb are used as outputs, Miso is used as input pins, And the pull-up resistor is turned on. Then, initialize the SPI registers and empty the spsr and SPDR registers (the SPDR operation will be automatically cleared after the spsr is read ), enable the ISP to wait for data sending.

The avr spi consists of a 16-bit cyclic shift register. when the data is removed from the host, the data from the slave is also migrated, therefore, SPI transmission and receipt are completed in an interrupted service. In the SPI interrupt service program, read a received byte from the SPDR and store it in the receiving data buffer. Then, extract a byte from the sending data buffer and write it into the SPDR. the ISP sends it to the slave machine. Once data is written to SPDR, the ISP hardware starts to send data. When the next ISP is interrupted, the sending is completed and a data is received at the same time. Similar to the usart interface described in this chapter, putspichar () and getspichar () in the program are the underlying interface functions of the application (the SPI driver is the SPI interrupt service program ), two data buffers are used to form a cyclic queue. This idea of program design not only has a complete program structure, but also solves the contradiction between high-speed MCU and low-speed serial port, so as to achieve Parallel Running of tasks in the program, this improves the MCU operation efficiency.

This routine is an example of batch output and input data through SPI. You can use a piece of ATmega128 to connect the MoSi and miso pins to form an ISP interface self-receiving system, demonstrate and verify the program. Note that the actual received bytes are the data sent during the last interruption, that is, the first received byte is a NULL byte.

Read and understand the processing ideas of the program. The reader can modify the program as needed, which is suitable for the actual system. For example, in actual application, the external slave is a SPI interface temperature chip. The Protocol is as follows: the host must first send three bytes of commands consecutively, then, a byte of data is returned from the machine. The user program can call the putspichar () function cyclically four times, send the Three-byte command and one byte of NULL data to the slave machine, and wait for a while, after processing other operations, call the getspichar () function four times, read four bytes from the receiving data buffer consecutively, and discard the first three NULL bytes, 4th bytes indicates the data returned from the slave.

========================================================== ============

Another day I wrote a spi program about DSP designed ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.