资源简介

此程序是通过STM32的TIM3定时器控制四通道PWM输出信号,代码是开源的, 下载不用积分,代码里有详细的注释。方便移植使用

资源截图

代码片段和文件信息

/**********************************************************************************
 * 文件名  :PWM_output.c
 * 描述    :PWM 应用函数库         
 * 实验平台:芯物联STM32核心板
 * 硬件连接:通道1为PA6 、 通道2为PA7 、 通道3为PB0 、 通道4为PB1  
 * 库版本  :ST_v3.5
**********************************************************************************/


#include “PWM_output.h“


TIM_TimebaseInitTypeDef  TIM_TimebaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;
uint16_t CCR1_Val = 4000; // (4000 / 8000) * 100% = 50%
uint16_t CCR2_Val = 3000; // (3000 / 8000) * 100% = 37.5%
uint16_t CCR3_Val = 2000; // (2000 / 8000) * 100% = 25%
uint16_t CCR4_Val = 1000; // (1000 / 8000) * 100% = 12.5%
uint16_t PrescalerValue = 0;


void RCC_Configuration(void);
void GPIO_Configuration(void);


/*******************************************************************************
* 函数名  : RCC_Configuration
* 描述    : RCC时钟配置函数
* 输入    : 无
* 输出    : 无
* 返回值  : 无
* 注意    : 无
*******************************************************************************/
void RCC_Configuration(void)
{
  /* TIM3 时钟使能 */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 ENABLE);

  /* GPIOA 与 GPIOB 时钟使能 */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO ENABLE);
}



/*******************************************************************************
* 函数名  : GPIO_Configuration
* 描述    : 管脚配置
* 输入    : 无
* 输出    : 无
* 返回值  : 无
* 注意    : 无
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOB &GPIO_InitStructure);

}


/*******************************************************************************
* 函数名  : PWM_Output_Init
* 描述    : PWM输出初始化函数
* 输入    : 无
* 输出    : 无
* 返回值  : 无
* 注意    : 无
*******************************************************************************/
void PWM_Output_Init(void)
{
  /* 时钟配置 */
  RCC_Configuration();

  /* 管脚配置 */
  GPIO_Configuration();

  /* -----------------------------------------------------------------------
    TIM3 配置: 输出 4 通道的 PWM 信号:
    TIM3 预分频系数计算:
     - Prescaler = (系统时钟 / TIM3 计数时钟) - 1
    默认使能的系统时钟为 72MHz

    TIM3 产生的频率为 36KHz: TIM3 Frequency = TIM3 计数时钟/(ARR + 1)
                                                  = 8 MHz / 8000 = 1 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
  ----------------------------------------------------------------------- */
  /* 预分

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        671  2015-05-16 14:48  四通道PWM输出实验\USER\JlinkSettings.ini

     文件        916  2015-05-28 21:59  四通道PWM输出实验\USER\main.c

     文件      72247  2014-06-15 22:25  四通道PWM输出实验\USER\Project.uvgui.admin

     文件      73552  2015-05-28 22:03  四通道PWM输出实验\USER\Project.uvgui.Administrator

     文件      73550  2015-05-28 21:59  四通道PWM输出实验\USER\Project.uvgui_Administrator.bak

     文件      19598  2015-05-28 22:03  四通道PWM输出实验\USER\Project.uvopt

     文件      20412  2015-05-28 21:48  四通道PWM输出实验\USER\Project.uvproj

     文件      10897  2015-05-28 22:03  四通道PWM输出实验\USER\Project_Project.dep

     文件      19596  2015-05-28 21:59  四通道PWM输出实验\USER\Project_uvopt.bak

     文件       3259  2011-04-04 19:03  四通道PWM输出实验\USER\stm32f10x_conf.h

     文件       4405  2011-04-04 19:03  四通道PWM输出实验\USER\stm32f10x_it.c

     文件       2086  2011-04-04 19:03  四通道PWM输出实验\USER\stm32f10x_it.h

     文件      19266  2015-05-17 16:39  四通道PWM输出实验\USER\Output\Project.hex

     文件        239  2015-05-28 22:03  四通道PWM输出实验\USER\Output\Project.plg

     文件       7046  2011-03-10 10:47  四通道PWM输出实验\LIB\src\misc.c

     文件      47201  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_adc.c

     文件       8463  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_bkp.c

     文件      45103  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_can.c

     文件      11656  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_cec.c

     文件       3347  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_crc.c

     文件      19087  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_dac.c

     文件       5149  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_dbgmcu.c

     文件      29607  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_dma.c

     文件       6959  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_exti.c

     文件      62551  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_flash.c

     文件      35484  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_fsmc.c

     文件      23221  2011-03-11 17:43  四通道PWM输出实验\LIB\src\stm32f10x_gpio.c

     文件      45781  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_i2c.c

     文件       4916  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_iwdg.c

     文件       8759  2011-03-10 10:47  四通道PWM输出实验\LIB\src\stm32f10x_pwr.c

............此处省略60个文件信息

评论

共有 条评论