2023-03-23 17:00:05 +08:00

192 lines
5.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "tm1650.h"
#include "delay.h"
#include "timer.h"
// 往一个指定的数码管位写入指定的显示数据
// 共阴数码管段码表:
// const unsigned char TUBE_TABLE_0[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴0~9的数字
const unsigned char TUBE_TABLE_0[16] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71}; // 共阴0~9~A~F
const unsigned char TUBE_TABLE_1[16]={0x01,0x21,0x30,0x18,0x08,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x08,0x0c,0x06,0x03};
// 产生IIC总线起始信号
static void TM1650_IIC_start(void)
{
scl = 1; // SCL=1
sda = 1; // SDA=1
Delay4us();
sda = 0; // SDA=0
Delay4us();
scl = 0; // SCL=0
}
// 产生IIC总线结束信号
static void TM1650_IIC_stop(void)
{
scl = 0; // SCL=0
sda = 0; // SDA=0
Delay4us();
scl = 1; // SCL=1
Delay4us();
sda = 1; // SDA=1
}
// 通过IIC总线发送一个字节
static void TM1650_IIC_write_byte(unsigned char dat)
{
unsigned char i;
scl = 0;
for (i = 0; i < 8; i++)
{
do
{
if (dat & 0x80)
sda = 1; // SDA=1
else
sda = 0;
} while (0);
dat <<= 1;
Delay2us();
scl = 1;
Delay2us();
scl = 0;
Delay2us();
}
}
// 通过IIC总线接收从机响应的ACK信号
static unsigned char TM1650_IIC_wait_ack(void)
{
unsigned char ack_signal = 0;
sda = 1; // SDA=1
Delay2us();
ack_signal = sda;
scl = 1;
Delay2us();
// if(sda == 0)
// ack_signal = 1; //如果读取到的是NACK信号
scl = 0;
Delay2us();
return ack_signal;
}
// 作用:设置显示参数
// 备注:这个操作不影响显存中的数据
// 用例:
// 设置亮度并打开显示:TM1650_cfg_display(TM1650_BRIGHTx)
// 关闭显示:TM1650_cfg_display(TM1650_DSP_OFF)
static void TM1650_cfg_display(unsigned char x)
{
TM1650_IIC_start();
TM1650_IIC_write_byte(0x48);
TM1650_IIC_wait_ack(); // 固定命令
TM1650_IIC_write_byte(x);
TM1650_IIC_wait_ack(); // 参数值
TM1650_IIC_stop();
}
// 在DIG1位上显示数字3: TM1650_print(TM1650_DIG1,TUBE_TABLE_0[3]);
void TM1650_print(unsigned char dig, unsigned char seg_data)
{
TM1650_IIC_start();
TM1650_IIC_write_byte(dig * 2 + 0x68);
TM1650_IIC_wait_ack(); // 显存起始地址为0x68
TM1650_IIC_write_byte(seg_data);
TM1650_IIC_wait_ack(); // 发送段码
TM1650_IIC_stop();
}
// 将显存数据全部刷为0清空显示
static void TM1650_clear(void)
{
unsigned char a;
for (a = TM1650_DIG1; a <= TM1650_DIG4; a++)
{
TM1650_print(a, 0); // 将显存数据刷为0
}
}
// TM1650初始化
void TM1650_init(void)
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_clear(); // 将显存内容清0
}
void TM1650_printpogrun(u8 set,u8 mode) // 显示run
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_print(TM1650_DIG1, TUBE_TABLE_1[mode]);
Delay4us();
TM1650_print(TM1650_DIG2, 0x73);
Delay4us();
TM1650_print(TM1650_DIG3, TUBE_TABLE_0[set]);
Delay4us();
TM1650_print(TM1650_DIG4, TUBE_TABLE_1[mode+8]);
}
void TM1650_printpogstop(u8 set) // 显示run
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_print(TM1650_DIG1, 0x40);
Delay4us();
TM1650_print(TM1650_DIG2, 0x73);
Delay4us();
TM1650_print(TM1650_DIG3, TUBE_TABLE_0[set]);
Delay4us();
TM1650_print(TM1650_DIG4, 0x40);
}
void TM1650_print_SMG1(u8 set) // 显示----页面0显示
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_print(TM1650_DIG1, 0x40);
Delay4us();
if (set > 99)
TM1650_print(TM1650_DIG2, TUBE_TABLE_0[set / 100 % 10]);
else
TM1650_print(TM1650_DIG2, 0x00);
Delay4us();
if (set > 9)
TM1650_print(TM1650_DIG3, TUBE_TABLE_0[set / 10 % 10]);
else
TM1650_print(TM1650_DIG3, 0x00);
Delay4us();
TM1650_print(TM1650_DIG4, TUBE_TABLE_0[set % 10]);
}
void TM1650_print_SMG2(u16 set) // 显示----页面0显示
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_print(TM1650_DIG1, 0x48);
Delay4us();
if (set > 99)
TM1650_print(TM1650_DIG2, TUBE_TABLE_0[set / 100 % 10]);
else
TM1650_print(TM1650_DIG2, 0x00);
Delay4us();
if (set > 9)
TM1650_print(TM1650_DIG3, TUBE_TABLE_0[set / 10 % 10]);
else
TM1650_print(TM1650_DIG3, 0x00);
Delay4us();
TM1650_print(TM1650_DIG4, TUBE_TABLE_0[set % 10]);
}
void TM1650_print_SMG3(u8 set) // 显示----页面0显示
{
sda = 1; // 释放SDA线
scl = 1; // 释放SCL线
TM1650_cfg_display(TM1650_BRIGHT5); // 初始化为5级亮度打开显示
TM1650_print(TM1650_DIG1, 0x49);
Delay4us();
if (set > 99)
TM1650_print(TM1650_DIG2, TUBE_TABLE_0[set / 100 % 10]);
else
TM1650_print(TM1650_DIG2, 0x00);
Delay4us();
if (set > 9)
TM1650_print(TM1650_DIG3, TUBE_TABLE_0[set / 10 % 10]);
else
TM1650_print(TM1650_DIG3, 0x00);
Delay4us();
TM1650_print(TM1650_DIG4, TUBE_TABLE_0[set % 10]);
}