西电2022年A测(线上)题目
这道A测题目老师在文档中已经给出了大部分代码,(不过是图片),我已经把代码,仿真电路都整理完毕,这篇文章主要帮助大家配置环境以及更换自己学号相关联的温度阈值。
祝大家毕业顺利!
首先你需要一个Keil5的安装包百度云连接:https://pan.baidu.com/s/1VwdItEH7FZ58tju6Zy24tw?pwd=0mo9
提取码: 0mo9
上述链接中,有两个文件,先运行Keil_uVision_MDK522.EXE
,一切按照默认进行,老师发的文件好像也有一个安装包,两个都可以使用,按照老师的教程可以完成此步骤。
看这个也行:Matcha_ice_cream的博客
见2中的链接博客。
老师源文件的器材库不太对火,我重新搞了一个,链接如下:
百度网盘链接:https://pan.baidu.com/s/1NzUNPwm7iMLjvIi2aNITPQ?pwd=s51v
提取码 s51v,
内含两个器件库,一个ARM的,一个STM32F的。直接双击就可以安装。
我们的实验是在STM32F103C6上进行的,故而需要我发送的第二个库。
至此,keil5安装完成。
完整的工程文件链接如下:基于stm32f103芯片的直流电机驱动控制仿真系统工程文件全套
其中main函数如下:其实你仅需要将老师的工程文件中的main函数做一个替换。(如果你跑不起来,可以参考上面的文件)
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* © Copyright (c) 2020 STMicroelectronics.
* All rights reserved.
*
* 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"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.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
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define DS18B20_DQ_H HAL_GPIO_WritePin(GPIOA,GPIO_PIN_11,GPIO_PIN_SET)
#define DS18B20_DQ_L HAL_GPIO_WritePin(GPIOA,GPIO_PIN_11,GPIO_PIN_RESET)
#define DS18B20_DQ_ReadPin HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_11)
#define DIR_ReadPin HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_8)
#define DIR_H HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,GPIO_PIN_SET)
#define DIR_L HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,GPIO_PIN_RESET)
#define PWM_H HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET)
#define PWM_L HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
uint8_t const table1[]="Temp=";
uint8_t const table2[]="DC Motor opened";
uint8_t const table3[]="DC Motor closed";
char buff[16];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
void Deplay_us(uint16_t us);
void DS18B20_DQ_DDR(uint8_t ddr);
uint8_t DS18B20_Rbit(void);
uint8_t DS18B20_Rbyte(void);
void DS18B20_Wbyte(uint8_t xbyte);
void DS18B20_Reset(void);
short DS18B20_Get_Temp(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1,(uint8_t*)&ch,1,0xFFFF);
return ch;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
int8_t temp;
/* 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_TIM3_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
DS18B20_Get_Temp();
printf("Testing OK\r");
HAL_Delay(800);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_12);
temp=DS18B20_Get_Temp();
sprintf(&buff[0],"Temp=%dC CCW",temp);
printf("%s\r",buff);
if(temp > 28.3)
{
printf("%s\r",table2);
PWM_H;
HAL_Delay(30);
PWM_L;
HAL_Delay(70);
}
else
{
printf("%s\r",table3);
PWM_H;
HAL_Delay(100);
}
}
}
/* 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 RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
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();
}
/** Initializes the CPU, AHB and APB buses 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();
}
}
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 72-1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 5000;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 19200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_RESET);
/*Configure GPIO pins : PA0 PA1 PA11 PA12 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* 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 */
}
void Delay_us(uint16_t us)
{
uint16_t differ=0xffff-us-5;
__HAL_TIM_SET_COUNTER(&htim3,differ);
HAL_TIM_Base_Start(&htim3);
while(differ<0xffff-6)
differ=__HAL_TIM_GET_COUNTER(&htim3);
HAL_TIM_Base_Stop(&htim3);
}
void DS18B20_DQ_DDR(uint8_t ddr)
{
if(ddr == 1)
{
GPIOA->CRH&= 0XFFFF1FFF;
GPIOA->CRH|= 0X00001000;
}
else
{
GPIOA->CRH&= 0XFFFF8FFF;
GPIOA->CRH|= 0X00008000;
}
}
uint8_t DS18B20_Rbit(void)
{
uint8_t date,x;
DS18B20_DQ_DDR(1);
DS18B20_DQ_L;
Delay_us(2);
DS18B20_DQ_H;
DS18B20_DQ_DDR(0);
Delay_us(12);
x = DS18B20_DQ_ReadPin;
if(x)
date = 0x80;
else
date = 0;
Delay_us(50);
return date;
}
uint8_t DS18B20_Rbyte(void)
{
uint8_t rbyte = 0,i = 0, tempbit = 0;
for(i=1;i<=8;i++)
{
tempbit = DS18B20_Rbit();
rbyte = rbyte >> 1;
rbyte = rbyte|tempbit;
}
return rbyte;
}
void DS18B20_Wbyte(uint8_t xbyte)
{
uint8_t i,x=0;
DS18B20_DQ_DDR(1);
for(i=1;i<=8;i++)
{
x = xbyte & 0x01;
if(x)
{
DS18B20_DQ_L;
Delay_us(2);
DS18B20_DQ_H;
Delay_us(60);
}
else
{
DS18B20_DQ_L;
Delay_us(60);
DS18B20_DQ_H;
Delay_us(2);
}
xbyte = xbyte >> 1;
}
}
void DS18B20_Reset(void)
{
DS18B20_DQ_DDR(1);
DS18B20_DQ_H;
Delay_us(700);
DS18B20_DQ_L;
Delay_us(500);
DS18B20_DQ_DDR(0);
DS18B20_DQ_H;
Delay_us(33);
DS18B20_DQ_ReadPin;
Delay_us(500);
}
short DS18B20_Get_Temp(void)
{
uint8_t fg;
uint8_t TL,TH;
short data;
DS18B20_Reset();
DS18B20_Wbyte(0xcc);
DS18B20_Wbyte(0x44);
DS18B20_Reset();
DS18B20_Wbyte(0xcc);
DS18B20_Wbyte(0xbe);
TL = DS18B20_Rbyte();
TH = DS18B20_Rbyte();
if(TH > 0x7)
{
TL = ~TL;
TH = ~TH;
fg = 0;
}
else
fg = 1;
data = TH;
data <<= 8;
data += TL;
data = (float)data*0.0625;
if(fg)
return data;
else
return -data;
}
#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****/
将main文件替换后,或者获得完整的工程文件后,你需要让编译器的设置做出一些改变。
点击rebuild,如下图:
得到一个可执行文件ATest.hex
文件存放在MDK-ARM\ATest
路径下
在仿真之前,你需要安装一个Proteus8.8,一定要8.8及以上版本。
百度网盘链接:https://pan.baidu.com/s/1S_STjAlPUkg4wQtZ2ZQNcQ?pwd=8r7g
提取码:8r7g
运行上述文件中的proteus8.8.SP1.exe
,在出现激活界面时,选择使用本地Licence文件,选择到文件中的Licence.lxk
,其他一切默认。
安装之后,运行Patch.exe
完成安装。
从工程文件ATest_2021fal_1.pdsprj
进入Proteus8.8,这个文件在老师给的资料中,也可以在我发的完整工程连接中。
从老师题目中拿到的工程文件需要做一些连线,如下图所示进行连线:
双击左侧的芯片,将program file栏目的路径,定位到keil的输出文件处,如下图所示:
点击左下角的开始按钮(1),开始仿真,通过修改温度(2)观察电机转动情况和终端输出。
找到keil5工程文件中的src
文件夹,打开main文件,更改第156行if(temp > 28.3)
中的数字即可(找不到的用VSCODE打开)