C语言实现 cortex-A7核 点LED灯 (附 汇编实现)

1 汇编实现


text
global _start
start:
************** LED1点灯 ---> PE10 **************/
************** RCC章节初始化 **************/
CC_INIT:
   @1.使能GPIOE组控制器,通过RCC_MP_AHB4ENSETR寄存器设置GPIOE组使能0x50000A28[4]= 1
   ldr r0,=0x50000A28       @准备一块地址空间0x50000A28
   ldr r1,[r0]              @将0x50000A28这块地址中的内容,读到目标寄存器r1中
   orr r1,r1,#(0x3 << 4)    @将r1寄存器第[5:4] = 11,保证其他位不变 
   str r1,[r0]              @将r1寄存器中的值,写回0x50000A28这块地址空间中

************** LED1对应GPIO章节初始化 **************/
ED1_INIT:
   @1.通过GPIOE_MODER寄存器,设置PE10引脚为 输出模式 0x50006000[21:20] = 01
   ldr r0,=0x50006000
   ldr r1,[r0]
   orr r1,r1,#(0x3 << 20)
   and r1,r1,#(~(0x1 << 21))
   str r1,[r0]

   @2.通过GPIOE_OTYPER寄存器,设置PE10引脚为 推挽输出类型 0x50006004[10] = 0
   ldr r0,=0x50006004
   ldr r1,[r0]
   and r1,r1,#(~(0x1 << 10))
   str r1,[r0]

   @3.通过GPIOE_OSPEEDR寄存器,设置PE10引脚为 低速输出 0x50006008[21:20] = 00
   ldr r0,=0x50006008
   ldr r1,[r0]
   and r1,r1,#(~(0x3 << 20))
   str r1,[r0]

   @4.通过GPIOE_PUPDR寄存器,设置PE10引脚 禁止上下拉电阻 0x5000600C[21:20] = 00
   ldr r0,=0x5000600C
   ldr r1,[r0]
   and r1,r1,#(~(0x3 << 20))
   str r1,[r0]

************** LED2对应GPIO章节初始化 **************/
ED2_INIT:
   @1.通过GPIOE_MODER寄存器,设置PF10引脚为 输出模式 0x50007000[21:20] = 01
   ldr r0,=0x50007000
   ldr r1,[r0]
   orr r1,r1,#(0x3 << 20)
   and r1,r1,#(~(0x1 << 21))
   str r1,[r0]

   @2.通过GPIOE_OTYPER寄存器,设置PF10引脚为 推挽输出类型 0x50007004[10] = 0
   ldr r0,=0x50007004
   ldr r1,[r0]
   and r1,r1,#(~(0x1 << 10))
   str r1,[r0]

   @3.通过GPIOE_OSPEEDR寄存器,设置PF10引脚为 低速输出 0x50007008[21:20] = 00
   ldr r0,=0x50007008
   ldr r1,[r0]
   and r1,r1,#(~(0x3 << 20))
   str r1,[r0]

   @4.通过GPIOE_PUPDR寄存器,设置PF10引脚 禁止上下拉电阻 0x5000700C[21:20] = 00
   ldr r0,=0x5000700C
   ldr r1,[r0]
   and r1,r1,#(~(0x3 << 20))
   str r1,[r0]

   /************** LED3对应GPIO章节初始化 **************/
ED3_INIT:
   @1.通过GPIOE_MODER寄存器,设置PE8引脚为 输出模式 0x50006000[17:16] = 01
   ldr r0,=0x50006000
   ldr r1,[r0]
   orr r1,r1,#(0x3 << 16)
   and r1,r1,#(~(0x1 << 17))
   str r1,[r0]

   @2.通过GPIOE_OTYPER寄存器,设置PE8引脚为 推挽输出类型 0x50006004[8] = 0
   ldr r0,=0x50006004
   ldr r1,[r0]
   and r1,r1,#(~(0x1 << 8))
   str r1,[r0]

   @3.通过GPIOE_OSPEEDR寄存器,设置PE8引脚为 低速输出 0x50006008[17:16] = 00
   ldr r0,=0x50006008
   ldr r1,[r0]
   and r1,r1,#(~(0x3 << 16))
   str r1,[r0]

   @4.通过GPIOE_PUPDR寄存器,设置PE8引脚 禁止上下拉电阻 0x5000600C[17:16] = 00
   ldr r0,=0x5000600C
   ldr r1,[r0]
   and r1,r1,#(~(0x3 <<16))
   str r1,[r0]

oop:
   bl LED1_ON
   bl delay_1s
   bl LED1_OFF
   bl delay_1s

   bl LED2_ON
   bl delay_1s
   bl LED2_OFF
   bl delay_1s

   bl LED3_ON                                                                                                                   
   bl delay_1s
   bl LED3_OFF
   bl delay_1s

   b Loop

   /************** LED1点亮 **************/
