STM32 TM1628程序

以下是.C

#include "tm1628.h"


/*************************************
函数名称:Tm1628_Write_Bit
函数说明:写单个字节
函数参数:命令参数
函数返回:NULL
*************************************/
void Tm1628_Write_Bit(uint8_t data)
{
uint8_t i;
Tm1628_CKL_H;
Tm1628_DIO_H;
for(i = 0; i < 8; i++)
{

Tm1628_CKL_L;
delay_us(100);
if((data & 0x01) == 1)
Tm1628_DIO_H;
else
Tm1628_DIO_L;
data = data >> 1;
Tm1628_CKL_H;
delay_us(100);
}
}


/*************************************
函数名称:Tm1628_Write_Command
函数说明:写命令
函数参数:命令参数
函数返回:NULL
*************************************/
void Tm1628_Write_Command(u8 unm)
{
Tm1628_STB_L;
delay_ms(1);
Tm1628_Write_Bit(unm);
Tm1628_STB_H;
delay_ms(1);
}




/*************************************
函数名称:Tm1628init
函数说明:TM1628初始化
函数参数:NULL
函数返回:NULL
*************************************/
void Tm1628init(void)
{
GPIO_InitTypeDef  GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);     //使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;    //LB6-->PB8 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //IO口速度为10MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);     //初始化GPIOB6 - 8
Tm1628_Write_Command(0x03);   //7位10段
Tm1628_ClearDisplay(); //清屏
Tm1628_GrayScale(2); //亮度2
Tm1628_Fixed(0xc2,0xff);
}






/*************************************
函数名称:Tm1628_Fixed
函数说明:固定写显示
函数参数:(1)data 地址
(2)add 数据
函数返回:NULL
*************************************/
void Tm1628_Fixed(uint8_t data, uint8_t add)
{
Tm1628_Write_Command(0x44);
Tm1628_STB_L;
delay_ms(1);
Tm1628_Write_Bit(data);
Tm1628_Write_Bit(add);
Tm1628_STB_H;
delay_ms(1);
}




/*************************************
函数名称:Tm1628_Continuous
函数说明:连续写显示
函数参数:(1)data 地址  从C0开始
(2)num 数据
(3)add  数量 最大14
函数返回:NULL
*************************************/


void Tm1628_Continuous(uint8_t data, uint8_t num, uint8_t *add)
{
uint8_t i;
Tm1628_Write_Command(0x40);
Tm1628_STB_L;
delay_ms(1);
Tm1628_Write_Bit(data);
for(i = 0; i < num; i++)
{
Tm1628_Write_Bit(add[i]);
}
Tm1628_STB_H;
delay_ms(1);
}
/*************************************
函数名称:Tm1628_ClearDisplay
函数说明:清屏
函数参数:NULL
函数返回:NULL
*************************************/


void Tm1628_ClearDisplay(void)
{
uint8_t i;
Tm1628_Write_Command(0x40);
Tm1628_STB_L;
delay_ms(1);
Tm1628_Write_Bit(0xC0);
for(i = 0; i < 14; i++)
{
Tm1628_Write_Bit(0x00);
}
Tm1628_STB_H;
delay_ms(1);
}








/*************************************
函数名称:Tm1628_GrayScale
函数说明:用于亮度调节 0 - 9
函数参数:亮度 0 - 9
函数返回:NULL
*************************************/
void Tm1628_GrayScale(uint8_t data)
{
switch(data)
{
case(0): Tm1628_Write_Command(GrayScale_ON);   break;
case(1): Tm1628_Write_Command(GrayScale1); break;
case(2): Tm1628_Write_Command(GrayScale2); break;
case(3): Tm1628_Write_Command(GrayScale3); break;
case(4): Tm1628_Write_Command(GrayScale4); break;
case(5): Tm1628_Write_Command(GrayScale5); break;
case(6): Tm1628_Write_Command(GrayScale6); break;
case(7): Tm1628_Write_Command(GrayScale7); break;
case(8): Tm1628_Write_Command(GrayScale8); break;
}

}

以下是.h


#ifndef __TM1628_H
#define __TM1628_H


#include "sys.h"
#include "delay.h"


#define Tm1628_CKL_H GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_SET)
#define Tm1628_CKL_L GPIO_WriteBit(GPIOB,GPIO_Pin_6,Bit_RESET)


#define Tm1628_DIO_H GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_SET)
#define Tm1628_DIO_L GPIO_WriteBit(GPIOB,GPIO_Pin_7,Bit_RESET)


#define Tm1628_STB_H GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET)
#define Tm1628_STB_L GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET)




#define GrayScale_OFF 0x80 //关显示
#define GrayScale_ON 0x81 //开显示




#define GrayScale1   0x88 //灰度等级1
#define GrayScale2   0x89 //灰度等级2
#define GrayScale3   0x8A //灰度等级3
#define GrayScale4   0x8B //灰度等级4
#define GrayScale5   0x8C //灰度等级5
#define GrayScale6   0x8D //灰度等级6
#define GrayScale7   0x8E //灰度等级7
#define GrayScale8   0x8F //灰度等级8


void Tm1628init(void);   //TM1628初始化
void Tm1628_Fixed(uint8_t data, uint8_t add); //固定写显示 data 地址 add 数据
void Tm1628_Continuous(uint8_t data, uint8_t num, uint8_t *add); //连续写显示
void Tm1628_ClearDisplay(void); //清屏
void Tm1628_GrayScale(uint8_t data); //亮度调节

#endif


楼主是新萌 之前想直接找一篇 代码拔下来 后来发现没有 就自己写了写 函数实测能用


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值