R5F104LEAFB GPIO尝试(驱动TM1637)

/ 0评 / 0

TM1637的驱动方式类似SPI,又类似I2C,反正不是标准协议.最好还是IO模拟不用烧脑.只需要两个IO就能完事.由于CPU本身不快,延迟都省了,我现在CPU速度4MHz,节约功耗下,完全不用延迟,如果是32MHz需要一些延迟,16MHz测试也不需要.

/*
 * TM1637.c
 *
 *  Created on: 2020年11月2日
 *      Author: TaterLi
 */

#include "../r_cg_macrodriver.h"

const char segmentMap[] = {
    0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, // 0-7
    0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, // 8-9, A-F
    0x00
};

static void TM1637_Start(void)
{
    P1_bit.no0 = 1;
    P1_bit.no1 = 1;
    P1_bit.no1 = 0;
}

static void TM1637_Stop(void)
{
    P1_bit.no0 = 0;
    P1_bit.no1 = 0;
    P1_bit.no0 = 1;
    P1_bit.no1 = 1;
}

static void TM1637_ReadDummy(void)
{
    P1_bit.no0 = 0;
    P1_bit.no0 = 1;
    P1_bit.no0 = 0;
}

static void TM1637_WriteByte(unsigned char b)
{
	uint8_t i = 0;
    for (i = 0; i < 8; ++i) {
        P1_bit.no0 = 0;
        if (b & 0x01) {
            P1_bit.no1 = 1;
        }
        else {
            P1_bit.no1 = 0;
        }
        b >>= 1;
        P1_bit.no0 = 1;
    }
}

void TM1637_SetBrightness(char brightness)
{
    TM1637_Start();
    TM1637_WriteByte(0x87 + brightness);
    TM1637_ReadDummy();
    TM1637_Stop();
}

void TM1637_Display(uint16_t v, uint8_t dot)
{
	uint8_t i = 0;
    unsigned char digitArr[4];
    for (i = 0; i < 4; ++i) {
        digitArr[i] = segmentMap[v % 10];
        if (i == 2 && dot) {
            digitArr[i] |= 1 << 7;
        }
        v /= 10;
    }

    TM1637_Start();
    TM1637_WriteByte(0x40);
    TM1637_ReadDummy();
    TM1637_Stop();

    TM1637_Start();
    TM1637_WriteByte(0xc0);
    TM1637_ReadDummy();

    for (i = 0; i < 4; ++i) {
        TM1637_WriteByte(digitArr[3 - i]);
        TM1637_ReadDummy();
    }

    TM1637_Stop();
}

然后在主程序显示就可以了.

TM1637_SetBrightness(3);
TM1637_Display(0,0);
R_RTC_Start();

好吧,我真的把他当51用而已.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注