野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 848|回复: 3

【交作业】用寄存器点亮LED灯并闪烁

[复制链接]
发表于 2023-8-22 08:57:27 | 显示全部楼层 |阅读模式
延时函数
void Delay_us(uint32_t xus)
{
        SysTick->LOAD = 72 * xus;                                //设置寄存器重装值
        SysTick->VAL = 0x00;                                        //清空当前计数值
        SysTick->CTRL = 0x00000005;                                //设置时钟为HCLK,启动定时器
        while(!(SysTick->CTRL & 0x00010000));        //等待计数到0
        SysTick->CTRL = 0x00000004;                                //关闭定时器
}
void Delay_ms(uint32_t xms)
{
        while(xms--)
        {
                Delay_us(1000);
        }
}
从网上找的,请大家参考
下面的函数是根据火哥的讲解,自己编写的
int main(void)
{
        while(1)
        {
        /******PB0控制绿灯亮灭*******/
        // 打开GPIOB端口
        *( unsigned int * )0x40021018 |=  ( (1) << 3 );       

        // 配置IO为输出
        *( unsigned int * )0x40010C00 |=  ( (1) << (4*0) );
       
        // 控制ODR寄存器
        *( unsigned int * )0x40010C0C &= ~(1<<0);
         Delay_ms(100);
         *( unsigned int * )0x40010C00  &= ~( 0x0F<< (4*0));//控制PB10端口位,关闭输出
       
                /******PB1控制蓝灯亮灭*******/
       
        *( unsigned int * )0x40010C00 |=  ( (1) << (4*1) );
        *( unsigned int * )0x40010C0C &= ~(1<<1);
        Delay_ms(100);
         *( unsigned int * )0x40010C00  &= ~( 0x0F<< (4*1));//控制PB1端口位,关闭输出
       
       
        /******PB5控制红灯亮灭*******/
        *( unsigned int * )0x40010C00 |=  ( (1) << (4*5) );
        *( unsigned int * )0x40010C0C &= ~(1<<5);
        Delay_ms(100);
         *( unsigned int * )0x40010C00  &= ~( 0x0F<< (4*5));//控制PB5端口位,关闭输出
}
       
}

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

使用道具 举报

发表于 2023-8-22 13:48:54 | 显示全部楼层
回复

使用道具 举报

发表于 2023-8-22 18:53:21 | 显示全部楼层
赞赞赞赞
回复

使用道具 举报

 楼主| 发表于 2023-8-24 09:17:05 | 显示全部楼层
【交作业】用结构体定义RCC和GPIO
//用来存放STM32寄存器映射的代码
//外设 periph

#define PERIPH_BASE                                ((UNSIGNED INT)0X40000000)
#define APB1PERIPH_BASE                PERIPH_BASE       
#define APB2PERIPH_BASE                (PERIPH_BASE+0X10000)
#define AHBPERIPH_BASE                (PERIPH_BASE+0X20000)

#define RCC_BASE                                        (AHBPERIPH_BASE+0X1000)
#define GPIOB_BASE                                        (APB2PERIPH_BASE+0X0C00)


typedef unsigned int uint32_t;
typedef unsigned int uint16_t;


typedef struct
{
        uint32_t CR;
        uint32_t CFGR;
        uint32_t CIR;
        uint32_t APB2RSTR;
        uint32_t APB1RSTR;
        uint32_t AHBENR;
        uint32_t APB2ENR;
        uint32_t APB1ENR;
        uint32_t BDCR;
        uint32_t CSR;
}RCC_TypeDef;


typedef struct
{
        uint32_t CRL; //0x00
        uint32_t CRH; //0x04
        uint32_t IDR;        //0x08
        uint32_t ODR;        //0x0c
        uint32_t BSRR;        //0x10
        uint32_t BRR;                //0x14
        uint32_t LCKR;        //0x18
}GPIO_TypeDef;                       
//定义GPIOx的端口配置

#define GPIOB                ((GPIO_TypeDef*)GPIOB_BASE)
#define RCC                ((RCC_TypeDef*)RCC_BASE)

同样实现灯的控制
主函数如下:
int main(void)
{
        while(1)
        {
                /******PB0¿ØÖÆÂ̵ÆÁÁÃð*******/
                // ´ò¿ªGPIOB¶Ë¿Ú
                RCC->APB2ENR |=  ( (1) << 3 );               

                // ÅäÖÃIO¿ÚΪÊä³ö
       
                GPIOB->CRL |= ((1)<<(4*0));
                // ¿ØÖÆODR¼Ä´æÆ÷
                GPIOB->ODR &=~(1<<0);
                delay(1200000);
                GPIOB->CRL &= ~( (0x0F)<< (4*0));
                /******PB1¿ØÖÆÀ¶µÆÁÁÃð*******/
                GPIOB->CRL |=  ( (1) << (4*1) );
                GPIOB->ODR &= ~(1<<1);
                delay(1200000);
                GPIOB->CRL &= ~( 0x0F<< (4*1));
                /******PB5¿ØÖƺìµÆÁÁÃð*******/
                GPIOB->CRL |=  ( (1) << (4*5) );
                GPIOB->ODR &= ~(1<<5);
                delay(1200000);
                GPIOB->CRL &= ~( 0x0F<< (4*5));

}
       
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-5 07:01 , Processed in 0.044972 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表