生活随笔
收集整理的這篇文章主要介紹了
20年吉林省综合测评
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于當時不讓帶手機,這里憑借深刻的記憶來描述一下題目要求:
設計一個波形發生器,產生200-1600Hz連續可調的方波設計一個二分頻器,對1生成的信號進行二分頻根據波形發生器的頻率實現以200Hz為單位依次點亮八個LED燈。
實際條件:一片CD4011,一塊洞洞板,一片stm32f103zet6最小系統,八個LED,電阻電容導線焊錫等
軟件部分
使用cube MX + Keil5快速建立程序框架,首先打開程序新建工程,詳細流程請參考我之前的博客:
https://editor.csdn.net/md/?articleId=108711636
硬件隊友焊接時,我們寫測頻程序,在硬件沒有焊出前需要把程序按照理想情況調試好(理想情況指波形是穩定的方波,上升沿下降沿沒有劇烈的抖動,頻率穩定,電壓合適),下面直接貼出我調試好的程序。
使用定時器2測量兩個上升沿之間的實際時間,倒數即為頻率,然后把得到的頻率傳到一個封裝好的函數,題目要求頻率誤差在10%以內,判斷是否在范圍內,然后按字輸出對應的管腳點亮LED。
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** <h2><center>© Copyright (c) 2020 STMicroelectronics.* All rights reserved.</center></h2>** This software component is licensed by ST under BSD 3-Clause license,* the "License"; You may not use this file except in compliance with the* License. You may obtain a copy of the License at:* opensource.org/licenses/BSD-3-Clause********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#ifdef __GNUC_
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);return ch;
}
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
uint8_t ledduan[8]={0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF}; //0~8 LED 共陽極
/* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
uint32_t capture[2];
uint16_t V,F;
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void light(uint16_t L)
{if(L>190&&L<210) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[0])); return;} else if (L>390&&L<410) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[1])); return; }else if (L>590&&L<605) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[2])); return; }else if (L>790&&L<805) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[3])); return; }else if (L>990&&L<1010) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[4])); return;}else if (L>1190&&L<1210) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[5])); return;}else if (L>1390&&L<1410) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[6])); return;}else if (L>1590&&L<1610) { GPIO_Write(GPIOC,(uint16_t)(~ledduan[7])); return;}
}
/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
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_TIM2_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */printf("F: %2d\r\n",F*2);HAL_Delay(100);light(F*2);}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
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_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;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_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_1) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{UNUSED(htim); static uint8_t i;if(htim->Instance == htim2.Instance){capture[i] = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2); i++;if(i==2) { i=0;V = capture[1]-capture[0]; }F = 1000000 / V; //得到實際頻率}
}
/* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
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****/
由于HAL庫并沒有字輸出函數,這里參考標準庫自己封裝一個,ODR寄存器對應16個管腳,1高0低。
程序調試完成后,使用信號發生器產生一個100-800Hz的3-5V的方波連接到PA1腳,使用USB轉TTL(核心板有集成CH340芯片不需要此步驟)對應連接到開發板,使用串口調試助手觀察打印信息,驗證程序測頻是否準確。
硬件部分
一片CD4011,集成4個與非門,兩個用來產生方波,兩個用來構成分頻器。具體原理圖如下。
Q和Q非任取一個即可,實測也可以直接焊在一起。
以上就是全部實測可行的方案,但是焊接這玩意有點玄學,可能由于一個小問題就是不好使,如果產生方波后,波形不穩定可以在CD4011的VCC與GND間加一個103電容,如果還不行就接一個負載(加一個電阻在輸出和地之間)。如果不能產生方波那就…程序產生PWM頻率根據電位器電壓調節,如果分頻器焊不出來那就繼續軟件二分頻,直接使用IO外部中斷,中斷兩次翻轉IO電平即可。
總結
以上是生活随笔為你收集整理的20年吉林省综合测评的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。