我们从蓝桥杯嵌入式的资源包里面找到原理图,上图就是我们led的原理图。从上图我们可以看到我们要导通led就必须使我们Q1-Q7进来的是低电压,也就是0。也就是HD0-HD7输0进来led才亮,
同时我们也要打开寄存器,既让NLE为1.
我们从上图J2找到N1和HD0-HD7所对应的引角分别是J1的PD2和PC8-PC15,所以我们要点亮led就要PC8-PC15的引角等于0,同时PD2为1.配置在如下。E为锁存控制端,OE为使能端,1D~8D为数据输入端,1Q~8Q为数据输出端。当LE为高时,Q输出将随数据D输入而变;当LE为低时,将输出锁存在已建立的数据电平上。锁存器的输出控制不影响锁存器的内部工作,即老数据可以保持,甚至当输出被关闭时,新的数据也可以置入。
//点亮led
#include"led.h"
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
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_2MHz;//传送速度
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//模式,我们选择的是推挽输出
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStruct);
}
/*输入输出,模式
(1)GPIO_Mode_IN_FLOATING // 浮空输入
(2)GPIO_Mode_IPU // 上拉输入
(3)GPIO_Mode_IPD // 下拉输入
(4)GPIO_Mode_AIN // 模拟输入
(5)GPIO_Mode_Out_OD // 开漏输出
(6)GPIO_Mode_AF_OD // 开漏复用输出
(7)GPIO_Mode_Out_PP // 推挽输出
(8)GPIO_Mode_AF_PP // 复用推挽输出
*/
led的初始化我们就配置好了相关的代码可以直接去官方给的资源包6-STM32固件库代码V3.5版>stm32f10x_stdperiph_lib>STM32F10x_StdPeriph_Lib_V3.5.0>Project>STM32F10x_StdPeriph_Examples>GPIO>IOToggle>main里面复制.下面我们进行led的点亮程序。
void LED_Disp(unsigned char ucLed)
{
GPIO_Write(GPIOC, ~ucLed << 8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
/* 这三个函数的定义都下面简简单单看看就明白了
* @brief Writes data to the specified GPIO data port.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param PortVal: specifies the value to be written to the port output data register.
* @retval None
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
GPIOx->ODR = PortVal;
}
* @brief Sets the selected data port bits.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bits to be written.
* This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
* @retval None
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
* @brief Clears the selected data port bits.
* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.
* @param GPIO_Pin: specifies the port bits to be written.
* This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
* @retval None
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = GPIO_Pin;
}
*/