This is the first part of a tutorial on driving MAX7221/7219 display drivers with STM32VLDiscovery board. In this part we will cover the basics, i.e. how 7-segment display work, how the shift registers work and how to talk to MAX7221/7219 chip. In the next section I will post the source code for STM32 Value Line Discover board and explain the kay points.
As I mention in the post on DRO Design Considerations, I decided to use standard 7-segment LED display for the position readout. Since the DRO is targeted at a small milling machines, 6 digits per axis is more than enough *. This means that we need to drive 18 dits total, and by far the most convenient way is provided by Maxim 7221/7219 shift registers. Many hobbyist are intimidated by these ICs, but under the hood they are very simple. Both chips use SPI protocol to receive data and can drive up to 64 LEDs, or 8 7-segment displays. MAX7219 and MAX7221 are almost identical, with one minute difference: MAX7219 is not SPI-compliant. I will elaborate on this a bit later, but for this project they are interchangeable.
Before goint furhter, I would encourage you to download the datasheed for the ICs.
|MAX7219 & MAX7221 Pin Configuration|
The rest of the pins are:
- CLK - SPI clock
- DIN - data input pin (MOSI)
- LOAD (CS) - chip select, data load
- DOUT - data output pin
- ISET - current set resistor (individual segments ton't require individual current-limiting resistors)
- V+ - 5 Volt power supply
- GND - digital ground
The first three pins are used for the communication. MAX7221 is fully SPI compliant, so those are simply SPI clock, MOSI and CS pins. To send data to the chip, master (micro controller) would pull the CS line chip low, and at each raising pulse of the CLK line send one bit of data to the DIN/MOSI line. After the last bit of data the MCU would pull the LOAD/CS line high to "latch" the data. The only thing that is different when using MAX7219 is the fact that it read the data line even when the LOAD line is high. This means that we can send the data without messing with the LOAD line, and simply pulse it after the last bit.
DOUT pin is the pin that makes the IC simply awesome: it enables us to cascade multiple
chips to drive a large matrix or dozens of 7-segment displays. This is done by connecting all
CS/LOAD lines together and then connecting each consecutive chip's DIN line to previous
chip's DOUT line.
ISET is a used to set the maximum current to all LEDs. The selection of the resistor is somewhat non-trivial. On page 10 of the datasheet there is a whole section dedicated to this subject, but if you want to skip the math, a 10K resistor is a good staring point.
V+ and GND pins are self-explanatory. The only thing to keep in mind is that the IC creates a lot of ripple on the power rail, so it's a good idea to place a 0.1 uf bypass and 10uF electrolytic capacitors as close to the V+ pin as possible.
Theory of Operation
Behind the fancy hardware provided by MAX72xx chips is a simple serial-in-parallel-out shift register that us used to load the data into internal RAM. The driver circuitry uses the content of the RAM to decide what segments to turn on for each digit (or what LEDs to turn on for each column of a matrix). The diagram below (from the datasheet) illustrates the guts of this circuit.
There are several interesting things going on here. First of all, at the bottom we can see the 16 bit shift register. The simple way to visualize how it works, imagine that you are placing "0"s or "1"s on a conveyor. You start by turning the conveyor on (pull the CS line low). After you place each digit, you press the switch to advance the belt (pulse the CLK line). After some number of digits is places (16 for each IC in the chain), you turn the conveyor off (pull the CS line high). At this point the "0"s and "1"s are red. The first 8 bits (rightmost bits) contain the data, the following 4 bits contain the register address for that data to be loaded to, and the last 4 digits don't matter.
There are 14 registers. Besides the 8 segment registers, there are a few registers that we need to
keep in mind.
- No-op register means just that - do nothing. This register is useful when driving multiple cascaded drivers. For a single IC it can be ignored.
- Decode mode tells the chip whether we will be sending digits or individual segment values. In the former case, the chip will display the digits as we pass them. In the later case each bit will determine the state of a corresponding segment. (please refer to Table 5 for the details)
- Intensity register is used to set LED brightness using the internal DAC
- Scan limit register determines how many digits the driver will show. Please keep in mind, the smaller the scan limit, the longer each digit will be lit for longer period of time. If your ISET resistor value is too low, you can damage the LED or the driver.
- Shutdown register turns off the display but retains the data.
- Display Test register turns all segments, so we can verify that all LEDs are working.
To turn on the DP segment, we need to turn the D7 bit on for the appropriate digit.
This is all there is to it. In the next section I will post the schematic and the board layout for the circuit used for the DRO. Please don't hesitate to ask questions etc.
* The mini mill has less than 10" of travel, so we can display 4 decimal places, one significant digit and a minus sign. For larger mill, you can either chose to display 3 decimal places or add one or two more LED display. In the later case, only a small piece of code will need to be tweaked, since the MAX7221 IC can drive up to 8 digits.