/**
******************************************************************************
* File Name : TIM.c
* Description : This file provides code for the configuration
* of the TIM instances.
******************************************************************************
** This notice applies to any and all portions of this file
* that are not between comment pairs USER CODE BEGIN and
* USER CODE END. Other portions of this file, whether
* inserted by the user or by software development tools
* are owned by their respective copyright owners.
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
#include "dac.h"
volatile uint16_t Data_tmp;
uint16_t Data1;
uint32_t Number = 44;
/* USER CODE END 0 */
TIM_HandleTypeDef htim6;
TIM_HandleTypeDef htim9;
/* TIM6 init function */
void MX_TIM6_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
htim6.Instance = TIM6;
htim6.Init.Prescaler = 4 - 1;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 1000 - 1 ;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
/* TIM9 init function */
void MX_TIM9_Init(void)
{
TIM_OC_InitTypeDef sConfigOC;
htim9.Instance = TIM9;
htim9.Init.Prescaler = 0;
htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
htim9.Init.Period = 500 - 1;
htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_PWM_Init(&htim9) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 125 * 0.5;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_MspPostInit(&htim9);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM6)
{
/* USER CODE BEGIN TIM6_MspInit 0 */
/* USER CODE END TIM6_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM6_CLK_ENABLE();
/* TIM6 interrupt Init */
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
/* USER CODE BEGIN TIM6_MspInit 1 */
/* USER CODE END TIM6_MspInit 1 */
}
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM9)
{
/* USER CODE BEGIN TIM9_MspInit 0 */
/* USER CODE END TIM9_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM9_CLK_ENABLE();
/* USER CODE BEGIN TIM9_MspInit 1 */
/* USER CODE END TIM9_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(timHandle->Instance==TIM9)
{
/* USER CODE BEGIN TIM9_MspPostInit 0 */
/* USER CODE END TIM9_MspPostInit 0 */
/**TIM9 GPIO Configuration
PE5 ------> TIM9_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF3_TIM9;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/* USER CODE BEGIN TIM9_MspPostInit 1 */
/* USER CODE END TIM9_MspPostInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM6)
{
/* USER CODE BEGIN TIM6_MspDeInit 0 */
/* USER CODE END TIM6_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM6_CLK_DISABLE();
/* TIM6 interrupt Deinit */
/* USER CODE BEGIN TIM6:TIM6_DAC_IRQn disable */
/**
* Uncomment the line below to disable the "TIM6_DAC_IRQn" interrupt
* Be aware, disabling shared interrupt may affect other IPs
*/
/* HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn); */
/* USER CODE END TIM6:TIM6_DAC_IRQn disable */
/* USER CODE BEGIN TIM6_MspDeInit 1 */
/* USER CODE END TIM6_MspDeInit 1 */
}
}
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM9)
{
/* USER CODE BEGIN TIM9_MspDeInit 0 */
/* USER CODE END TIM9_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM9_CLK_DISABLE();
/* USER CODE BEGIN TIM9_MspDeInit 1 */
/* USER CODE END TIM9_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == htim6.Instance)
{
// Data_tmp = (SOUND_DATA[Number + 1] << 8)|(SOUND_DATA[Number]) ;
// if(Data_tmp&0x8000)
// {
// Data_tmp = ~Data_tmp + 1;
// Data_tmp &= 0x7fff;
//
// Data_tmp = 0x7FFF - Data_tmp ;
// }
// else
// {
// Data_tmp = Data_tmp + 0x8000 ;
// }
//
//
// Data_tmp = ( (float)Data_tmp / 65535 ) * 100;
//
// if(Data_tmp == Data1 ){}
// else
// {
// USER_PWM_SetDutyRatio(&htim9,TIM_CHANNEL_1,Data_tmp,SET) ;
// Data1 = Data_tmp;
// }
//
// if(Number > 712013)
// {
// Number = 44;
// HAL_TIM_Base_Stop_IT(&htim6);
// }
// else
// {
// Number += 4;
// }
//*************************************************************************************
Data_tmp = (SOUND_DATA[Number + 1] << 8)|(SOUND_DATA[Number]) ;
if(Data_tmp&0x8000)
{
Data_tmp = ~Data_tmp + 1;
Data_tmp &= 0x7fff;
Data_tmp = 0x7FFF - Data_tmp ;
}
else
{
Data_tmp = Data_tmp + 0x8000 ;
}
Data_tmp = ( (float)Data_tmp / 65535 ) * 4096;
if(Data_tmp == Data1 ){}
else
{
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_2, DAC_ALIGN_12B_R, Data_tmp);
HAL_DAC_Start(&hdac, DAC_CHANNEL_2);
Data1 = Data_tmp;
}
if(Number > 712013)
{
评论1
最新资源