03 ARM11 時鐘、led、串口、蜂鳴器、按鍵

led.bin:start.o uart.o key_led.o
        arm-linux-ld -Ttext 0 -o led.elf start.o uart.o key_led.o
        arm-linux-objcopy -O binary led.elf led.bin
        arm-linux-objdump -D led.elf>led.dis
        cp *.bin /tftpboot/ -r
start.o:start.S
        arm-linux-gcc -c start.S -o start.o
uart.o:uart.c
        arm-linux-gcc -c uart.c -o uart.o
key_led.o:key_led.c
        arm-linux-gcc -c key_led.c -o key_led.o
clean:
        rm -rf *.o *.elf *.dis *.bin /tftpboot/led.bin

Makefile :添加key_led.o , uart.o linux

@
@ 功能:運行後,進入串口功能,按'q'退出進入key_led功能,按key1,key2,key3,key4點
@       亮相應的led燈,按key5所有點亮,按key6進入led循環點亮,並使蜂鳴器響起。
@ 時間:2012-12-01
@

.globl _start
_start:

@外設地址範圍
#define busaddr 0x70000000
#define wtcon 0x7e004000

@led燈使用的寄存器地址
#define gpmcon 0x7f008820
#define gpmdat 0x7f008824

@時鐘使用的寄存器地址
#define apll_lock 0x7e00f000
#define others 0x7e00f900
#define clk_div0 0x7e00f020
#define apll_con 0x7e00f00c
#define mpll_con 0x7e00f010
#define clk_src 0x7e00f01c

@串口使用的寄存器地址
#define gpacon 0x7f008000
#define ulcon0 0x7f005000
#define ucon0 0x7f005004
#define ufcon0 0x7f005008
#define umcon0 0x7f00500c
#define ubrdiv0 0x7f005028
#define udivslot0 0x7f00502c
#define ufstat0 0x7f005018
#define utxh0 0x7f005020
#define urxh0 0x7f005024

@蜂鳴器使用的寄存器
#define gpfcon 0x7f0080a0
#define gpfdat 0x7f0080a4

@告訴CPU外設地址範圍
	ldr r0, =busaddr
	orr r0, r0, #0x13
	mcr p15, 0, r0, c15, c2, 4

@關閉看門狗
	ldr r0, =wtcon
	mov r1, #0
	str r1, [r0]
@調用時鐘初始化代碼,調用led點燈代碼
	bl clock_init
	bl uart_init
	ldr sp, =8*1024
	bl uart
	bl key_led
	bl pwm_buzzer
	bl led_loop

	b .

@蜂鳴器
pwm_buzzer:
	ldr r0, =gpfcon
	ldr r1, =(1<<30)
	str r1, [r0]

	ldr r0, =gpfdat
	ldr r1, =(1<<15)
	str r1, [r0]

	mov pc, lr

@led循環點燈程序
led_loop:
@設置引腳爲輸出模式
	ldr r0, =gpmcon
	ldr r1, =0x1111
	str r1, [r0]

@設置輸出引腳循環輸出低電平
	ldr r0, =gpmdat
led_loop1:
	mov r1, #0xe
	str r1, [r0]
	bl delay
	mov r1, #0xd
	str r1, [r0]
	bl delay
	mov r1, #0xb
	str r1, [r0]
	bl delay
	mov r1, #0x7
	str r1, [r0]
	bl delay
	b led_loop1

@延時
delay:
	ldr r2, =100000
delay_sub:
	sub r2, r2, #1
	cmp r2, #0
	bne delay_sub
	moveq pc, lr


@時鐘初始化爲532MHz
clock_init:

