当前位置:网站首页>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
边栏推荐
- 知道这几个命令让你掌握Shell自带工具
- Several forms of buffer in circuit
- What is the process of switching C # read / write files from user mode to kernel mode?
- Hyperf框架使用阿里云OSS上传失败
- 最小二乘系统辨识课 中篇:递归最小二乘
- Structure the graduation project of actual combat camp
- 牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
- 【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
- Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction
猜你喜欢

MS|谢黎炜组发现混合益生菌制剂及其代谢产物可缓解结肠炎
Scala 基础 (二):变量和数据类型

r329(MAIX-II-A(M2A)资料汇总

JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序

In a bad mood, I just write code like this

Knowing these commands allows you to master shell's own tools

What does the inner structure of the neural network "alchemy furnace" look like? An interpretation of the thesis by the doctor of Oxford University

【力扣刷题】11.盛最多水的容器//42.接雨水

Learn about common functional interfaces

100+ data science interview questions and answers Summary - basic knowledge and data analysis
随机推荐
The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
Scala Foundation (2): variables et types de données
【207】Apache崩溃的几个很可能的原因,apache崩溃几个
Knowing these commands allows you to master shell's own tools
Redis migration (recommended operation process)
How can I get the stock account opening discount link? Is online account opening safe?
Data analysis - numpy quick start
When a programmer is disturbed 10 times a day, the consequences are amazing!
(DFS search) acwing 2005 horseshoe
Solution for filtering by special string of microservice
Redis migration (recommended operation process) 1
SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
Keepalived 实现 Redis AutoFailover (RedisHA)1
神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation
C# 读写文件从用户态切到内核态,到底是个什么流程?
Redis的ACID
[understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
基于STM32+华为云IOT设计的云平台监控系统
LeetCode 单周赛298,前三题