#define DIO_PORT GPIOC
#define DIO_PIN GPIO_Pins_9
#define CLK_PORT GPIOC
#define CLK_PIN GPIO_Pins_8
#define STB_PORT GPIOC
#define STB_PIN GPIO_Pins_7
#define DIO_L GPIO_ResetBits(DIO_PORT,DIO_PIN)
#define DIO_H GPIO_SetBits(DIO_PORT,DIO_PIN)
#define CLK_L GPIO_ResetBits(CLK_PORT,CLK_PIN)
#define CLK_H GPIO_SetBits(CLK_PORT,CLK_PIN)
#define STB_L GPIO_ResetBits(STB_PORT,STB_PIN)
#define STB_H GPIO_SetBits(STB_PORT,STB_PIN)
#define DATA_READ GPIO_ReadInputDataBit(DIO_PORT,DIO_PIN)
//显示模式设置命令
#define V_MDSP1 0x00 //4位13段
#define V_MDSP2 0x01 //5位12段
#define V_MDSP3 0x02 //6位11段
#define V_MDSP4 0x03 //7位10段
//数据命令设置
#define V_MDAT1 0x40 //写数据到显示区
#define V_MDAT2 0x42 //读键扫数据
#define V_MDAT3 0x40 //自动地址增加
#define V_MDAT4 0x44 //固定地址
//地址命令设置
#define V_ADDR0 0xC0 //地址0
#define V_ADDR1 0xC1 //地址1
#define V_ADDR2 0xC2 //地址2
#define V_ADDR3 0xC3 //地址3
#define V_ADDR4 0xC4 //地址4
#define V_ADDR5 0xC5 //地址5
#define V_ADDR6 0xC6 //地址6
#define V_ADDR7 0xC7 //地址7
#define V_ADDR8 0xC8 //地址8
#define V_ADDR9 0xC9 //地址9
#define V_ADDR10 0xCA //地址10
#define V_ADDR11 0xCB //地址11
#define V_ADDR12 0xCC //地址12
#define V_ADDR13 0xCD //地址13
//显示控制 - 亮度调节
#define V_DIS16_01 0x80 //显示宽度1/16
#define V_DIS16_02 0x81 //显示宽度2/16
#define V_DIS16_03 0x82 //显示宽度3/16
#define V_DIS16_10 0x83 //显示宽度10/16
#define V_DIS16_11 0x84 //显示宽度11/16
#define V_DIS16_12 0x85 //显示宽度12/16
#define V_DIS16_13 0x86 //显示宽度13/16
#define V_DIS16_14 0x87 //显示宽度14/16
#define V_DIS16_OFF 0x80 //显示关
#define V_DIS16_ON 0x88 //显示开
//---------------------------------------------
#define V_LED_LIGHT (V_DIS16_10|V_DIS16_ON) //显示亮度设置
//----------------------------
#define V_NOP 3//5
#define DATA_NB 14
//-----User_Code_define End-----//
//+++++User_Code_funcs Begin+++++//
void gpio_init(void)
{
GPIO_InitType GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_OD;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = DIO_PIN;
GPIO_Init(DIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = STB_PIN;
GPIO_Init(STB_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Pins = CLK_PIN;
GPIO_Init(CLK_PORT, &GPIO_InitStructure);
}
void me_delay(unsigned char x)
{
unsigned char i;
for(;x>0;x--) for(i=110;i>0;i--);
}
//**************************************
// 函数名称:write_byte_data
// 函数功能:TM1668发送一字节数据
// 入口参数:要发送的数据
// 出口参数:
//***************************************
void write_byte_data(unsigned char data)
{
unsigned char i;
me_delay(V_NOP) ;
for(i=8;i>0;i--)
{
CLK_L ;
if((data & 0x01) == 0)
{
DIO_L ;
}
else
{
DIO_H ;
}
data >>= 1 ;
me_delay(V_NOP) ;
CLK_H ;
me_delay(V_NOP) ;
}
}
//**************************************
// 函数名称:read_byte_data
// 函数功能:读TM1668一字节数据
// 入口参数:无
// 出口参数:
// 返回值 : 所读的数据
//***************************************
unsigned char read_byte_data(void)
{
unsigned char i;
unsigned char ret_value = 0 ;
CLK_L ;
for(i=0;i<8;i++)
{
me_delay(V_NOP) ;
CLK_H ;
ret_value >>= 1 ; //先读出的是低位
if(DATA_READ)
{
ret_value |= 0x80 ;
}
CLK_L ;
}
return (ret_value);
}
//**************************************
// 函数名称:write_command
// 函数功能:写设置命令
// 入口参数:设置命令参数
// 出口参数:无
//***************************************
void write_command(unsigned char command)
{
STB_L ;
me_delay(V_NOP) ;
write_byte_data(command);
STB_H ;
}
//**************************************
// 函数名称:display_off
// 函数功能:关闭显示
// 入口参数:无
// 出口参数:无
//***************************************
void display_off(void)
{
STB_L ;
me_delay(V_NOP) ;
write_byte_data(V_DIS16_OFF);
STB_H ;
}
//**************************************
// 函数名称:write_addr_data
// 函数功能:向固定地址写一个数据
// 入口参数:地址 数据
// 出口参数:无
//***************************************
void write_addr_data(unsigned char addr,unsigned char data)
{
STB_L ;
write_byte_data(addr); //写地址
write_byte_data(data); //写数据
STB_H ;
}
//**************************************
// 函数名称:write_led_dat
// 函数功能:
// 入口参数:
// 出口参数:无
//***************************************
void write_led_dat_increase(unsigned char *p, unsigned char n_bytes, unsigned char brightness)
{
unsigned char i = 0;
write_command(V_MDSP4); // 设置显示模式:7位10段
write_command(V_MDAT3) ; //设置数据命令:地址自增
STB_L;
me_delay(V_NOP) ;
write_byte_data(V_ADDR0); //设置显示地址:起始地址
for (i = 0; i < n_bytes; i++)
{
write_byte_data(p[i]); //写数据
}
STB_H;
write_command(V_LED_LIGHT) ; //设置亮度 开启显示write_command(V_LED_LIGHT) ; //设置亮度 开启显示
}
//**************************************
// 函数名称:write_led_dat
// 函数功能:
// 入口参数:
// 出口参数:无
//***************************************
void write_led_dat_fixed(unsigned char *p, unsigned char n_bytes, unsigned char brightness)
{
unsigned char i = 0;
write_command(V_MDSP4); // 设置显示模式:7位10段
write_command(V_MDAT4) ; // 设置数据命令:地址固定
for (i = 0; i < n_bytes; i++)
{
STB_L;
write_addr_data(V_ADDR0+i,*(p+i));//写地址数据
STB_H;
}
write_command(V_LED_LIGHT) ; //设置亮度 开启显示write_command(V_LED_LIGHT) ; //设置亮度 开启显示
}
//**************************************
// 函数名称:write_led_dat
// 函数功能:
// 入口参数:
// 出口参数:无
//***************************************
void write_led_dat(unsigned char *in_dat)
{
unsigned char i ;
unsigned char addr,tmp2 ;
write_command(V_MDSP4) ;//7位10段
write_command(V_MDAT1) ; //写数据到1668 固定模式
//-----
addr = V_ADDR2 ;
for(i=0;i<5;i++) //刷显数据
{
if((*in_dat) & 0x01)
{
tmp2 = 0x01 ;
}
else
{
tmp2 = 0x00 ;
}
write_addr_data(addr,tmp2) ;
addr += 2 ; //地址加2
(*in_dat) >>= 1 ;
}
//-----
write_command(V_LED_LIGHT) ; //设置亮度 开启显示
}
//**************************************
// 函数名称:read_key
// 函数功能:TM1668 读按键值
// 入口参数:无
// 出口参数:
// 返回值 : 返回按键值
//***************************************
void read_key(unsigned char *out_dat)
{
unsigned char i ;
STB_L ;
me_delay(V_NOP) ;
write_byte_data(V_MDAT2) ; //读按键值
me_delay(V_NOP) ;
me_delay(V_NOP) ;
DIO_H ; //读前数据置高
// *out_dat = read_byte_data() ;// 只读一个字节
for(i=5;i>0;i--) //读所有按键的值
{
*out_dat = read_byte_data() ;
out_dat ++ ;
}
STB_H ;
}
//**************************************
// 函数名称:tm1668_init
// 函数功能:TM1668 初始化
// 入口参数:无
// 出口参数:无
// 返回值 : 无
//***************************************
void tm1668_init(void)
{
unsigned char nop_buff[14]={0};
gpio_init();
write_led_dat_increase(nop_buff,14,V_DIS16_OFF);
}
TM1668驱动
最新推荐文章于 2024-06-14 16:51:14 发布