I am new to single-chip microcomputers. Here is a record of using TM1668 to drive a common anode LED patch.
#include "config.h"
void Send_Data(unsigned char datas);
unsigned char getConmergeSegs(unsigned char data6,unsigned char data5,unsigned char data4,unsigned char data3,unsigned char data2,unsigned char data1,unsigned char bits);
void display_all(unsigned char data1,unsigned char data2,unsigned char data3,unsigned char data4,unsigned char data5,unsigned char data6);
//const unsigned char segs[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
const unsigned char segs[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
sbit DIO_1668 = P4^2 ;
sbit CLK_1668 = P3^4 ;
sbit STB_1668 = P3^5 ;
/*
* 程序名:开拓者NO.1
* 描述 :tm1668.c
* 输入 :无
* 输出 :无
*/
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
{
for(j=0;j<1000;j++)
{
}
}
}
//=============================================================
//================== 发送数据 =============================
void Send_Data(unsigned char datas)
{
unsigned char i;
STB_1668 = 0;
delay(25);
for(i=0;i<8;i++)
{
CLK_1668 = 0; //PC8=0
if((datas&0x01)==0x00)
{
DIO_1668=0;
}
else
{
DIO_1668=1;
}
delay(25);
CLK_1668 = 1; //PC8=1
delay(25);
datas=datas>>1;
}
delay(25);
}
//==================================================================
//============ 共阴极显示转共阳极显示 ==========================
unsigned char getConmergeSegs(unsigned char data6,unsigned char data5,unsigned char data4,unsigned char data3,unsigned char data2,unsigned char data1,unsigned char bits) //bit=0..6
{
unsigned char cal=0;
unsigned char s6,s5,s4,s3,s2,s1;
s6=segs[data6]>>bits;
s5=segs[data5]>>bits;
s4=segs[data4]>>bits;
s3=segs[data3]>>bits;
s2=segs[data2]>>bits;
s1=segs[data1]>>bits;
s6=(s6)&0x01;
s5=(s5)&0x01;
s4=(s4)&0x01;
s3=(s3)&0x01;
s2=(s2)&0x01;
s1=(s1)&0x01;
s6=s6<<5;
s5=s5<<4;
s4=s4<<3;
s3=s3<<2;
s2=s2<<1;
cal=s6+s5+s4+s3+s2+s1;
return cal;
}
//==================================================================
//====================== 显示函数 ==============================
void display_all(unsigned char data1,unsigned char data2,unsigned char data3,unsigned char data4,unsigned char data5,unsigned char data6)
{
unsigned char dataa=0;
unsigned char i=0;
unsigned char m=0;
STB_1668 = 0;
Send_Data(0x03);
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0x40);
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0xC0);
for(i=0;i<7;i++)
{
if(m==0)
{
dataa=getConmergeSegs(data6, data5, data4, data3, data2, data1,i);
Send_Data(dataa);
}
else
{
Send_Data(0x0);
}
Send_Data(0x01);
}
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0x8f);
STB_1668 = 1;
delay(25);
}
//==================================================================
/*
extern void Delayms( uint32_t ms );
void tm1668_test(void)
{
tm1668_init();
//while(1)
{
display_all(0x1,2,3,4);
Delay_MS(20000);
display_all(0x2,3,4,5);
Delay_MS(20000);
}
display_all(0x5,6,7,8);
Delay_MS(20000);
while(1){}
} */
//========================= TM1668初始化 =========================================
void tm1668_init(void)
{
unsigned char dataa=0;
unsigned char i=0;
unsigned char m=0;
STB_1668 = 0;
Send_Data(0x03);
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0x40);
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0xC0);
for(i=0;i<7;i++)
{
if(m==0)
{
Send_Data(0xff);
}
else
{
Send_Data(0x0);
}
Send_Data(0x0);
}
STB_1668 = 1;
delay(25);
STB_1668 = 0;
Send_Data(0x8f);
STB_1668 = 1;
delay(25);
// display_all(i1,i1,i1,i1);
// delay(1000);
}
This content is originally created by yleer3407 , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source