#include "stm8s.h"
#include "pub.h"
#include "dali_cmd.h"
#include "dali_regs.h"
#include "dali_pub.h"
/*****************************************************************************
常量定义
******************************************************************************/
#define KEY_UNABLE 0x0000
#define KEY1_VALUE 0x0002
#define KEY2_VALUE 0x1000
#define KEY3_VALUE 0x0200
#define tube_light_level 0x88
#define uchar unsigned char
#define uint unsigned int
#define Dormancy 6000 //定义休眠时间倒计数
#define Extinguish_Tube 0x80 //显示屏关
#define Tube_Light_Level 0x88 //定义数码管亮度等级
#define Lock_Count_Number 1500 //定义上锁时间
unsigned char Long_Press_Flag = 1; //定义长按标记
extern unsigned char Pc_Alloct_flag;
unsigned int Pwer_on_Flag; //上电标志位
unsigned int Extinguish_Tube_flag; //60s标志位
unsigned int Extinguish_Tube_count=Dormancy;
unsigned int Lock_Keyscan_count=Lock_Count_Number; //15s标志位
unsigned char Ulock_flag_2s; //解锁标志位
unsigned char Flash_flag; //500ms标志位
unsigned char Flash_count;
unsigned int addr;
unsigned int addr_1,addr_2; //定义地址
unsigned int i_key_value; //按键值
unsigned char c_key_step; //按键步骤
unsigned char c_key_up; //按键去除后计数,要200毫秒才算
unsigned char c_key_count;
unsigned char key_frist_times;
unsigned char c_key_time; //按键后计时,30秒后锁定键盘
unsigned char KEY_TIMES=2;
unsigned int read_key(void);
unsigned char c_read_key[3];
u8 key_number;
u8 number;
u8 key_frist_times;
unsigned char equipment_config;//自动分配标志位
/*****************************************************************************
管脚定义及宏定义
******************************************************************************/
#define DIO_PIN 4 //PD4
#define CLK_PIN 3 //PD3
#define STB_PIN 2 //PD2
#define STB_ON() GPIOD->ODR|= (1<<STB_PIN)
#define STB_OFF() GPIOD->ODR&=~(1<<STB_PIN)
#define CLK_ON() GPIOD->ODR|= (1<<CLK_PIN)
#define CLK_OFF() GPIOD->ODR&=~(1<<CLK_PIN)
#define DIO_ON() GPIOD->ODR|= (1<<DIO_PIN)
#define DIO_OFF() GPIOD->ODR&=~(1<<DIO_PIN)
#define DIO_ENABLE_OUT() GPIOD->DDR|= (1<<DIO_PIN)
#define DIO_ENABLE_IN() GPIOD->DDR&=~(1<<DIO_PIN)
#define READ_DIO() GPIOD->IDR&(1<<DIO_PIN)
void Save_DATA_To_EEPROM(unsigned int EEPROM_add,uchar number);//存储地址和数据
void display(uchar tube_addr,uchar location,uchar x);//输入显示地址和要输出的数字
/*****************************************************************************
******************************************************************************/
//地址数组0-63
const unsigned char A0_A63[64]=
{
0x01,0x03,0x05,0x07,0x09,0x0B,0x0D,0x0F,
0x11,0x13,0x15,0x17,0x19,0x1B,0x1D,0x1F,
0x21,0x23,0x25,0x27,0x29,0x2B,0x2D,0x2F,
0x31,0x33,0x35,0x37,0x39,0x3B,0x3D,0x3F,
0x41,0x43,0x45,0x47,0x49,0x4B,0x4D,0x4F,
0x51,0x53,0x55,0x57,0x59,0x5B,0x5D,0x5F,
0x61,0x63,0x65,0x67,0x69,0x6B,0x6D,0x6F,
0x71,0x73,0x75,0x77,0x79,0x7B,0x7D,0x7F
};
uchar correspond_addr(uchar input)//把真实地址转换成显示地址
{
uchar temp,End_number;
for(temp=0,End_number=63;temp<33;temp++,End_number--)
{
if( input == A0_A63[temp])
{
return temp;
//break;
}
else if(input == A0_A63[End_number])
{
return End_number;
}
}
return temp;
}
void Store_Key_As_Addr();
const unsigned char tube_1[4][2]=
{
{0x06,0x20},{0x1B,0x30},{0x0f,0x30},{0x06,0x38}
};
//0到9数组定义 {0x17,0x18}A
const unsigned char tube_2[9][2]=
{
{0x1F,0x08},{0x06,0x00},{0x1B,0x10},{0x0f,0x10},{0x06,0x18},
{0x0D,0x18},{0x1D,0x18},{0x11,0x18},{0x00,0x00}
};
//0到9,F数组定义 {0x1E,0x08}
const unsigned char tube_3[11][2]=
{
{0x1F,0x08},{0x06,0x00},{0x1B,0x10},{0x0f,0x10},{0x06,0x18},
{0x0D,0x18},{0x1D,0x18},{0x07,0x00},{0x1F,0x18},{0x0F,0x18},{0x11,0x18}
};
const unsigned char code_data[10][2]=
{
{0x1F,0x08},{0x06,0x00},{0x1B,0x10},{0x0f,0x10},{0x06,0x18},
{0x0D,0x18},{0x1D,0x18},{0x07,0x00},{0x1F,0x18},{0x0F,0x18}
};
void _delay_us(unsigned int i)
{
while(i--);
}
void delay_ms(u8 i)//单位1ms
{
u8 y;
for(y=0;y<i;y++)
{
_delay_us(1000);
}
}
void delay_100ms(u8 i)//单位500ms
{
u8 y;
for(y=0;y<i;y++)
{
delay_ms(100);
}
}
void send_tm1618_byte(unsigned char c_data)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(c_data&0x01)
{
DIO_ON();
}
else
{
DIO_OFF();
}
_delay_us(20);
CLK_OFF();
_delay_us(20);
CLK_ON();
_delay_us(20);
c_data=c_data>>1;
}
}
void send_tm1618_command(unsigned char c_com)
{
STB_ON();
_delay_us(20);
STB_OFF();
_delay_us(20);
send_tm1618_byte(c_com);
}
void DALI_I2C_Init(void)//重新上电时,在EEPROM取出键值
{
u8 dtr;
GPIOD->DDR |= 0x1C; /* Output. 输出GPIOD 234 */
GPIOD->CR1 |= 0x1C; /* PushPull. */
GPIOD->CR2 = 0x00; /* Output speed up to 2MHz. */
send_tm1618_command(0x00);
send_tm1618_command(0x40);
send_tm1618_command(0xC0);
send_tm1618_command(0x88);
equipment_config = FLASH_ReadByte(0x43F5);//读出标志位
if(equipment_config== 0)//按键设定地址状态
{
Ulock_flag_2s = 1;//上电显示解锁状态
dtr = FLASH_ReadByte(0x43F0);
addr_1 = (dtr&0xF0)>>4;
addr_2 = dtr&0x0F;
Store_Key_As_Addr();
// display(0xC0,1,1);
send_tm1618_byte(0x00);
send_tm1618_byte(0x00);
display(0xC2,addr_1,2);
display(0xC4,addr_2,3);
//tm1650_set(0x48,Nexie_Tube_Set_Level);//显示屏开,一级亮度
//tm1650_set(0x68,(c_code_7_1[addr_1]));
//tm1650_set(0x6A,(c_code_7_2[addr_2]));
}
if(equipment_config == 1)//上位机自动分配地址状态
{
Ulock_flag_2s = 0;
Pc_Alloct_flag = 1;
dtr = FLASH_ReadByte(0x43F1);
addr_1 = (dtr&0xF0)>>4;
addr_2 = dtr&0x0F;
Store_Key_As_Addr();
display(0xC0,0,1);
display(0xC2,addr_1,2);
display(0xC4,addr_2,3);
//tm1650_set(0x48,Nexie_Tube_Set_Level);//显示屏开,一级亮度
//tm1650_set(0x68,(key_code_8_1[addr_1]));
//tm1650_set(0x6A,(key_code_8_2[addr_2]));
}
//Save_Addr_To_EEPROM(0x18);
//addr = FLASH_ReadByte(0x43F0);
//addr_1 = (addr&0xF0)>>4;
// addr_2 = addr&0x0F;
//if(addr_1!=0||addr_1!=1||addr_1!=2||addr_1!=3||addr_1!=4||addr_1!=5||addr_1!=6||addr_1!=0x0F)
//{
// addr_1 = 0;
// }
// tm1650_set(0x68,c_code_8_1[addr_1]);
//tm1650_set(0x6A,c_code_8_2[addr_2]);
}
/******************************************************************************
功能说明: 处理按键
输入参数: 无
输出参数: 无
返回值: 无
******************************************************************************/
void display(uchar tube_addr,uchar location,uchar x)//输入显示地址和要输出的数字
{
send_tm1618_command(tube_addr);//显示地址 +2
send_tm1618_command(tube_light_level);//数码管亮度等级
if(x==1)
{
send_tm1618_byte(tube_1[location][0]);
send_tm1618_byte(tube_1[location][1]);
}
if(x==2)
{
send_tm1618_byte(tube_2[location][0]);
send_tm1618_byte(tube_2[location][1]);
}
if(x==3)
{
send_tm1618_byte(tube_3[location][0]);
send_tm1618_byte(tube_3[location][1]);
}
}
void press_key_handle(unsigned char c_key)
{
if(c_key==KEY_UNABLE)
{
return;
}
switch(c_key)
{
case 2:
addr_1+=1;
if(addr_1 == 8)
{
addr_1 =0;
}
display(0xC2,addr_1,2);
// tm1650_set(0x68,(c_code_7_1[addr_1]));//DIG1
评论0