当前位置: 首页 > news >正文

stm32仿真 74hc238流水灯 数码管动态数字显示

请添加图片描述

f103c6t6a_hex文件

#include "main.h"![请添加图片描述](https://i-blog.csdnimg.cn/direct/8c0d44b121134cf08f5186df316ea07f.gif)

#include "stdlib.h"



void SystemClock_Config(void);
static void MX_GPIO_Init(void);
// 自定义abc引脚
#define A_PIN GPIO_PIN_1
#define B_PIN GPIO_PIN_2 
#define C_PIN GPIO_PIN_0 


//y0到y7输出
void segment(char c){
	if(c == 'a'){
		GPIOB->BRR = A_PIN | B_PIN | C_PIN; 
	}
	else if(c == 'b'){
		GPIOB->BSRR = A_PIN; 
		GPIOB->BRR =  B_PIN | C_PIN; 
}
		else if(c == 'c'){
		GPIOB->BSRR = B_PIN; 
		GPIOB->BRR =  A_PIN | C_PIN; 
}
			else if(c == 'd'){
		GPIOB->BSRR = A_PIN|B_PIN ; 
		GPIOB->BRR =   C_PIN; 
}
				else if(c == 'e'){
		GPIOB->BSRR = C_PIN; 
		GPIOB->BRR =  A_PIN | B_PIN; 
}
					else if(c == 'f'){
		GPIOB->BSRR = A_PIN| C_PIN; 
		GPIOB->BRR =  B_PIN ; 
}
					
		else if(c == 'g'){
		GPIOB->BSRR =B_PIN| C_PIN; 
		GPIOB->BRR = A_PIN ; 
}
		else if(c == 'p'){
		GPIOB->BSRR =A_PIN |B_PIN| C_PIN; 
}

}

char segment_list[]={'a','b','c','d','e','f','g','p'};
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
	
while (1)
    {
     for(int i=0;i<=7;i++){
	segment(segment_list[i]);
		HAL_Delay(500);
	}
    } 
	
	
 

}




void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  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_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIOInitStruct = {0}; // 初始化 GPIO 结构体

    // 使能 GPIO 端口时钟
    __HAL_RCC_GPIOB_CLK_ENABLE();

    // 设置 GPIO 的通用配置
    GPIOInitStruct.Mode = GPIO_MODE_OUTPUT_PP;    // 设置为推挽输出模式
    GPIOInitStruct.Pull = GPIO_NOPULL;            // 不使用上拉或下拉
    GPIOInitStruct.Speed = GPIO_SPEED_FREQ_LOW;   // 设置为低速

    // 配置 GPIOB 端口的所有引脚
    GPIOInitStruct.Pin = GPIO_PIN_All; // 指定所有引脚
    HAL_GPIO_Init(GPIOB, &GPIOInitStruct); // 初始化配置


}

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT

控制74hc238显示变化的数字
请添加图片描述

c6t6控制74hc238 hex文件

#include "main.h"
#include "stdlib.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
// 定义74hc238输入引脚
#define A_PIN GPIO_PIN_1
#define B_PIN GPIO_PIN_2 
#define C_PIN GPIO_PIN_0 

// 数码管段码
const uint16_t num_to_segment[] = {
  0x3F,0x06,0x5b,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F
};

void segment(char c){//74HC238三位输入对应八位输出
	
GPIOB -> BRR = GPIO_PIN_8;//E3拉低 关闭输出
	if(c == 'a'){
		GPIOB->BRR = A_PIN | B_PIN | C_PIN; 
	}
	else if(c == 'b'){
		GPIOB->BSRR = A_PIN; 
		GPIOB->BRR =  B_PIN | C_PIN; 
}
		else if(c == 'c'){
		GPIOB->BSRR = B_PIN; 
		GPIOB->BRR =  A_PIN | C_PIN; 
}
			else if(c == 'd'){
		GPIOB->BSRR = A_PIN|B_PIN ; 
		GPIOB->BRR =   C_PIN; 
}
				else if(c == 'e'){
		GPIOB->BSRR = C_PIN; 
		GPIOB->BRR =  A_PIN | B_PIN; 
}
					else if(c == 'f'){
		GPIOB->BSRR = A_PIN| C_PIN; 
		GPIOB->BRR =  B_PIN ; 
}
					
		else if(c == 'g'){
		GPIOB->BSRR =B_PIN| C_PIN; 
		GPIOB->BRR = A_PIN ; 
}
		else if(c == 'p'){
		GPIOB->BSRR =A_PIN |B_PIN| C_PIN; 
}

GPIOB -> BSRR = GPIO_PIN_8;//E3拉高 输出
HAL_Delay(1/100);//显示1ms
}


