tm1637.c
#include <reg51.h>
#include <intrins.h>
#include "tm1637.h"
sfr P4 = 0xc0;
sfr P5 = 0xc8;
sbit SDIO = P5^5; // TM1637
sbit SDCK = P4^4;
void tm1637(unsigned char intensity2, unsigned char ledA1, unsigned char ledA2)
{
tm_start();
tm_write(0x40); // 设置写RAM,地址自动+1
tm_stop();
_nop_();
_nop_();
_nop_();
tm_start();
tm_write(0xc0); // 设置写RAM地址
tm_write(ledA1); // write RAM
tm_write(ledA2); // write RAM
tm_write(0); // write RAM
tm_write(0); // write RAM
tm_write(0); // write RAM
tm_write(0); // write RAM
tm_stop();
_nop_();
_nop_();
_nop_();
tm_start();
tm_write(intensity2);
tm_stop();
}
void tm_start(void)
{
SDIO = 1;
SDCK = 1;
DL1uS(3);
SDIO = 0;
DL1uS(1);
SDCK = 0;
}
void tm_stop(void)
{
SDIO = 0;
SDCK = 0;
DL1uS(3);
SDCK = 1;
DL1uS(3);
SDIO = 1;
}
void tm_write(unsigned char x2)
{
unsigned char i2;
for(i2=0; i2<8; i2++)
{
SDCK = 0;
if((1&x2) == 0)
{ SDIO = 0; }
else
{ SDIO = 1; }
DL1uS(1);
SDCK = 1;
x2 = x2 >> 1;
}
SDCK = 0;
DL1uS(1);
while(SDIO == 1);
DL1uS(1);
SDCK = 1;
}
unsigned char tm1637_key(void)
{
unsigned char i2,k2;
tm_start();
tm_write(0x42);
SDIO = 1;
k2 = 0;
for(i2=0; i2<8; i2++)
{
SDCK = 0;
k2 = k2 >> 1;
DL1uS(1);
SDCK = 1;
if(SDIO == 1)
{ k2 = k2 | 0x80; }
DL1uS(1);
}
SDCK = 0;
DL1uS(1);
while(SDIO == 1);
DL1uS(1);
SDCK = 1;
DL1uS(1);
tm_stop();
return k2;
}
void DL1uS(unsigned char z2)
{ // 1T 22.1184MHz 1uS*(z2+1) STC8A8K tested
unsigned char x2,y2;
for(x2=0; x2<z2; x2++)
{
for(y2=0; y2<2; y2++)
{ }
}
_nop_();
}
tm1637.h
#define _TM1637_H
void tm1637(unsigned char intensity2, unsigned char ledA1, unsigned char ledA2);
void tm_start(void);
void tm_stop(void);
void tm_write(unsigned char x2);
unsigned char tm1637_key(void);
void DL1uS(unsigned char z2);
这里是tm1637.c和tm1637.h文件,只用了2个数码管,显示和按键都是测试过的。 |