STM32 PWM控制LED流水灯 学习记录随笔

代码部分

#include "stm32f10x.h"                  // Device header
#include "Delay.h"


int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//启用系统寄存器时钟,使能GPIOC组,并启动
    GPIO_InitTypeDef GPIO_InitStructure;    //初始化GPIO_Init后需要定义一个结构体配置信息,根据stm32f10x_gpio.c文件要求需要定义
    /*
    以下三项定义与stm32f10x_gpio.h文件中选择
    Mode模式Pin引脚Speed速度即频率
    */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//OUT PP通用推挽输出
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;//注意Pin引脚选择
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    

    GPIO_Init(GPIOA,&GPIO_InitStructure);//初始化结构体配置信息,并指向上4列代码配置信息
while(1)
        {
            unsigned char i,j,temp;
////单个接口LED PWM,pin脚选择Pin_0
//                for(temp=0;temp<20;temp++)
//            {
//                for(i=0;i<3;i++)
//                {
//                GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//                Delay_ms(temp);
//                GPIO_SetBits(GPIOA,GPIO_Pin_0);
//                Delay_ms(20-temp);
//                }
//            }
//            for(temp=20;temp>0;temp--)
//            {
//                for(i=0;i<3;i++)
//                {
//                    GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//                    Delay_ms(temp);
//                    GPIO_SetBits(GPIOA,GPIO_Pin_0);
//                    Delay_ms(20-temp);
//                }
//            }            
////8位PWM流水LED灯 Pin脚Pin_All
for(j=0;j<8;j++)
            {
                for(temp=0;temp<20;temp++)
            {
                for(i=0;i<3;i++)
                {
                    GPIO_Write(GPIOA,~(0x0001<<j));
                    Delay_ms(temp);
                    GPIO_SetBits(GPIOA,GPIO_Pin_All);
                    Delay_ms(20-temp);
                }
            }
            for(temp=20;temp>0;temp--)
            {
                for(i=0;i<3;i++)
                {
                    GPIO_Write(GPIOA,~(0x0001<<j));
                    Delay_ms(temp);
                    GPIO_SetBits(GPIOA,GPIO_Pin_All);
                    Delay_ms(20-temp);
                }
            }
            }
        }
}
其中延时函数使用B站UP江协科技Delay延时函数

实验随笔视频:B站BV18u4y1a7Pt

 

posted @   上够了逼班的老王  阅读(119)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示