-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTM1650.cpp
More file actions
136 lines (132 loc) · 3.51 KB
/
Copy pathTM1650.cpp
File metadata and controls
136 lines (132 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Author:jieliang mo
// Date:2 July, 2020
// github: https://github.com/mworkfun/TM1650.git
// Applicable Module:
// 4 digital tube v1.0.0
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 1.0.0 of the License, or (at your option) any later version.
/*******************************************************************************/
#include "TM1650.h"
#include <Arduino.h>
int8_t NUM[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //number:0~9
int8_t DIG[] = {0x68,0x6a,0x6c,0x6e};
int8_t DOT[] = {0,0,0,0};
TM1650::TM1650(uint8_t Clk, uint8_t Data){
Clkpin = Clk;
Datapin = Data;
setBrightness();
setMode();
displayOnOFF();
pinMode(Clkpin,OUTPUT);
pinMode(Datapin,OUTPUT);
}
void TM1650::writeByte(int8_t wr_data){
uint8_t i,count1;
for(i=0;i<8;i++) //sent 8bit data
{
digitalWrite(Clkpin,LOW);
if(wr_data & 0x80)
digitalWrite(Datapin,HIGH);//LSB first
else
digitalWrite(Datapin,LOW);
delayMicroseconds(3);
wr_data <<= 1;
digitalWrite(Clkpin,HIGH);
delayMicroseconds(3);
}
}
//send start signal to TM1650
void TM1650::start(void){
digitalWrite(Clkpin,HIGH);//send start signal to TM1650
digitalWrite(Datapin,HIGH);
delayMicroseconds(2);
digitalWrite(Datapin,LOW);
//delayMicroseconds(2);
//digitalWrite(Clkpin,LOW);
}
//ack function
void TM1650::ack(void){
int dy=0;
digitalWrite(Clkpin,LOW);
delayMicroseconds(5);
pinMode(Datapin,INPUT);
while(digitalRead(Datapin)){ //wait for the ACK
delayMicroseconds(1);
dy += 1;
if(dy > 5000) //Prevent infinite loop
break;
}
digitalWrite(Clkpin,HIGH);
delayMicroseconds(2);
digitalWrite(Clkpin,LOW);
pinMode(Datapin,OUTPUT);
}
//End of transmission
void TM1650::stop(void){
digitalWrite(Clkpin,HIGH);
digitalWrite(Datapin,LOW);
delayMicroseconds(2);
digitalWrite(Datapin,HIGH);
}
//display number
void TM1650::displayBit(uint8_t Bit, uint8_t Num){
if(Num > 9 || Bit > 4)return;
start(); //start signal sent to TM1650 from MCU
writeByte(ADDR_DIS); //send mode command
ack();
writeByte(DisplayCommand); //set display mode
ack();
stop();
start();
writeByte(DIG[Bit]); //send the address
ack();
if(DOT[Bit] == 1){ //display dot
writeByte(NUM[Num] | 0x80);
}
else{
writeByte(NUM[Num]); //display data
}
ack();
stop();
}
//clear display
void TM1650::clearBit(uint8_t Bit){
if(Bit > 4)return;
start(); //start signal sent to TM1650 from MCU
writeByte(ADDR_DIS); //send mode command
ack();
writeByte(DisplayCommand); //set display mode
ack();
stop();
start();
writeByte(DIG[Bit]); //send the address
ack();
writeByte(0x00); //display data
ack();
stop();
}
//set display brightness
void TM1650::setBrightness(uint8_t brightness){
DisplayCommand = (DisplayCommand & 0x0f)+(brightness<<4);
}
//7 or 8 segment digital tube
void TM1650::setMode(uint8_t segment){
DisplayCommand = (DisplayCommand & 0xf7)+(segment<<3);
}
//display on or off
void TM1650::displayOnOFF(uint8_t OnOff){
DisplayCommand = (DisplayCommand & 0xfe)+OnOff;
}
//display dot
void TM1650::displayDot(uint8_t Bit, boolean OnOff){
if(Bit > 4)return;
if(OnOff){
DOT[Bit] = 1;
}
else{
DOT[Bit] = 0;
}
}