STM32F103(纯代码)-点亮LED灯

1.Led.h

#ifndef __Led_H
#define __Led_H

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

//LED0 GPIOÅäÖÃ
#define LED0_GPIO_CLK RCC_APB2Periph_GPIOB //GPIOBʱÖÓ
#define	LED0_GPIO_PORT GPIOB //GPIOB
#define LED0_GPIO_Mode GPIO_Mode_Out_PP //ÍÆÍìÊä³ö
#define LED0_GPIO_Pin GPIO_Pin_5 //Pin_5

//LED1 GPIOÅäÖÃ
#define LED1_GPIO_CLK RCC_APB2Periph_GPIOE //GPIOEʱÖÓ
#define LED1_GPIO_PORT GPIOE //GPIOE
#define LED1_GPIO_Mode GPIO_Mode_Out_PP //ÍÆÍìÊä³ö
#define LED1_GPIO_Pin GPIO_Pin_5 //Pin_5

//
#define LED0 PBout(5)// PB5
#define LED1 PEout(5)// PE5	

void Led_Init(void);

#endif

2.Led.c

#include "Led.h"


void Led_Init(void)
{
	RCC_APB2PeriphClockCmd(LED0_GPIO_CLK | LED1_GPIO_CLK,ENABLE); //ʹÄÜGPIOB GPIOE¶Ë¿ÚʱÖÓ
	
	//PB5 PE5 ¶Ë¿Ú³õʼ»¯ ÍÆÍìÊä³ö 50MHz
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=LED0_GPIO_Mode;
	GPIO_InitStructure.GPIO_Pin=LED0_GPIO_Pin;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	
	GPIO_Init(LED0_GPIO_PORT,&GPIO_InitStructure);

	
	GPIO_InitStructure.GPIO_Mode=LED1_GPIO_Mode;
	GPIO_InitStructure.GPIO_Pin=LED1_GPIO_Pin;
	GPIO_Init(LED1_GPIO_PORT,&GPIO_InitStructure);
	
	GPIO_SetBits(LED0_GPIO_PORT,LED0_GPIO_Pin);
	GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_Pin);
	
//	GPIO_ResetBits(LED0_GPIO_PORT,LED0_GPIO_Pin);
//	GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_Pin);
}

3.main

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

#include "Led.h"	


int main(void)
{
	
	Led_Init();

	while(1)
	{
		LED0 = 0;
		LED1 = 0;
		
	}	
	
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要连接STM32F103C8T6的串口和LED。将串口的TX端口连接到LED的正极,RX端口连接到LED的负极。接着,我们需要编写程序来控制串口发送数据以点亮LED。 在程序中,我们需要先初始化串口和LED。选择一个合适的波特率并打开串口,接着我们通过GPIO口控制LED的引脚输出高电平或低电平来点亮或熄灭LED。然后我们可以通过串口发送数据,当数据被发送时,LED的状态将随之改变。 下面是一个简单的代码示例,可以点亮一个连接到PA8引脚的LED: \#include "stm32f10x.h" \#include "stm32f10x_gpio.h" \#include "stm32f10x_rcc.h" \#include "stm32f10x_usart.h" void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //LED引脚配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); } void UART_Configuration(void) { USART_InitTypeDef USART_InitStructure; //使能USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //USART1配置 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } int main() { GPIO_Configuration(); UART_Configuration(); while(1) { //发送字符'A' USART_SendData(USART1, 'A'); //等待发送完成 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //控制LED亮或灭 GPIO_WriteBit(GPIOA, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_8))); //延时 Delay(10000); } } 需要注意的是,在使用串口控制LED时,我们还需要注意GPIO的初始化和串口的配置。同时,由于串口发送指令需要一定的延时,因此需要在程序中加入延时函数来控制发送间隔。 最后,将编写好的代码烧录到MCU中,连接电源即可点亮LED

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值