ARM day5 (点灯实验 汇编&C)

汇编

.text 
.global _start
_start: 
	/**********LED1点灯**************/
	/**********RCC草节初始化**************/

	//1.通过RCC_MP_AHB4ENSETR寄存器使能GPIOE组控制器0x50000A28[4] = 1
	ldr r0,=0x50000A28
	ldr r1,[r0]
	orr r1,r1,#(0x1 << 4)
	str r1,[r0]

	//1.通过RCC_MP_AHB4ENSETR寄存器使能GPIOF组控制器0x50000A28[5] = 1
	ldr r0,=0x50000A28
	ldr r1,[r0]
	orr r1,r1,#(0x1 << 5)
	str r1,[r0]

	/*********GPIO章节初始化*************/

LED1_INIT:

	@1.通过GPIOE_MODER寄存器设置PE10引脚为输出模式0x50006000[21:20] = 01
	ldr r0,=0x50006000
	ldr r1,[r0]
	and r1,r1,#(~(0x3 << 20))
	orr r1,r1,#(0x1 << 20)
	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_OSPBEDR寄存器设置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_INIT:

	@1.通过GPIOF_MODER寄存器设置PF10引脚为输出模式0x50007000[21:20] = 01
	ldr r0,=0x50007000
	ldr r1,[r0]
	and r1,r1,#(~(0x3 << 20))
	orr r1,r1,#(0x1 << 20)
	str r1,[r0]

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


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


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

	@1.通过GPIOE_MODER寄存器设置PE8引脚为输出模式0x50006000[17:16] = 01
	ldr r0,=0x50006000
	ldr r1,[r0]
	and r1,r1,#(~(0x3 << 16))
	orr r1,r1,#(0x1 << 16)
	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_OSPBEDR寄存器设置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]
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:
	@通过GPIOE_ODR寄存器设置PE10引脚输出高电平0x50006014[10] = 1
	ldr r0,=0x50006014
	ldr r1,[r0]
	orr r1,r1,#(0x1 << 10)
	str r1,[r0]
	mov pc,lr


LED1_OFF:

	@通过GPIOE_ODR寄存器设置PE10引脚输出低电平0x50006014[10] = 0
	ldr r0,=0x50006014
	ldr r1,[r0]
	and r1,r1,#(~(0x1 << 10))
	str r1,[r0]
	mov pc,lr


LED2_ON:
	@通过GPIOF_ODR寄存器设置PF10引脚输出高电平0x50007014[10] = 1
	ldr r0,=0x50007014
	ldr r1,[r0]
	orr r1,r1,#(0x1 << 10)
	str r1,[r0]
	mov pc,lr


LED2_OFF:

	@通过GPIOF_ODR寄存器设置PF10引脚输出低电平0x50007014[10] = 0
	ldr r0,=0x50007014
	ldr r1,[r0]
	and r1,r1,#(~(0x1 << 10))
	str r1,[r0]
	mov pc,lr


LED3_ON:
	@通过GPIOE_ODR寄存器设置PE8引脚输出高电平0x50006014[8] = 1
	ldr r0,=0x50006014
	ldr r1,[r0]
	orr r1,r1,#(0x1 << 8)
	str r1,[r0]
	mov pc,lr


LED3_OFF:

	@通过GPIOE_ODR寄存器设置PE8引脚输出低电平0x50006014[8] = 0
	ldr r0,=0x50006014
	ldr r1,[r0]
	and r1,r1,#(~(0x1 << 8))
	str r1,[r0]
	mov pc,lr

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


.end

C语言

led.h

#ifndef __LED_H__
#define __LED_H__

//声明一个结构体
typedef struct pio
{
	unsigned int MODER;   //00
	unsigned int OTYPER;  //04
	unsigned int OSPEEDR; //08
	unsigned int PUPDR;   //0C
	unsigned int IDR;     //10
	unsigned int ODR;     //14
}gpio_t;

#define GPIOE ((volatile gpio_t*)0x50006000)
#define GPIOF ((volatile gpio_t*)0x50007000)


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

led.c

#include "led.h"

void led1_init()
{
	GPIOE->MODER &= (~(0x3 << 20));
	GPIOE->MODER |= (0x1 << 20);
	GPIOE->OTYPER &=(~(0x1 << 10));
	GPIOE->OSPEEDR &= (~(0x3 << 20));
	GPIOE->PUPDR &= (~(0X3 << 20));
}

void LED1_on()
{

	GPIOE->ODR |= (0x1 << 10);
}

void led1_off()
{
	GPIOE->ODR &= (~(0x1 << 10));
}



void led2_init()
{
	GPIOF->MODER &= (~(0x3 << 20));
	GPIOF->MODER |= (0x1 << 20);
	GPIOF->OTYPER &=(~(0x1 << 10));
	GPIOF->OSPEEDR &= (~(0x3 << 20));
	GPIOF->PUPDR &= (~(0X3 << 20));
}

void LED2_on()
{
	GPIOF->ODR |= (0x1 << 10);
}

void led2_off()
{

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




void led3_init()
{
	GPIOE->MODER &= (~(0x3 << 16));
	GPIOE->MODER |= (0x1 << 16);
	GPIOE->OTYPER &=(~(0x1 << 8));
	GPIOE->OSPEEDR &= (~(0x3 << 16));
	GPIOE->PUPDR &= (~(0X3 << 16));

}

void LED3_on()
{

	GPIOE->ODR |= (0x1 << 8);
}

void led3_off()
{

	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()
{

	*(volatile unsigned int *)0x50000A28 |= (0x3 << 4);

	led1_init(); // LED灯初始化
	led2_init();
	led3_init();
	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;
}

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