浅浅看看stm32单片机

文章详细介绍了STM32单片机中如何使用GPIO配置输出和定时器实现不同级别的延时,包括毫秒和秒级别,以及RCC_APB2PeriphClockCmd用于时钟管理的部分。
摘要由CSDN通过智能技术生成

以下是我的stm32单片机的闪光灯代码:

void Delay_us(uint32_t xus)
{
    SysTick->LOAD = 72 * xus;                //设置定时器重装值
    SysTick->VAL = 0x00;                    //清空当前计数值
    SysTick->CTRL = 0x00000005;                //设置时钟源为HCLK,启动定时器
    while(!(SysTick->CTRL & 0x00010000));    //等待计数到0
    SysTick->CTRL = 0x00000004;                //关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
    while(xms--)
    {
        Delay_us(1000);
    }
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
    while(xs--)
    {
        Delay_ms(1000);
    }

ll

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

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE );
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    //GPIO_SetBits(GPIOC,GPIO_Pin_13);
//    GPIO_ResetBits(GPIOC,GPIO_Pin_13);
    

    while(1)
    {
        GPIO_Write(GPIOA,~0x0001);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0002);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0004);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0008);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0010);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0020);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0040);
        Delay_ms(500);
        GPIO_Write(GPIOA,~0x0080);
        Delay_ms(500);
    }
    
    
}

要了解GPIO的意义,理解时钟的作用方法。

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值