Tm1629 digital tube driver first. I have read a lot on the internet, mostly common cathode. The common cathode is easy to operate, there are few digital tubes under control, and there are many digital tubes under control of common anode, but the conversion is required.
Tm16290.h
# Ifndef _ tm1629_h
# DEFINE _ tm1629_h
# Define tm1_stb_pingpio_pin_0
# Define tm1_stb_portgpiof
# Define tm1_dio_pingpio_pin_2
# Define tm1_dio_portgpiof
# Define tm1_clk_pingpio_pin_1
# Define tm1_clk_portgpiof
# Define tm1_stb_h gpio_setbits (tm1_stb_port, tm1_stb_pin)
# Define tm1_stb_l gpio_resetbits (tm1_stb_port, tm1_stb_pin)
# Define tm1_dio_h gpio_setbits (tm1_dio_port, tm1_dio_pin)
# Define tm1_dio_l gpio_resetbits (tm1_dio_port, tm1_dio_pin)
# Define tm1_clk_h gpio_setbits (tm1_clk_port, tm1_clk_pin)
# Define tm1_clk_l gpio_resetbits (tm1_clk_port, tm1_clk_pin)
# Define read_tm1_dio gpio_readinputdatabit (tm1_dio_port, tm1_dio_pin)
/*************************** Public ********** **************************************** *******/
Void tm1629a_gpio_init (void); // initialize the tm1629a port and push the output
Void tm1629_init (void); // tm1629 chip initialization program
/*************************** Private ********** **************************************** *******/
Void tm1629sendbyte (unsigned char data); // in serial mode, the MCU outputs 8 bits at a time.
Void tm1629sendcmd (unsigned char cmd); // write the command
Void tm1639_write (unsigned char * data); // enable tm1629 and display the specified data
Void write_tm1639_off (unsigned char * data); // disable tm1629 display
Void convert (unsigned char srcnum, unsigned char * pdst, unsigned char seg, unsigned char dot); // convert the data format of gongyang digital tube to 0 ~ 9 Conversion
Void convert2 (unsigned char srcnum, unsigned char * pdst, unsigned char SEG); // special format conversion for co-yang digital data format conversion
# Endif
Tm1629.c
# Include "tm1629.h"
# Include "stm32f10x. H" // you only need to add this header file when calling the library function.
# Include "delay. H"
/*************************************** *************
**************************************** *************
* ********************* Public **************** ********
**************************************** *************
**************************************** ************/
/* Initialize the tm1629 port. All three ports are push-pull output */
Void tm1629a_gpio_init (void ){
Gpio_inittypedef gpio_initstructure;
Rcc_apb2periphclockcmd (rcc_apb2periph_gpiof, enable); // enable the PF port clock
Gpio_initstructure.gpio_mode = gpio_mode_out_pp; // STB pulling output of TM1
Gpio_initstructure.gpio_pin = tm1_stb_pin;
Gpio_initstructure.gpio_speed = gpio_speed_50mhz;
Gpio_init (tm1_stb_port, & gpio_initstructure );
Gpio_initstructure.gpio_pin = tm1_dio_pin; // Dio push-pull output of TM1
Gpio_init (tm1_dio_port, & gpio_initstructure );
Gpio_initstructure.gpio_pin = tm1_clk_pin; // output the CLK of TM1
Gpio_init (tm1_clk_port, & gpio_initstructure );
}
/* A startup mode is required to start tm1629 */
Void tm1629_init (void ){
Tm1_clk_h;
Tm1_dio_h;
Tm1_stb_h;
Tm1629sendcmd (0x03); // write the display mode of tm1629.
Tm1629sendcmd (0x40); // The Address Auto-increment is used to write data to the display register.
Tm1629sendcmd (0x88); // on-screen display. The brightness can be adjusted by changing the lower three digits.
}
/*************************************** *************
**************************************** *************
* ********************** Private **************** *******
**************************************** *************
**************************************** ************/
/* Serial mode writes data to the LED driving IC-tm16xx through MCU on the rising edge of the clock */
Void tm1629sendbyte (unsigned char dat ){
Unsigned char I;
Tm1_stb_l; // ensure that "STB" is low, and the program does not depend on the status of the previous port; ensure that the program does not encounter "port loss" during actual operation"
For (I = 0; I <8; I ++)
{
Tm1_clk_l; // CLK is set to low, waiting for data to be sent
If (DAT & 0x01) = 0x01) {// send the highest bit to Di
Tm1_dio_h; // if the low level of the data to be transferred is "1", set "Dio" to high.
}
Else {
Tm1_dio_l; // if the low level of the data to be transferred is "0", set "Dio" to zero.
}
Delay_ms (1 );
Tm1_clk_h; // The rising edge of the sent clock
Dat> = 1; // prepare to send a bit
// Delay_ms (1 );
}
}
/* Write command */
Void tm1629sendcmd (unsigned char cmd ){
Tm1_stb_h;
Tm1_clk_h;
Tm1_dio_h; // all communication ports are initialized to "1" before communication starts"
Delay_ms (2 );
Tm1_stb_l; // slice selection (low level)
Tm1629sendbyte (CMD );
Tm1_stb_h;
}
/* Open tm1629 and display the specified data */
Void tm1639_write (unsigned char * Data ){
Unsigned char I = 0;
Tm1_stb_h;
Tm1629sendbyte (0x40); // you can run the data transmission setting command to display data by automatically adding one address.
Tm1_stb_h;
Tm1629sendbyte (0xc0); // start address of the transfer. After the address command is sent, the value of "STB" is 0 and the data to be displayed is continuously transmitted.
For (I = 0; I <16; I ++) // a Summary of the continuously displayed data process. The value of "STB" is always 0,
{
Tm1629sendbyte (data [I]);
}
Tm1_stb_h; // After transmitting all the displayed data (up to 14 bytes), set STB to 1
Tm1629sendbyte (0x88); // 0x8f // on display, the pulse width is 1/16, which is the lowest pulse width.
Tm1_stb_h;
}
/* Disable tm1629 display */
Void write_tm1639_off (unsigned char * Data ){
Unsigned char I = 0;
Tm1_stb_h;
Tm1629sendbyte (0x40); // you can run the data transmission setting command to display data by automatically adding one address.
Tm1_stb_h;
Tm1629sendbyte (0xc0); // start address of the transfer. After the address command is sent, the value of "STB" is 0 and the data to be displayed is continuously transmitted.
For (I = 0; I <16; I ++)
{
Tm1629sendbyte (data [I]);
}
Tm1_stb_h;
Tm1629sendbyte (0x87); // off display, pulse width is 14/16
Tm1_stb_h;
}
/* Convert the data format of gongyang digital tube
Input parameter: srcnum is 0-9
Pdst is the converted data.
SEG is the sel pin number corresponding to the current digital tube minus 1. For example, if the digital card is selected to the seg2 chip, seg is 1
Dot indicates whether the current digital tube has a dot display.
*/
Void convert (unsigned char srcnum, unsigned char * pdst, unsigned char seg, unsigned char dot ){
Unsigned char table [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
Unsigned char I, usrc;
If (DOT = 0)
Usrc = table [srcnum];
Else
Usrc = table [srcnum] | 0x80;
For (I = 0; I <8; I ++) {// one-digit-one displacement past
If (usrc & 0x01) = 1) {// If the bit to be moved is 1, write 1 at the corresponding position, followed by the last bit to write 0
If (SEG <8 ){
Pdst [2 * I] = (1 <SEG); // seg <8, write valid data to address 0, 2, 4, 6, 8, 10, 12, and 14
Pdst [2 * I + 1] = 0; // fill in the values of 1, 3, 5, 7, 9, 11, 13, and 15 in the address
}
Else if (seg> 7 ){
Pdst [2 * I] = 0; // seg> 8. Fill in 0, 2, 4, 6, 8, 10, 12, and 14 of the address.
Pdst [2 * I + 1] = (1 <(SEG-8 )); // write valid numbers in the addresses 1, 3, 5, 7, 9, 11, 13, and 15 (9 is left shifted by 1, and so on)
}
}
Else {// If the bit to be moved is 0, write 0 at the corresponding position, followed by the last bit to write 0
Pdst [2 * I] = 0;
Pdst [2 * I + 1] = 0;
}
Usrc> = 1; // shift right to the second place
}
}
/* Convert the data format of gongyang digital tube
Special Character Display
Input parameter: srcnum indicates the data to be displayed. For example, to display 'l', the data is expressed as h g f e d c B A, which is 00111000 (0x38)
Pdst is the converted data.
SEG is the sel pin number corresponding to the current digital tube minus 1. For example, if the digital card is selected to the seg2 chip, seg is 1
A
F B
G
E c
D H
*/
Void convert2 (unsigned char srcnum, unsigned char * pdst, unsigned char SEG ){
// Uchar table [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
Unsigned char I, usrc;
Usrc = srcnum;
For (I = 0; I <8; I ++)
{
If (usrc & 0x01) = 1)
{
If (SEG <8)
{
Pdst [2 * I] = (1 <SEG );
Pdst [2 * I + 1] = 0;
}
Else if (seg> 7)
{
Pdst [2 * I] = 0;
Pdst [2 * I + 1] = (1 <(SEG-8 ));
}
}
Else
{
Pdst [2 * I] = 0;
Pdst [2 * I + 1] = 0;
}
Usrc> = 1;
}
}
Digital Display of stm32f103 (1)