当前位置:网站首页>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. 寻找两个正序数组的中位数
- 股票开户优惠链接,我如何才能得到?在线开户安全么?
- JS tutorial - printing stickers / labels using the electronjs desktop application
- MHA switching (recommended operation process)
- Cuckoo filter for Chang'an chain transaction
- C. Inversion Graph
- 数据分析----numpy快速入门
- Develop operator based on kubebuilder (for getting started)
- 李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...
- 理想路径问题
猜你喜欢

Oilfield exploration problems

『C语言』题集 of ⑩

理想路径问题

神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读

stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题

Net based on girdview control to delete and edit row data

【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
Practice of federal learning in Tencent micro vision advertising

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

牛客小白月赛50
随机推荐
Cuckoo filter for Chang'an chain transaction
【时间复杂度和空间复杂度】
请指教同花顺软件究竟是什么?网上开户是否安全么?
Net基于girdview控件实现删除与编辑行数据
Cloud platform monitoring system based on stm32+ Huawei cloud IOT design
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
pybullet机器人仿真环境搭建 5.机器人位姿可视化
1-12vmware adds SSH function
JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
知道这几个命令让你掌握Shell自带工具
Cookie和Session详解
Codeforces Round #802 (Div. 2)
6 custom layer
【从删库到跑路】JDBC 完结篇(一天学完系列!!学完赶紧跑!)
R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
How to implement interface current limiting?
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
基于STM32+华为云IOT设计的云平台监控系统
[learn FPGA programming from scratch -46]: Vision - development and technological progress of integrated circuits