在线时间0 小时
UID332936
ST金币0
蝴蝶豆0
注册时间2011-12-7
新手上路
- 最后登录
- 1970-1-1
|
a0a.1 0b0c
/*这个是我自己写的四个通道的代码,我想通过通道2来控制PA7的led渐亮和渐暗,不过不知道为什么不行,跪求各位达人帮我看看,当然是有空的话。。。*/
#include "stm32f10x.h"
#include
#include
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t CCR1_Val = 300;
uint16_t CCR2_Val = 0;
uint16_t CCR3_Val = 750;
uint16_t CCR4_Val = 0;
uint16_t PrescalerValue = 0;
//void RCC_Configuration(void); //时钟配置
//void GPIO_Configuration(void); //管脚配置
void Delay (__IO uint32_t nCount); //延时函数
int main(void)
{
uint16_t dir=1;
RCC_open();
//GPIO_Configuration();
GPIO_open();
PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
TIM_TimeBaseStructure.TIM_Period = 900; //周期
//TIM_TimeBaseStructure.TIM_P.rescaler = PrescalerValue; //分频
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数模式,向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始TIM3
/*************************** 通道1 ********************************/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //写比较值(占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //置高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //选择1通道
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //一通道使能
/****************************** 通道2 ******************************/
/*PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
//TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/******************************* 通道3 *********************************/
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****************************** 通道4 *********************************/
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); //
TIM_Cmd(TIM3, ENABLE); //使能计数
while (1)
{
/*CCR3_Val++;
if(CCR3_Val==1000)CCR3_Val=0;
TIM_SetCompare3(TIM3,CCR3_Val); //占空比调节
Delay(0xFfff);
*/
if(dir)
CCR2_Val++;
else
CCR2_Val--;
if(CCR2_Val>300)
dir=0;
if(CCR2_Val==0)
dir=1;
TIM_SetCompare3(TIM3,CCR2_Val);
Delay(0xFfff); //为什么要延时这么久?
}
}
/*************************** 时钟配置 *******************************/
/*void RCC_Configuration(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //T3时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
//管脚时钟
} */
/************************** 管脚配置 ******************************/
/*void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = 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 = 0x7fff;
GPIO_Init(GPIOB, &GPIO_InitStructure);
} */
/************************** 延时函数 ******************************/
void Delay (__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
//子函数
#include
void GPIO_open(void)
{
GPIO_InitTypeDef GPIO_opening;
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable ,ENABLE);
/*GPIO_opening.GPIO_Pin = 0xffff;
GPIO_opening.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_opening.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_opening);
*/
GPIO_opening.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_6;
GPIO_opening.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_opening.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_opening);
}
#include
void RCC_open(void)
{
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
} |
|