分别用汇编语言与c语言点亮灯

文章展示了如何使用汇编语言和C语言在STM32微控制器上分别实现对LED1/LED2/LED3的初始化和点亮操作。代码详细设置了GPIO寄存器以配置输出模式、速度和上下拉状态,并通过循环实现灯的闪烁效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.用汇编语言实现LED1/LED2/LED3三盏灯点亮

2.用C语言实现LED1/LED2/LED3三盏灯点亮

汇编点亮

.text 
.global _start
_start: 
    /**********LED1点灯**************/
    /**********RCC章节***************/
@1.设置GPIOE组时钟使能,通过RCC_AHB4ENSETR寄存器设置 0x5000 0A28[4] = 1
    ldr r0, =0x50000A28         @准备地址空间
    ldr r1,[r0]                 @将r0数据指向的地址空间的内容读取到r1中
    orr r1,r1, #(0x1 << 4)      @将r1寄存器第四位置1
    str r1,[r0]               
    /**********GPIOE10章节**************/
@2.设置PE10引脚为输出模式01
    ldr r0, =0x50006000
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    orr r1,r1,#(0x1 << 20)
    str r1,[r0]

@3.设置PE10引脚为推挽输出0
    ldr r0, =0x50006004
    ldr r1,[r0]
    and r1,r1, #(~(0x1 << 10))
    str r1,[r0]

@4.设置PE10引脚为低速模式00
    ldr r0, =0x50006008
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    str r1,[r0]
   
@5.设置PE10引脚为无上下拉模式00
    ldr r0, =0x5000600c
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    str r1,[r0]
    
    /**********GPIOE8章节**************/
@2.设置PE10引脚为输出模式01
    ldr r0, =0x50006000
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 16))
    orr r1,r1,#(0x1 << 16)
    str r1,[r0]
@3.设置PE10引脚为推挽输出0
    ldr r0, =0x50006004
    ldr r1,[r0]
    and r1,r1, #(~(0x1 << 8))
    str r1,[r0]
@4.设置PE10引脚为低速模式00
    ldr r0, =0x50006008
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 16))
    str r1,[r0]
   
@5.设置PE10引脚为无上下拉模式00
    ldr r0, =0x5000600c
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 16))
    str r1,[r0]

    ldr r0, =0x50000A28         @准备地址空间
    ldr r1,[r0]                 @将r0数据指向的地址空间的内容读取到r1中
    orr r1,r1, #(0x1 << 5)      @将r1寄存器第五位置1
    str r1,[r0]               
    /**********GPIOF10章节**************/
@2.设置PF10引脚为输出模式01
    ldr r0, =0x50007000
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    orr r1,r1,#(0x1 << 20)
    str r1,[r0]

@3.设置PF10引脚为推挽输出0
    ldr r0, =0x50007004
    ldr r1,[r0]
    and r1,r1, #(~(0x1 << 10))
    str r1,[r0]

@4.设置PF10引脚为低速模式00
    ldr r0, =0x50007008
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    str r1,[r0]

@5.设置PF10引脚为无上下拉模式00
    ldr r0, =0x5000700c
    ldr r1,[r0]
    and r1,r1,#(~(0x3 << 20))
    str r1,[r0]

loop:
    bl LED1_ON
    bl delay_1s
    bl LED1_OFF
    bl LED2_ON
    bl delay_1s
    bl LED2_OFF
    bl LED3_ON
    bl delay_1s
    bl LED3_OFF
    b loop

LED1_ON:
    @点亮 1
    ldr r0, =0x50006014
    ldr r1,[r0]
    orr r1,r1,  #(0x1 << 10)
    str r1,[r0]
    mov pc,lr 

LED1_OFF:
    @关闭 0
    ldr r0, =0x50006014
     ldr r1,[r0]
    and r1,r1, #(~(0x1 << 10))
    str r1,[r0]  
    mov pc,lr 


LED2_ON:
    @点亮 1
    ldr r0, =0x50007014
    ldr r1,[r0]
    orr r1,r1,  #(0x1 << 10)
    str r1,[r0]
    mov pc,lr 

LED2_OFF:
    @关闭 0
    ldr r0, =0x50007014
     ldr r1,[r0]
    and r1,r1, #(~(0x1 << 10))
    str r1,[r0]  
    mov pc,lr 


