Interface-Oriented Programming - SPI Bus and IIC Bus

>>> 4.4.1 Interrupt and Event Driven

Regardless of the bus type used, the clock signal and data/control lines are essential. The MCU master controls the clock signal. The SPI bus for control has four signals, including a chip select (CS) signal. The host uses this signal to determine which device is the target.

> > > 4.6.1 Introduction to SPI Bus

This section discusses the principle and application design of the SPI bus as a master, using the LPC824 as an example.

SSEL: Chip Select Input

When the SPI is in master mode, the SSEL signal is activated before data transmission and released afterward. It can be active low or high. When the SPI is a slave, an active SSEL indicates that the slave is being addressed.

MOSI: Master Output Slave Input

MOSI transmits data from the master to the slave. In master mode, the data is output via MOSI; in slave mode, it is input via MOSI.

MISO: Master Input Slave Output

MISO transmits data from the slave to the master. In master mode, the data is input via MISO; in slave mode, it is output via MISO.

SCK: Clock Signal

SCK is the synchronous clock signal used for data transfer. It is driven by the master. The clock can be programmed to be active high or low.

> > > 4.6.2 74HC595 Interface

The 74HC595 converts serial data to parallel output under the same clock signal. It has 8 parallel outputs and five control inputs. These include Q0–Q7, VCC, CP (clock), D (data), and STR (latch). These are connected to SCK, MOSI, and SSEL of the MCU, respectively.

Figure 4.18 74HC595 Circuit Diagram

On the rising edge of the CP signal, data on the D side is sent to the 74HC595. After shifting, the STR signal triggers the output on Q0–Q7. This ensures stable output during shifting.

The MiniPort-595 module expands 8 I/O channels using the 74HC595, directly driving LED modules. The function definition of the AMPort-Core MiniPort interface is shown in Figure 4.19(b). The MiniPort-595 module's output pin J2 is defined in Figure 4.19(c).

Figure 4.19 595 Module Physical and Control Interface Definition

To send 1000 0000 to the 74HC595’s Q0–Q7, the data is sent bit by bit from Q7 to Q0 under the clock pulse. After 8 pulses, the data is output in parallel. AMetal provides the hc595.h interface for the 74HC595, as detailed in Listing 4.44.

Listing 4.44 hc595.h Interface

By calling hc595_send_data() after initialization, the 74HC595 outputs 8-bit binary data in parallel. This allows the 74HC595’s output pins to act as an 8-bit I/O port, saving I/O resources for other functions.

With the AM824-Core host, the MiniPort A of the MiniPort-595 module, and the MiniPort B of the MiniPort-LED module inserted together, the schematic is shown in Figure 4.20. See Figure 4.21(a). The MiniPort interface J4 function is defined in Figure 4.21(b). The MCU controls the 74HC595 to drive 8 LEDs through STR, CP, and D. See Listing 4.45 for details.

Figure 4.20 8-Output I/O Expansion

Figure 4.21 Module Combination Physical and Control Interface Definition

Listing 4.45 74HC595 Universal Interface Sample Program

The 74HC595 can also drive 8-segment digital tubes. The schematic is shown in Figure 4.22.

Figure 4.22 2-Digit Digital Tube Driver Circuit

The MiniPort A (pin header) of the AM824-Core and MiniPort-595 modules and the MiniPort B (mother socket) of the MiniPort-View module can be directly connected. For details, see Figure 4.23(a). The corresponding MCU MiniPort interface J4 function is shown in Figure 4.23(b). The MCU uses COM0 and COM1 to control the position of the digital tube.

Figure 4.23 Module Combination Physical and Control Interface Definition

The 74HC595 can directly drive the digital tube, with only the segment code output method differing. Modify the segment code transfer function and add the implementation code to digitron1.c. See Listing 4.46 for details.

Listing 4.46 Adds the Relevant Function of the 74HC595 Driver

Since the user does not directly call digitron_hc595_segcode_set(), only digitron_hc595_init() and digitron_hc595_disp_scan() are added to the digitron1.h interface shown in Listing 4.47.

Listing 4.47 digitron1.h

When using the 74HC595 to drive the digital tube, simply call the relevant function, as shown in Listing 4.48.

Listing 4.48 74HC595 Driver Digital Tube Implementation Seconds Counter Sample Program

4.7 I 2 C Bus

> > > 4.7.1 Introduction to I 2 C Bus

The I2C bus, developed by NXP, connects microcontrollers with peripheral devices. It offers simple hardware, reusability, and portability. It supports communication between devices on the board and long-distance transmission through repeaters. Its key features include:

- Only two signal lines needed, reducing board space and interconnection cost;

- Multiple devices can be connected using unique addresses, eliminating the need for extra I/O or address decoders;

- Easy self-test implementation for quick fault detection;

- Electrical compatibility through open-drain I/O, enabling 3V/5V logic level compatibility with pull-up resistors;

- Supports multiple communication modes, including single master, dual-host, multi-master, and broadcast;

- High communication speed: 100Kbps (standard) and 400Kbps (fast), up to 3.4Mbps in later revisions.

