当前位置:网站首页>TM1637带秒点四位LED显示器模块ARDUINO驱动程序
TM1637带秒点四位LED显示器模块ARDUINO驱动程序
2022-07-25 09:24:00 【悟渔】
几块钱网上买了块LED显示模块,IIC接口,TM1637驱动,四位显示,带秒点.写了个用于ESP32模块的ARDUINO驱动程序,将笔记放这里,以备未来使用.

模块文件:TM1637.C
#ifndef TM1637_H
#define TM1637_H
/*TM1637四位LED数码管(带秒点)显示驱动,每位数码管可以显示一位十六进制数据0~F,支持秒点显示和亮度调节.
* _________________________________
*GND---- | __ __ __ __ |
*VCC---- | |__| |__| . |__| |__| |
*DIO---- | |__| |__| . |__| |__| |
*CLK---- |_________________________________|
* LED3 LED2 LED1 LED0
*/
class TM1637{
private:
//1/4脉冲宽度设置。使用不同的时钟频率需要调节此参数以适应IIC总线操作速度.
#define TM1637_DELAY_US 1
const uint8_t displayChar[16]={0X3F,0X30,0X5B,0X79,0X74,0X6D,0X6F,0X38,0X7F,0X7D,0X7E,0X67,0X0F,0X73,0X4F,0X4E};//十六进制数字BCD码(请根据印刷板数码管连接情况使用不同的编码数组)。
uint8_t DIO=26;//IIC数据线。
uint8_t CLK=27;//IIC时钟线。
uint8_t Brightness=0;//亮度值。
boolean secondPoint=true;//是否显示秒点。(实际就是LED1的小数点)
uint8_t displayBuffer[6];//显存。
protected:
void init(void){uint8_t index;pinMode(DIO, OUTPUT);pinMode(CLK, OUTPUT);iic_stop();for(index=0;index<6;index++){displayBuffer[index]=0xff;}Brightness=7;writeBuffer();}
void iic_start(void){digitalWrite(CLK,LOW);delayMicroseconds(TM1637_DELAY_US);digitalWrite(DIO,HIGH);delayMicroseconds(TM1637_DELAY_US);digitalWrite(CLK,HIGH);delayMicroseconds(TM1637_DELAY_US);digitalWrite(DIO, LOW);delayMicroseconds(TM1637_DELAY_US);}
void iic_stop(void){ digitalWrite(CLK,LOW);delayMicroseconds(TM1637_DELAY_US);digitalWrite(DIO, LOW);delayMicroseconds(TM1637_DELAY_US);digitalWrite(CLK,HIGH);delayMicroseconds(TM1637_DELAY_US);digitalWrite(DIO,HIGH);delayMicroseconds(TM1637_DELAY_US);}
void iic_waitACK(void){uint16_t Index;digitalWrite(CLK, LOW);delayMicroseconds(TM1637_DELAY_US);digitalWrite(DIO, HIGH);for(Index=0;Index<10;Index++){if(!digitalRead(DIO)){break;}delayMicroseconds(TM1637_DELAY_US);}digitalWrite(CLK,HIGH);delayMicroseconds(TM1637_DELAY_US);digitalWrite(CLK,LOW);delayMicroseconds(TM1637_DELAY_US);}
void iic_writeByte(uint8_t InData){uint8_t Index;for(Index=0;Index<8;Index++){digitalWrite(CLK, LOW);delayMicroseconds(TM1637_DELAY_US);if(InData&0x01){digitalWrite(DIO, HIGH);}else{digitalWrite(DIO, LOW);}delayMicroseconds(TM1637_DELAY_US);digitalWrite(CLK, HIGH);delayMicroseconds(TM1637_DELAY_US);delayMicroseconds(TM1637_DELAY_US);InData=InData>>1;}digitalWrite(CLK, LOW);delayMicroseconds(TM1637_DELAY_US);}
void writeBuffer(void){uint8_t Index;iic_start();iic_writeByte(0x40);iic_waitACK();iic_stop();iic_start();iic_writeByte(0xc0);iic_waitACK();for(Index=0;Index<6;Index++){if(secondPoint==true && Index==1){iic_writeByte(displayBuffer[Index]|0x80);}else{iic_writeByte(displayBuffer[Index]);}iic_waitACK();}iic_stop();iic_start();iic_writeByte(Brightness&0x07|0x88);iic_waitACK();iic_stop();}
public:
TM1637(void){;}
TM1637(uint8_t In_DIO,uint8_t In_CLK){DIO=In_DIO;CLK=In_CLK;}
void begin(void){init();}
void begin(uint8_t In_DIO,uint8_t In_CLK){DIO=In_DIO;CLK=In_CLK;begin();}
uint8_t read(uint8_t Address){if(Address>=6){return 0;}else{return displayBuffer[Address];}}
uint8_t write(uint8_t Address,uint8_t Data){if(Address>=6||Data>=16){return 1;}displayBuffer[Address]=displayChar[Data];writeBuffer();return 0;}//写显存(00H~05H)。成功返回0,失败返回1。
uint8_t brightness(uint8_t In_Data){if(In_Data>7){Brightness=7;}else{Brightness=In_Data;}writeBuffer();return Brightness;}//设置亮度(0~7)。
uint8_t brightness(void){return Brightness;}//获取亮度值。
void point(void){secondPoint=!secondPoint;writeBuffer();}//改变秒点显示状态。
void point(boolean In_secondPoint){secondPoint=In_secondPoint;writeBuffer();}//显示(熄灭)秒点。
void point(int In_secondPoint){if(In_secondPoint==0){secondPoint=false;}else{secondPoint=true;}writeBuffer();}//显示(熄灭)秒点。
};
#endif主程序文件:TM1637.INO(演示数码管显示,秒点闪烁,亮度调节)
#include "TM1637.C"
TM1637 LED_FOR;
void setup() {
LED_FOR.begin(26,27);//初始化,设置IIC通信引脚.
LED_FOR.brightness(7);//LED亮度最大.
}
void loop() {
uint8_t index;
uint16_t TemNum;
TemNum=millis();
if(TemNum%1000<500){LED_FOR.point(true);}else{LED_FOR.point(false);}//秒点闪烁.
for(index=0;index<4;index++){
TemNum=TemNum/10;
LED_FOR.write(index,TemNum%10);//显示四位数字.
}
}边栏推荐
猜你喜欢

