STM32 TM1650数码管显示与按键识别

STM 32TM1650 控制程序(可移植)

这代码弄了好几天才弄好,现在给大家分享一下

#include "tm1650.h"

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);
}
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);	
}


void TM1650_delay_us(unsigned short j)
{
  unsigned short i,k;
  for(k=0;k<j;k++)
  {
    for(i=0;i<1;i++)
    {
    }
  }
}


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_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;
	key=TM1650_Read_KEY();
	uint32_t key_name,i,j;
	for(i=0;i<7;i++)
		for(j=0;j<7;j++)
		{
			if(key == key_numberH[i][j])
			{
				key_name = key_number[i][j];
				return key_name;
			}
		}
	
	return key_name;
}

头文件

#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

static uint8_t s_7number[16] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//static uint8_t s_8number[10] = {0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF};
static 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};
static 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,};
#define TM1650_GPIO_PORT GPIOB

#define TM1650_SCL_pin GPIO_Pin_3
#define TM1650_SDA_pin GPIO_Pin_4
#define TM1650_SCL_H    GPIO_SetBits(TM1650_GPIO_PORT,TM1650_SCL_pin)    
#define TM1650_SCL_L    GPIO_ResetBits(TM1650_GPIO_PORT,TM1650_SCL_pin)     
#define TM1650_SDA_H    GPIO_SetBits(TM1650_GPIO_PORT,TM1650_SDA_pin)
#define TM1650_SDA_L    GPIO_ResetBits(TM1650_GPIO_PORT,TM1650_SDA_pin)
#define READ_SDA GPIO_ReadInputDataBit(GPIOB,TM1650_SDA_pin)




void DisplayNumber_4BitDig(unsigned short num);

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 "stm32f10x.h"
#include "led.h"
#include "tm1650.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"


int main(void)
{
	LEN_GPIO_Config();
	TM1650_Init();//³õʼ»¯1650оƬ
	DisplayNumber_4BitDig(0);
	delay_init();
	uint32_t Key_name;
	
	while(1)
		{
			Key_name=TM1650_Gte_KEY();
			if(Key_name == 11) break;
		}
  while (1)
  {
		LED_R(ON);
		DisplayNumber_4BitDig(1254);
		delay_ms(300);
		LED_R(OFF);
				DisplayNumber_4BitDig(1121);
		delay_ms(300);

  }
}

我没有写注释,因为我的MDK中文是一堆问号,所以我就没加。

里面主要是库函数,也有寄存器。我这代码是达到了我的预期
1.输入想输出数字(四位内,多了就取后四位)
2.八种辉度已宏定义
3.4*7个按键可以全部识别(会返回按键“坐标”位置)
4.引脚已宏定义,方便移植
5.有部分还没弄懂,但是程序不影响使用

你可能感兴趣的:(单片机,stm32,单片机)