目录
一、原理图:
由于开发板上的LED受 锁存器 的控制,在使用LED中需要将引脚 PD2 先置高。
如下图所示,PC8~PC15为LED的引用引脚。(将引脚置为低电平则LED为亮)
提醒:LED与LCD中存在冲突,在使用过程要注意(这里就不展开解析)
二、部分代码:
1.led.c:
#include "stm32f10x.h" //调用使用的头文件
#include "led.h"
void led_Init(void) // 初始化PC,PD
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);
//使能PC,PD的时钟
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12| GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
GPIO_Init(GPIOC,&GPIO_InitStruct);
}
void LED_Control(u16 led) //控制LED的函数
{
GPIO_ResetBits(GPIOC,led<<8); //左移8位
GPIO_SetBits(GPIOD,GPIO_Pin_2); //锁存器的使用(先置高电平)
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
提醒:a、锁存器一定先置1,然后在置0才能正常使用LED
2.led.h:
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
void led_Init(void);
void LED_Control(u16 led);
#endif
3.main.c:
#include "stm32f10x.h"
#include "lcd.h"
#include "stdio.h"
#include "led.h"
u32 TimingDelay = 0;
void Delay_Ms(u32 nTime);
int main(void)
{
SysTick_Config(SystemCoreClock/1000);
Delay_Ms(200);
STM3210B_LCD_Init();
led_Init();
LED_Control(0x01);
while(1);
}
void Delay_Ms(u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
对LED_Control()中的解析:
如下图所示:
由于0x01左移8位所得为0x0100,则使用0x01时就GPIO_Pin_8的使用
(若要使用GPIO_Pin_8则使用0x02)
<0x01即为0000 0000 0000 0001 移8位则为0x0100即为0000 0001 0000 0000>
总结:
这里对蓝桥杯嵌入式的点灯的主要代码做了部分描述,在入门期间要注意LED的锁存器(除蓝桥杯的开发板中,一般比较少见),
需要对移位等操作知识有所了解。
文章若出现什么问题,谢谢及时反馈。
点个赞在走吧!!!!