STM32F103点亮发光二极管

点亮PB0\PB1\PB5发光二极管

注意,每次使用IO口要设置IO口是输出还是输入,还要打开时钟

#include "stm32f10x.h"

void sleep(unsigned int t)
{
    while(t--);
}
    

int main (void)
{
#if 0
    // 打开GPIOB端口的时钟
    *(unsigned int *)0x40021018 |= ((1)<<3);
    // 配置IO口为输出
    *(unsigned int *)0x40010C00 |= ((1)<<(4*0));
    *(unsigned int *)0x40010C00 |= ((1)<<(4*1));
    *(unsigned int *)0x40010C00 |= ((1)<<(4*5));
    
    while(1)
    {
        // 控制ODR寄存器
        *(unsigned int *)0x40010C0C &= ~(1<<0);
        sleep(0xFFFF);
        sleep(0xFFFF);
        *(unsigned int *)0x40010C0C |= (1<<0);
        sleep(0xFFFF);
        sleep(0xFFFF);
        
        *(unsigned int*)0X40010C0C &= ~(1<<1);
        sleep(0xFFFF);
        sleep(0xFFFF);
        *(unsigned int*)0X40010C0C |= (1<<1);
        sleep(0xFFFF);
        sleep(0xFFFF);
        
        *(unsigned int*)0X40010C0C &= ~(1<<5);
        sleep(0xFFFF);
        sleep(0xFFFF);
        *(unsigned int*)0X40010C0C |= (1<<5);
        sleep(0xFFFF);
        sleep(0xFFFF);

    }
#else
    
    // 打开GPIOB端口的时钟
    RCC_APB2ENR |= ((1)<<3);
    // 配置IO口为输出
    GPIOB_CRL |= ((1)<<(4*0));
    GPIOB_CRL |= ((1)<<(4*1));
    GPIOB_CRL |= ((1)<<(4*5));
    
    while(1)
    {
        
        // 控制ODR寄存器
        GPIOB_ODR &= ~(1<<0);
        sleep(0xFFFF);
        sleep(0xFFFF);
        GPIOB_ODR |= (1<<0);
        sleep(0xFFFF);
        sleep(0xFFFF);

        GPIOB_ODR &= ~(1<<1);
        sleep(0xFFFF);
        sleep(0xFFFF);
        GPIOB_ODR |= (1<<1);
        sleep(0xFFFF);
        sleep(0xFFFF);
        
        GPIOB_ODR &= ~(1<<5);
        sleep(0xFFFF);
        sleep(0xFFFF);
        GPIOB_ODR |= (1<<5);
        sleep(0xFFFF);
        sleep(0xFFFF);
    
    }
#endif

}

// 置位 != 清零

void SystemInit(void){
    // 函数体为空,目的是为了骗过编译器不报错
}

 

 

// 用来存放STM32寄存器映射的代码

// 外设

#define PERI_BASE                    ((unsigned int)0x40000000)
#define APB1_PERI_BASE        PERI_BASE
#define APB2_PERI_BASE        (PERI_BASE + 0x10000)
#define AHB_PERI_BASE            (PERI_BASE + 0x20000)

// 复位和时钟控制器
#define RCC_BASE                    (AHB_PERI_BASE + 0x1000)
// GPIOB
#define GPIOB_BASE                (APB2_PERI_BASE + 0x0C00)

// APB2时钟使能寄存器
#define RCC_APB2ENR             *(unsigned int*)(RCC_BASE + 0x18)
    
// 端口配置低寄存器
#define GPIOB_CRL                    *(unsigned int*)(GPIOB_BASE + 0x00)
#define GPIOB_CRH                    *(unsigned int*)(GPIOB_BASE + 0x04)
#define GPIOB_ODR                    *(unsigned int*)(GPIOB_BASE + 0x0C)

 

posted @ 2021-03-07 18:04  lovleo  阅读(506)  评论(0编辑  收藏  举报