LED3_ON:
    @点亮 1
    ldr r0, =0x50006014
    ldr r1,[r0]
    orr r1,r1,  #(0x1 << 8)
    str r1,[r0]
    mov pc,lr

LED3_OFF:
    @关闭 0
    ldr r0, =0x50006014
     ldr r1,[r0]
    and r1,r1, #(~(0x1 << 8))
    str r1,[r0]
    mov pc,lr

@约1ms的延时函数
delay_1s:
    mov r3, #0x10000000
    mm:
    cmp r3, #0
    subne r3, r3, #1
    bne mm
    mov pc, lr

.end

C语言点亮

头文件:

#ifndef __LED_H__
#define __LED_H__

typedef struct{

       volatile unsigned int MODER; //00
       volatile unsigned int OTYPER; //0
       volatile unsigned int OSPEEDR; //00
       volatile unsigned int PUPDR; //0C
       volatile unsigned int IDR; //1
       volatile unsigned int ODR;//14
}gpio_t;

#define RCC_AHB4_ENSETR  (*(volatile unsigned int*) 0x50000A28)
#define GPIOE ((gpio_t*) 0x50006000)
#define GPIOF ((gpio_t*) 0x50007000)


void hal_gpio_init();

void led1_init();
void led1_on();
void led1_off();

void led2_init();
void led2_on();
void led2_off();

void led3_init();
void led3_on();
void led3_off();

#endif

源文件:

#include "led.h"

void hal_gpio_init()
{
    led1_init();
    led2_init();
    led3_init();
}
void led1_init()
{
     RCC_AHB4_ENSETR = RCC_AHB4_ENSETR | (0x1 << 4);
     
     GPIOE->MODER = GPIOE->MODER & (~(0x3 << 20));
     GPIOE->MODER = GPIOE->MODER | (0x1 << 20);

     GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1 << 10));

     GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x3 << 20));
    
     GPIOE->PUPDR = GPIOE->PUPDR & (~(0x3 << 20));
}
void led2_init()
{
     RCC_AHB4_ENSETR = RCC_AHB4_ENSETR | (0x1 << 5);
     
     GPIOF->MODER = GPIOF->MODER & (~(0x3 << 20));
     GPIOF->MODER = GPIOF->MODER | (0x1 << 20);

     GPIOF->OTYPER = GPIOF->OTYPER & (~(0x1 << 10));

     GPIOF->OSPEEDR = GPIOF->OSPEEDR & (~(0x3 << 20));
    
     GPIOF->PUPDR = GPIOF->PUPDR & (~(0x3 << 20));
}
void led3_init()
{
     RCC_AHB4_ENSETR = RCC_AHB4_ENSETR | (0x1 << 4);
     
     GPIOE->MODER = GPIOE->MODER & (~(0x3 << 16));
     GPIOE->MODER = GPIOE->MODER | (0x1 << 16);

     GPIOE->OTYPER = GPIOE->OTYPER & (~(0x1 << 8));

     GPIOE->OSPEEDR = GPIOE->OSPEEDR & (~(0x3 << 16));
    
     GPIOE->PUPDR = GPIOE->PUPDR & (~(0x3 << 16));
}
void led1_on()
{
    GPIOE->ODR = GPIOE->ODR | (0x1 << 10);
}
void led1_off()
{
    GPIOE->ODR = GPIOE->ODR & (~(0x1 << 10));
}
void led2_on()
{
    GPIOF->ODR = GPIOF->ODR | (0x1 << 10);
}
void led2_off()
{
    GPIOF->ODR = GPIOF->ODR & (~(0x1 << 10));
}
void led3_on()
{
    GPIOE->ODR = GPIOE->ODR | (0x1 << 8);
}
void led3_off()
{
    GPIOE->ODR = GPIOE->ODR & (~(0x1 << 8));
}

Main文件

#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
    int i,j;
    for(i = 0; i < ms;i++)
        for (j = 0; j < 1800; j++);
}


int main()
{
    hal_gpio_init(); // LED灯初始化
    while(1)
    {
        led1_on();
        delay_ms(500);
        led1_off();
        led2_on();
        delay_ms(500);
        led2_off();
        led3_on();
        delay_ms(500);
        led3_off();
    }
    return 0;
}

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值