#include "include.h"
//函数
//管脚操作函数
#define SETDIN() GPIO_SetBits(GPIOC,TM1640DIN_Pin)
#define CLRDIN() GPIO_ResetBits(GPIOC,TM1640DIN_Pin)
#define SETDIN2() GPIO_SetBits(GPIOC,TM1640DIN2_Pin)
#define CLRDIN2() GPIO_ResetBits(GPIOC,TM1640DIN2_Pin)
#define SETSCLK() GPIO_SetBits(GPIOB,TM1640SCLK_Pin)
#define CLRSCLK() GPIO_ResetBits(GPIOB,TM1640SCLK_Pin)
#define LEDREGMAXLEN 16 //最大16个LED寄存器
#define LONGDELAY 50 //最大16个LED寄存器
#define SHORTELAY 5 //最大16个LED寄存器
void SendLEDCmd(unsigned char cmd);//发送命令函数
void SendLEDData(unsigned char cmd2,unsigned char dat);//发送数据函数
//段码从空格开始,到~结束,共95个
static unsigned char ASCII[]={
/* ! " # $ % & ' ( ) * + , - . / */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0X39,0x0F,0x00,0x00,0x00,0x40,0x80,0x00,
/*0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ */
0X3f,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
/*A B C D E F G H I J K L M N O P */
0X77,0X7c,0X39,0X5e,0X79,0X71,0X7D,0X76,0X30,0X0e,0x00,0X38,0x00,0x37,0X3f,0X73,
/*Q R S T U V W X Y Z [ \ ] ^ _ ` */
0X67,0X31,0X6D,0X78,0X3e,0X3e,0X00,0X00,0X6e,0X5B,0X39,0x00,0x0F,0x00,0X08,0x00,
/*a b c d e f g h i j k l m n o p */
0X77,0X7c,0X39,0X5e,0X79,0X71,0X6F,0X74,0X10,0X0e,0x00,0X10,0x00,0x54,0X5c,0X73,
/*q r s t u v w x y z { | } ~ */
0X67,0X31,0X6D,0X78,0X1C,0X1C,0X00,0X00,0X6e,0X5B,0X39,0x00,0x0F,0x01,
};
//延时程序
void I2c_Delay(u32 time)
{
while(time--)
{
}
}
//起始信号程序
void I2c_Start(u8 chip_num)
{
if(chip_num == tm1640_1)
{
I2C_SDA1_LED(1);
I2c_Delay(SHORTELAY);
I2C_SCL_LED(1);
I2c_Delay(LONGDELAY);
I2C_SDA1_LED(0);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(0);
}
else
{
I2C_SDA2_LED(1);
I2c_Delay(SHORTELAY);
I2C_SCL_LED(1);
I2c_Delay(LONGDELAY);
I2C_SDA2_LED(0);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(0);
}
}
//终止信号程序
void I2c_Stop(u8 chip_num)
{
if(chip_num == tm1640_1)
{
I2C_SDA1_LED(0);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(1);
I2c_Delay(LONGDELAY);
I2C_SDA1_LED(1);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(0);
//I2c_Delay(LONGDELAY);
}
else
{
I2C_SDA2_LED(0);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(1);
I2c_Delay(LONGDELAY);
I2C_SDA2_LED(1);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(0);
}
}
/*向IIC总线送8位数据*/
void out8bit(u8 chip_num,u8 data)
{
u8 i;
for(i=0;i<8;i++)
{
if(chip_num == tm1640_1)
{
if(data&0x01)
I2C_SDA1_LED(1);
else
I2C_SDA1_LED(0);
}
else
{
if(data&0x01)
I2C_SDA2_LED(1);
else
I2C_SDA2_LED(0);
}
I2c_Delay(LONGDELAY);
I2C_SCL_LED(1);
I2c_Delay(LONGDELAY);
I2C_SCL_LED(0);
I2c_Delay(LONGDELAY);
data>>=1;
}
}
/***********
TM1640 显示屏管脚初始化
**********/
void TM1640_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//显示屏输出管脚
GPIO_InitStructure.GPIO_Pin = GPIO_LEDSCL_Pin ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LEDSCL, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_LEDSDA1_Pin ;
GPIO_Init(GPIO_LEDSDA1, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_LEDSDA2_Pin ;
GPIO_Init(GPIO_LEDSDA2, &GPIO_InitStructure);
}
/**************
设置背光亮度等级,分1-15级
***************/
/*void SetLEDBackLightLevel(u8 level)
{
LEDBLlevel =level;
}*/
void TM1640_WriteBuf(u8* buf,u8 len)
{ //static u16 aa =0;
u8 i;
static u16 aa =0;
static u8 LEDBLlevel =7;
aa++;
if(QSumTimer(Display_Lever_TIMER,OS_TICKS_PER_SEC*20) !=FALSE )
{
if(Bakup_YYflag.NIGHT_FLAGE)
{
if((LEDBLlevel>1)&&((aa%500) ==1))
LEDBLlevel --;
}
else
{
if((LEDBLlevel>2)&&((aa%500) ==1))
LEDBLlevel --;
}
}
else
{
aa =0;
LEDBLlevel =(6-(u8)(Bakup_YYflag.NIGHT_FLAGE)*2);
}
/*tm1640_1的驱动显示*/
I2c_Start(tm1640_1);
out8bit(tm1640_1,AUTO_DATA_COMMAND);
I2c_Stop(tm1640_1);
I2c_Start(tm1640_1);
out8bit(tm1640_1,ADDR0_COMMAND);
for(i=0;i<LEDREGMAXLEN;i++)
{
out8bit(tm1640_1,buf[i]);
}
I2c_Stop(tm1640_1);
I2c_Start(tm1640_1);
out8bit(tm1640_1,DISPLAY_ON_COMMAND+LEDBLlevel);
//if((++aa%100) ==1)
// out8bit(tm1640_1,DISPLAY_ON_COMMAND+aa/100);
I2c_Stop(tm1640_1);
/*tm1640_2的驱动显示*/
I2c_Start(tm1640_2);
out8bit(tm1640_2,AUTO_DATA_COMMAND);
I2c_Stop(tm1640_2);
I2c_Start(tm1640_2);
out8bit(tm1640_2,ADDR0_COMMAND);
for(i=LEDREGMAXLEN;i<len;i++)
{
out8bit(tm1640_2,buf[i]);
}
for(i=21;i<len;i++)
{
out8bit(tm1640_2,buf[i]);
}
I2c_Stop(tm1640_2);
I2c_Start(tm1640_2);
//if((aa%100) ==1)
// out8bit(tm1640_2,DISPLAY_ON_COMMAND+aa/100);
out8bit(tm1640_2,DISPLAY_ON_COMMAND+LEDBLlevel);
I2c_Stop(tm1640_2);
}
/********************
发送命令函数,用于发送设置数据明令和显示控制命令
********************/
//设置LED显示
void SetLEDON(void)
{
static u16 aa =0;
static u8 LEDBLlevel =7;
aa++;
if(QSumTimer(Display_Lever_TIMER,OS_TICKS_PER_SEC*60) !=FALSE)
{
if(Bakup_YYflag.NIGHT_FLAGE)
{
if((LEDBLlevel>1)&&((aa%1000) ==1))
LEDBLlevel --;
}
else
{
if((LEDBLlevel>2)&&((aa%1000) ==1))
LEDBLlevel --;
}
}
else
{
aa =0;
LEDBLlevel =(4-(u8)(Bakup_YYflag.NIGHT_FLAGE));
}
I2c_Start(tm1640_1);
//aa++;
//if((aa%100) ==1)
// out8bit(tm1640_1,DISPLAY_ON_COMMAND+aa/100);
out8bit(tm1640_1,DISPLAY_ON_COMMAND+LEDBLlevel);
I2c_Stop(tm1640_1);
I2c_Start(tm1640_2);
// if((aa%100) ==1)
// out8bit(tm1640_2,DISPLAY_ON_COMMAND+aa/100);
out8bit(tm1640_2,DISPLAY_ON_COMMAND+LEDBLlevel);
I2c_Stop(tm1640_2);
}
/********************
函数功能:LED显示格式化函数,把要显示的字符串对应的显示段码放入显示缓冲区
********************/
void LEDPlayStr(unsigned char *str,unsigned char *dst,unsigned char len)
{
unsigned char i,k;
k = 0;
for(i=0;i<len;i++)
{
if((str[i] =='.')&&(k !=0))
{
dst[i] = dst[i -1]|0x80;
dst[i -1] = ASCII[' ' - 0x20];
}
else
{ dst[i] = ASCII[str[i] - 0x20];
k++;
}
}
}
/********************
函数功能:LED显示格式化函数,把要显示的数字对应的显示段码放入显示缓冲区
********************/
void LEDPlayNum(unsigned char *str,unsigned char *dst,unsigned char len)
{
unsigned char i,j,k;
k = 1;
for(i=0;i<len;i++)
{
j = str[len-1-i];
if(str[len-1-i] == '.')
{
i++;
j = str[len-1-i];
dst[len-k] = ASCII[j - 0x20];
dst[len-k] |= 0x80;
}
else
{
if(dst[len-k] < 10)
dst[len-k] = ASCII[j + 16];
else if(dst[len-k] < 16)
dst[len-k] = ASCII[j + 23];
else
dst[len-k] = 0;
}
k++;
}
}
评论6