#include "TM16xx.h"
#include "time.h"
#include "systick.h"
#include "Global_Variable.h"
//共阴数码管
//extern unsigned char tab[];
void TM1638_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_1;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_Level_1;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void TM1638_Write(unsigned char DATA) //写数据函数
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK_0;
if(DATA&0X01)
DIO_1;
else
DIO_0;
DATA>>=1;
CLK_1;
}
}
unsigned char TM1638_Read(void) //读数据函数
{
unsigned char i;
unsigned char temp=0;
DIO_1; //设置为输入
for(i=0;i<8;i++)
{
temp>>=1;
CLK_0;
if(DIO)
temp|=0x80;
CLK_1;
}
return temp;
}
void Write_COM(unsigned char cmd) //发送命令字
{
STB_0;
TM1638_Write(cmd);
STB_1;
}
unsigned char Read_key2(void)
{
unsigned char c[4],i,key_value=0;
STB_0;
TM1638_Write(0x42); //读键扫数据 命令
for(i=0;i<4;i++)
c[i]=TM1638_Read();
STB_1; //4个字节数据合成一个字节
for(i=0;i<4;i++)
key_value|=c[i]<<i;
for(i=0;i<8;i++)
if((0x01<<i)==key_value)
break;
return i;
}
extern uint8_t Main_Interface;
unsigned char Read_key(void)
{
unsigned char c[4],i,key_value=0;
static unsigned char key_value_REG=0;
STB_0;
TM1638_Write(0x42);
// Delay_ms(300);
for(i=0;i<4;i++)
c[i]=TM1638_Read();
STB_1; //4个字节数据合成一个字节
switch(c[3])
{
case 0x10:key_value=1;
break;
case 0x20:key_value=2;
break;
case 0x40:key_value=3;
break;
case 0x80:key_value=4;
break;
default:key_value=0;
break;
}
if(key_value_REG!=key_value)
{
key_value_REG=key_value;
return (key_value);
}
if(c[3]==0x10)
{
key_value=1;
return (key_value);
}
if(Main_Interface==0&&EEPROM.tongsuo==0)
{
if(c[3]==0x20)
{
key_value=2;
return (key_value);
}
}
return 0;
}
uint8_t KEY2=0;
extern uint8_t KEY;
uint8_t Key_State;
uint8_t Pushflag;
uint8_t anjianbufang;
void Key(void)
{
uint8_t Temp;
static uint16_t Addflag;
// static uchar leijia;
// bit Fast;
KEY2=Read_key();
if(TIMing_BSAT)//在中断里Key_Work = 1;,相当于一个滤波
{
TIMing_BSAT = 0;
switch(Key_State)
{
case 0:
{
if((KEY2 != 0))
{
Key_State++;
Pushflag = 0;
anjianbufang = 0;
// if((P4 == 0xef)||(P4 == 0xfd))
// {
// leijia = 10;
// }
// else
// {
// leijia = 12;
// }
}
}break;
case 1://短按
{
if((KEY2 != 0))
{
Addflag++;
if(Pushflag ++ > 11) //10
{
Temp = KEY2;
Pushflag = 0;
{
KEY = Read_key();
}
{
Key_State = 0;
Addflag = 0;
}
}
}
else
{
Key_State = 0;
anjianbufang = 0;
}
}break;
case 2://长按
{
if((KEY2 != 0))
{
if(Pushflag ++>= 1)
{
Temp = KEY2;
Pushflag = 0;
// if(stop == 0)
// {
// KeyValue = STOP;
// }
// else
{
KEY = Read_key();//长按一直返回键值
}
}
}
else
{
Key_State = 0;
anjianbufang = 0;
}
}break;
default:
{
Key_State = 0;
}break;
}
}
}
void Write_DATA(unsigned char add,unsigned char DATA) //指定地址写入数据
{
Write_COM(0x44);
STB_0;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB_1;
}
/*
void Write_oneLED(unsigned char num,unsigned char flag) //单独控制一个LED函数,num为需要控制的led序号,flag为0时熄灭,不为0时点亮
{
if(flag)
Write_DATA(2*num+1,1);
else
Write_DATA(2*num+1,0);
} */
void Write_allLED(unsigned char LED_flag) //控制全部LED函数,LED_flag表示各个LED状态
{
unsigned char i;
for(i=0;i<8;i++)
{
if(LED_flag&(1<<i))
//Write_DATA(2*i+1,3);
Write_DATA(2*i+1,1);
else
Write_DATA(2*i+1,0);
}
}
//TM129D初始化函数
void init_TM1629D(void)
{
unsigned char i;
Write_COM(0x8b); //亮度 (0x88-0x8f)8级亮度可调
Write_COM(0x40); //采用地址自动加1
STB_0; //
TM1638_Write(0xc0); //设置起始地址
for(i=0;i<16;i++) //传送16个字节的数据
TM1638_Write(0x00);
STB_1;
}
unsigned char DisBuffer[8]={0,0,0,0,0,0,0,0}; /*显示缓存区*/ //各个数码管显示的值
// 10 11 12 13 14 15 16 17
// 0 1 2 3 4 5 6 7 8 9 A b C d E F - 空
unsigned char tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x40,0x00,
// 18 19 20 21 23 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. A. b. C. d. E. F. -. 空.
0xef,0xef,0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0xF7,0xFC,0xB9,0xDE,0xF9,0xF1,0xC0,0x80};
void LedDisplay(unsigned char data0,unsigned char data1,unsigned char data2,unsigned char data3,
unsigned char data4,unsigned char data5,unsigned char data6,unsigned char data7);
void LedDisplay(unsigned char data0,unsigned char data1,unsigned char data2,unsigned char data3,
unsigned char data4,unsigned char data5,unsigned char data6,unsigned char data7);
unsigned char key_value;
uint16_t a1,a2,a3,a4;
void main2(void)
{
LedDisplay(1,3,7,0,2,1,0,2);
// delay_ms(1000);
while(1)
{
// key_value=Read_key();
// Now_Disp(a1,a2,a3,a4);
switch (key_value)
{
case 1:
LedDisplay(1,0,0,0,0,0,0,0);
break;
case 2 :
LedDisplay(0,2,0,0,0,0,0,0);
break;
case 3 :
LedDisplay(0,0,3,0,0,0,0,0);
break;
case 4 :
LedDisplay(0,0,0,4,0,0,0,0);
break;
case 5 :
LedDisplay(0,0,0,0,5,0,0,0);
break;
case 6 :
LedDisplay(0,0,0,0,0,6,0,0);
break;
case 7 :
LedDisplay(0,0,0,0,0,0,7,0);
break;
case 8 :
LedDisplay(0,0,0,0,0,0,0,8);
break;
case 9:
LedDisplay(9,0,0,0,0,0,0,0);
break;
case 10 :
LedDisplay(0,1,0,0,0,0,0,0);
break;
case 11 :
LedDisplay(1,1,0,0,0,0,0,0);
break;
case 12 :
LedDisplay(2,1,0,0,0,0,0,0);
break;
case 13 :
LedDisplay(3,1,0,0,0,0,0,0);
break;
case 14 :
LedDisplay(4,1,0,0,0,0,0,0);
break;
case 15:
LedDisplay(5,1,0,0,0,0,0,0);
break;
case 16 :
LedDisplay(6,1,0,0,0,0,0,0);
break;
default :
break;//LedDisplay(0,0,0,0,0,0,0,0);
}
}
}
void LedDisplay(unsigned char ddata0,unsigned char ddata1,unsigned char ddata2,unsigned char ddata3,
unsigned char ddata4,unsigned char ddata5,unsigned char ddata6,unsigned char ddata7)
{
unsigned char writedata0,writedata1,writedata2,writedata3,writedata4,writedata5,writedata6,writedata7;
unsigned char data0,data1,data2,data3,data4,data5,data6,data7;
// LedDisplay(1,3,7,0,2,1,0,2);
data0=ddata4;//2
data1=ddata5;//1
data2=ddata6;//0
data3=ddata7;//2
data4=ddata0;//1
data5=ddata1;//3
data6=ddata2;//7
data7=ddata3;//0
//unsigned char code tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x7