一个接收指令的简单的stm32小程序用的普中的板子 f103zeHAL库的 软件用的cubemx
单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- #include "stdio.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- int ch;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- static void Show_Message(void)
- {
- printf("\r\n 这是一个通过串口通信指令控制LED灯或蜂鸣器实验 \n");
- printf("指令对应如下:\n");
- printf(" 指令 ------ 状态 \n");
- printf(" 1 ------ LED1翻转 \n");
- printf(" 2 ------ LED2翻转 \n");
- printf(" 3 ------ LED3翻转 \n");
- printf(" 4 ------ 蜂鸣器响 \n");
- printf(" 5 ------ 蜂鸣器不响 \n");
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
-
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_TIM3_Init();
- /* USER CODE BEGIN 2 */
- Show_Message();
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- ch=getchar();
- printf("接收到字符:%c\n",ch);
-
- /* 根据字符指令控制RGB彩灯颜色 */
- switch(ch)
- {
-
- case '1':
- HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0); //LED1翻转
- break;
- case '2':
- HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_1); //LED2翻转
- break;
- case '3':
- HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_2); //LED3翻转
- break;
- case '4': //蜂鸣器响
-
- HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
- __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2,50);
- break;
- case '5':
- HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_2); //蜂鸣器不响
- break;
- default:
- /* 如果不是指定指令字符,打印提示信息 */
- Show_Message();
- break;
- }
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**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_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- int fputc(int ch, FILE *f)
- {
- HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
- return ch;
- }
- int fgetc(FILE * f)
- {
- uint8_t ch = 0;
- HAL_UART_Receive(&huart1,&ch, 1, 0xffff);
- return ch;
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
control.rar
(9.02 MB, 下载次数: 13)
|