匯編語言實現led燈的跑馬燈


led實驗
1.看原理圖
看設備工作的原理(可能需要閱讀芯片手冊),看設備與cpu的連接關系

 

GPIO具有輸入輸出功能。
輸入:cpu想知道io引腳是高電平還是低電平那么就是輸入方式
輸出:cpu想控制io引腳為高電平還是低電平那么就是輸出方式
跟電流的方向沒有任何關系


2. cpu的相關章節
GPJ2CON control是配置這個引腳是什么功能
GPJ2有8個引腳,每個引腳由con寄存器中的4個位進行配置

GPJ2DAT 如果cpu要輸出高電平或者低電平,就需要設置該寄存器,只有8位有效
When the port is configured as input port, the corresponding
bit is the pin state. When the port is configured as output
port, the pin state is the same as the corresponding bit.
When the port is configured as functional pin, the undefined
value will be read.
當配置為輸入模式的時候,dat寄存器中的某一位的值由引腳設置,引腳是高電平是,對應的位為1,引腳為低電平時,對應的位為0
當配置為輸出模式的時候,dat寄存器中的某一位控制引腳的電平,對應的位為1時,對應的引腳輸出高電平,對應的位為0時,對應的引腳輸出為低電平
其他功能模式時,讀到的值是未定義的。

 

@gec210 light first led
.globl _start
_start:
ldr r0,=0xe0200280
mov r1,#1<<4
str r1,[r0] @config pin0 output mode

mov r1,#0
str r1,[r0,#4] @output low level

b . @same as while(1);

 


arm-linux-gcc -c led.S -o led.o //編譯不鏈接
arm-linux-ld -Ttext 0x30008000 led.o -o led.elf //鏈接指定代碼段起始位置
arm-linux-objcopy -O binary led.elf led.bin //生成二進制執行文件
arm-linux-objdump -D led.elf > led.dis //生成反匯編代碼


uboot的幾個常用命令
printenv 打印顯示環境變量

ipaddr=192.168.1.4 //開發板的ip
serverip=192.168.1.2 //tftp服務器的ip

設置為各自的ip,只是設置到內存,掉電就沒有
setenv ipaddr 192.168.1.x
setenv serverip 192.168.1.x

saveenv //保存到flash中,再次啟動后為剛剛設置的值

ping //單向的,只能從開發板ping電腦
alive表示網絡是通的,not alive表示網絡不通


tftp 30008000 led.bin //下載二進制文件到內存0x30008000地址
go 30008000 //跳轉到0x30008000運行程序

 

 


@gec210 light first led
.globl _start
_start:
ldr r0,=0xe0200280
ldr r1,=(1<<0 | 1<<4 | 1<<8 | 1<<12)
str r1,[r0] @config pin0-pin3 output mode

mov r1,#0
str r1,[r0,#4] @output low level

b . @same as while(1);

 

 

簡單的makefile
APP=led

$(APP).bin:$(APP).o
arm-linux-ld -Ttext 0x30008000 $^ -o $(APP).elf
arm-linux-objcopy -O binary $(APP).elf $@
arm-linux-objdump -D $(APP).elf > $(APP).dis
cp $@ /home/gec/tftp/

%.o:%.s
arm-linux-gcc $^ -c -o $@

%.o:%.S
arm-linux-gcc $^ -c -o $@

%.o:%.c
arm-linux-gcc $^ -c -o $@


clean:
@rm -f $(APP).bin $(APP).elf $(APP).dis *.o

 

 

 

四個燈同時點亮或者同時熄滅
循環閃爍
@gec210 light first led
.globl _start
_start:
ldr r0,=0xe0200280
ldr r1,=(1<<0 | 1<<4 | 1<<8 | 1<<12)
str r1,[r0] @config pin0-pin3 output mode

loop:
mov r1,#0
str r1,[r0,#4] @output low level

bl delay

mov r1,#0xf
str r1,[r0,#4]

bl delay

b loop

b . @same as while(1);


delay:
mov r4,#0xff00000
delay1:
subs r4,r4,#1
bne delay1
mov pc,lr

 

#define GPJ2CON (unsigned long *)0xe0200280

unsigned long *p = (unsigned long *)0xe0200280;
p


練習:
第1個燈到第4個燈依次點亮,第4個燈到第1個燈依次熄滅,循環。

.globl _start
_start:
ldr r0,=0xe0200280
ldr r1,=0x1111
str r1,[r0] @config pin0 output mode

loop:
mov r1,#0xe
str r1,[r0,#4]
bl delay

mov r1,#0xc
str r1,[r0,#4]
bl delay

mov r1,#0x8
str r1,[r0,#4]
bl delay

mov r1,#0
str r1,[r0,#4]
bl delay

mov r1,#0x8
str r1,[r0,#4]
bl delay

mov r1,#0xc
str r1,[r0,#4]
bl delay

mov r1,#0xe
str r1,[r0,#4]
bl delay

mov r1,#0xf
str r1,[r0,#4]
bl delay

b loop

b . @same as while(1);

delay:
mov r4,#0xff00000
delay1:
subs r4,r4,#1
bne delay1
mov pc,lr


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM