当前位置:网站首页>STM32F103C8T6实现呼吸灯代码
STM32F103C8T6实现呼吸灯代码
2022-06-26 16:30:00 【与太阳有关_】
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); //开启定时器2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //设置 P1 P2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
TIM_InternalClockConfig(TIM2);//TIM的时基单元由内部时钟控制
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 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC 预分频器的值 对72M(720000000)进行 7200分频 即10K的频率下 计10000个数 1s的时间
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器的值
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;//输出极性选择
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出状态使能
TIM_OCInitStructure.TIM_Pulse = 10;//CCR
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare1(uint16_t Compare)//不断调整CCR,实现呼吸灯效果
{
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);//延迟一下,不然会太快了
}
for ( i = 0; i <= 100; i++)
{
PWM_SetCompare1(100 - i);
Delay_ms(10);
}
}
}
偷偷努力,慢慢成长
边栏推荐
- 4 custom model training
- Ideal path problem
- 100+ data science interview questions and answers Summary - basic knowledge and data analysis
- Net基于girdview控件实现删除与编辑行数据
- 网页课程设计大作业——华山旅游网
- R language plotly visualization: Violin graph, multi category variable violin graph, grouped violin graph, split grouped violin graph, two groups of data in each violin graph, each group accounts for
- 首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
- 10 tf. data
- SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
- 【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
猜你喜欢

Make up the weakness - Open Source im project openim about initialization / login / friend interface document introduction
![[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul](/img/26/c0c8a406ff4ffe0ae37d277f730bd0.png)
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul

大话领域驱动设计——表示层及其他

Arduino UNO + DS1302简单获取时间并串口打印

国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知

【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃

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

# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍

Structure the graduation project of actual combat camp

1-12Vmware新增SSH功能
随机推荐
JS教程之 使用 Electron.JS 构建原生桌面应用程序乒乓游戏
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
JS tutorial electron JS is a good tool for designing powerful multi platform desktop applications
牛客小白月赛50
100+数据科学面试问题和答案总结 - 基础知识和数据分析
Hyperf框架使用阿里云OSS上传失败
MHA switching (recommended operation process)
【力扣刷题】单调栈:84. 柱状图中最大的矩形
油田勘探问题
I regard it as a dry product with a monthly income of more than 30000 yuan for sidelines and more than 10000 yuan for novices!
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
当一个程序员一天被打扰 10 次,后果很惊人!
R language uses cor function to calculate the correlation matrix for correlation analysis, uses corrgram package to visualize the correlation matrix, reorders rows and columns using principal componen
[207] several possible causes of Apache crash
100+ data science interview questions and answers Summary - basic knowledge and data analysis
7 user defined loss function
Big talk Domain Driven Design -- presentation layer and others
固件供应链公司Binarly获得WestWave Capital和Acrobator Ventures的360万美元投资
IAR工程适配GD32芯片
心情不好,我就这样写代码