ED1_ON:
   @1.通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[10] = 1
   ldr r0,=0x50006014
   ldr r1,[r0]
   orr r1,r1,#(0x1 << 10)
   str r1,[r0]
   mov pc,lr
   /************** LED1熄灭 **************/
ED1_OFF:
   @1.通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0
   ldr r0,=0x50006014
   ldr r1,[r0]
   bic r1,r1,#(0x1 << 10)
   str r1,[r0]
   mov pc,lr

   /************** LED2点亮 **************/
ED2_ON:
   @1.通过GPIOF_ODR寄存器,设置PE10引脚输出高电平 0x50007014[10] = 1
   ldr r0,=0x50007014
   ldr r1,[r0]
   orr r1,r1,#(0x1 << 10)
   str r1,[r0]
   mov pc,lr
   /************** LED2熄灭 **************/
ED2_OFF:
   @1.通过GPIOF_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0
   ldr r0,=0x50007014
   ldr r1,[r0]
   bic r1,r1,#(0x1 << 10)
   str r1,[r0]
   mov pc,lr

   /************** LED3点亮 **************/
ED3_ON:
   @1.通过GPIOF_ODR寄存器,设置PE10引脚输出高电平 0x50006014[8] = 1
   ldr r0,=0x50006014
   ldr r1,[r0]
   orr r1,r1,#(0x1 << 8)
   str r1,[r0]
   mov pc,lr
   /************** LED3熄灭 **************/
ED3_OFF:
   @1.通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[8] = 0
   ldr r0,=0x50006014
   ldr r1,[r0]
   bic r1,r1,#(0x1 << 8)
   str r1,[r0]
   mov pc,lr

 大概1s的延时函数
elay_1s:
   mov r3, #0x10000000
   mm:
   cmp r3, #0
   subne r3, r3, #1
   bne mm
   mov pc, lr
end                                                                                                                           

2 C语言实现

led.h

#ifndef __LED_H__                                                 
#define __LED_H__                                                 
                                                                  
//结构体封装                                                      
typedef struct{                                                   
    volatile unsigned int MODER;                                  
    volatile unsigned int OTYPER;                                 
    volatile unsigned int OSPEEDR;                                
    volatile unsigned int PUPDR;                                  
    volatile unsigned int IDR;                                    
    volatile unsigned int ODR;                                    
}gpio_t;                                                          
                                                                  
#define GPIOE ((gpio_t*)0x50006000)                               
#define GPIOF ((gpio_t*)0x50007000)                               
#define RCC_MP_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)  
                                                                  
//LED1  ===>  PE10                                                
//RCC/GPIO章节初始化                                              
void led1_rcc_gpio_init();                                        
//LED1点亮                                                        
void led1_on();                                                   
//LED1熄灭                                                        
void led1_off();                                                  
//LED1  ===>  PE10                                                
                                                                  
//LED2 ===> PF10                                                  
//RCC/GPIO章节初始化                                              
void led2_rcc_gpio_init();                                        
//LED2点亮                                                        
void led2_on();                                                   
//LED2熄灭                                                        
void led2_off();                                                  
                                                                  
//LED3  ===>  PE8                                                 
//RCC/GPIO章节初始化                                              
void led3_rcc_gpio_init();                                        
//LED3点亮                                                        
void led3_on();                                                   
//LED3熄灭                                                        
void led3_off();                                                  
#endif                                                            

led.c

#include "led.h"                                                                                 
                                                                                                 
//LED1 ---> PE10                                                                                 
//RCC/GPIO章节初始化                                                                             
void led1_rcc_gpio_init()                                                                        
{                                                                                                
    //0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[4] = 1         
    RCC_MP_AHB4_ENSETR |= (0X1 << 4);                                                            
    //1、通过GPIOE_MODER寄存器,设置PE10引脚为输出模式 0x50006000[21:20] = 01                    
    GPIOE->MODER &= (~(0x1 << 21));  //21位清0                                                   
    GPIOE->MODER |= (0x1 << 20);     //20位置1                                                   
    //2、通过GPIOE_OTYPER寄存器,设置PE10引脚为推挽输出类型 0x50006004[10] = 0                   
    GPIOE->OTYPER &= (~(0x1 << 10));                                                             
    //3、通过GPIOE_OSPEEDR寄存器,设置PE10引脚为低速输出 0x50006008[21:20] = 00                  
    GPIOE->OSPEEDR &= (~(0x3 << 20));                                                            
    //4、通过GPIOE_PUPDR寄存器,设置PE10引脚禁止上下拉电阻 0x5000600C[21:20] = 00                
    GPIOE->PUPDR &= (~(0x3 << 20));                                                              
}                                                                                                
                                                                                                 
