极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13607|回复: 0

8F328P-U 與 TM1637 LED數碼管驅動程序

[复制链接]
发表于 2018-10-26 17:28:34 | 显示全部楼层 |阅读模式
TM1637 is a multi-segment(8*6) LED driver with 8*2 scan keys.
a)      數據輸入的開始條件是CLK 為高電平時,DIO 由高變低
b)      在輸入時,當CLK 是高電平時,DIO 上的信號必須保持不變;只有CLK 上的時鐘信號為低電平時,DIO 上的信號才能改變
c)      結束條件是CLK 為高時,DIO 由低電平變為高電平
  
Key
  
  
S16
  
  
S15
  
  
S14
  
  
S13
  
  
S12
  
  
S11
  
  
S10
  
  
S9
  
  
S8
  
  
S7
  
  
S6
  
  
S5
  
  
S4
  
  
S3
  
  
S2
  
  
S1
  
  
Matrice
  
  
SG8
  K1
  
  
SG8
  K2
  
  
SG4
  K2
  
  
SG4
  K1
  
  
SG7
  K1
  
  
SG7
  K2
  
  
SG3
  K2
  
  
SG3
  K1
  
  
SG6
  K1
  
  
SG6
  K2
  
  
SG2
  K2
  
  
SG2
  K1
  
  
SG5
  K1
  
  
SG5
  K2
  
  
SG1
  K2
  
  
SG1
  K1
  
  
hex
  
  
F0
  
  
E8
  
  
EC
  
  
F4
  
  
F1
  
  
E9
  
  
ED
  
  
F5
  
  
F2
  
  
EA
  
  
EE
  
  
F6
  
  
F3
  
  
EB
  
  
EF
  
  
F7
  


Command:

  
Test Mode
  
0x48
01001000
test mode (internal use)
Normal Mode
0x40
01000000
the byte sent below is a datum towards the  display, with the automatic increase in destination addresses
0x44
01000100
the byte sent below is a datum towards  the display, with the fixed destination addresses
0x42
01000010
read the input situation
Display Control
0x80
10000000
display off
0x88-0x8F
10001000-100001111
display on with brightness control, 7  step

/* Program for Display on TM1637 */

const int clock = 3;
const int data = 2;
/*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/
uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };


void setup()
{
  Serial.begin(115200);
  pinMode(clock, OUTPUT);
  pinMode(data, OUTPUT);

  start();
  writeValue(0x8c);
  stop();

  // clear display
  write(0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

}

void loop()
{
  unsigned int temp = keyscan();
  Serial.println(temp, HEX);
  write(digits[0], digits[2], digits[3], digits[4], digits[5], digits[6]);
}

void write(uint8_t first, uint8_t second, uint8_t third, uint8_t fourth, uint8_t fifth, uint8_t sixth)
{
  start();
  writeValue(0xc0);
  writeValue(first);
  writeValue(second);
  writeValue(third);
  writeValue(fourth);
  writeValue(fifth);
  writeValue(sixth);
  stop();
}

void start(void)
{
  digitalWrite(clock, HIGH); //send start signal to TM1637
  digitalWrite(data, HIGH);
  delayMicroseconds(5);

  digitalWrite(data, LOW);
  digitalWrite(clock, LOW);
  delayMicroseconds(5);
}

void stop(void)
{
  digitalWrite(clock, LOW);
  digitalWrite(data, LOW);
  delayMicroseconds(5);

  digitalWrite(clock, HIGH);
  digitalWrite(data, HIGH);
  delayMicroseconds(5);
}

bool writeValue(uint8_t value)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    digitalWrite(clock, LOW);
    delayMicroseconds(5);
    digitalWrite(data, (value & (1 << i)) >> i);
    delayMicroseconds(5);
    digitalWrite(clock, HIGH);
    delayMicroseconds(5);
  }

  // wait for ACK
  digitalWrite(clock, LOW);
  delayMicroseconds(5);

  pinMode(data, INPUT);

  digitalWrite(clock, HIGH);
  delayMicroseconds(5);

  bool ack = digitalRead(data) == 0;

  pinMode(data, OUTPUT);

  return ack;
}


uint8_t keyscan(void)
{
  uint8_t rekey;
  start();
  writeValue(0x42);

  delayMicroseconds(2);
  pinMode(data, INPUT);

  for (uint8_t i = 0; i < 8; i++)
  {
    digitalWrite(clock, LOW);

    rekey = rekey >> 1;

    digitalWrite(clock, HIGH);
    if (digitalRead(data))
    {
      rekey = rekey | 0x80;
    }
    else
    {
      rekey = rekey | 0x00;

    }

    delayMicroseconds(5);
  }

  pinMode(data, OUTPUT);

  digitalWrite(data, LOW);
  // digitalWrite(clock, LOW);

  // wait for ACK
  digitalWrite(clock, LOW);
  delayMicroseconds(5);

  pinMode(data, INPUT);

  digitalWrite(clock, HIGH);
  delayMicroseconds(5);

  pinMode(data, OUTPUT);

  delayMicroseconds(5);
  stop();
  return rekey;
}

void writeByte(int8_t wr_data)
{
  uint8_t i, count1;
  for (i = 0; i < 8; i++) //sent 8bit data
  {
    digitalWrite(clock, LOW);
    if (wr_data & 0x01)digitalWrite(data, HIGH); //LSB first
    else digitalWrite(data, LOW);
    wr_data >>= 1;
    digitalWrite(clock, HIGH);

  }
  digitalWrite(clock, LOW); //wait for the ACK
  digitalWrite(data, HIGH);
  digitalWrite(clock, HIGH);
  pinMode(data, INPUT);
  while (digitalRead(data))
  {
    count1 += 1;
    if (count1 == 200) //
    {
      pinMode(data, OUTPUT);
      digitalWrite(data, LOW);
      count1 = 0;
    }
    pinMode(data, INPUT);
  }
  pinMode(data, OUTPUT);

}



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 14:03 , Processed in 0.046620 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表