当前位置:   article > 正文

stm32实现呼吸灯_stm32呼吸灯程序

stm32呼吸灯程序

##新建.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

编写.c文件

#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 的函数
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

}
void TIM3_SetCompareValue(uint8_t B,uint8_t G,uint8_t R){

TIM_SetCompare1(TIM3,B);
TIM_SetCompare2(TIM3,G);
TIM_SetCompare3(TIM3,R);
  • 1
  • 2
  • 3

}

##编写主函数
#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);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/302249
推荐阅读
相关标签
  

闽ICP备14008679号