Serial port control based on SPI device of single chip microcomputer

Publisher:zeta16Latest update time:2010-03-22 Source: 现代电子技术Keywords:SPI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

0 Introduction

Serial Peripheral Interface (SPI) is a high-speed synchronous serial input/output port. In recent years, it has been widely used in the expansion of external devices such as shift registers, D/A converters, A/D converters, serial E2PROMs, LED display drivers, etc. The SPI interface can be shared, which is convenient for forming a system with multiple SPI interface devices. Its transmission rate is programmable, the number of connecting lines is small, and it has good scalability.

1 SPI interface introduction

SPI is a synchronous serial communication interface introduced by Motorola, which is used for serial connection between microprocessors, microcontrollers and peripheral expansion chips. It has now developed into an industrial standard. At present, various semiconductor companies have launched a large number of chips with SPI interfaces and various functions, such as RAM, E2PROM, FLASH ROM, A/D converter, D/A converter, LED & LCD display driver, I/O interface chip, real-time clock, UART transceiver, etc., providing users with extremely flexible and inexpensive options for peripheral expansion. Since the SPI bus interface only occupies four I/O lines of the microprocessor, the use of the SPI bus interface can simplify the circuit design. It saves a lot of interface devices and I/O lines in conventional circuits and improves the reliability of the design. At present, some manufacturers' single-chip microcomputers have SPI interfaces, such as Philips' P89LPC900, Atmel's Atmega128, etc., but most single-chip microcomputers do not support SPI interfaces. In practical applications, due to considerations such as product volume, cost and scalability, designers often want to use single-chip microcomputers without SPI interfaces to control peripheral devices with SPI interfaces. Taking the AT89C2051 microcontroller simulating SPI bus operation serial AD7390 as an example, as shown in Figure 1, the implementation method of using the microcontroller's I/O port to simulate the SPI bus through software is introduced.

System Block Diagram

The four lines used by SPI are the serial clock line (SCK), the master input slave output line (MISO), the master output slave input line (MOSI), and the low-level effective enable signal line (CS). In this way, only 3 to 4 data lines and control lines are needed to expand various I/O devices with SPI interface.

The SPI bus has the following characteristics:

(1) Since there are fewer wires, the circuit design can be simplified. The parallel bus expansion method usually requires 8 data lines, 8 to 16 address lines, and 2 to 3 control lines. The SPI bus design only requires 4 data lines and control lines to complete the functions realized by parallel expansion.

(2) The devices are uniformly addressed and have nothing to do with the system address, so the SPI operation has good independence.

(3) Device operations follow unified specifications, making the system hardware and software universal.

2 Basic structure and working principle of AD7390

AD7390 is a 12-bit resolution voltage output digital/analog converter produced by AD1. This DAC provides users with a low-cost, high-precision controllable voltage solution, powered by a single power supply +3V system. The device guarantees that the power supply voltage is less than 100mA when it is 2.7~5.5V, so the device is suitable for some devices with high power requirements, such as battery-powered systems. The output voltage depends on the external reference input voltage. The DACOUT output is any value between REFIN and ground. Its internal block diagram is shown in Figure 2.

Internal Block Diagram

The internal double-buffered serial data interface provides high-speed conversion, and the microcontroller controls the device through a three-wire SPI, serial data (SDI), clock (CLK), and enable (LD) pins. In addition, the output can be cleared using the CLR input. The operation timing is shown in Figure 3.

Operation Timing

3 Serial port control of AD7390

The computer sends a control signal to the MCU via RS 232, and then the MCU controls the AD7390 to complete the entire setting process. The software flow of controlling the AD7390 via the serial port is shown in Figure 4. Once data is written to the MCU, the MCU will reset the AD7390.

Software flow of controlling AD7390 through serial port

The microcontroller program is divided into three modules: SPI control module, serial port receiving module and operation information feedback module.

The SPI control module generates CLK and LD, and ensures the stability of SDI data before the rising edge of CLK arrives, so that AD7390 can accurately collect data. The frequency of CLK clock is determined by the typical delay of the device. Different devices have different delays, so it determines that the clocks of different devices should be different.

The serial port receiving module receives data from the computer and saves the data. A MAX 232 completes the conversion between RS 232 level and TTL level. Because the data transmitted is continuous, when writing the receiving program, pay attention to the shielding of the interrupt application to accurately receive all the data.

