打印
[应用相关]

STM32PWM输出实现LED灯渐亮渐灭

[复制链接]
1917|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
huangcunxiake|  楼主 | 2017-1-13 23:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 huangcunxiake 于 2017-1-13 23:29 编辑

*说明:
*PA0:KEY1;PA1:KEY2;
*PA2:LED1;PA3:LED2;
*PA9:USART1_TX;PA10:USART1_RX
*/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"

void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM2_Configuration(void);
void delay_ms(u16 time);

int main()
{
    u8 led_fx=1;
    u16 led_dt=0;
   
    RCC_Configuration();
    GPIO_Configuration();
    TIM2_Configuration();
               
    while(1)
    {
      delay_ms(10);
      if(led_fx==1)
      {
        led_dt++;
      }else{
        led_dt--;
      }
      
      if(led_dt>100) led_fx=0;//可以和初始值一样
      if(led_dt==0)  led_fx=1;
      
      TIM_SetCompare3(TIM2,led_dt);//通道3没有重影像是PA2对应LED1
      TIM_SetCompare4(TIM2,led_dt);//通道4没有重影像是PA3对应LED2
    }
}

void RCC_Configuration(void)  
{   
  SystemInit();
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
  //GPIO_AFIODeInit();和下面相同效果,可以查看源码证明
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}

void GPIO_Configuration(void)  
{  
  GPIO_InitTypeDef GPIO_InitStructure;  
  
  //GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE);//端口映射函数
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//GPIO_Pin_3对应channel3,主函数改为:TIM_SetCompare3
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void TIM2_Configuration(void)  
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;
  
  TIM_DeInit(TIM2);
  TIM_InternalClockConfig(TIM2);
  //定时器初始化
  TIM_TimeBaseStructure.TIM_Period = 100-1;//初始值是100,2MHz=2000KHz,2MHz/100=0.02MHz,所有周期T=1/0.02MHz=50us,示波器显示周期应该为50us
  TIM_TimeBaseStructure.TIM_Prescaler = 36-1;//36倍分频,72MHz/36=2MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  
  //PWM初始化
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 50;//占空比值是50,所以占空比是:50/100=50%
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC3Init(TIM2,&TIM_OCInitStructure);
  
  TIM_OCInitStructure.TIM_Pulse = 20;
  TIM_OC4Init(TIM2,&TIM_OCInitStructure);
  //TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
  //TIM_ARRPreloadConfig(TIM2, ENABLE);
  TIM_Cmd(TIM2,ENABLE);
  TIM_CtrlPWMOutputs(TIM2,ENABLE);
}
void delay_ms(u16 time)   
{  
  u16 i=0;  
  while(time--)   
  {  
    i=12000;  
    while(i--);  
  }  
}  
</font>


沙发
huangcunxiake|  楼主 | 2017-1-13 23:31 | 只看该作者

Stm32共有11个定时器:

1.两个高级定时器:TIM1、TIM8-------------------------APB2

2.四个通用定时器:TIM2~TIM5-------------------------APB1

3.两个基本定时器:TIM6、TIM7-------------------------APB1

4.两个看门狗

5.一个系统嘀嗒定时器(SysTick)

/*
*说明:
*PA0:KEY1;PA1:KEY2;
*PA2:LED1;PA3:LED2;
*PA9:USART1_TX;PA10:USART1_RX
*/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"


void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM3_Configuration(void);
void NVIC_Configuration(void);


int main()
{
    SystemInit();
   
    RCC_Configuration();
    GPIO_Configuration();
    TIM3_Configuration();
    NVIC_Configuration();
                                                            
    while(1);
}


void RCC_Configuration(void)  
{   
  ////USART2和USART3都在在APB1上而USART1是在APB2上的
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);   
}


void GPIO_Configuration(void)  
{  
  GPIO_InitTypeDef GPIO_InitStructure;  
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}


void TIM3_Configuration(void)  
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  
  TIM_TimeBaseStructure.TIM_Period = 2000;//频率:72MHz  72000000/36000=2000
  TIM_TimeBaseStructure.TIM_Prescaler = 35999;//36000-1=35999
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );
  TIM_Cmd(TIM3,ENABLE);
}


