#include
#include <stdlib.h>
#include
#include
#include
#include
#define CLK 2
#define DIO 3
char segdata[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0-9
//char segdatap[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//带小数可以实现时间
void tm1637_start()
{
digitalWrite(CLK,1);
usleep(140);
digitalWrite(DIO,1);
usleep(140);
digitalWrite(DIO,0);
usleep(140);
digitalWrite(CLK,0);
usleep(140);
}
void tm1637_stop()
{
digitalWrite(CLK,0);
usleep(140);
digitalWrite(DIO,0);
usleep(140);
digitalWrite(CLK,1);
usleep(140);
digitalWrite(DIO,1);
usleep(140);
}
void write_bit(char bit)
{
digitalWrite(CLK,0);
usleep(140);
if(bit)
{
digitalWrite(DIO,1);
}
else
{
digitalWrite(DIO,0);
}
usleep(140);
digitalWrite(CLK,1);
usleep(140);
}
void write_byte(char data) //写字节
{
for(int i=0; i<8; i++)
{
write_bit((data>>i)&0x01);
}
digitalWrite(CLK,0);
usleep(140);
digitalWrite(DIO,1);
usleep(140);
digitalWrite(CLK,1);
usleep(140);
pinMode(DIO,INPUT);
while (digitalRead(DIO));
pinMode(DIO,OUTPUT);
}
void write_command(char cmd) //写命令
{
tm1637_start();
write_byte(cmd);
tm1637_stop();
}
void write_data(char addr,char data) //写值
{
tm1637_start();
write_byte(addr);
write_byte(data);
tm1637_stop();
}
void time_dislaly(int h_shi,int h_ge,int m_shi,int m_ge) //显示
{
write_command(0x40); //写数据
write_command(0x44); //固定地址
write_data(0xc0,segdata[h_shi]);
write_data(0xc1,segdata[h_ge]);
write_data(0xc2,segdata[m_shi]);
write_data(0xc3,segdata[m_ge]);
write_command(0x88); //显示开
}
void tm1637_init()
{
if(wiringPiSetup()<0)
{
perror(“wiring pi error\n”);
exit(1);
}
pinMode(CLK,OUTPUT);
pinMode(DIO,OUTPUT);
}
int main()
{
tm1637_init(); //初始化
printf(“tm1637_init calling…\n”);
while(1)
{
for(int i=0; i<10; i++)
{
time_dislaly(i,i,i,i);
printf(“%d\n”,i);//控制台输出
sleep(1);
}
}
}