本帖最后由 hrsoftgao 于 2017-10-24 16:54 编辑
void TM1638_Write(unsigned char DATA) //дÊý¾Ýº¯Êý
{
unsigned char i;
for(i=0;i<8;i++)
{
CLK=0;
if(DATA&0X01)
DIO=1;
else
DIO=0;
DATA>>=1;
CLK=1;
}
}
unsigned char TM1638_Read(void) //¶ÁÊý¾Ýº¯Êý
{
unsigned char i;
unsigned char temp=0;
DIO=1; //ÉèÖÃΪÊäÈë
for(i=0;i<8;i++)
{
delay_us(5) ;
temp>>=1;
CLK=0;
if(DIO)
temp|=0x80;
CLK=1;
}
return temp;
}
void Write_COM(unsigned char cmd) //·¢ËÍÃüÁî×Ö
{
STB=0;
TM1638_Write(cmd);
STB=1;
}
unsigned char Read_key(void)
{
unsigned char c[4],i,key_value=0;
STB=0;
delay_ms(100);
TM1638_Write(0x42);
delay_ms(100);
for(i=0;i<4;i++)
{
c=TM1638_Read();
}
STB=1; //4¸ö×Ö½ÚÊý¾ÝºÏ³ÉÒ»¸ö×Ö½Ú
if(c[0]==0x04) key_value=1;
if(c[0]==0x40) key_value=2;
if(c[1]==0x04) key_value=3;
if(c[1]==0x40) key_value=4;
if(c[2]==0x04) key_value=5;
if(c[2]==0x40) key_value=6;
if(c[3]==0x04) key_value=7;
if(c[3]==0x40) key_value=8;
if(c[0]==0x02) key_value=9;
if(c[0]==0x20) key_value=10;
if(c[1]==0x02) key_value=11;
if(c[1]==0x20) key_value=12;
if(c[2]==0x02) key_value=13;
if(c[2]==0x20) key_value=14;
if(c[3]==0x02) key_value=15;
if(c[3]==0x20) key_value=16;
return (key_value);
}
网上的代码,显示没问题。
while (1)
{
LedDisplay(1,3,7,0,2,1,0,2);
delay_ms(1000);
key_value=Read_key();
switch (key_value)
{
case 1:
LedDisplay(1,0,0,0,0,0,0,0);
break;
case 2 :
LedDisplay(2,0,0,0,0,0,0,0);
break;
case 3 :
LedDisplay(3,0,0,0,0,0,0,0);
break;
case 4 :
LedDisplay(4,0,0,0,0,0,0,0);
break;
case 5 :
LedDisplay(5,0,0,0,0,0,0,0);
break;
case 6 :
LedDisplay(6,0,0,0,0,0,0,0);
break;
case 7 :
LedDisplay(7,0,0,0,0,0,0,0);
break;
case 8 :
LedDisplay(8,0,0,0,0,0,0,0);
break;
case 9:
LedDisplay(9,0,0,0,0,0,0,0);
break;
case 10 :
LedDisplay(0,1,0,0,0,0,0,0);
break;
case 11 :
LedDisplay(1,1,0,0,0,0,0,0);
break;
case 12 :
LedDisplay(2,1,0,0,0,0,0,0);
break;
case 13 :
LedDisplay(3,1,0,0,0,0,0,0);
break;
case 14 :
LedDisplay(4,1,0,0,0,0,0,0);
break;
case 15:
LedDisplay(5,1,0,0,0,0,0,0);
break;
case 16 :
LedDisplay(6,1,0,0,0,0,0,0);
break;
default :
break;//LedDisplay(0,0,0,0,0,0,0,0);
}
void Gpio_Inits(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_5 ;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_10;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_11;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Mode =GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Mode =GPIO_MODE_IT_RISING; //设置为上升沿中断触发
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pin = GPIO_PIN_13;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
//串口2引脚功能配置
GPIO_InitStruct.Mode=GPIO_MODE_AF_PP;
GPIO_InitStruct.Pin=GPIO_PIN_2;
GPIO_InitStruct.Speed=GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate=GPIO_AF7_USART1;
GPIO_InitStruct.Pull=GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Alternate=GPIO_AF7_USART1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pin=GPIO_PIN_3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//串口2引脚功能配置
/* Enable and set EXTI lines 10 to 15 Interrupt to the lowest priority */
// HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0); //设置外部中断15——10优先级
// HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); //使能外部中断
}
void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
/* MSI is enabled after System reset, activate PLL with MSI as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLP = 7;
RCC_OscInitStruct.PLL.PLLQ = 4;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
/* Initialization Error */
while(1);
}
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
/* Initialization Error */
while(1);
}
}
void TM1638_Write(unsigned char DATA)
{
unsigned char i;
// GPIOSetDir(PORT0,Bit18,1); //DIOд
for(i=0;i<8;i++)
{
CLK_L;
if(DATA&0X01)
{
DIO_H;
}
else
{
DIO_L;
}
DATA>>=1;
CLK_H;
}
}
void Write_COM(unsigned char cmd)
{
STB_L;
TM1638_Write(cmd);
STB_H;
}
void Write_DATA(unsigned char add,unsigned char DATA)
{
Write_COM(0x44);
STB_L;
TM1638_Write(0xc0|add);
TM1638_Write(DATA);
STB_H;
}
void Write_allLED(unsigned char LED_flag) //???LED??,LED_flag????LED??
{
unsigned char i;
for(i=0;i<8;i++)
{
if(LED_flag&(1<<i))
//Write_DATA(2*i+1,3);
Write_DATA(2*i+1,1);
else
Write_DATA(2*i+1,0);
}
}
void init_TM1638(void)
{
unsigned char i;
Write_COM(0x8b); //? (0x88-0x8f)8?????
Write_COM(0x40); //??????1
STB_L; //
TM1638_Write(0xc0); //?????
for(i=0;i<16;i++) //?16??????
TM1638_Write(0x00);
STB_H;
}
unsigned char TM1638_Read(void) //????
{
unsigned char i;
unsigned char temp=0;
DIO_H;
for(i=0;i<8;i++)
{
temp>>=1;
CLK_L;
if(DIO_R)
{
temp|=0x80;
}
CLK_H;
}
return temp;
}
unsigned char Read_key(void)
{
unsigned char c[4],i,key_value=0;
STB_L;
TM1638_Write(0x42); //???? ??
for(i=0;i<4;i++)
c[i]=TM1638_Read();
STB_H; //4???????????
for(i=0;i<4;i++)
key_value|=c[i]<<i;
for(i=0;i<8;i++)
if((0x01<<i)==key_value)
break;
return i;
}
#define __PERIPH_H
#include "stm32l4xx_hal.h"
#include "stdio.h"
#define CLK_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_RESET);
#define CLK_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
#define STB_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
#define STB_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_RESET);
#define DIO_H HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_SET);
#define DIO_L HAL_GPIO_WritePin(GPIOC,GPIO_PIN_12,GPIO_PIN_RESET);
#define DIO_R HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)
#define DATA_COMMAND 0X40
#define DISP_COMMAND 0x80
#define ADDR_COMMAND 0XC0
void TM1638_Write(unsigned char DATA);
unsigned char TM1638_Read(void);
unsigned char Read_key(void);
void Write_COM(unsigned char cmd);
unsigned char TM1638_Read(void);
void Write_DATA(unsigned char add,unsigned char DATA);
void Write_allLED(unsigned char LED_flag);
void init_TM1638(void);
void Gpio_Inits(void);
void SystemClock_Config(void);
#endif
一周热门 更多>