- #include <stc8.h>
- #include <intrins.h>
- sbit CLK = P1^0; // 连接到TM1640的CLK引脚
- sbit DIO = P1^1; // 定义DIO引脚,连接到TM1640的DIO引脚
- //本地常量声明
- unsigned char code table[]={ //标准字库
- // 0 1 2 3 4 5 6 7 8 9 A B C D E F
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
- //black - H J K L N o P U t G Q r M y
- 0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e};
- void TM1640_Start(void)
- {
- CLK = 1; // CLK引脚置高电平
- DIO = 1; // DIO引脚置高电平
- _nop_();_nop_();//延时2个时钟
- DIO = 0; // DIO引脚置低电平
- CLK = 0;
- }
- void TM1640_Stop(void)
- {
- DIO = 0; // DIO引脚置低电平
- _nop_();_nop_();//延时2个时钟
- CLK = 1; // CLK引脚置高电平
- _nop_();_nop_();//延时2个时钟
- DIO = 1; // DIO引脚置高电平
- }
- void TM1640_Write_Byte(unsigned char data1)
- {
- unsigned char i;
- CLK = 0;
- DIO = 0;
- for(i=0; i<8; i++) // 循环8次,写入8位数据
- {
- CLK = 0; // CLK引脚置低电平
- if(data1 & 0x01)
- DIO = 1; // 将data1的最低位写入DIO引脚
- else
- DIO = 0;
- CLK = 1;
- data1 >>= 1; // data1右移1位,准备写入下一位
- }
- CLK = 0; // CLK引脚置低电平
- DIO = 0; // DIO引脚置低电平
- }
- void TM1640_Write_Com(unsigned char date)//发送命令字
- {
- TM1640_Start();
- TM1640_Write_Byte(date);
- TM1640_Stop();
- }
- void TM1640_Display()
- {
- unsigned char i;
- TM1640_Write_Com(0x40);//44固定地址 40连续地址
- TM1640_Start(); // 发送起始信号
- TM1640_Write_Byte(0xc0);//从第一个地址开始
- for(i=0;i<3;i++)
- TM1640_Write_Byte(table[i+1]); // 连续发3个数字。最多16个数字
- TM1640_Stop(); // 发送结束信号
- }
- void TM1640_Cls()//清屏函数
- {
- unsigned char i;
- TM1640_Write_Com(0x40);//地址自动加1模式
- TM1640_Start();
- TM1640_Write_Byte(0xc0);
- for(i=0;i<16;i++)
- TM1640_Write_Byte(0x00);
- TM1640_Stop();
- }
- void TM1640_Init()
- {
- TM1640_Cls();
- TM1640_Write_Com(0x8c);//亮度0x88~0x8F
- }
- void main()
- {
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P2M0 = 0x00;
- P2M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P4M0 = 0x00;
- P4M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- P6M0 = 0x00;
- P6M1 = 0x00;
- P7M0 = 0x00;
- P7M1 = 0x00;
- TM1640_Init();
- TM1640_Display();//连续发1、2、3。
- while(1);
- }
复制代码 |