//Applicable to STM8S003F3
//main.c file
//Experimental phenomenon: When the power is turned on, all digital tubes are on and display 0; when a button is pressed, the corresponding digital tube number increases by 1, and the corresponding LED light turns on
#include "stm8s.h"
#include "tm1638.h"
unsigned char num[8]; //The value displayed by each digital tube
int main(void)
{
u8 i;
init_TM1638(); //Initialize TM1638
for(i=0;i<8;i++)
Write_DATA(i<<1,tab[0]); //Initialize registers (all digital tubes display 0 when powered on)
while(1)
{
i=Read_key(); //Read key value
if(i<8)
{
num[i]++;
while(Read_key()==i); //Wait for the key to be released
if(num[i]>15)
num[i]=0; //Display values from 0 to F
Write_DATA(i*2,tab[num[i]]);
Write_allLED(1< }
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(u8* file,u32 line)
{
while(1)
{
}
}
#endif
//tm1638.h file
//PD4 pin connected to DIO //Data output and input
//PD5 pin connected to CLK //clock
//PD6 pin connected to STB
#ifndef _TM1638_H
#define _TM1638_H
#include "stm8s.h"
#define DATA_COMMAND 0X40
#define DISP_COMMAND 0x80
#define ADDR_COMMAND 0XC0
//Common cathode digital tube display code
unsigned char tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; //1~f
void TM1638_Write(unsigned char DATA) //Write data function
{
u8 i;
for(i=0;i<8;i++)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_5); //CLK=0
if(DATA&0X01)
GPIO_WriteHigh(GPIOD,GPIO_PIN_4); //DIO=1
else
GPIO_WriteLow(GPIOD,GPIO_PIN_4); //DIO=0
DATA>>=1;
GPIO_WriteHigh(GPIOD,GPIO_PIN_5); //CLK=1
}
}
unsigned char TM1638_Read(void) // read data function
{
u8 i;
u8 temp=0; //Store the read value
GPIO_WriteHigh(GPIOD,GPIO_PIN_4); //Set to input (DIO=1)
for(i=0;i<8;i++)
{
temp>>=1;
GPIO_WriteLow(GPIOD,GPIO_PIN_5); //CLK=0
if(GPIO_ReadInputPin(GPIOD,GPIO_PIN_4))//DIO==1?
temp|=0x80;
GPIO_WriteHigh(GPIOD,GPIO_PIN_5); //CLK=1
}
return temp;
}
void Write_COM(unsigned char cmd) //Send command word
{
/*
Initialize the serial interface on the rising or falling edge, and then wait for receiving instructions. The first byte after STB is low is the instruction.
When STB is high, CLK is ignored.
*/
GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0
TM1638_Write(cmd);
GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1
}
unsigned char Read_key(void)
{
u8 c[4],i,key_value=0;
GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0
TM1638_Write(0x42); //Read key scan data command
for(i=0;i<4;i++)
c[i]=TM1638_Read(); // Need to read 4 bytes continuously, can't skip
GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //4 bytes of data are combined into one byte
for(i=0;i<4;i++)
key_value|=c[i]< for(i=0;i<8;i++)
if((0x01< break;
return i; //Return the key number read
}
void Write_DATA(unsigned char add,unsignedchar DATA) //Write data to the specified address
{
Write_COM(0x44);
GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0
TM1638_Write(0xc0|add); //The first byte of STB is low as the instruction
TM1638_Write(DATA); //The second byte and after are used as data
GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1
}
/*
void Write_oneLED(unsigned char num,unsigned char flag) //Control a LED function individually, num is the LED number to be controlled, flag is off when 0, and is on when not 0
{
if(flag)
Write_DATA(2*num+1,1);
else
Write_DATA(2*num+1,0);
} */
void Write_allLED(unsigned char LED_flag) //Control all LED functions, LED_flag indicates the status of each LED
{
u8 i;
for(i=0;i<8;i++)
{
if(LED_flag&(1< //Write_DATA(2*i+1,3);
Write_DATA(2*i+1,1);
else
Write_DATA(2*i+1,0);
}
}
//TM1638 initialization function
void init_TM1638(void)
{
u8 i;
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_SLOW); //Initialize related GPIO ports
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_SLOW);
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_OUT_PP_HIGH_SLOW);
Write_COM(0x8b); //Brightness (0x88-0x8f) 8 levels of brightness adjustable
Write_COM(0x40); //Add address automatically plus 1
GPIO_WriteLow(GPIOD,GPIO_PIN_6); //STB=0
TM1638_Write(0xc0); //Set the starting address
for(i=0;i<16;i++) //Transmit 16 bytes of data
TM1638_Write(0x00);
GPIO_WriteHigh(GPIOD,GPIO_PIN_6); //STB=1
}
#endif
Previous article:STM8S003F3 usage summary - timer
Next article:STM8S003 external interrupt recognizes the forward and reverse rotation of the mechanical encoder
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- DIY Small Communication System - FM Transmitter Radio Station
- msp430f149 baud rate setting
- Bought a very good ESP32-S2 development board
- What is the IC model of infrared remote control transmitter?
- Image frequency suppression measurement
- NUCLEO_G431RB Review - CRC Calculation
- Please help me explain this circuit schematic, how it works, and where each module is.
- How to use printf for debugging
- Comparison between Internal PA and External FEM
- When studying (100 examples of single-chip C language programming practice), I encountered a problem with undefined identifiers. Please tell me how to solve it!