@設置lock_time,有默認值,不設置也能夠
	ldr r0, =apll_lock
	ldr r1, =0xffff
	str r1, [r0]
	str r1, [r0,#4]
	str r1, [r0,#8]

@開啓異步模式
	ldr r0, =others
	ldr r1, [r0]
	bic r1, r1, #0xc0
	str r1, [r0]

@等待異步模式啓動動完成
clock_loop1:
	ldr r1, [r0]
	and r1, #0xf00
	cmp r1, #0
	bne clock_loop1

@設置分頻係數
	ldr r0, =clk_div0
	ldr r1, =((0)|(0<<4)|(1<<8)|(1<<9)|(3<<12))
	str r1, [r0]

@設定apll時鐘頻率
	ldr r0, =apll_con
	ldr r1, =((1)|(3<<8)|(266<<16)|(1<<31))
	str r1, [r0]

@設定mpll時鐘頻率
	ldr r0, =mpll_con
	ldr r1, =((1)|(3<<8)|(266<<16)|(1<<31))
	str r1, [r0]

@切換到apll和mpll時鐘工做
	ldr r0, =clk_src
	mov r1, #0x3
	str r1, [r0]

@初始化完畢,返回
	mov pc, lr

@串口的初始化
uart_init:
@將GPA0、GPA一、GPA二、GPA3設爲對應功能引腳RXD[0],TXD[0],CTSn[0],RTSn[0]
	ldr r0, =gpacon
	ldr r1, =0x22
	str r1, [r0]

@數據位,中止位,校驗位,模式的設置
	ldr r0, =ulcon0
	mov r1, #0x3
	str r1, [r0]

@時鐘的選擇,發送接收的使能
	ldr r0, =ucon0
	mov r1, #0x5
	str r1, [r0]

@FIFO啓用
	ldr r0, =ufcon0
	ldr r1, =0x01
	str r1, [r0]

@流量控制,默認配置,能夠省略
	ldr r0, =umcon0
	mov r1, #0
	str r1, [r0]

@波特率整數部分設置
	ldr r0, =ubrdiv0
	mov r1, #34
	str r1, [r0]

@波特率小數部分設置
	ldr r0, =udivslot0
	ldr r1, =0xdfdd
	str r1, [r0]

	mov pc, lr

 

start.S : 整合時鐘、led、按鍵、串口、蜂鳴器功能。 異步

#define ufstat0 (*(volatile unsigned long *)0x7f005018)
#define urxh0 (*(volatile unsigned char *)0x7f005024)
#define utxh0 (*(volatile unsigned char *)0x7f005020)

char getchar_uart(void)
{
	while ((ufstat0 & 0x7f) == 0)
		;
	return urxh0;
}

void putchar_uart(char c)
{
	while (ufstat0 & (1<<14))
		;
	utxh0 = c;
}

int uart(void)
{
	char p;
	while(1){
		p = getchar_uart();
		if (p == 'q')
			return 0;
		putchar_uart(p);
	}

	return 0;
}

 

uart.c :start.S文件中調用的串口使能函數代碼。 函數

 

#define gpncon (*(volatile unsigned long *)0x7f008830)

#define gpndat (*(volatile unsigned long *)0x7f008834)

#define gpmcon (*(volatile unsigned long *)0x7f008820)

#define gpmdat (*(volatile unsigned long *)0x7f008824)



int key_led(void)

{

	gpncon &= ~(0xfff);

	gpmcon &= ~(0xffff);

	gpmcon |= 0x1111;

	while(1){

		if(gpndat & (1<<0))

			gpmdat |= (1<<0);

		else

			gpmdat &= ~(1<<0);

		if(gpndat & (1<<1))

			gpmdat |= (1<<1);

		else

			gpmdat &= ~(1<<1);

		if(gpndat & (1<<2))

			gpmdat |= (1<<2);

		else

			gpmdat &= ~(1<<2);

		if(gpndat & (1<<3))

			gpmdat |= (1<<3);

		else

			gpmdat &= ~(1<<3);

		if(gpndat & (1<<4))

			gpmdat |= 1;

		else

			gpmdat &= 0;

		if(gpndat & (1<<5))

			gpmdat |= 1;

		else{

			gpmdat &= 0;

			return 0;

		}

	}

	return 0;

}

 

key_led.c :按鍵初始化函數。 oop

 

整個程序功能:運行後,進入串口功能,按'q'退出進入key_led功能,按key1,key2,key3,key4點亮相應的led燈,按key5所有點亮,按key6進入led循環點亮,並使蜂鳴器響起。 code

相關文章
相關標籤/搜索