The following is the .h file:
#ifndef __TM1650_I2C_H
#define __TM1650_I2C_H
#include "stm8s.h"
#include "stm8s_gpio.h"
#include "tim1.h"
#include "usart2.h"
#include #include /********************************** TM1650 chip controls 20 street lights function PB7 is SCL port PB6 is the SDA port **********************************/ #define SCL_TM1650 PC_ODR_ODR7 #define SDA_TM1650 PC_ODR_ODR6 #define SDAM_TM1650 PC_IDR_IDR6 #define SET_SCL_OUT_TM1650() {PC_DDR_DDR7=1; PC_CR1_C17 = 1; PC_CR2_C27 = 0;} #define SET_SDA_OUT_TM1650() {PC_DDR_DDR6=1; PC_CR1_C16 = 1; PC_CR2_C26 = 0;} #define SET_SDA_IN_TM1650() {PC_DDR_DDR6=0; PC_CR1_C16 = 0; PC_CR2_C26 = 0;} void IIC_Init_TM1650(void); void TDelay_us(u8 z); void I2C_Start_TM1650(void); void I2C_Stop_TM1650(void); void IIC_Ack_TM1650(void); void IIC_NAck_TM1650(void); uint8_t IIC_Wait_Ack_TM1650(void); void IIC_WrByte_TM1650(uint8_t txd); //u8 Scan_Key(void); void TM1650_Set(u8 add,u8 dat); void Init_Tm1650(void); #endif The following is the .c file: #include "TM1650_I2C.h" //-------------------------------------------------------------- // Prototype : void IIC_Init_TM1650(void) // Calls : // Description : //-------------------------------------------------------------- void IIC_Init_TM1650(void) { SET_SCL_OUT_TM1650(); SET_SDA_OUT_TM1650(); SCL_TM1650 = 1; SDA_TM1650 = 1; } //-------------------------------------------------------------- // Prototype : void Delay_us(void) // Description: Approximate delay z us //-------------------------------------------------------------- void TDelay_us(u8 z) { //u8 i; //fcpu 8MHz //for (i=50; i>0; i--); while(z--) { nop();nop();nop();nop(); } } //-------------------------------------------------------------- // Prototype : void I2C_Start(void) // Calls : Delay_5us() // Description : Start Singnal //-------------------------------------------------------------- void I2C_Start_TM1650(void) { // SDA 1->0 while SCL High //During the high level of SCL, a falling edge appears on SDA to indicate the start signal SET_SDA_OUT_TM1650(); SDA_TM1650 = 1; //The data line is kept high first, and the start signal needs the falling edge of the port TDelay_us(4); SCL_TM1650 = 1; //The clock line remains high TDelay_us(40); //There is a delay of about 5us, which depends on the device SDA_TM1650 = 0; //The data line is pulled low and a falling edge appears TDelay_us(4); //Delay for a short while to ensure a reliable falling edge SCL_TM1650 = 0; //Pull the clock line low to ensure that the data line is allowed to change next } //-------------------------------------------------------------- // Prototype : void I2C_Stop(void) // Calls : Delay_5us() // Description : Stop Singnal //-------------------------------------------------------------- void I2C_Stop_TM1650(void) { // SDA 0->1 while SCL High //During the SCL high level period, SDA generates a rising edge to indicate stop SET_SDA_OUT_TM1650(); SCL_TM1650 = 0; TDelay_us(2); SDA_TM1650 = 0; // Ensure the data line is low level TDelay_us(40); SCL_TM1650 = 1; //First ensure that the clock line is high TDelay_us(10); //Delay to get a reliable level signal SDA_TM1650 = 1; // Rising edge on data line TDelay_us(40); //Delay to ensure a reliable high level } //Response function void IIC_Ack_TM1650(void) { //The data line remains at a low level, and a rising edge on the clock line indicates a response SET_SDA_OUT_TM1650(); TDelay_us(10); SDA_TM1650 = 0; TDelay_us(10); SCL_TM1650 = 0; TDelay_us(40); SCL_TM1650 = 1; TDelay_us(40); //After the response is completed, pull the clock line low to allow data modification SCL_TM1650 = 0; } //Non-response void IIC_NAck_TM1650(void) { //Non-response is the opposite of response. The difference is that the data line remains at a high level. SET_SDA_OUT_TM1650(); TDelay_us(10); SDA_TM1650 = 1; TDelay_us(10); SCL_TM1650 = 0; TDelay_us(40); SCL_TM1650 = 1; TDelay_us(40); //Finally, pull the clock line low to allow data to change SCL_TM1650 = 0; } //Wait for response uint8_t IIC_Wait_Ack_TM1650(void) //0 means there is a response, 1 means there is no response { //Response waiting count uint8_t ackTime = 0; //First set the data line to input mode. This program does not reflect this. If there is a response, a falling edge will appear. SCL_TM1650 = 0; SET_SDA_OUT_TM1650(); TDelay_us(10); SDA_TM1650 = 1;// TDelay_us(30); SET_SDA_IN_TM1650(); //Switch to input mode //Pull the clock line high SCL_TM1650 = 1; TDelay_us(30); //Wait for the data line to pull low for response while(SDAM_TM1650){ //If it is not pulled low within this time ackTime ++; if(ackTime > 250) { // Consider it a non-response stop signal I2C_Stop_TM1650(); return 1; } } SCL_TM1650 = 0; return 0 ; } void IIC_WrByte_TM1650(uint8_t txd) { //Define a counting variable uint8_t i; SET_SDA_OUT_TM1650(); //Pull the clock line low to allow data to change // SCL = 0; //Send data bit by bit for(i = 0;i < 8; i ++) { TDelay_us(2); if((txd&0x80)>>7) //0x80 1000 0000 SDA_TM1650=1; else SDA_TM1650=0; txd<<=1; TDelay_us(20); SCL_TM1650=1; TDelay_us(20); SCL_TM1650=0; TDelay_us(20); } } /*************************** u8 Scan_Key(void) //Key scan { u8 i; u8 rekey; I2C_Start_TM1650(); IIC_WrByte_TM1650(0x49); //Read key command IIC_Ack_TM1650(); //DIO_H; SET_SDA_IN_TM1650(); //Switch to input mode for(i=0;i<8;i++) { SCL_TM1650=1; rekey = rekey<<1; if(SDAM_TM1650) { rekey++; } TDelay_us(5); SCL_TM1650=0; } IIC_Ack_TM1650(); I2C_Stop_TM1650(); return(rekey); } ****************************/ void TM1650_Set(u8 add,u8 dat) //digital tube display { //Writing video memory must start from a high address I2C_Start_TM1650(); IIC_WrByte_TM1650(add); //The first video memory address IIC_Ack_TM1650(); IIC_WrByte_TM1650(dat); IIC_Ack_TM1650(); I2C_Stop_TM1650(); } void Init_Tm1650(void) { IIC_Init_TM1650(); delay_ms(50); //A short delay is required, otherwise the display will be unresponsive TM1650_Set(0x48,0x31); //Initialize to 5 gray levels, turn on display } Note: It may take some time for the TM1650 chip to start up, so you should delay for a short time before sending the display, otherwise it will not be received.
Previous article:STM8S103K3 I2C
Next article:STM8S MAX7219 dot matrix module driver chip program
- 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
- Why don't all Android phones use Type-C ports?
- Will the timed interrupt enter the interruption immediately after it is turned on?
- Live Registration | Building a Sensor-Based Test System Using CompactDAQ and LabVIEW
- How does a microcontroller MCU allow part of the code to run in RAM? See here
- Meaning and use of TI mmWave sensor CQ2 data
- "Being lazy" is trending on the Internet! A third-year student in Nanjing made a dorm light-off device that went viral. Netizens: Hope it can be mass-produced
- Please ask a GPIO basic question
- [ESP32-S2-Kaluga-1 Review] Running ESP32 3 Driver Installation Bug
- MicroPython controlling Arduino via 1-wire
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] Environment Preparation + Server Construction