> > > 4.7.2 LM75B Interface

The AM824-Core uses the LM75B temperature sensor, compatible with LM75A and consuming 1μA in shutdown mode.

1. Features

The LM75B is a digital temperature sensor with an I2C interface from NXP. Key features include:

- Device address 1001xxx, allowing 8 devices on the same bus;

- Power supply range: 2.8–5.5V, temperature range: –55–125°C;

- 11-bit ADC with 0.125°C resolution;

- Accuracy: ±2°C (-25–100°C), ±3°C (-55–125°C).

The LM75B pinout is shown in Figure 4.24. The slave address is 0x90, with A0–A2 for address selection. Pull-up resistors must be added to SCL and SDA. If the chip overheats, the output turns off.

Figure 4.24 LM75B Pin Diagram

2. Application Circuit

The typical LM75B application circuit is shown in Figure 4.25. R5 and R6 are pull-up resistors for the I2C bus. Since there is only one LM75B, A0–A2 can be grounded. The OS pin is the overheating output, which can be controlled via an external relay. The temperature is measured by the MCU, so this pin can be left floating. Connect SDA and SCL to PIO0_18 and PIO0_16 by shorting (J13_1, J13_2) and (J11_1, J11_2).

Figure 4.25 LM75B Application Circuit Diagram

Figure 4.26 shows the heating circuit of the AM824-Core. R32 is a 20–50Ω (2W) resistor. Short J14_2 and J14_3 to connect R32 near the LM75B to the KEY. Pressing the KEY button activates R32, generating heat that transfers to the LM75B through thicker wires. The LM75B then follows the temperature change. Lower resistance means higher current and more heat. When the button is pressed, the circuit turns on, and the temperature rise can be observed through the measurement circuit.

Figure 4.26 Heating Circuit

3. Temperature Measurement

AMetal provides a software package for the LM75B, with the function prototype:

Using these interfaces, reading the current temperature value is straightforward. Since only two digital tubes are available, only the integer part is displayed. If the temperature is negative, it is displayed without the negative sign. See Listing 4.49 for details.

Listing 4.49 LM75 Temperature Reading and Display Sample Program

After compiling and downloading the program to the development board, if the digital tube displays 28, the current temperature is 28°C. Pressing the heating button activates R32, causing the displayed temperature to rise.

When displaying the temperature, the value read by lm75_read() is shifted right by 8 bits. This is because the value is 256 times the actual temperature, so dividing by 256 gives the correct value. Only the integer part is shown, hence the shift.

> > > 4.7.3 Thermostat

Combining the previous program, we use LED, buzzer, digital tube, matrix keyboard, and temperature acquisition to create a simple thermostat.

1. Function Introduction

The LM75B temperature sensor collects temperature data via the I2C interface. Only the integer part is displayed due to two digital tubes. Negative temperatures are displayed without the negative sign.

The upper and lower temperature limits can be set. If the temperature exceeds the upper limit or falls below the lower limit, the buzzer sounds.

2. Status Indication

Two LEDs indicate the status during adjustment, with J9 and J10 connected via a short jumper.

- LED0 on: Indicates the upper limit value, with the digital tube showing the upper limit.

- LED1 on: Indicates the lower limit value, with the digital tube showing the lower limit.

- Both LEDs flashing: Normal operation, with the digital tube showing ambient temperature.

3. Instructions

Four buttons are used to set the upper and lower limits:

- SET button: Enters the setting state. Clicking it first enters the upper limit setting, then the lower limit, and finally returns to normal mode.

- Left/Right Shift key: Switches the currently adjusted digit (ones/tens). During setting, the flashing digit changes when this key is pressed.

- Add 1 key: Increases the value of the current flashing digit during setting.

- Subtract 1 key: Decreases the value of the current flashing digit during setting.

(1) Set Upper Limit

Press the SET button once to enter upper limit setting. LED0 lights up, and the digital tube displays the upper limit. The single digit stops flashing. Use “Add 1” or “Subtract 1” to adjust the value of the current flashing position. Use “Left/Right” to switch the currently adjusted digit.

(2) Set Lower Limit

After setting the upper limit, press the SET button again to enter lower limit setting. LED1 lights up, and the digital tube displays the lower limit. The single digit blinks. Adjust the value using “Add 1” or “Subtract 1”, and switch the digit using “Left/Right”.

4. Function Realization

As shown in Listing 4.50, the thermostat sample program is complex. To clarify the structure, three functions are written for each of the three buttons: switching state (KEY0), switching the current adjustment bit (KEY2), and adjusting the current bit value (KEY1 and KEY3). These functions are called in key_process(). Other parts are handled in the main loop, which includes temperature acquisition every 500ms, keyboard scanning every 10ms, and digital tube scanning every 5ms.

Listing 4.50 Comprehensive Experimental Sample Program

O.D80MM

O.D80MM Hydraulic Dc Motors,O.D80MM Hydraulic Dc Motor,O.D80MM Dc Hydraulic Pump,O.D80MM Dc Hydraulic Pump Motor

Wuxi Jinle Automobile Motor Factory , https://www.wxjldj.com