From a14b21fc96e36c1503044709943c86523f7f3180 Mon Sep 17 00:00:00 2001 From: zhyinch <zhyinch@gmail.com> Date: 星期一, 02 十一月 2020 16:39:30 +0800 Subject: [PATCH] 增加蜂鸣器驱动代码,可以响,但是不连续。 --- Src/main.c | 149 +++++++++++++++++++++++++++++++++++++------------ 1 files changed, 111 insertions(+), 38 deletions(-) diff --git a/Src/main.c b/Src/main.c index 679a53c..d9fa477 100644 --- a/Src/main.c +++ b/Src/main.c @@ -40,8 +40,8 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ //#define DEBUG_MODE -#define Period (uint32_t) 32768 - 1 -#define Timeout (uint32_t) 32768 - 1//(32768 - 1) +//#define LPTIM_Period (uint32_t) 32768 - 1 +//#define Timeout (uint32_t) 32768 - 1//(32768 - 1) /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -55,6 +55,8 @@ LPTIM_HandleTypeDef hlptim1; SPI_HandleTypeDef hspi1; + +TIM_HandleTypeDef htim2; UART_HandleTypeDef huart1; DMA_HandleTypeDef hdma_usart1_rx; @@ -72,6 +74,7 @@ static void MX_SPI1_Init(void); static void MX_ADC_Init(void); static void MX_DMA_Init(void); +static void MX_TIM2_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ @@ -123,29 +126,29 @@ // LED_TR_OFF; current_time=GetLPTime(); //motor_state=2; - if(g_com_map[MOTOR_ONTIME]>0) + if(g_com_map[MOTOR_ONTIME_INDEX]>0) { - g_com_map[MOTOR_ONTIME]--; + g_com_map[MOTOR_ONTIME_INDEX]--; motor_state = 1; } switch(motor_state) {case 0: - MOTOR_OFF; + // MOTOR_OFF; break; case 1: if(current_time<MOTOR_ONTIME) { - MOTOR_ON; + // MOTOR_ON; }else{ - MOTOR_OFF; + // MOTOR_OFF; } break; case 2: - MOTOR_ON; + // MOTOR_ON; break; case 3: - MOTOR_OFF; + // MOTOR_OFF; break; } if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin)) @@ -210,23 +213,27 @@ } -u16 total_slotnum,current_slotnum; +u16 current_slotnum; extern int32_t offsettimeus; #define FIXSLOT +#define FIXSLOTPOS 3 +u16 slotpos_intoatl; uint16_t bigslot_num; -uint8_t fix_slotpos =8; +uint8_t fix_slotpos =FIXSLOTPOS; int32_t lpcount,lptime,target_time; void SetNextPollTime(u16 slotpos) { -u16 slotpos_intoatl; + #ifdef FIXSLOT slotpos = fix_slotpos; #endif - if(current_slotnum>=total_slotnum) - current_slotnum-=total_slotnum; + if(current_slotnum>=tag_frequency) + current_slotnum-=tag_frequency; slotpos_intoatl = current_slotnum*bigslot_num+slotpos; + if(current_slotnum==19) + {slotpos_intoatl = current_slotnum*bigslot_num+slotpos;} target_time=(slotpos_intoatl*SLOTTIME_MS+100)*100; //if(target_time<90000) { @@ -248,25 +255,26 @@ Usart1ParseDataCallback = UsartParseDataHandler; parameter_init(); //deca_sleep(1000); - g_com_map[VERSION] = 0x0204; + g_com_map[VERSION] = 0x0205; #ifdef DEBUG_MODE // g_com_map[DEV_ROLE]=1; - g_com_map[DEV_ID]=0x123; - g_com_map[COM_INTERVAL]=100; +// g_com_map[DEV_ID]=0x123; +// g_com_map[COM_INTERVAL]=100; // g_com_map[MAX_REPORT_ANC_NUM]=1; // g_com_map[NOMOVESLEEP_TIME]=10; -// g_com_map[IMU_ENABLE]=0; + g_com_map[IMU_ENABLE]=0; + g_com_map[COM_INTERVAL]=100; #endif //g_com_map[NOMOVESLEEP_TIME]=10; - g_com_map[COM_INTERVAL]=50; + // g_com_map[COM_INTERVAL]=100; if(g_com_map[COM_INTERVAL]==0) { g_com_map[COM_INTERVAL]=100; } - if(module_power>MAX_RFPOWER) - {module_power=MAX_RFPOWER;} - if(module_power<0) - {module_power=0;} + if(g_com_map[POWER]>MAX_RFPOWER) + {g_com_map[POWER]=MAX_RFPOWER;} + if(g_com_map[POWER]<0) + {g_com_map[POWER]=0;} module_power = g_com_map[POWER]; imu_enable=g_com_map[IMU_ENABLE]; motor_enable=g_com_map[MOTOR_ENABLE]; @@ -280,8 +288,7 @@ slot_startcount=tyncpoll_time*1000/LPTIMER_LSB; lastpoll_time=tyncpoll_time; tag_frequency = 1000/g_com_map[COM_INTERVAL]; - total_slotnum = 1000/g_com_map[COM_INTERVAL]; - bigslot_num = TOTAL_SLOTNUM/tag_frequency; + bigslot_num = TOTAL_SLOTNUM/tag_frequency; current_slotnum=1; current_time=GetLPTime(); if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK) //system time is 1010ms 1 puls=30.518us @@ -311,6 +318,7 @@ uint32_t temp; extern uint8_t sleep_flag,m_bEUARTTxEn; uint16_t waitusart_timer; +extern double pressure,temperature; //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) //{ // /* Set transmission flag: trasfer complete*/ @@ -354,14 +362,16 @@ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_LPTIM1_Init(); - MX_DMA_Init(); MX_USART1_UART_Init(); MX_SPI1_Init(); MX_ADC_Init(); + MX_DMA_Init(); + MX_TIM2_Init(); /* USER CODE BEGIN 2 */ LED_LG_ON; Program_Init(); LIS3DH_Data_Init(); +// BarInit(); Dw1000_Init(); Dw1000_App_Init(); @@ -373,6 +383,7 @@ { Error_Handler(); } + // HAL_Delay(2000); // mcu_sleep(); //LED0_BLINK; @@ -395,7 +406,7 @@ { static uint16_t blink_count=0; SystemClock_Config(); g_start_send_flag = 0; - + GetPressAndTemp(); // if(current_count>slot_startcount&¤t_count<slot_startcount+interval_count) // { // HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET); @@ -622,6 +633,69 @@ } /** + * @brief TIM2 Initialization Function + * @param None + * @retval None + */ +static void MX_TIM2_Init(void) +{ + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + TIM_OC_InitTypeDef sConfigOC = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ + htim2.Instance = TIM2; + htim2.Init.Prescaler = 9; + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 1171; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + if (HAL_TIM_PWM_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = 585; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM2_Init 2 */ +if(HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3) != HAL_OK) + { + /* Starting Error */ + Error_Handler(); + } + /* USER CODE END TIM2_Init 2 */ + HAL_TIM_MspPostInit(&htim2); + +} + +/** * @brief USART1 Initialization Function * @param None * @retval None @@ -703,7 +777,7 @@ HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|MOTOR_Pin|PWR_CTL_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|PWR_CTL_Pin, GPIO_PIN_SET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin, GPIO_PIN_RESET); @@ -750,20 +824,18 @@ GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStruct); - /*Configure GPIO pins : MOTOR_Pin LED1_G_Pin LED1_R_Pin LED2_R_Pin - PWR_CTL_Pin */ - GPIO_InitStruct.Pin = MOTOR_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin - |PWR_CTL_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - /*Configure GPIO pin : INPUT_5V_Pin */ GPIO_InitStruct.Pin = INPUT_5V_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(INPUT_5V_GPIO_Port, &GPIO_InitStruct); + + /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin LED2_R_Pin PWR_CTL_Pin */ + GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|PWR_CTL_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : DW_WKUP_Pin DW_CTRL_Pin LED2_G_Pin */ GPIO_InitStruct.Pin = DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin; @@ -809,6 +881,7 @@ /*Enter the Standby mode*/ HAL_PWR_EnterSTANDBYMode(); } +extern u16 tagslotpos; extern uint8_t tx_near_msg[80],stationary_flag,gotosleep_flag; float key_keeptime; void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim) @@ -817,7 +890,7 @@ i++; g_start_send_flag=1; current_slotnum++; - SetNextPollTime(tyncpoll_time); + SetNextPollTime(tagslotpos); nomove_count+=(float)g_com_map[COM_INTERVAL]/1000; if(!GET_USERKEY) { -- Gitblit v1.9.3