STM32基础-LED
本文最后更新于183 天前,其中的信息可能已经过时,如有错误请发送邮件到 2871065713@qq.com

1、点亮两颗LED

在main.c中,编写以下代码:

/* Includes */
#include "stm32f10x.h"

int main(void)
{
    // 配置
    //定义一个 GPIO 初始化结构体,用来一次性描述“要配置哪些引脚、什么模式、什么速率”
    GPIO_InitTypeDef GPIO_InitStruct;

    //给 GPIOB 外设开时钟(APB2 总线)。没开时钟写 GPIO 寄存器不会生效,甚至可能 HardFault
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    
    //选择 PB5;
    //模式为 推挽输出(Out_PP),适合驱动 LED;
    //速率 2 MHz:F1 的“速率”本质是输出驱动能力/翻转上限(2/10/50 MHz 三档),对点灯选 2 MHz 即可、功耗更低。
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

    //调库函数把以上设置写入寄存器:对于 F1,PB0–PB7 在 CRL,PB8–PB15 在 CRH;此处把 MODE5=10(2 MHz),CNF5=00(推挽)。
    GPIO_Init(GPIOB, &GPIO_InitStruct);

    return 0;
}

此代码作用是点亮LED0,因为LED0是接在B组GPIO的pin5,因此需要初始化B组GPIO,如果需要点亮LED1,查看原理图可知,LED0接在E组GPIO的pin5,因此需要初始化E组GPIO


#include "stm32f10x.h"

int main(void)
{
    // 配置
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOE, &GPIO_InitStruct);

    //只低电平点亮没用,闪烁逻辑有用
    GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET)
    GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET)
    return 0;
}

添加闪烁逻辑

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

void delay_ms(uint32_t ms){
	uint32_t count = 10000;
	
	while(ms --){
		count = 10000;
		while(count --);
	}	
}

int main(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOE, &GPIO_InitStruct);
	
	while(1){
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);
		delay_ms(500);
		GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET);
		delay_ms(500);
		GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);
		delay_ms(500);
		GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_SET);
		delay_ms(500);
	}
	return 0;
}

10-13控制寄存器

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

#define S_GPIO_BASE 0X40010C00
#define S_CRL S_GPIO_BASE + 0x00
#define S_ODR S_GPIO_BASE +0x0C

void delay_ms(uint32_t ms){
	uint32_t count = 10000;
	
	while(ms --){
		count = 10000;
		while(count --);
	}	
}
int main(void)
{
    uint32_t temreg = 0;

    /* GPIOB Periph clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    temreg = *((uint32_t*)(S_CRL));
    temreg &= 0xFF0FFFFF;
    temreg += 0x00200000;
    *((uint32_t*)(S_CRL)) = temreg;

    temreg = *((uint32_t*)(S_ODR));
    temreg &= 0x0EF;
    *((uint32_t*)(S_ODR)) = temreg;

    while(1);
}

//int main(void)
//{
//	//ÅäÖÃ
//	GPIO_InitTypeDef GPIO_InitStruct;
//	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//	
//	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
//	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
//	GPIO_Init(GPIOB, &GPIO_InitStruct);
//	
//	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
//	
//	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
//	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
//	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
//	GPIO_Init(GPIOE, &GPIO_InitStruct);
//	
//	while(1){
//		GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_RESET);
//		delay_ms(500);
//		GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_RESET);
//		delay_ms(500);
//		GPIO_WriteBit(GPIOB, GPIO_Pin_5, Bit_SET);
//		delay_ms(500);
//		GPIO_WriteBit(GPIOE, GPIO_Pin_5, Bit_SET);
//		delay_ms(500);
//	}
//	return 0;
//}
文末附加内容
暂无评论

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