第一次使用这个器件,没详细看芯片资料,掉进了一个坑,以为是使用I2C驱动,结果使用F103的库函数去驱动,结果消息是发出去了,但是数码管没显示。还用逻辑分析仪抓了数据,发现I2C上把消息都发出去了,就是没响应。对比时序图发现和I2C的定义有区别。
类似于I2C,不等于是I2C.
网上找了个参考,自己稍微修改了下调通了,发出来共享给需要的人。参考文章如下:
https://blog.csdn.net/weixin_561 ... 1001.2014.3001.5502
使用起来很简单
代码很简单直接上源码
#ifndef __TM1650_H__
#define __TM1650_H__
#include "stm32f10x.h"
#define brighting_8 0x00
#define brighting_7 0x07
#define brighting_6 0x06
#define brighting_5 0x05
#define brighting_4 0x04
#define brighting_3 0x03
#define brighting_2 0x02
#define brighting_1 0x01
//uint8_t display[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //共阴极字段码
#define TM1650_GPIO_PORT GPIOB
#define TM1650_SCL_pin GPIO_Pin_6
#define TM1650_SDA_pin GPIO_Pin_7
#define TM1650_SCL_H TM1650_GPIO_PORT->BSRR=TM1650_SCL_pin //置 1
#define TM1650_SCL_L TM1650_GPIO_PORT->BRR=TM1650_SCL_pin //置 0
#define TM1650_SDA_H TM1650_GPIO_PORT->BSRR=TM1650_SDA_pin
#define TM1650_SDA_L TM1650_GPIO_PORT->BRR=TM1650_SDA_pin
#define READ_SDA GPIO_ReadInputDataBit(TM1650_GPIO_PORT,TM1650_SDA_pin) //读电平
void TM1650_SendDigData(uint16_t index,uint16_t num);
void DisplayNumber_4BitDig(unsigned short num);
void TM1650_SendCommand(unsigned char add,unsigned char dat);
unsigned char TM1650_Read_KEY(void);
void TM1650_Init(void);
void DisplayNumber_HexDig(unsigned short num);
unsigned char TM1650_Read_KEY(void);
uint32_t TM1650_Gte_KEY(void);
#endif
#include "tm1650.h"
uint8_t s_7number[16] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
// uint8_t s_8number[10] = {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};
uint32_t key_number[7][4] = {11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,51,52,53,54,61,62,63,64,71,72,73,74};
uint8_t key_numberH[7][4] =
{0x44,0x45,0x46,0x47,\
0x4C,0x4D,0x4E,0x4F,\
0x54,0x55,0x56,0x57,\
0x5C,0x5D,0x5E,0x5F,\
0x64,0x65,0x66,0x67,\
0x6C,0x6D,0x6E,0x6F,\
0x74,0x75,0x76,0x77,};
void SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructa;
GPIO_InitStructa.GPIO_Pin = TM1650_SDA_pin;
GPIO_InitStructa.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB,&GPIO_InitStructa);
// GPIO_Init(GPIOM, TM1650_SDA_pin, 0, 0, 0, 1);
}
void SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructA;
GPIO_InitStructA.GPIO_Pin = TM1650_SDA_pin;
GPIO_InitStructA.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructA.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructA);
// GPIO_Init(GPIOM, TM1650_SDA_pin, 1, 0, 0, 1);
}
void TM1650_delay_us(unsigned short i)
{
for(;i>0;i--)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
}
void TM1650_Start(void)
{
SDA_OUT();
TM1650_SDA_H;
TM1650_SCL_H;
TM1650_delay_us(4);
TM1650_SDA_L;
TM1650_delay_us(4);
TM1650_SCL_L;
}
void TM1650_Stop(void)
{
SDA_OUT();
TM1650_SCL_L;
TM1650_SDA_L;
TM1650_delay_us(4);
TM1650_SCL_H;
TM1650_delay_us(4);
TM1650_SDA_H;
}
unsigned char TM1650_Wait_Ack(void)
{
unsigned char ucErrTime=0;
SDA_IN();
TM1650_SDA_H;TM1650_delay_us(1);
TM1650_SCL_H;TM1650_delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
TM1650_Stop();
return 1;
}
}
TM1650_SCL_L;
return 0;
}
void TM1650_Ack(void)
{
TM1650_SCL_L;
SDA_OUT();
TM1650_SDA_L;
TM1650_delay_us(4);
TM1650_SCL_H;
TM1650_delay_us(4);
TM1650_SCL_L;
}
void TM1650_NAck(void)
{
TM1650_SCL_L;
SDA_OUT();
TM1650_SDA_H;
TM1650_delay_us(4);
TM1650_SCL_H;
TM1650_delay_us(4);
TM1650_SCL_L;
}
void TM1650_Send_Byte(unsigned char oneByte)
{
unsigned char t;
SDA_OUT();
TM1650_SCL_L;
for(t=0;t<8;t++)
{
if((oneByte&0x80)==0x80) TM1650_SDA_H;
else TM1650_SDA_L;
oneByte<<=1;
TM1650_delay_us(4);
TM1650_SCL_H;
TM1650_delay_us(4);
TM1650_SCL_L;
TM1650_delay_us(4);
}
}
unsigned char TM1650_Read_Byte(void)
{
unsigned char i,rekey=0;
SDA_IN();
for(i=0;i<8;i++ )
{
TM1650_SCL_L;
TM1650_delay_us(4);
TM1650_SCL_H;
rekey<<=1;
if(READ_SDA) rekey++;
TM1650_delay_us(4);
}
return rekey;
}
void TM1650_SendCommand(unsigned char add,unsigned char dat)
{
TM1650_Start();
TM1650_Send_Byte(add);
TM1650_Wait_Ack();
TM1650_Send_Byte(dat);
TM1650_Wait_Ack();
TM1650_Stop();
}
void TM1650_SendDigData(uint16_t index,uint16_t num)
{
uint8_t indexAddr = 0;
uint8_t numValue = 0;
switch(index)
{
case 1:indexAddr = 0x68;break;
case 2:indexAddr = 0x6A;break;
case 3:indexAddr = 0x6C;break;
case 4:indexAddr = 0x6E;break;
default:break;
}
numValue = s_7number[num];
TM1650_Start();
TM1650_Send_Byte(indexAddr);
TM1650_Wait_Ack();
TM1650_Send_Byte(numValue);
TM1650_Wait_Ack();
TM1650_Stop();
}
void TM1650_SetDisplay(uint8_t brightness)//
{
TM1650_SendCommand(0x48,brightness*16 + 1*4 + 1);
}
void TM1650_Init(void)
{
// GPIO_Init(GPIOD, TM1650_SCL_pin, 1, 0, 0, 1);
// GPIO_Init(GPIOM, TM1650_SDA_pin, 1, 0, 0, 1);
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStruct.GPIO_Pin = TM1650_SCL_pin|TM1650_SDA_pin;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TM1650_GPIO_PORT,&GPIO_InitStruct);
//AFIO->MAPR|=0x02000000;//PB3,PB4???JATG?,??JATG
TM1650_SCL_H;
TM1650_SDA_H;
TM1650_SetDisplay(brighting_2);
}
void DisplayNumber_4BitDig(uint16_t Num)
{ uint16_t Numb;
Numb = Num+10000;
TM1650_SendDigData(1,Numb/1000%10);
TM1650_SendDigData(2,Num/100%10);
TM1650_SendDigData(3,Num/10%10);
TM1650_SendDigData(4,Num%10);
}
unsigned char TM1650_Read_KEY(void)
{
unsigned char temp;
TM1650_Start();
TM1650_Send_Byte(0x49);
TM1650_Wait_Ack();
temp=TM1650_Read_Byte();
TM1650_Wait_Ack();
TM1650_Stop();
return temp;
}
uint32_t TM1650_Gte_KEY(void)
{ unsigned char key;
uint32_t key_name,i,j;
key=TM1650_Read_KEY();
for(i=0;i<7;i++)
for(j=0;j<7;j++)
{
if(key == key_numberH[j])
{
key_name = key_number[j];
return key_name;
}
}
return key_name;
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/hyhsandy1803/article/details/136464857
|
|