void hc238_disnumber(int num){//74hc238显示数字
	
	
	
	for(int i7=0;i7<2;i7++){
	GPIOB->BSRR = GPIO_PIN_5|GPIO_PIN_6; //拉高公共端(阴极),关闭显示	
	int num_1 = num%10;
	num = num/10;
			
	if(i7==0){
	GPIOB->BRR = GPIO_PIN_6; //显示个位
	//HAL_Delay(1);
			//	break;
}
else{
	GPIOB->BRR = GPIO_PIN_5; //显示十位
	//HAL_Delay(1);
			//	break;
}
switch(num_1){
	case 0://显示0
				segment('a');
				segment('b');
				segment('c');
				segment('d');
				segment('e');
				segment('f');
		break;

		
	case 1://显示1
				segment('b');
				segment('c');
			break;
	
		case 2://显示2
				segment('a');
				segment('b');
				segment('g');
				segment('d');
				segment('e');
				break;
		
	case 3:

				segment('a');
				segment('b');
				segment('c');
				segment('d');
				segment('g');
			break;
				
	case 4:


				segment('b');
				segment('c');
				segment('f');
				segment('g');
			break;
				
	case 5:

				segment('a');
				segment('f');
				segment('c');
				segment('d');
				segment('g');
			break;
		case 6:

				segment('a');
				segment('c');
				segment('d');
				segment('e');
				segment('f');
				segment('g');
			break;
			case 7:

				segment('a');
				segment('b');
				segment('c');
			
			
			break;
			case 8:

				segment('a');
				segment('b');
				segment('c');
				segment('d');
				segment('e');
				segment('g');
				segment('f');

			break;
					case 9:

				segment('a');
				segment('b');
				segment('f');
				segment('c');
				segment('d');
				segment('g');
			break;
				

}

if (num==0){
break;
}


}
}


void display_number1(int n);//函数声明

int main(void){

  SystemClock_Config();
  MX_GPIO_Init();
while (1)	//while (1)
    {
			
				 for(int i=0;i<=9;i++){
					 for(int t=0;t<13;t++){//显示时间控制

							display_number1(i+50);//直连数码管数字显示
		
							hc238_disnumber(i*10);//控制74hc238显示数字
			}
							
			 }
			
		
    } //while
	
	
 

}//main


int p1;
int i1;

void display_number1(int n1){


	for (i1=0;i1<2;i1++){
		GPIOA->BRR = GPIO_PIN_All;//清除GPIO端口A的所有引脚
		p1 = n1%10;//求余数,47%10=7,4%10=4
		GPIOA->ODR =num_to_segment[p1];//向gpioA端口写入段码
		n1 = n1/10;//进位,47/10=4
		GPIOA->BSRR = GPIO_PIN_11|GPIO_PIN_12; //共阴极拉高,清除显示
				
		switch(i1){
			case 0:
				GPIOA->BRR = GPIO_PIN_12; //个位
				break;
			case 1:
				GPIOA->BRR = GPIO_PIN_11; //十位
				break;
		}
		HAL_Delay(1);
		if (n1 == 0){
			break;
			}
					}
	

			
}








void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  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_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIOInitStruct = {0}; // 初始化 GPIO 结构体

    // 使能 GPIO 端口时钟
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOD_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
		
		
	
    // 设置 GPIO 的通用配置
    GPIOInitStruct.Mode = GPIO_MODE_OUTPUT_PP;    // 设置为推挽输出模式
    GPIOInitStruct.Pull = GPIO_NOPULL;            // 不使用上拉或下拉
    GPIOInitStruct.Speed = GPIO_SPEED_FREQ_LOW;   // 设置为低速

	// 配置 GPIOA和B 端口的所有引脚
    GPIOInitStruct.Pin = GPIO_PIN_All; // 指定所有引脚
    HAL_GPIO_Init(GPIOB, &GPIOInitStruct); // 初始化B配置
		HAL_GPIO_Init(GPIOA, &GPIOInitStruct);// 初始化A配置 

}



void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

相关文章:

  • 快速入门——前端数据模拟MockJS
  • java后端开发day19--学生管理系统升级
  • TypeError: the JSON object must be str, bytes or bytearray, not dict
  • LLM全栈框架完整分类清单(预训练+微调+工具链)
  • VMware中的linux常用指令
  • STM32 缺一不可的最基础的初始化部分
  • CSS—引入方式、选择器、复合选择器、文字控制属性、CSS特性
  • smolagents学习笔记系列(六)Secure code execution
  • Redis 面试题
  • RT-Thread+STM32L475VET6——TF 卡文件系统
  • 创建型模式 - 原型模式 (Prototype Pattern)
  • 【Leetcode】两数之和
  • 【Blender】三、材质篇--01,Blender材质基础 原理化BSDF
  • Go红队开发—基础语法入门
  • 如何在 Ubuntu 上安装和使用 Podman ?
  • 【STL】4.<list>
  • kotlin 知识点 七 泛型的高级特性
  • Java 入门第一课 InteliJ IDEA 的快捷操作
  • 阿里云可观测全面拥抱 OpenTelemetry 社区
  • 【Keil5教程及技巧】耗时一周精心整理万字全网最全Keil5(MDK-ARM)功能详细介绍【建议收藏-细细品尝】
  • 江苏疾控:下设部门无“病毒研究所”,常荣山非本单位工作人员
  • 持续降雨存在落石风险,贵州黄果树景区水帘洞将封闭至6月初
  • 北方首场高温将进入鼎盛阶段,江南华南多地需警惕降雨叠加致灾
  • 芬兰直升机相撞坠毁事故中五名人员全部遇难
  • AI赋能科学红毯,机器人与科学家在虚实之间叩问“科学精神”
  • 自然资源部:不动产登记累计化解遗留问题房屋2000多万套