LIUBEIHUA 发表于 2017-4-26 16:15:26

我的板小不能放晶振用的是内部时钟,用示波器看过可以工作的,采用的是硬石的风格。用TIM3,14,15,17都试过都有脉冲输出。

LIUBEIHUA 发表于 2017-4-26 16:22:41

初始化PWM后
HAL_TIM_PWM_Start(&htimx,TIM_CHANNEL_2);
打开PWM,示波器上就有波形输出,不能关!

user_pwm_setvalue(0);用这个关,实际上PWM没关给它低电平。

PWM关就再也打不开了

zbber 发表于 2017-4-26 16:28:12

/**,我只是路过打酱油的,嘿嘿

konway 发表于 2017-4-27 08:03:17

本帖最后由 konway 于 2017-4-27 08:08 编辑

LIUBEIHUA 发表于 2017-4-26 16:22
初始化PWM后
HAL_TIM_PWM_Start(&htimx,TIM_CHANNEL_2);
打开PWM,示波器上就有波形输出,不能关!

谢谢你的热心回复用这个函数关也是有延迟
HAL_TIM_PWM_Start(&htimx,TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(&htimx,TIM_CHANNEL_2);



user_pwm_setvalue(0);用这个关掉再打开PWM
关是关了,情况没改善,跟我用寄存器的方式是一样的问题
比如我只要关1ms
实际会关5.6ms

另外我也是内部晶振

海迹天涯 发表于 2017-4-27 08:36:01

来学习下

LIUBEIHUA 发表于 2017-4-27 12:54:57

中午吃饭休息时间做的红外发射程序

LIUBEIHUA 发表于 2017-4-27 12:56:18

/**
******************************************************************************
* File Name          : main.c
* Description      : Main program body
******************************************************************************
*
* 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 "main.h"
#include "stm32f0xx_hal.h"

uint8_t keyfg;
uint8_t sendfg;
uint8_t button;
uint8_t controlcode;
uint8_t compensate;
uint8_t User_code={0x86, 0x79};

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);                                    
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

void user_pwm_setvalue(uint16_t value)
{
    TIM_OC_InitTypeDef sConfigOC;

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = value;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
}

void Bit0_Encode_SignalGenerate(void)
{
        user_pwm_setvalue(316);
        HAL_Delay(56);
        user_pwm_setvalue(0);
        HAL_Delay(56);
}

void Bit1_Encode_SignalGenerate(void)
{
        user_pwm_setvalue(316);
        HAL_Delay(56);
        user_pwm_setvalue(0);
        HAL_Delay(168);
}

void Head_Encode_SignalGenerate(void)
{
        user_pwm_setvalue(316);
        HAL_Delay(900);
        user_pwm_setvalue(0);
        HAL_Delay(450);
}

void End_Encode_SignalGenerate(void)
{
        user_pwm_setvalue(316);
        HAL_Delay(56);
        user_pwm_setvalue(0);
        HAL_Delay(56);
        user_pwm_setvalue(316);
        HAL_Delay(56);
        user_pwm_setvalue(0);
}

void Encode_SignalGenerate(uint8_t * dat)
{
        uint8_t usercode;
        uint8_t i, j;
        usercode=dat;
        usercode=dat;
        for(j=0; j<2; j++)
        {
                for(i=0; i<8; i++)
                {
                        if(usercode&0x80)
                                Bit1_Encode_SignalGenerate();
                        else
                                Bit0_Encode_SignalGenerate();
                        usercode=usercode<<1;
                }
        }
}

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();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();

/* 启动通道PWM输出 */
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
user_pwm_setvalue(0);

controlcode=0xd2;
controlcode=0x2d;
while (1)
{
       Head_Encode_SignalGenerate();
       Encode_SignalGenerate(User_code);
       Encode_SignalGenerate(controlcode);
       End_Encode_SignalGenerate();
       HAL_Delay(4200);
}

}

/** System Clock Configuration
*/
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.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
    Error_Handler();
}

    /**Configure the Systick interrupt time
    */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/100000);

    /**Configure the Systick
    */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1266;
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();
}

if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
    Error_Handler();
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
    Error_Handler();
}

sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 633;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
    Error_Handler();
}

HAL_TIM_MspPostInit(&htim3);

}

/** Configure pins as
      * Analog
      * Input
      * Output
      * EVENT_OUT
      * EXTI
*/
static void MX_GPIO_Init(void)
{

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @briefThis function is executed in case of error occurrence.
* @paramNone
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler */
}

#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,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */

}

#endif

/**
* @}
*/

/**
* @}
*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

LIUBEIHUA 发表于 2017-4-27 13:00:55

/**Configure the Systick interrupt time
    */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/100000); //此处100000 这样HAL_Delay()变成10us

    /**Configure the Systick
HAL_Delay(56);就是560us

LIUBEIHUA 发表于 2017-4-27 13:02:26

完整的程序

konway 发表于 2017-4-27 17:31:32

LIUBEIHUA 发表于 2017-4-27 13:02
完整的程序

非常感谢您的热心回复。
今天下午改用GPIO脚加三极管来开关红外发射,PWM则一直输出38K方波
发现个奇葩的现象:
用GPIO控制一样有延迟问题,而且延迟的时间也是一样。
看来这不是PWM的问题
我把while大循环的所有程序屏蔽掉,只留红外发射,就没有延迟
如果加上
HAL_Delay(10);//10ms
也没延迟,改到15ms以上就有有延迟了。。。
这个问题搞了几天,我快抓狂了。。。

我看了一下我的中断,中断里面没加什么耗时的程序,都是只有一点点简单的减法。为了省时间我连回调函数都关掉了........




最后,发现是发送红外信号时,板子上的红外接收头收到了刚发送的信号,(板子有发送也有接收)进入了接收中断。然后就不用说了。。。。。

非常感谢您的热心回复。

页: 1 [2] 3
查看完整版本: STM32F030 PWM延迟严重,求指点迷津