本帖最后由 konway 于 2017-4-25 16:49 编辑
用STM32F030的TIM3做38K红外发射
要实现的功能很简单,
要发射高电平1,就把PWM口的pwm置为低电平;
要发低电平0,就在PWM口发送38K的方波。
现在碰到个问题
停止PWM后,想隔2ms再发送pwm,时间就会延迟5.6ms才发送。
只要设的时间小于5.6ms,低电平的时间就会是5.6ms。
今天发现把TIM3->CCR3=0;
改为
TIM3->CCR3=1;
时间间隔就是对的,可是这不能用啊。
求正确的解决办法。
附代码。
//TM3初始化:这部分由CUBE MX生成,实测为38KHz
- 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 = 209;
- htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- 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 = 0;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&htim3);
- }
复制代码 //开关PWM波形
- IR_send_Count_time=40;//4ms 低电平
- TIM3->CCR3=70;//IR发低电平
- while(IR_send_Count_time);//<span style="font-size: 13.63636302948px; line-height: 19.0909080505371px;">IR_send_Count_time在定时器中断里减,每100us减1</span>
- IR_send_Count_time=40;//4ms 高电平
- TIM3->CCR3=0;//IR发低电平
- while(IR_send_Count_time);
- IR_send_Count_time=40;//4ms 低电平
- TIM3->CCR3=70;//IR发低电平
- while(IR_send_Count_time);
- IR_send_Count_time=40;//4ms 高电平
- TIM3->CCR3=0;//IR发低电平
- while(IR_send_Count_time);
复制代码
|
******************************************************************************
* 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[2];
uint8_t compensate;
uint8_t User_code[3]={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[2];
uint8_t i, j;
usercode[0]=dat[0];
usercode[1]=dat[1];
for(j=0; j<2; j++)
{
for(i=0; i<8; i++)
{
if(usercode[j]&0x80)
Bit1_Encode_SignalGenerate();
else
Bit0_Encode_SignalGenerate();
usercode[j]=usercode[j]<<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[0]=0xd2;
controlcode[0]=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 */
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @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****/
* 函数功能: 通用定时器初始化并配置通道PWM输出
* 输入参数: 无
* 返 回 值: 无
* 说 明: 无
*/
void GENERAL_TIMx_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htimx.Instance = GENERAL_TIMx;
htimx.Init.Prescaler = GENERAL_TIM_PRESCALER;
htimx.Init.CounterMode = TIM_COUNTERMODE_UP;
htimx.Init.Period = GENERAL_TIM_PERIOD;
htimx.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htimx);
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htimx, &sClockSourceConfig);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htimx, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = GENERAL_TIM_CH1_PULSE;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htimx, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_MspPostInit(&htimx);
}
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(&htimx, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htimx, 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(45);
}
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 Usercode_Encode_SignalGenerate(uint8_t * dat)
{
uint8_t usercode[3];
uint8_t i, j;
usercode[0]=dat[0];
usercode[1]=dat[1];
usercode[2]=dat[2];
for(j=0; j<3; j++)
{
for(i=0; i<8; i++)
{
if(usercode[j]&0x80)
Bit1_Encode_SignalGenerate();
else
Bit0_Encode_SignalGenerate();
usercode[j]=usercode[j]<<1;
}
}
}
void Controlcode_Encode_SignalGenerate(uint8_t * dat)
{
uint8_t i, j;
for(j=0; j<2; j++)
{
for(i=0; i<8; i++)
{
if(dat[j]&0x80)
Bit1_Encode_SignalGenerate();
else
Bit0_Encode_SignalGenerate();
dat[j]=dat[j]<<1;
}
}
}
#define GENERAL_TIM_RCC_CLK_ENABLE() __HAL_RCC_TIM3_CLK_ENABLE()
#define GENERAL_TIM_RCC_CLK_DISABLE() __HAL_RCC_TIM3_CLK_DISABLE()
#define GENERAL_TIM_GPIO_RCC_CLK_ENABLE() {__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();}
#define GENERAL_TIM_CH1_PORT GPIOA
#define GENERAL_TIM_CH1_PIN GPIO_PIN_6
#define GENERAL_TIM_CH2_PORT GPIOA
#define GENERAL_TIM_CH2_PIN GPIO_PIN_7
#define GENERAL_TIM_CH3_PORT GPIOB
#define GENERAL_TIM_CH3_PIN GPIO_PIN_0
#define GENERAL_TIM_CH4_PORT GPIOB
#define GENERAL_TIM_CH4_PIN GPIO_PIN_1
#define GENERAL_TIM_CH1_AF GPIO_AF1_TIM3
建议你用HAL库的函数来实现
不是用寄存器更加直接吗?
请教,是用哪个函数来实现呢?
我用过这两个,是不行的,关闭PWM后低电平电压漂移,而且也一样没有解决延迟问题
HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel);
應該不是。
如果是電路的問題,那TIM3->CCR3設爲1應該也會有延遲。
å®æ¶å¨é ç½®
æ¶éé ç½®
remoter.rar
2017-4-26 10:57 上传
点击文件名下载附件
下载积分: ST金币 -11.17 KB, 下载次数: 6, 下载积分: ST金币 -1
用你给的函数做PWM赋值
结果还是一样的
试着改系统时钟为48M(原来是8M,为了省电),也还是没用。
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_3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
}
HAL_TIM_PWM_Start(&htimx,TIM_CHANNEL_2);
打开PWM,示波器上就有波形输出,不能关!
user_pwm_setvalue(0); 用这个关,实际上PWM没关给它低电平。
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
另外我也是内部晶振