当前位置:网站首页>Stm32f103c8t6 realize breathing lamp code

Stm32f103c8t6 realize breathing lamp code

2022-06-26 16:33:00 Related to the sun_

PWM.h

#ifndef __PWM_H
#define __PWM_H

void PWM_Init();
void PWM_SetCompare1(uint16_t Compare);

#endif

PWM.c

#include "stm32f10x.h" // Device header

void PWM_Init()
{
    
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  // Turn on timer 2
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);// passageway 2 Clock enable function 
	
	GPIO_InitTypeDef GPIO_InitStructure; 				// Definition GPIO Initializing structure variables 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	// Set up GPIO For push-pull output mode 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;			// Set up  P1 P2
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	// The speed is set to  50MHz
	GPIO_Init(GPIOA, &GPIO_InitStructure);				// Proceed according to the above parameters  GPIO The initialization 
	
	TIM_InternalClockConfig(TIM2);//TIM The time base unit of is controlled by the internal clock 
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR  The value of the automatic reloader 
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC  Prescaler value   Yes 72M(720000000) Conduct  7200 frequency division   namely 10K At different frequencies   meter 10000 Number  1s Time for 
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;// Repeat counter value 
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure); 
	 	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;// Output polarity selection 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;// Output status enable 
	TIM_OCInitStructure.TIM_Pulse = 10;//CCR
	TIM_OC1Init(TIM2,&TIM_OCInitStructure);
	
	
	TIM_Cmd(TIM2,ENABLE);
}


void PWM_SetCompare1(uint16_t Compare)// Constant adjustment CCR, Achieve breathing lamp effect 
{
    
	TIM_SetCompare1(TIM2, Compare);
}

main.c

#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "OLED.H"
#include "PWM.H"

uint8_t i;

int main(void)
{
    
	OLED_Init();
	PWM_Init();
	
	while (1)
	{
    
		for ( i = 0; i <= 100; i++)
		{
    
			PWM_SetCompare1(i);
			Delay_ms(10);// Delay , Otherwise it will be too fast 
		}	
		for ( i = 0; i <= 100; i++)
		{
    
			PWM_SetCompare1(100 - i);
			Delay_ms(10);
		}
	}
}

Secretly trying , Grow slowly

原网站

版权声明
本文为[Related to the sun_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261624027174.html