单片机源程序如下:
- /***************************************************************************
- 深圳天微电子
- *文 件 名:TM1680-V1.0
- *当前版本:V1.0
- *注意事项:1、A1\A0 采用MCU进行控制,实际使用时,可以将A1\A0任意接高低电平,TM1680 ID改为相应指令即可;
- 2、该程序采用STC15W芯片模拟IIC协议,IO口为双向IO口(无需设置输入与输出),如果MCU的IO口需要设置输入和输出,则在ACK时需要设置为输入
- 3、该芯片支持标准IIC协议
- 4、SDA、SCL需要外接上拉电阻;
- 5、不同的MCU需要根据芯片时钟频率调整延时时间;
- *免责声明:1.此程序为TM1680驱动LED演示程序,仅作参考之用。
- ********************************************************************************/
- #include <reg52.h>
- #include <intrins.h>
- #include <tm1680.h>
- /***端口定义***/
- //sbit SDA=P3^0; //TM1680通讯端口设置
- //sbit SCL=P3^1;
- sbit SDA=P1^0; //TM1680通讯端口设置
- sbit SCL=P1^1;
- sbit KEY=P3^2;
- //sbit MA1=P1^6;
- //sbit MA0=P1^7;
- ///***按键功能设置***/
- //sbit KEY0=P3^2;
- //sbit KEY1=P3^3;
- //sbit KEY2=P3^6;
- /***显示数据***/
- unsigned char TM1680perseg[8]={0x10,0x20,0x40,0x80,0x01,0x02,0x04,0x8};
- //unsigned char DispA[8]={0x10,0xFE,0x92,0x92,0xFE,0x92,0x10,0x10};
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table0[24]={0xff,0xff,0xff,0xff,0xF0,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //0 ok
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table1[24]={0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0xFF,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x0C,0xff,0xff,0xff,0x00,0x00,0x00,0xff}; //1 ok
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table2[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x0c,0xff,0xff,0xff,0xff}; //2 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table3[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x0C,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //3 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table4[24]={0xff,0xf0,0x00,0xff,0xf0,0xff,0xff,0xFF,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x0C,0xff,0xff,0xff,0x00,0x00,0x00,0xff}; //4 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table5[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xFF,0xff,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x0C,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //5 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table6[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xFF,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //6 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table7[24]={0xff,0xff,0xff,0xFF,0xF0,0x00,0x00,0xFF,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x00,0x0C,0xff,0xff,0xff,0x00,0x00,0x00,0xff}; //7 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table8[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xFF,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //8 ok
-
- //地址 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
- unsigned char code table9[24]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xFF,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x0C,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //9 ok
-
- unsigned char KEY_TEMP;
- /*******************************/
- /****** 函数功能:延时函数 *****/
- /*******************************/
- void delayms(unsigned int n)
- {
- unsigned int i;
- while(n--)
- {
- for(i=0;i<300;i++);
- }
- }
- /*******************************/
- /****** 函数功能:延时函数 *****/
- /*******************************/
- void delayus(unsigned char n) //256
- {
- while (--n)
- {
- _nop_();
- }
- }
- /*******************************/
- /****** 函数功能:起始操作 *****/
- /*******************************/
- void TM1680start(void)
- {
- SDA=1;
- SCL=1;
- SDA=1;
- delayus(4);
- delayus(10);
- SDA=0;
- delayus(14); //起始信号,必须大于4.7us
- SCL=0;
- }
- /*******************************/
- /****** 函数功能:反馈操作 *****/
- /*******************************/
- void Ack(void)
- {
- SCL = 0;
- delayus(8);
- SCL = 1;
- delayus(8);
- while(SDA);
- SCL=0;
- delayus(15);
- }
- /*******************************/
- /****** 函数功能:反馈操作 *****/
- /*******************************/
- /*void TM1680SetAck(bit ack)
- {
- SCL=0; delayus(5);
- SDA = ack; //写应答信号
- SCL = 1; //拉高时钟线
- delayus(5); //延时
- SCL = 0; //拉低时钟线
- delayus(5); //延时
- }*/
- /*******************************/
- /****** 函数功能:停止操作 *****/
- /*******************************/
- void TM1680stop(void)
- {
- SDA=0;
- SCL=1;
- delayus(10);
- SDA=1; //停止信号,大于5us
- delayus(10);
- SCL=1;
- SDA=1;
- }
- /*******************************/
- /****** 函数功能:送数据 *****/
- /*******************************/
- void TM1680SendByte(unsigned char sbyte)
- {
- unsigned char i=0;
- for(i=0; i<8; i++)
- {
- SCL=0;delayus(2);
- if(sbyte&0x80)
- {
- SDA=1; //高位先发
- }else{
- SDA=0;
- }
- delayus(5);
- SCL=1;
- delayus(5); //高电平的时间大于4us
- sbyte<<=1;
- delayus(3);
- }
- SCL=0; delayus(5);
- SDA=0; delayus(5);
- }
- /*******************************/
- /****** 函数功能: 读操作 *****/
- /*******************************/
- /*unsigned char TM1680RecvByte(void)
- {
- unsigned char i=0, sbyte;
-
- SDA=1;delayus(6);
- for(i=0; i<8; i++)
- {
- SCL=0;delayus(6);
- if(SDA)
- {
- sbyte |= 0x01 ; //置1
- }else{
- sbyte &= 0xfe; //置0
- }
- delayus(3);
- SCL=1;
- delayus(5); //高电平的时间大于4us
- sbyte<<=1;
- }
- SCL=0;
- return sbyte;
- }*/
- /******************************底层函数结束**************************/
- /******************************功能函数**************************/
- /***单字节写操作函数***/
- /***写命令函数:开始--ID-ACK--命令-ACK--结束***/
- /****************************************************************/
- /*
- void TM1680WriteCmd(unsigned char scmd)
- {
- TM1680start();
- TM1680SendByte(TM1680ID);
- Ack();
- TM1680SendByte(scmd);
- Ack();
- TM1680stop();
- }*/
- /************* 写一个字节数据:***********************/
- /********** 开始--ID-ACK-内部地址-ACK--数据-ACK-结束**/
- /***************************************************/
-
-
- void TM1680WriteOneByte(unsigned char faddr, unsigned char sdate)
- {
- TM1680start();
-
- TM1680SendByte(TM1680ID); //写TM1680器件地址
- Ack();
- TM1680SendByte(faddr); //eeprom 地址
- Ack();
-
- TM1680SendByte(sdate); //写数据
- Ack();
-
- TM1680stop();
- }
- /******************************************/
- /**** 函数功能:函数功能:页操作 *******/
- /*****************************************/
-
- void TM1680PageAllWrite(unsigned char faddr, unsigned char sdat1,unsigned char sdat2,unsigned char cnt)
- {
- unsigned char i=0;
- TM1680start();
-
- TM1680SendByte(TM1680ID);
- Ack();
- TM1680SendByte(faddr); //eeprom 地址
- Ack();
- for(i=0; i<cnt;i++)
- {
- TM1680SendByte(sdat1); //写数据
- Ack();
- TM1680SendByte(sdat2); //写数据
- Ack();
- }
- TM1680stop();
- }
- void TM1680PageAll_GD_Write(unsigned char faddr, unsigned char sdat1, unsigned char sdat2,unsigned char cnt)
- {
- unsigned char i=0;
- for(i=0; i<cnt; i=i+2)
- {
- TM1680WriteOneByte( faddr+i, sdat1);
- TM1680WriteOneByte( faddr+2, sdat2);
-
- }
- TM1680stop();
- }
- /******************************************/
- /**** 按键函数功能:函数功能: 按键 *******/
- /*****************************************/
-
- void KEY_DEL()
- {
- if(KEY==0)
- {
- delayus(30);
- if(KEY==0);
- {
- delayus(10);
- while(KEY==0);
- KEY_TEMP++;
- }
- }
- if( KEY_TEMP==20)
- KEY_TEMP=0;
-
- }
- /********************************************************/
- /**** 函数功能: 先初始化后 再写命令+写数据 *******/
- /******************************************************/
- /*void TM1680WriteCmdDate(unsigned char faddr, unsigned char sdate, unsigned char cnt)
- {
- unsigned char i=0;
- TM1680start();
- TM1680SendByte(TM1680ID);
- Ack();
- TM1680SendByte(SYSDIS);
- Ack();
- TM1680SendByte(COM8NMOS);
- Ack();
- TM1680SendByte(RCMODE1);
- Ack();
- TM1680SendByte(SYSEN);
- Ack();
- TM1680SendByte(LEDON);
- Ack();
- TM1680SendByte(PWM16);
- Ack();
- TM1680SendByte(BLINKOFF);
- Ack();
-
- TM1680SendByte(faddr);
- Ack();
-
- for(i=0; i<cnt; i++)
- {
- TM1680SendByte(sdate);
- Ack();
- }
-
- TM1680stop();
- }
- */
- /******************************************/
- /**** 函数功能: TM1680 初始化 *******/
- /*****************************************/
- void TM1680Init(void)
- {
- TM1680start();
- TM1680SendByte(TM1680ID);
- Ack();
- TM1680SendByte(SYSDIS);
- Ack();
- TM1680SendByte(COM16NMOS); //根据需求进行选择
- Ack();
- TM1680SendByte(RCMODE1);
- Ack();
- TM1680SendByte(SYSEN);
- Ack();
- TM1680SendByte(LEDON);
- Ack();
- // TM1680SendByte(LEDOFF);
- // Ack();
- TM1680SendByte(PWM12);
- Ack();
- TM1680SendByte(BLINKOFF);
- Ack();
-
- TM1680stop();
- delayms(50);
- }
- /******************************************/
- /************* PWM调节测试 ***********/
- /*****************************************/
- //void PWMTest(unsigned char sTime)
- //{
- // unsigned char i=0;
- //
- // for(i=0;i<16;i++)
- // {
- // TM1680WriteCmd(PWM01 | i);
- // delayms(sTime);
- // delayms(sTime);
- // }
- //}
- /******************************************/
- /************* TM1680ID判断 *********/
- /*****************************************/
- //void TM1680IDChange(void)
- //{
- // switch(TM1680ID)
- // {
- // case 0xe4: MA1=0; MA0=0; break;
- // case 0xe5: MA1=0; MA0=1; break;
- // case 0xe6: MA1=1; MA0=0; break;
- // case 0xe7: MA1=1; MA0=1; break;
- // default:break;
- // }
- //}
- /******************************************/
- /******** 函数功能:逐渐点亮每一段 *********/
- /*****************************************/
- void TM1680PerDisp(void)
- {
- unsigned char i=0,j=0;
- unsigned char faddr=0x00, fdate=TM1680perseg[0];
- for(i=0;i<24; i++)
- {
- for(j=0;j<8;j++)
- {
- fdate=fdate|TM1680perseg[j];
- TM1680WriteOneByte(faddr,fdate);
- delayms(20);
- }
- faddr+=2;
- fdate=TM1680perseg[0];
- }
- }
- void TM1680_SZR(unsigned char *ss) small
- {
- unsigned char dd;
- unsigned char *tab;
- tab=ss;
-
- for(dd=0;dd<24;dd++)
- {
- TM1680WriteOneByte(4*dd,*tab) ;
- tab = tab+1;
- }
- }
-
- ///////////////////////////////////////////////
-
- void TM1680_SZG(unsigned char *ss) small
- {
- unsigned char dd;
- unsigned char *tab;
- tab=ss;
-
- for(dd=0;dd<24;dd++)
- {
- TM1680WriteOneByte((4*dd+2),*tab) ;
- tab = tab+1;
- }
- }
-
-
-
-
-
-
- //void TM1680_SZ0(void)
- //{
- // TM1680WriteOneByte(0,table0[0]);
- // TM1680WriteOneByte(4,table0[1]);
- // TM1680WriteOneByte(8,table0[2]);
- // TM1680WriteOneByte(12,table0[3]);
- // TM1680WriteOneByte(16,table0[4]);
- // TM1680WriteOneByte(20,table0[5]);
- // TM1680WriteOneByte(24,table0[6]);
- // TM1680WriteOneByte(28,table0[7]);
- // TM1680WriteOneByte(32,table0[8]);
- // TM1680WriteOneByte(36,table0[9]);
- // TM1680WriteOneByte(40,table0[10]);
- // TM1680WriteOneByte(44,table0[11]);
- // TM1680WriteOneByte(48,table0[12]);
- // TM1680WriteOneByte(52,table0[13]);
- // TM1680WriteOneByte(56,table0[14]);
- // TM1680WriteOneByte(60,table0[15]);
- // TM1680WriteOneByte(64,table0[16]);
- // TM1680WriteOneByte(68,table0[17]);
- // TM1680WriteOneByte(72,table0[18]);
- // TM1680WriteOneByte(76,table0[19]);
- // TM1680WriteOneByte(80,table0[20]);
- // TM1680WriteOneByte(84,table0[21]);
- // TM1680WriteOneByte(88,table0[22]);
- // TM1680WriteOneByte(92,table0[23]);
- //
- //}
- //
- //
- //
-
-
- ////////////////////////////////////////////
- ////////////// 主函数 ///////////////
- ///////////////////////////////////////////
- void main(void)
- {
- //unsigned char i=0,j=0,//fdate=0x10;
- //TM1680IDChange();
- delayms(200);
-
- TM1680Init();
-
- TM1680PageAllWrite(0x00, 0x00, 0x00,48); //上电清零
- //TM1680PageAll_GD_Write(0x00, 0x00, 0x00,96);
- delayms(20);
-
- TM1680PageAllWrite(0x00, 0xff,0xff, 48); //上电全部显示
- //TM1680PageAll_GD_Write(0x00, 0xFF, 0xff,96);
- delayms(800);
-
- TM1680Init();
- TM1680PageAllWrite(0x00, 0x00, 0x00,48); //上电清零
- delayms(20);
-
- TM1680PageAllWrite(0x00, 0xff,0x00, 48);
- //TM1680PageAll_GD_Write(0x00, 0x00, 0x00,96);
- ///delayms(20);
-
- ///TM1680PageAll_GD_Write(0x00, 0xFF,0x00,96);
- delayms(800);
-
-
- TM1680Init();
- TM1680PageAllWrite(0x00, 0x00, 0x00,48); //上电清零
- delayms(20);
- TM1680PageAllWrite(0x00, 0x00,0xff, 48); //清零
- //TM1680PageAll_GD_Write(0x00, 0x00, 0xFF,96);
- delayms(800);
- //TM1680PerDisp(); //每一段显示
- //TM1680Init();
- TM1680Init();
- TM1680PageAllWrite(0x00, 0x00, 0x00,48); //上电清零
- delayms(20);
- // TM1680WriteOneByte(0,0xff);
- // TM1680WriteOneByte(4,0xff);
- // TM1680WriteOneByte(8,0xff);
- //
- //
- //
-
-
-
- //TM1680WriteOneByte(4,0xff);
- // delayms(20);
-
-
- //TM1680_SZ0( );
-
- while(1)
- {
- KEY_DEL() ;
- switch(KEY_TEMP)
- {
-
- case 0:
- {
- TM1680_SZR(table0);
-
- }
- break;
-
- case 1:
- {
-
- TM1680_SZR(table1);
-
-
- }
- break;
-
- case 2:
- {
- TM1680_SZR(table2);
- }
- break;
-
- case 3:
- {
- TM1680_SZR(table3);
- }
- break;
-
- case 4:
- {
- TM1680_SZR(table4);
-
- }
- break;
-
- case 5:
- {
- TM1680_SZR(table5);
-
- }
- break;
- case 6:
- {
- TM1680_SZR(table6);
-
- }
- break;
- case 7:
- {
- TM1680_SZR(table7);
-
- }
- break;
-
- case 8:
- {
- TM1680_SZR(table8);
-
- }
- break;
-
- case 9:
- {
- TM1680_SZR(table9);
- }
- break;
-
-
-
- case 10:
- {
- TM1680PageAllWrite(0x00, 0x00, 0x00,48); //上电清零
- delayms(20);
-
- TM1680_SZG(table0);
-
- }
- break;
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码 原理图: 无
仿真: 无
代码:
TM1680CX-16X24.zip
(44.66 KB, 下载次数: 58)
|