点亮第一盏LED灯,点亮另一片世界

    xiaoxiao2023-03-24  4

    千里之行,始于足下

    main.c

    #include "led.h" #include "delay.h" int main() { delay_init(); LED_Init(); /* // 库函数版本; while (1) { GPIO_SetBits(GPIOB, GPIO_Pin_5); GPIO_SetBits(GPIOE, GPIO_Pin_5); delay_ms(300); GPIO_ResetBits(GPIOB, GPIO_Pin_5); GPIO_ResetBits(GPIOE, GPIO_Pin_5); delay_ms(300); } */ /* // 寄存器版本; ODR(输出寄存器) while(1) { GPIOB->ODR |= 1<<5; GPIOE->ODR |= 1<<5; delay_ms(3000); GPIOB->ODR = ~(1<<5); GPIOE->ODR = ~(1<<5); delay_ms(3000); } */ // BRR寄存器,BSRR寄存器; while (1) { GPIOB->BSRR = GPIO_Pin_5; GPIOE->BSRR = GPIO_Pin_5; delay_ms(100); GPIOB->BRR = GPIO_Pin_5; GPIOE->BRR = GPIO_Pin_5; delay_ms(100); } /* // 位带操作; while(1) { PBout(5) = 1; PEout(5) = 1; delay_ms(300); PBout(5) = 0; PEout(5) = 0; delay_ms(300); } */ }

    led.h

    #ifndef __LED_H #define __LED_H #include "sys.h" // LED端口定义; #define LED0 PBout(5) #define LED1 PEout(5) void LED_Init(void); #endif

    led.c

    #include "led.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" void LED_Init(void) { // 寄存器操作; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); // LED0-->PB.5 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_5); // PB.5 输出高电平; // LED1-->PE.5 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_SetBits(GPIOE, GPIO_Pin_5); // PE.5 输出低电平; /* // 位带操作; RCC->APB2ENR |= 1 << 3; RCC->APB2ENR |= 1 << 6; // LED0-->PB.5 GPIOB->CRL &= 0xFF0FFFFF; // 对应位清零; GPIOB->CRL |= 0x00300000; // 00 通用推挽输出,11输出最大速度为50MHz 0011 = 3; GPIOB->ODR |= 1 <<5; // PB.5 输出高电平; // LED1-->PE.5 GPIOE->CRL &= 0xFF0FFFFF; // 对应位清零; GPIOE->CRL |= 0x00300000; // 00 通用推挽输出,11输出最大速度为50MHz 0011 = 3; GPIOE->ODR |= 1 << 5; // PE.5 输出高电平; */ }

    本人刚收到正点原子的F103精英版,边看边学,希望前辈们多多指教。

    转载请注明原文地址: https://ju.6miu.com/read-1202225.html
    最新回复(0)