//LED1点亮                                                                                       
void led1_on()                                                                                   
{                                                                                                
    //通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[10] = 1                             
    GPIOE->ODR |= (0x1 << 10);                                                                   
}                                                                                                
//LED1熄灭                                                                                       
void led1_off()                                                                                  
{                                                                                                
    //通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[10] = 0                             
    GPIOE->ODR &= (~(0x1) << 10);                                                                
}                                                                                                
                                                                                                 
//LED2 ---> PF10                                                                                 
//RCC/GPIO章节初始化                                                                             
void led2_rcc_gpio_init()                                                                        
{                                                                                                
    //0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[5] = 1         
    RCC_MP_AHB4_ENSETR |= (0X1 << 5);                                                            
    //1、通过GPIOF_MODER寄存器,设置PF10引脚为输出模式 0x50007000[21:20] = 01                    
    GPIOF->MODER &= (~(0x1 << 21));  //21位清0                                                   
    GPIOF->MODER |= (0x1 << 20);     //20位置1                                                   
    //2、通过GPIOE_OTYPER寄存器,设置PF10引脚为推挽输出类型 0x50007004[10] = 0                   
    GPIOF->OTYPER &= (~(0x1 << 10));                                                             
    //3、通过GPIOE_OSPEEDR寄存器,设置PF10引脚为低速输出 0x50007008[21:20] = 00                  
    GPIOF->OSPEEDR &= (~(0x3 << 20));                                                            
    //4、通过GPIOE_PUPDR寄存器,设置PF10引脚禁止上下拉电阻 0x5000700C[21:20] = 00                
    GPIOF->PUPDR &= (~(0x3 << 20));                                                              
}                                                                                                
                                                                                                 
//LED2点亮                                                                                       
void led2_on()                                                                                   
{                                                                                                
    //通过GPIOF_ODR寄存器,设置PF10引脚输出高电平 0x50007014[10] = 1                             
    GPIOF->ODR |= (0x1 << 10);                                                                   
}                                                                                                
//LED2熄灭                                                                                       
void led2_off()                                                                                  
{                                                                                                
    //通过GPIOF_ODR寄存器,设置PF10引脚输出低电平 0x50007014[10] = 0                             
    GPIOF->ODR &= (~(0x1) << 10);                                                                
}                                                                                                
                                                                                                 
//LED3 ---> PE8                                                                                  
//RCC/GPIO章节初始化                                                                             
void led3_rcc_gpio_init()                                                                        
{                                                                                                
    //0、使能GPIOE组控制器,通过RCC_MP_AHB4_ENSETR寄存器设置GPIOE组使能0x50000A28[4] = 1         
    RCC_MP_AHB4_ENSETR |= (0X1 << 4);                                                            
    //1、通过GPIOE_MODER寄存器,设置PE10引脚为输出模式 0x50006000[17:16] = 01                    
    GPIOE->MODER &= (~(0x1 << 17));  //17位清0                                                   
    GPIOE->MODER |= (0x1 << 16);     //16位置1                                                   
    //2、通过GPIOE_OTYPER寄存器,设置PE10引脚为推挽输出类型 0x50006004[8] = 0                    
    GPIOE->OTYPER &= (~(0x1 << 8));                                                              
    //3、通过GPIOE_OSPEEDR寄存器,设置PE10引脚为低速输出 0x50006008[17:16] = 00                  
    GPIOE->OSPEEDR &= (~(0x3 << 16));                                                            
    //4、通过GPIOE_PUPDR寄存器,设置PE10引脚禁止上下拉电阻 0x5000600C[17:16] = 00                
    GPIOE->PUPDR &= (~(0x3 << 16));                                                              
}                                                                                                
                                                                                                 
//LED1点亮                                                                                       
void led3_on()                                                                                   
{                                                                                                
    //通过GPIOE_ODR寄存器,设置PE10引脚输出高电平 0x50006014[8] = 1                              
    GPIOE->ODR |= (0x1 << 8);                                                                    
}                                                                                                
//LED1熄灭                                                                                       
void led3_off()                                                                                  
{                                                                                                
    //通过GPIOE_ODR寄存器,设置PE10引脚输出低电平 0x50006014[8] = 0                              
    GPIOE->ODR &= (~(0x1) << 8);                                                                 
}       

main.c

#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()
{
    led1_rcc_gpio_init(); // LED灯初始化
    led2_rcc_gpio_init(); // LED灯初始化
    led3_rcc_gpio_init(); // LED灯初始化
    while(1)
    {
        led1_on();
        delay_ms(500);
        led1_off();
        delay_ms(500);

        led2_on();
        delay_ms(500);
        led2_off();
        delay_ms(500);
                                                     
        led3_on();
        delay_ms(500);
        led3_off();
        delay_ms(500);
    }
    return 0;
}

