` 本帖最后由 QQSD 于 2020-5-23 21:40 编辑
这个是以前大二无聊打发时间做的项目,目的也是为了学一下STM32CUBE。发出来跟大家交流交流,不足之处也请大佬们多多指点。orz
功能有:
1.播放音乐门铃,且通过u***可以进行更换音乐
2.红外模块检测到门口有人长时间逗留会播放报警提示并且会发送短信提示客户
3.主人不在家时我们可以通过拨打电话联系主人
4.可以进行录音留言,主人回家后可以进行播放录音(播放录音的按键是在内侧的,录音按键是在外侧的,你说贴心不哈哈哈哈)
直接上实物图吧
(这样布局呢就按键、喇叭对准外面的人,然后那些模块芯片啊都在内测)
主要零件:STM32F103C8T6、 JQ8900-16P 模块、ISD1820录音芯片,GPS通讯模块及其驱动电路、红外对管、点动按钮、自锁开关 OK,那接下来讲一下STM32CUBE中怎么配置和生产代码。
首先配置RCC(复位和时钟控制),选择外部高速始终,如下图
进入时钟配置,我们使用的STM32F103核心板外部晶振是8MHz的,在Input freequency 输入8,在HCLK那里会提示最大频率72MHz,输入72,然后自动寻求配置,注意第一次配置出来的还是内部时钟HSI的,要选择为HSE,配置好后如下图
结着我们对需要使用到的IO口进行配置,同时标上标签。
打开这个界面,可以配置IO口、定时器和中断。
配置定时器,定时器的频率设置为2HZ
配值输入引脚上拉输入,配置输出引脚
同时设置三个串口的波特率。
ok了,之后就点击generate code,选择输入为MDK5工程(这个可以根据自己的编译器选择)可以生成驱动代码了。
STM32CUBE的方便之处在于我们可以不用去查详细的手册就可以直接开始撸代码了。感谢STM32CUBE为我们提供了HAL库。
接下来就是捋一下逻辑然后撸代码了,附上代码
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* 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 */
- uint8_t M1[6] = {0xAA, 0x07, 0x02, 0x00, 0x01, 0xB4};
- uint8_t M2[6] = {0xAA, 0x07, 0x02, 0x00, 0x02, 0xB5};
- uint8_t End[1] = {0x1A};
- uint16_t Tcount;
- uint16_t Messgae,FFlag;
- /* USER CODE END 0 */
- /**
- * [url=home.php?mod=space&uid=2666770]@Brief[/url] 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_USART2_UART_Init();
- MX_USART3_UART_Init();
- MX_TIM1_Init();
- /* USER CODE BEGIN 2 */
- //HAL_UART_Transmit(&huart1,M1,6,10);//播放第一首
- //HAL_UART_Transmit(&huart1,M2,6,10);//播放第二首
- HAL_TIM_Base_Start_IT(&htim1);
- Tcount = 0;
- FFlag = 1;
- HAL_UART_Transmit(&huart1,"初始化OK了
- ",sizeof("初始化OK了
- "),10);
- HAL_UART_Transmit(&huart3,"U3OK
- ",sizeof("U3OK
- "),10);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- if(HAL_GPIO_ReadPin(Doorbell_GPIO_Port,Doorbell_Pin)==RESET)
- {
- HAL_Delay(10);
- if(HAL_GPIO_ReadPin(Doorbell_GPIO_Port,Doorbell_Pin)==RESET)
- {
- while(HAL_GPIO_ReadPin(Doorbell_GPIO_Port,Doorbell_Pin)==RESET);
- HAL_UART_Transmit(&huart2,M1,6,10);//播放第一首
- HAL_UART_Transmit(&huart1,"响门铃
- ",sizeof("响门铃
- "),10);
- }
- }
- if(HAL_GPIO_ReadPin(Call_GPIO_Port,Call_Pin)==RESET)
- {
- HAL_Delay(10);
- if(HAL_GPIO_ReadPin(Call_GPIO_Port,Call_Pin)==RESET)
- {
- while(HAL_GPIO_ReadPin(Call_GPIO_Port,Call_Pin)==RESET);
- HAL_UART_Transmit(&huart3,"ATD13*********;
- ",sizeof("ATD13*********;
- "),10);
- }
- }
- if(FFlag){
- if(Messgae){
- HAL_UART_Transmit(&huart3,"AT+CSCS="GSM"
- ",sizeof("AT+CSCS="GSM"
- "),10);
- HAL_Delay(100);
- HAL_UART_Transmit(&huart3,"AT+CMGF=1
- ",sizeof("AT+CMGF=1
- "),10);
- HAL_Delay(100);
- HAL_UART_Transmit(&huart3,"AT+CMGS="13*********"
- ",sizeof("AT+CMGS="13********* "
- "),10);
- HAL_Delay(100);
- HAL_UART_Transmit(&huart3,"Danger!!!",sizeof("Danger!!!"),10);
- HAL_Delay(100);
- HAL_UART_Transmit(&huart3,End,1,10);
- Messgae = 0;
- FFlag = 0;
- }
- }
-
-
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* 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 */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if (htim->Instance == htim1.Instance)
- {
-
-
-
- if(HAL_GPIO_ReadPin(Check_GPIO_Port,Check_Pin)==RESET)
- {
- //HAL_Delay(10);
- if(HAL_GPIO_ReadPin(Check_GPIO_Port,Check_Pin)==RESET)
- {
- //while(HAL_GPIO_ReadPin(Doorbell_GPIO_Port,Doorbell_Pin)==SET);
- Tcount++;
- }
- }else{
- Tcount = 0;
- }
-
-
- if(Tcount == 10){
- HAL_UART_Transmit(&huart2,M2,6,10);//播放第二首
- HAL_UART_Transmit(&huart1,"检查到入侵
- ",sizeof("检查到入侵
- "),10);
- Tcount = 0;
- Messgae = 1;
- }
- }
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * [url=home.php?mod=space&uid=3142012]@param[/url] 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
- /**
- * @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
- ", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
复制代码
好了,如果需要详细工程的话可以留言附上邮箱,我看到了会发给你滴!!
`
|