When debugging with the serial port debugging assistant, pay attention to the format of the sent data. Whether it is sent in hexadecimal will affect the received data. For example, if the letter "E" is sent in hexadecimal, the actual data sent is "15", and the received data is the ASCII code of "15" "49", "53"; if it is not sent in hexadecimal, the ASCII code of the letter "E" is "69'. From this, we can see the difference between them. Therefore, if the receiver wants to get the desired data, it needs to be converted. The conversion can be seen in the following program.

The operation information feedback module completes the visual output of the operation results and outputs the set voltage value through internal calculation, which greatly facilitates the debugging process.

AD7390 is a 12-bit D/A converter. The device receives 16-bit data, and the data format is shown in Figure 5. The lower 12 bits are taken as valid data, and the upper 4 bits are automatically discarded. Because the microcontroller transmits data in bytes (8 bits), the 16-bit transmission mode is used.

Data Format

Some program codes are as follows:

program

program

4 Conclusion

Through the application of serial port, the control of SPI device becomes very convenient. Because the serial port occupies less resources, the more and more extensive application of SPI interface in integrated chip will make this control mode become the typical design in future systems.

Keywords:SPI Reference address:Serial port control based on SPI device of single chip microcomputer

Previous article:Design and implementation of infrared data transmission system based on USB2.0
Next article:Design of multi-parameter real-time acquisition and processing system controlled by single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 21:30

MSP430 SPI driver code design process
In daily work, if you use MSP430 as the main control chip, you often need to write SPI or I2C drivers to read and control peripherals (such as LCD screens and some sensors). In order to reduce repetitive work, this article summarizes the detailed steps of SPI driver writing with a specific example (using MSP430FR6989
[Microcontroller]
MSP430 SPI interface (mode) communication
SPI (Serial Peripheral Interface) is a synchronous serial data transmission standard proposed by Motorola and is widely used in many devices. 1. Interface The SPI interface is often referred to as a 4-wire serial bus, which works in master/slave mode, and the data transmission process is initiated by the host. As sh
[Microcontroller]
C51 simulates SPI interface
include /************************************************           Analog SPI interface I/O definition ********************************************/ sbit spi_cs=P1^2;   sbit spi_di=P1^0;   sbit spi_clk=P1^1; sbit spi_do=P1^3;  #define set_spi_cs  spi_cs=1   #define clr_spi_cs  spi_cs=0   #define set_spi_clk
[Microcontroller]
28. Introduction to SD card (SPI mode)——for mini version only
1. Introduction to SD Card 1. What is an SD card? 2. Characteristics of SD card 3. SD card register The CSD register includes the card capacity, the size of the data block, and very important registers. 2. How to access SD card in SPI mode 1. SD card command format in SPI mode 2. SD card command introduc
[Microcontroller]
28. Introduction to SD card (SPI mode)——for mini version only
How to use isoSPI data link to realize high-performance vehicle battery management system?
   introduction   One of the key factors in achieving high reliability, performance and long life for battery packs designed into HEV, PHEV and EV powertrains is the electronics used in the battery management system (BMS). To date, most battery pack designs have used centralized practical BMS hardware, which is limit
[Power Management]
How to use isoSPI data link to realize high-performance vehicle battery management system?
AVR microcontroller learning SPI serial port and DS1302
Introduction: Basic principles of synchronous serial port SPI Interface timing diagram of DS1302 Use of DS1302 Use of M16 SPI SPI Basic Principles SPI is the most commonly used synchronous serial interface: synchronization means using the clock line to synchronize the serial data and latch the data at the rising o
[Microcontroller]
AVR microcontroller learning SPI serial port and DS1302
Implementation of SPI bus of 51 single chip microcomputer
//-----------------------Function declaration, variable definition------------------------------------------------------ #include reg51.h #include intrins.h sbit SCK=P1^0; // Output the analog clock of port p1.0 sbit MOSI=P1^1; // Output the p1.1 port analog host sbit MISO=P1^2; // Simulate host input on port
[Microcontroller]
51 MCU-SPI interface
SPI (Serial Peripheral Interface) is a synchronous serial interface that uses a clock line to synchronize data bits, and the rising and falling edges of the clock latch data. There are two types of SPI:     Four-wire SPI: CS, SCK (synchronous clock), MOSI (master out slaver in), MISO: full-duplex   Three-wire SPI:
[Microcontroller]
51 MCU-SPI interface
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号