Solve the Chinese garbled code error of qtcreator compiling with vs

¥ 1-1 SWUST OJ 941: implementation of consolidation operation of ordered sequence table

Server CUDA toolkit multi version switching

Defect detection network -- hybrid supervision (kolektor defect data set reproduction)

CCF 201512-4 送货

CDA LEVELⅠ2021新版模拟题一(附答案)

CCF 201509-4 高速公路

无向连通图邻接矩阵的创建输出广度深度遍历

工程监测多通道振弦传感器无线采集仪外接数字传感器过程
![[deep learning] self encoder](/img/7e/c3229b489ec72ba5d527f6a00ace01.png)
[deep learning] self encoder
随机推荐
CCF 201509-3 模板生成系统
LoRA转4G及网关中继器工作原理
ARMv8通用定时器简介
~2 CCF 2022-03-1 uninitialized warning
目标检测与分割之MaskRCNN代码结构流程全面梳理+总结
Gartner 2022年顶尖科技趋势之超级自动化
SystemVerilog语法
[data mining] Chapter 2 understanding data
Exciting method and voltage of vibrating wire sensor by hand-held vibrating wire acquisition instrument
ISP图像信号处理
单目深度估计模型Featdepth实战中的问题和拓展
ARMV8体系结构简介
CCF 201512-4 送货
matlab如何导入大量数据
Segmentation based deep learning approach for surface defect detection
~3 CCF 2022-03-2 travel plan
Binary Cross Entropy真的适合多标签分类吗?
Temperature, humidity and light intensity acquisition based on smart cloud platform
Swift creates weather app
matlab绘图|坐标轴axis的一些常用设置