大学生
最后登录1970-1-1
在线时间 小时
注册时间2017-3-3
|
楼主 |
发表于 2017-3-17 20:40:01
|
显示全部楼层
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_usart1.h"
#include "bsp_AdvanceTim.h"
int main()
{
uint8_t ch;
//初始化USART 配置为模式115200 8-N-1,中断接收
USARTx_Config();
//高级定时器初始化
General_TIM_GPIO_Config();
while(1)
{
ch = getchar();
printf("ch= %c \n",ch);
switch(ch)
{
case '1' :General_TIM_Mode_Config(General_TIM_PERIOD1,General_TIM_PULSE1) ;
break;
case '2' :General_TIM_Mode_Config(General_TIM_PERIOD2,General_TIM_PULSE2) ;
break;
case '3' :General_TIM_Mode_Config(General_TIM_PERIOD3,General_TIM_PULSE3) ;
break;
case '4' :General_TIM_Mode_Config(General_TIM_PERIOD4,General_TIM_PULSE4);
break;
default:General_TIM_Mode_Config(General_TIM_PERIOD5,General_TIM_PULSE5);
break;
}
}
}
#ifndef _BSP_ADVANCETIM_H
#define _BSP_ADVANCETIM_H
#include "stm32f10x.h"
#define General_TIM TIM3
#define General_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd
#define General_TIM_CLK RCC_APB1Periph_TIM3
// PWM 信号的频率 F = TIM_CLK/{(ARR+1)*(PSC+1)}
//PWM频率为100KHZ
#define General_TIM_PERIOD1 (80-1)
//PWM频率为200KHZ
#define General_TIM_PERIOD2 (40-1)
//PWM频率为500KHZ
#define General_TIM_PERIOD3 (16-1)
//PWM频率为1MHZ
#define General_TIM_PERIOD4 (8-1)
//PWM频率为2MHZ
#define General_TIM_PERIOD5 (4-1)
#define General_TIM_PSC (9-1)
//频率为100KHZ,占空比为30%
#define General_TIM_PULSE1 24
//频率为200KHZ,占空比为20%
#define General_TIM_PULSE2 8
//频率为500KHZ,占空比为50%
#define General_TIM_PULSE3 8
//频率为1MHZ,占空比为40%
#define General_TIM_PULSE4 3.2
//频率为2MHZ,占空比为50%
#define General_TIM_PULSE5 2
#define General_TIM_IRQ TIM3_UP_IRQn
#define General_TIM_IRQHandler TIM3_UP_IRQHandler
// TIM3 输出比较通道
#define General_TIM_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
#define General_TIM_CH1_PORT GPIOA
#define General_TIM_CH1_PIN GPIO_Pin_6
//#define General_TIM_CH2_GPIO_CLK RCC_APB2Periph_GPIOA
//#define General_TIM_CH2_PORT GPIOA
//#define General_TIM_CH2_PIN GPIO_Pin_7
//#define General_TIM_CH3_GPIO_CLK RCC_APB2Periph_GPIOB
//#define General_TIM_CH3_PORT GPIOB
//#define General_TIM_CH3_PIN GPIO_Pin_0
//#define General_TIM_CH4_GPIO_CLK RCC_APB2Periph_GPIOB
//#define General_TIM_CH4_PORT GPIOB
//#define General_TIM_CH4_PIN GPIO_Pin_1
void General_TIM_GPIO_Config(void);
void General_TIM_Mode_Config(uint8_t General_TIM_PERIODx,uint8_t General_TIM_PULSEx);
#endif
#include "bsp_AdvanceTim.h"
//定时器复用功能引脚初始化
void General_TIM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 输出比较通道 GPIO 初始化
RCC_APB2PeriphClockCmd(General_TIM_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = General_TIM_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(General_TIM_CH1_PORT, &GPIO_InitStructure);
// RCC_APB2PeriphClockCmd(General_TIM_CH2_GPIO_CLK, ENABLE);
// GPIO_InitStructure.GPIO_Pin = General_TIM_CH2_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(General_TIM_CH2_PORT, &GPIO_InitStructure);
//
// RCC_APB2PeriphClockCmd(General_TIM_CH3_GPIO_CLK, ENABLE);
// GPIO_InitStructure.GPIO_Pin = General_TIM_CH3_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(General_TIM_CH3_PORT, &GPIO_InitStructure);
//
// RCC_APB2PeriphClockCmd(General_TIM_CH4_GPIO_CLK, ENABLE);
// GPIO_InitStructure.GPIO_Pin = General_TIM_CH4_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(General_TIM_CH4_PORT, &GPIO_InitStructure);
}
//定时器模式配置
//C99支持任意地方定义变量,C11 这个是最新的C语言标准
void General_TIM_Mode_Config(uint8_t General_TIM_PERIODx,uint8_t General_TIM_PULSEx)
{
// 开启定时器时钟,即内部时钟CK_INT=72M
General_TIM_APBxClock_FUN(General_TIM_CLK,ENABLE);
/*--------------------时基结构体初始化-------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = General_TIM_PERIODx ;
// 驱动CNT 计数器的时钟 = Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_Prescaler= General_TIM_PSC;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
// 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(General_TIM, &TIM_TimeBaseStructure);
/*--------------------输出比较结构体初始化-------------------*/
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置为PWM 模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// 输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// 设置占空比大小
TIM_OCInitStructure.TIM_Pulse = General_TIM_PULSEx;
// 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//对输出通道初始化
TIM_OC1Init(General_TIM, &TIM_OCInitStructure);
//使能TIMX在CCR1上的预装载寄存器
TIM_OC1PreloadConfig(General_TIM, TIM_OCPreload_Enable);
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_Pulse = General_TIM_PULSE2;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OC2Init(General_TIM, &TIM_OCInitStructure);
// TIM_OC2PreloadConfig(General_TIM, TIM_OCPreload_Enable);
//
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_Pulse = General_TIM_PULSE3;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OC3Init(General_TIM, &TIM_OCInitStructure);
// TIM_OC3PreloadConfig(General_TIM, TIM_OCPreload_Enable);
//
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// TIM_OCInitStructure.TIM_Pulse = General_TIM_PULSE4;
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OC4Init(General_TIM, &TIM_OCInitStructure);
// TIM_OC4PreloadConfig(General_TIM, TIM_OCPreload_Enable);
// 使能计数器
TIM_Cmd(General_TIM, ENABLE);
}
火哥,我发给你看看,这样子行不行?
|
|