3 循环实现

led.h

#ifndef __LED_H__                                                 
#define __LED_H__                                                 
                                                                  
#define RCC_MP_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)  
                                                                  
//结构体封装                                                      
typedef struct{                                                   
    volatile unsigned int MODER;                                  
    volatile unsigned int OTYPER;                                 
    volatile unsigned int OSPEEDR;                                
    volatile unsigned int PUPDR;                                  
    volatile unsigned int IDR;                                    
    volatile unsigned int ODR;                                    
}gpio_t;                                                          
                                                                  
typedef enum{                                                     
    LED_OFF=0,                                                    
    LED_ON,                                                       
}LED_C;                                                           
                                                                  
typedef enum{                                                     
    LED1=0,                                                       
    LED2,                                                         
    LED3                                                          
}LED;                                                             
                                                                  
#define GPIOE ((gpio_t*)0x50006000)                               
#define GPIOF ((gpio_t*)0x50007000)                               
                                                                  
void led_init(LED l);                                             
void led_con(LED l,LED_C c);                                      
#endif                                                            

led.c

#include "led.h"                                         
//初始化灯                                               
void led_init(LED led)                                   
{                                                        
    switch(led)                                          
    {                                                    
    case LED1:                                           
        RCC_MP_AHB4_ENSETR |= (0X1 << 4);                
        GPIOE->MODER &= (~(0x1 << 21));  //21位清0       
        GPIOE->MODER |= (0x1 << 20);     //20位置1       
        GPIOE->OTYPER &= (~(0x1 << 10));                 
        GPIOE->OSPEEDR &= (~(0x3 << 20));                
        GPIOE->PUPDR &= (~(0x3 << 20));                  
        break;                                           
    case LED2:                                           
        RCC_MP_AHB4_ENSETR |= (0X1 << 5);                
        GPIOF->MODER &= (~(0x1 << 21));  //21位清0       
        GPIOF->MODER |= (0x1 << 20);     //20位置1       
        GPIOF->OTYPER &= (~(0x1 << 10));                 
        GPIOF->OSPEEDR &= (~(0x3 << 20));                
        GPIOF->PUPDR &= (~(0x3 << 20));                  
        break;                                           
    case LED3:                                           
        RCC_MP_AHB4_ENSETR |= (0X1 << 4);                
        GPIOE->MODER &= (~(0x1 << 17));  //17位清0       
        GPIOE->MODER |= (0x1 << 16);     //16位置1       
        GPIOE->OTYPER &= (~(0x1 << 8));                  
        GPIOE->OSPEEDR &= (~(0x3 << 16));                
        GPIOE->PUPDR &= (~(0x3 << 16));                  
        break;                                           
    }                                                    
}                                                        
//控制灯                                                 
void led_con(LED led,LED_C c)                            
{                                                        
    switch(led)                                          
    {                                                    
    case LED1:                                           
        switch(c)                                        
        {                                                
        case LED_OFF:                                    
            GPIOE->ODR &= (~(0X1 << 10));                
            break;                                       
        case LED_ON:                                     
            GPIOE->ODR |= (0X1 << 10);                   
            break;                                       
        }                                                
        break;                                           
    case LED2:                                           
        switch(c)                                        
        {                                                
        case LED_OFF:                                    
            GPIOF->ODR &= (~(0X1 << 10));                
            break;                                       
        case LED_ON:                                     
            GPIOF->ODR |= (0X1 << 10);                   
            break;                                       
        }                                                
        break;                                           
    case LED3:                                           
        switch(c)                                        
        {                                                
        case LED_OFF:                                    
            GPIOE->ODR &= (~(0X1 << 8));                 
            break;                                       
        case LED_ON:                                     
            GPIOE->ODR |= (0X1 << 8);                    
            break;                                       
        }                                                
        break;                                           
    }                                                    
}                                                        

main.c

#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()
{
    led_init(LED1);
    led_init(LED2);
    led_init(LED3);
    while(1)
    {
        led_con(LED1,LED_ON);
        delay_ms(200);
        led_con(LED1,LED_OFF);
        delay_ms(200);

        led_con(LED2,LED_ON);
        delay_ms(200);
        led_con(LED2,LED_OFF);
        delay_ms(200);

        led_con(LED3,LED_ON);
        delay_ms(200);
        led_con(LED3,LED_OFF);
        delay_ms(200);
    }
    return 0;
}                                        
                                         

你可能感兴趣的:(ARM,c语言,汇编,开发语言)