void NVIC_Configuration(void)//配置中断优先级  
{  
  NVIC_InitTypeDef NVIC_InitStructure;  
   
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);  
   
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);  
}  


使用特权

评论回复
板凳
huangcunxiake|  楼主 | 2017-1-13 23:31 | 只看该作者
中断函数stm32f10x_it.c


#include "..\include\stm32f10x.h"
#include "..\include\stm32f10x_it.h"
#include "..\include\stm32f10x_gpio.h"
#include "..\include\stm32f10x_tim.h"

void TIM3_IRQHandler(void)
{
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

  if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)==Bit_RESET)
  {
    GPIO_SetBits(GPIOA,GPIO_Pin_2);
  }else{
    GPIO_ResetBits(GPIOA,GPIO_Pin_2);
  }
}


使用特权

评论回复
地板
huangcunxiake|  楼主 | 2017-1-13 23:34 | 只看该作者
中断函数声明头文件stm32f10x_it.h:
#ifndef __STM32F10x_IT_H
#define __STM32F10x_IT_H

/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */  
void NMIException(void);
void HardFaultException(void);
void MemManageException(void);
void BusFaultException(void);
void UsageFaultException(void);
void DebugMonitor(void);
void SVCHandler(void);
void PendSVC(void);
void SysTickHandler(void);
void WWDG_IRQHandler(void);
void PVD_IRQHandler(void);
void TAMPER_IRQHandler(void);
void RTC_IRQHandler(void);
void FLASH_IRQHandler(void);
void RCC_IRQHandler(void);
void EXTI0_IRQHandler(void);
void EXTI1_IRQHandler(void);
void EXTI2_IRQHandler(void);
void EXTI3_IRQHandler(void);
void EXTI4_IRQHandler(void);
void DMA1_Channel1_IRQHandler(void);
void DMA1_Channel2_IRQHandler(void);
void DMA1_Channel3_IRQHandler(void);
void DMA1_Channel4_IRQHandler(void);
void DMA1_Channel5_IRQHandler(void);
void DMA1_Channel6_IRQHandler(void);
void DMA1_Channel7_IRQHandler(void);
void ADC1_2_IRQHandler(void);
void USB_HP_CAN_TX_IRQHandler(void);
void USB_LP_CAN_RX0_IRQHandler(void);
void CAN_RX1_IRQHandler(void);
void CAN_SCE_IRQHandler(void);
void EXTI9_5_IRQHandler(void);
void TIM1_BRK_IRQHandler(void);
void TIM1_UP_IRQHandler(void);
void TIM1_TRG_COM_IRQHandler(void);
void TIM1_CC_IRQHandler(void);
void TIM2_IRQHandler(void);
void TIM3_IRQHandler(void);
void TIM4_IRQHandler(void);
void I2C1_EV_IRQHandler(void);
void I2C1_ER_IRQHandler(void);
void I2C2_EV_IRQHandler(void);
void I2C2_ER_IRQHandler(void);
void SPI1_IRQHandler(void);
void SPI2_IRQHandler(void);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
void EXTI15_10_IRQHandler(void);
void RTCAlarm_IRQHandler(void);
void USBWakeUp_IRQHandler(void);
void TIM8_BRK_IRQHandler(void);
void TIM8_UP_IRQHandler(void);
void TIM8_TRG_COM_IRQHandler(void);
void TIM8_CC_IRQHandler(void);
void ADC3_IRQHandler(void);
void FSMC_IRQHandler(void);
void SDIO_IRQHandler(void);
void TIM5_IRQHandler(void);
void SPI3_IRQHandler(void);
void UART4_IRQHandler(void);
void UART5_IRQHandler(void);
void TIM6_IRQHandler(void);
void TIM7_IRQHandler(void);
void DMA2_Channel1_IRQHandler(void);
void DMA2_Channel2_IRQHandler(void);
void DMA2_Channel3_IRQHandler(void);
void DMA2_Channel4_5_IRQHandler(void);
                                         
#endif /* __STM32F10x_IT_H */

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


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

213

主题

3552

帖子

11

粉丝