完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 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); } |
|
相关推荐
9个回答
|
|
|
|
|
|
有需要 stm32 驱动tm1638的工程的告诉我 我把我的工程放上来
|
|
|
|
悬赏这份 放在那里,没放上积分
|
|
|
|
悬赏这份 放在那里,没放上积分
|
|
|
|
#include "periph.h"
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< //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]< for(i=0;i<8;i++) if((0x01< break; return i; } |
|
|
|
#ifndef __PERIPH_H
#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 |
|
|
|
#include "stm32l4xx_hal.h"
#include "periph.h" #include "stdio.h" #include "string.h" unsigned char tab[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; unsigned char num[8]; int main() { uint8_t i; HAL_Init(); SystemClock_Config(); Gpio_Inits(); init_TM1638(); for(i=0;i<8;i++) Write_DATA(i<<1,tab[i]); //?????? while(1) { i=Read_key(); //???? if(i<8) { num[i]++; while(Read_key()==i); //?????? if(num[i]>15) num[i]=0; Write_DATA(i*2,tab[num[i]]); Write_allLED(1< } } } |
|
|
|
引脚有没有设置成OD门,读取按键之前有没有把输出设置成高电平
|
|
|
|
兄弟,STM32是3.3V的端口,TM1638是5VDC的端口,二者怎么接线的啊,还有接的8位共阴数码管,例程能否发给我参考一下?QQ 1964439971 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1049 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1017 浏览 2 评论
2123 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1219 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1639 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 10:57 , Processed in 0.595558 second(s), Total 60, Slave 53 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号