Featured image of post 点亮一个led

点亮一个led

点亮一个led

所需代码 参考来源网络
延时函数参考:

Delay.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
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);
	}
} 

Delay.h

1
2
3
4
5
6
7
#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);
#endif

LED.c

用的PIN脚是PC13 可根据自己的情况修改

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "stm32f10x.h"       // Device header

// LED 初始化 
void LED_Init(void){

	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStruct);
	// 初始化置1 
	GPIO_SetBits(GPIOC,GPIO_Pin_13);
}

void LED1_ON(void){
	GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}
void LED1_OFF(void){
	GPIO_SetBits(GPIOC,GPIO_Pin_13);
}

// 高低电平反转 
void LED1_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_13)==0)
	{
		GPIO_SetBits(GPIOC,GPIO_Pin_13);
	}
	else
	{
		GPIO_ResetBits(GPIOC,GPIO_Pin_13);
	}
}

LED.h

1
2
3
4
5
6
7
8
9
#ifndef __LED_H
#define __LED_H

void LED_Init(void);  // LED初始化
void LED1_ON(void);   // 
void LED1_OFF(void);
void LED1_Turn(void);

#endif

main.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "stm32f10x.h"  

#include "LED.h"
#include "Delay.h"

int main(void)
{
	LED_Init(); // 初始LED

	while (1)
	{
		LED1_OFF();
		Delay_ms(500);
		LED1_ON();
		Delay_ms(500);

	}
	
}

编译 下载
亮了
参考链接1:

最后更新于 Mar 21, 2025 15:42 +0800
世界是你们
使用 Hugo 构建
主题 StackJimmy 设计