Controlling 7 Segment 4 Digit LCD - part 1
The first module to control is a 7-segment 4-digit LCD.
First, we need to be able to write numbers to this module.
Next, after measuring the temperature with a sensor and getting the value, we will create a function to display the temperature on this LCD.
So, how do we use this?
Although we could look at the datasheet for information and implement it from scratch, it might be faster to find example code to see how it works first, and then refer to the datasheet for more details.
The text on this module says "4-BIT LED Digital Tube Module" and "3461BS1-7.3." On the back, there are two Integrated Circuits (IC) connected.
There are a total of 5 pins labeled VCC, SCLK, RCLK, DIO, and GND.
Now, let's ask AI.
AI explains that this is a module made up of four 7-segment displays. It also indicates that it is an IC that communicates with the MCU.
I searched for the module name but couldn't find it, and I assumed AI couldn't find it either, so I turned to Google. Searching on Google wasn't easy either, but after a few hours of searching, I finally found some sample code and tutorial lectures.
https://www.youtube.com/watch?v=E9lUH5dT6Xc
https://github.com/magiccow/_7seg-display-simple/blob/master/_7seg-display-simple.ino
Looking at the code above, it is Arduino code, but we will need to convert it properly to STM32 code. Since it is in C language, I don't think there will be much difference.
First, the pin mode setup is done, and the three connected pins are DIO, SCLK, and RCLK.
Before looking at the code further, if we look closely at the pins, we can see that SCLK indicates serial communication. It seems to receive data through I^2C (Inter-integrated Circuit) or SPI communication and then display the information.
Also, because there is RCLK, I guess it is SPI communication, not I2C, which has only two lines, SCLK and DIO. I suspect that RCLK functions as the CS of SPI.
Looking further into the code, there is an array to display numbers on the 7-segment display. This array will probably be referenced later to send data to the module. If you want to send 1, the code will use hexDigitalValue[1], and it will send 0x60 to the chip via the DIO line.
Now, let's go to the loop() function, which acts as main().
A static int count is declared, and when the count reaches 10000, it resets to 0 in the loop.
Then, we use the displayNumber(count) function.
So, looking at the function called displayNumber, it takes a number to be displayed, splits it into four digits, and displays each digit one by one.
For example, if the number to be displayed is 1234:
When i=0, it displays 4 using the setDigit function. When i=1, it displays 3 using the setDigit function. When i=2, it displays 2 using the setDigit function. When i=3, it displays 1 using the setDigit function.
To give a simple example, when i=0, setDigit(0, 1234 % 10, false) is called. This means it displays 4 in the 0th position, and then divides the number by 10, making 1234 / 10 = 123. And then when i=1, setDigit(1, 123%10, false) is called. This means the remainder, 3, is displayed in the 1st position, and 123/10 = 12. So, it repeats until i=4.
Now, let's look at the setDigit function. The setDigit function basically determines which position to place which number.
At first, you see row 0, 1, 2, 3, and the number you want to display as a digit, with a boolean to set the decimal point. So far, everything has been set to false.
By performing bitwise operations on the variable rowselector
, bits for the row are set. It's my first time seeing the bit()
function. I think it must be a function that performs bitwise operations.
Since there are a total of 4 rows (0, 1, 2, 3), it seems to perform 3-row, and then it shifts 4 spaces. For example, if i=0, row = 0, then 3-0 << 4 is 0000 0011 << 4.
0011 0000 would be the result.
0xF is 0000 1111.
For example, let's say you want to write 6.
Then
data = ~ hexDigitValue[ digit & 0xF ] ;
6 & 15 =
0000 0110
& 0000 1111
0000 0110 = 0x6.
So, we retrieve 6 from hexDigitValue, apply ~, and store it in the data variable.
We perform a bitwise operation with the number we want to write and 15, then retrieve it from the digital value.
The reason for using 0xF is to use only the lower 4 bits and cancel the rest. For example, if 1111 0110 comes in, the first 1111 is canceled.
The reason for using ~ is probably because it turns on the LED by setting it to LOW.
And the unique point is RCLK - ST_CP,
Looking at the back of the module, you can see that it is made by connecting two modules together.
Why is RCLK needed? What is its purpose?
Since it's not very clear, let's take a moment to look at the datasheet.
So, looking at this, there are a total of 16 pins. Pins 1-7 and 15 are QA-QH, which are the final outputs. Tri-state means it can be in one of three states: High impedance, Low, or High. High Impedance means floating, it doesn't seem to have any value. I remember reading in a book that hardware engineers call it the high impedance state, while embedded software engineers refer to it as floating.
Now, looking at the timing diagram and function definition, we can see that RCK and SCK are independent. SCK is only needed when writing data, and if SCLR goes low, it clears, and if it's high, it holds. So, it's a LOW active pin.
So, you turn on SI with SCK and receive data. After the data ends, it holds, then you raise RCK to send it, and after sending, you lower it.
Then, it seems you lower the output to Low and then raise it.
Let's go back to the code for a moment.
Send the data, raise RCK, send the address, and lower RCK.
But hold on, we're not controlling OE right now, why? - But since there's no command to control OE anywhere, let's ignore it for now.
So, when you send 8 bits, the data goes into the 7-segment display like this.
Here, 1-12 are all pins.
So, you can think of each pin as a bit.
If you make pin 11 ACTIVE, all 4 A segments will turn on.
If you want to turn on a specific digit, you select one of 12, 9, 8, or 6 to make active, and then make 11 active, which will turn on the A segment of the desired digit.
In the next post, I will briefly explain the shift register to understand why it works this way.