TM1650中文手册:https://download.csdn.net/download/guaizaiguaizai/12094929
//tm1650.c
#include "tm1650.h"
#include "delay.h"
#include "led.h"
void TM1650_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/*
数据输入开始条件是CLK高时候,DIO由高变成低
*/
void I2CStart(void) //开始信号
{
TM1650_CLK_H;
TM1650_DIO_H;
delay_us(5);
TM1650_DIO_L;
}
/*
TM1650_READ_DIO 是读取SDA信号
*/
void I2CAsk(void) //ACK信号
{
uchar timeout = 1;
TM1650_CLK_H;
delay_us(5);
TM1650_CLK_L;
while((TM1650_READ_DIO)|(timeout<=100))
{
timeout++;
}
delay_us(5);
TM1650_CLK_L;
}
/*
数据输入结束条件是CLK高时候,DIO由低变成高
*/
void I2CStop(void) //停止信号
{
TM1650_CLK_H;
delay_us(5);
TM1650_DIO_L;
delay_us(5);
TM1650_DIO_H;
}
void I2CWrByte(uchar oneByte) //写一个字节高位在前,低位在后
{
uchar i;
TM1650_CLK_L;
delay_us(5);
for(i=0;i<8;i++)
{
if((oneByte&0x80)) TM1650_DIO_H;
else TM1650_DIO_L;
TM1650_CLK_L;
delay_us(5);
TM1650_CLK_H;
delay_us(5);
TM1650_CLK_L;
delay_us(5);
oneByte<<=1;
}
// TM1650_CLK_H;
delay_us(5);
}
void TM1650_Set(uchar add,uchar dat) //数码管显示
{
//写显存必须从高地址开始写
I2CStart();
I2CWrByte(add); //第一个显存地址
I2CAsk();
I2CWrByte(dat);
//I2CAsk();//不能加,加上出错
I2CStop();
}
//tm1650.h
#ifndef __TM1650_H
#define __TM1650_H
//#include "stm32f0xx.h"
#define uchar unsigned char
#define uint unsigned int
#define TM1650_CLK_H GPIO_SetBits(GPIOA,GPIO_Pin_5)
#define TM1650_CLK_L GPIO_ResetBits(GPIOA,GPIO_Pin_5)
#define TM1650_DIO_H GPIO_SetBits(GPIOA,GPIO_Pin_6)
#define TM1650_DIO_L GPIO_ResetBits(GPIOA,GPIO_Pin_6)
#define TM1650_READ_DIO GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)
void TM1650_Init(void);
void I2CStart(void);
void I2Cask(void);
void I2CStop(void);
void I2CWrByte(uchar oneByte);
void TM1650_Set(uchar add,uchar dat);
uchar Scan_Key(void);
#endif
//调用示例
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
int main(void)
{
delay_init();
LED_Init();
TM1650_Init();
LED_ON;
// USART1_Init(9600);
// printf("STM32F030F4P6 USART TEST\r\n");
TM1650_Set(0x48,0x51);//数据命令:0x48,5级亮度,7段显示
TM1650_Set(0x68,table[0]);
TM1650_Set(0x6a,table[1]);
TM1650_Set(0x6c,table[2]);
TM1650_Set(0x6e,table[3]);
//LED_OFF;
while (1);
}
下载到板子上面,数码管显示0123.
工程下载:https://download.csdn.net/download/guaizaiguaizai/12094949