赞
踩
##新建.h文件commontim.h
#ifndef COMMONTIM_H
#define COMMONTIM_H
#include “stm32f10x.h”
void CommonTIM3_Config(void);
void TIM3_SetCompareValue(uint8_t B,uint8_t G,uint8_t R);
#endif
#include “commontim.h”
void CommonTIM3_Config(void){
GPIO_InitTypeDef GPIO_InitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; //打开时钟---GPIOC TIM3 AFIO RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 初始化GPIO PIN6 7 8 复用推挽输出 50hz GPIO_InitStruct.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8; GPIO_InitStruct.GPIO_Mode =GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz; GPIO_Init(GPIOC,&GPIO_InitStruct); //TIM3完全重映射 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //初始化TIM3 ARR-255 预分频器的值-1-1 向上计数 一分? TIM_TimeBaseInitStruct.TIM_ClockDivision =TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode =TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period =255; TIM_TimeBaseInitStruct.TIM_Prescaler =1-1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); //TMI3的通道初始化 ccr的值-0 低电平 PWM1模式 通道1 2 3 TIM_OCInitStruct.TIM_OCMode =TIM_OCMode_PWM1; TIM_OCInitStruct.TIM_OCPolarity =TIM_OCPolarity_Low; TIM_OCInitStruct.TIM_Pulse =0; TIM_OCInitStruct.TIM_OutputState =TIM_OutputState_Enable; TIM_OC1Init(TIM3, &TIM_OCInitStruct); TIM_OC2Init(TIM3, &TIM_OCInitStruct); TIM_OC3Init(TIM3, &TIM_OCInitStruct); //使能ARR的预装载寄存器 TIM_ARRPreloadConfig(TIM3,ENABLE); //使能通道1,2,3的预装载寄存器 TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); //使能定时器 TIM_Cmd(TIM3,ENABLE); //写一个设置CRR1 2 3 的函数
}
void TIM3_SetCompareValue(uint8_t B,uint8_t G,uint8_t R){
TIM_SetCompare1(TIM3,B);
TIM_SetCompare2(TIM3,G);
TIM_SetCompare3(TIM3,R);
}
##编写主函数
#include “commontim.h”
uint8_t i;
uint8_t j;
int main(void)
{
Systick_Config(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
CommonTIM3_Config();
while(1){
//LET_CTRL("SEDG","OPEN");
//呼吸灯
for(i=0;i<255;i++){
TIM3_SetCompareValue((uint8_t)i,(uint8_t)i,(uint8_t)i);
Systick_DelayNms(3);
}
for(j=i;j>0;j--){
TIM3_SetCompareValue((uint8_t)j,(uint8_t)j,(uint8_t)j);
Systick_DelayNms(5);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。