/* *********
* File: tm1640.h
* Author: Administrator
*
* Created on 2015年11月23日, 下午6:14
*/
#define TM1640Port (GPIOD->ODR)
#define TMSDA RC5
#define TMSCL RC4
/***************************TM1640显示缓冲区*********************************/
unsigned char led_buffer[11]={0,0,0,0,0,0,0,0, //8位数字缓冲区
0,0,0}; //18个灯缓冲区
/***************************************************************************/
//位选地址
#define DIG1 0xc0
#define DIG2 0xc1
#define DIG3 0xc2
#define DIG4 0xc3
#define DIG5 0xc4
#define DIG6 0xc5
#define DIG7 0xc6
#define DIG8 0xc7
#define DIG9 0xc8
#define DIG10 0xc9
#define DIG11 0xca
#define DIG12 0xcb
#define DIG13 0xcc
#define DIG14 0xcd
#define DIG15 0xce
#define DIG16 0xcf
//显示亮度级别
#define LEVEL_OFF 0x80
#define LEVEL_1 0x88
#define LEVEL_2 0x89
#define LEVEL_3 0x8a
#define LEVEL_4 0x8b
#define LEVEL_5 0x8c
#define LEVEL_6 0x8d
#define LEVEL_7 0x8e
#define LEVEL_8 0x8f
#define DATA_CMD_ADDR_INC 0x40 //地址自动加1
#define DATA_CMD_ADDR_FIXED 0x44 //固定地址
#define TM1640_DELAY NOP();NOP();
//点定义
#define V1DOT 31
#define V2DOT 23
#define V3DOT 15
#define DBM1DOT 63
#define DBM2DOT 55
#define DBM3DOT 47
#define LED_3MV 64
#define LED_30MV 65
#define LED_300MV 66
#define LED_3V 67
#define LED_30V 68
#define LED_300V 69
#define LED_AUTO 70
#define LED_MANU 71
#define LED_KEYDB 72
#define LED_KEYDBM 73
#define LED_CH1 74
#define LED_CH2 75
#define LED_UNITDBM 76
#define LED_UNITDB 77
#define LED_MV 78
#define LED_V 79
#define LED_OVER 80
#define LED_UNDER 81
//*********START***********************************************
void start() //1640开始
{
TMSDA=1;
TMSCL=1;
TM1640_DELAY;
TMSDA=0;
TM1640_DELAY;
TMSCL=0;
}
//**************************************************************
void stop() //1640结束
{
TMSDA=0;
TMSCL=1;
TM1640_DELAY;
TMSDA=1;
TM1640_DELAY;
TMSCL=1;
}
/*******向TM1640写字节***********/
void write(unsigned char date) //1640写字节
{
unsigned char i,aa;
aa=date;
for(i=0;i<8;i++)
{
TMSCL=0; TM1640_DELAY;
if(aa&0x01)
{
TMSDA=1;
}
else
{
TMSDA=0;
}
TM1640_DELAY;
TMSCL=1;
TM1640_DELAY;
aa=aa>>1;
TM1640_DELAY;
}
}
/*将TM1640显示缓冲区写入到芯片中*/
/*可能需要在语句间增加必要的延时*/
/*如果128个点全部用到,需要修改变量I的值*/
void flood_led_data(void)
{
unsigned int temp_data;
start();
write(DATA_CMD_ADDR_INC);//设置地址自动+1模式
stop();
start();
write(DIG1); //设置起始地址
for(unsigned char i=0;i<=11;i++)
{
temp_data = led_buffer[i];
write(temp_data);
}
stop();
}
/*向TM1640直接写入一个字符*/
/*display=1 显示*/
/*display=0 关闭*/
void led_char(unsigned char name,unsigned char display)
{
unsigned char i,j;
i= name/8;
j= name%8;
if (display == 1) led_buffer[i] |= 1<<j;
else led_buffer[i] &= ~(1<<j);
flood_led_data();
}
/*清屏*/
/*如果128个点全部用到,需要修改变量I的值*/
void led_clr()
{
for (char i=0; i<=11;i++) led_buffer[i]=0;
flood_led_data();
}
/*全显*/
/*如果128个点全部用到,需要修改变量I的值*/
void led_full()
{
for (char i=0; i<=11;i++) led_buffer[i]=0xff;
flood_led_data();
}
//向TM1640写一个0~9的数
//电压显示数位左至右为0~3,DB/DBM显示数位左至右为4~7;
//number:想显示的数 0~9 (10:显示H,仅0-3位数字有效)
//position:显示的数位
//display:显示开关,0 关闭该数位显示(此时number值无效),1 显示该数位数字
void led_number(unsigned char number,unsigned char position,unsigned char display)
{
switch (position)
{
case 0:
led_buffer[3] &= 0x80;
if (display == 0)
break;
else switch(number)
{
case 0:led_buffer[3] |= 0x3f;
break;
case 1:led_buffer[3] |= 0x06;
break;
case 2:led_buffer[3] |= 0x5b;
break;
case 3:led_buffer[3] |= 0x4f;
break;
case 4:led_buffer[3] |= 0x66;
break;
case 5:led_buffer[3] |= 0x6d;
break;
case 6:led_buffer[3] |= 0x7d;
break;
case 7:led_buffer[3] |= 0x07;
break;
case 8:led_buffer[3] |= 0x7f;
break;
case 9:led_buffer[3] |= 0x6f;
break;
case 10:led_buffer[3] |=0x76; //显示H
break;
case 11:led_buffer[3] |=0x40; //显示-
break;
}
break;
case 1:
led_buffer[2] &= 0x80;
if (display == 0)
break;
else switch (number)
{
case 0:led_buffer[2] |= 0x3f;
break;
case 1:led_buffer[2] |= 0x06;
break;
case 2:led_buffer[2] |= 0x5b;
break;
case 3:led_buffer[2] |= 0x4f;
break;
case 4:led_buffer[2] |= 0x66;
break;
case 5:led_buffer[2] |= 0x6d;
break;
case 6:led_buffer[2] |= 0x7d;
break;
case 7:led_buffer[2] |= 0x07;
break;
case 8:led_buffer[2] |= 0x7f;
break;
case 9:led_buffer[2] |= 0x6f;
break;
case 10:led_buffer[2] |=0x76; //显示H
break;
case 11:led_buffer[2] |=0x40; //显示-
break;
}
break;
case 2:
led_buffer[1] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[1] |= 0x3f;
break;
case 1:led_buffer[1] |= 0x06;
break;
case 2:led_buffer[1] |= 0x5b;
break;
case 3:led_buffer[1] |= 0x4f;
break;
case 4:led_buffer[1] |= 0x66;
break;
case 5:led_buffer[1] |= 0x6d;
break;
case 6:led_buffer[1] |= 0x7d;
break;
case 7:led_buffer[1] |= 0x07;
break;
case 8:led_buffer[1] |= 0x7f;
break;
case 9:led_buffer[1] |= 0x6f;
break;
case 10:led_buffer[1] |=0x76; //显示H
break;
case 11:led_buffer[1] |=0x40; //显示-
break;
}
break;
case 3:
led_buffer[0] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[0] |= 0x3f;
break;
case 1:led_buffer[0] |= 0x06;
break;
case 2:led_buffer[0] |= 0x5b;
break;
case 3:led_buffer[0] |= 0x4f;
break;
case 4:led_buffer[0] |= 0x66;
break;
case 5:led_buffer[0] |= 0x6d;
break;
case 6:led_buffer[0] |= 0x7d;
break;
case 7:led_buffer[0] |= 0x07;
break;
case 8:led_buffer[0] |= 0x7f;
break;
case 9:led_buffer[0] |= 0x6f;
break;
case 10:led_buffer[0] |=0x76; //显示H
break;
case 11:led_buffer[0] |=0x40; //显示-
break;
}
break;
case 4:
led_buffer[7] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[7] |= 0x3f;
break;
case 1:led_buffer[7] |= 0x06;
break;
case 2:led_buffer[7] |= 0x5b;
break;
case 3:led_buffer[7] |= 0x4f;
break;
case 4:led_buffer[7] |= 0x66;
break;
case 5:led_buffer[7] |= 0x6d;
break;
case 6:led_buffer[7] |= 0x7d;
break;
case 7:led_buffer[7] |= 0x07;
break;
case 8:led_buffer[7] |= 0x7f;
break;
case 9:led_buffer[7] |= 0x6f;
break;
case 10:led_buffer[7] |=0x76; //显示H
break;
case 11:led_buffer[7] |=0x40; //显示-
break;
}
break;
case 5:
led_buffer[6] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[6] |= 0x3f;
break;
case 1:led_buffer[6] |= 0x06;
break;
case 2:led_buffer[6] |= 0x5b;
break;
case 3:led_buffer[6] |= 0x4f;
break;
case 4:led_buffer[6] |= 0x66;
break;
case 5:led_buffer[6] |= 0x6d;
break;
case 6:led_buffer[6] |= 0x7d;
break;
case 7:led_buffer[6] |= 0x07;
break;
case 8:led_buffer[6] |= 0x7f;
break;
case 9:led_buffer[6] |= 0x6f;
break;
case 10:led_buffer[6] |=0x76; //显示H
break;
case 11:led_buffer[6] |=0x40; //显示-
break;
}
break;
case 6:
led_buffer[5] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[5] |= 0x3f;
break;
case 1:led_buffer[5] |= 0x06;
break;
case 2:led_buffer[5] |= 0x5b;
break;
case 3:led_buffer[5] |= 0x4f;
break;
case 4:led_buffer[5] |= 0x66;
break;
case 5:led_buffer[5] |= 0x6d;
break;
case 6:led_buffer[5] |= 0x7d;
break;
case 7:led_buffer[5] |= 0x07;
break;
case 8:led_buffer[5] |= 0x7f;
break;
case 9:led_buffer[5] |= 0x6f;
break;
case 10:led_buffer[5] |=0x76; //显示H
break;
case 11:led_buffer[5] |=0x40; //显示-
break;
}
case 7:
led_buffer[4] &= 0x80;
if (display == 0) break;
else switch (number)
{
case 0:led_buffer[4] |= 0x3f;
break;
case 1:led_buffer[4] |= 0x06;
break;
case 2:led_buffer[4] |= 0x5b;
break;
case 3:led_buffer[4] |= 0x4f;
break;
case 4:led_buffer[4] |= 0x66;
break;
case 5:led_buffer[4] |= 0x6d;
break;
case 6:led_buffer[4] |= 0x7d;
break;
case 7:led_buffer[4] |= 0x07;
break;
case 8:led_buffer[4] |= 0x7f;
break;
case 9:led_buffer[4] |= 0x6f;
break;
case 10:led_buffer[4] |=0x76; //显示H
break;
case 11:led_buffer[4] |=0x40; //显示-
break;
}
break;
}
flood_led_data();
}
/*初始化*/
void init_tm1640(void)
{
TMSCL = 1;
TMSDA = 1;
start();
write(LEVEL_5); //设置显示亮度
stop();
}