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

发布时间 2023-11-04 13:14:45作者: 上够了逼班的老王

代码部分

#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