当前位置:网站首页>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. 寻找两个正序数组的中位数
- SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)
- 1-12Vmware新增SSH功能
- mha 切换(操作流程建议)
- 6 custom layer
- 11 introduction to CNN
- Natural language inference with attention and fine tuning Bert pytorch
- 股票开户优惠链接,我如何才能得到?在线开户安全么?
- Learn about common functional interfaces
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
猜你喜欢

Angel 3.2.0 new version released! Figure the computing power is strengthened again

IAR工程适配GD32芯片

1-12vmware adds SSH function

Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system

Net based on girdview control to delete and edit row data

当一个程序员一天被打扰 10 次,后果很惊人!

What is the process of switching C # read / write files from user mode to kernel mode?

3. Keras version model training

11 introduction to CNN

【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
随机推荐
Pybullet robot simulation environment construction 5 Robot pose visualization
用Attention和微调BERT进行自然语言推断-PyTorch
"C language" question set of ⑩
C. Inversion Graph
Failed to upload hyperf framework using alicloud OSS
Data analysis - numpy quick start
【小5聊】毕业8年,一直在追梦的路上
Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock
SAP OData 开发教程 - 从入门到提高(包含 SEGW, RAP 和 CDP)
IAR工程适配GD32芯片
理想路径问题
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged
架构实战营毕业设计
Ideal path problem
Codeforces Round #802 (Div. 2)
Redis的ACID
[force deduction question] two point search: 4 Find the median of two positive arrays
Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit