1、流水灯原理分析
流水灯又叫跑马灯,实现的效果就是:挨着的LED依次点亮熄灭(同时只有1颗LED亮的)
2、流水灯编写(使用循环)
LED1亮延时 + LED2亮延时 + LED3亮延时 + 循环
4、总结:跟着朱老师一步步做实验根本不难。
编程操控硬件的步骤:1、分析硬件工作原理、2、分析原理图、3、分析数据手册、4、找到相关的SFR、5、写代码设置寄存器得到想要的效果。每个硬件都是这样操控的。
示例代码1:
#define GPJ0CON 0xE0200240
#define GPJ0DAT 0xE0200244
.global _start
_start:
flah:
//第一步:设置GPJ0CON控制寄存器,设置方法是直接把所有引脚设置为输出模式
ldr r0, =0x11111111
ldr r1, =GPJ0CON
str r0, [r1]
//第二步:点亮LED1,其它的灭
ldr r0, =(0<<3)|(1<<4)|(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
//第三步:点亮LED2,其它的灭
ldr r0, =(1<<3)|(0<<4)|(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
//第四步:点亮LED3,其它的灭
ldr r0, =(1<<3)|(1<<4)|(0<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
bl flah
//延时函数
delay:
ldr r2, =9000000
ldr r3, =0x00
delay_oop:
sub r2, r2, #1
cmp r2, r3
bne delay_oop
mov pc, lr
示例代码2—位运算小技巧
#define GPJ0CON 0xE0200240
#define GPJ0DAT 0xE0200244
.global _start
_start:
flah:
//第一步:设置GPJ0CON控制寄存器,设置方法是直接把所有引脚设置为输出模式
ldr r0, =0x11111111
ldr r1, =GPJ0CON
str r0, [r1]
//第二步:点亮LED1,其它的灭
ldr r0, = ~(1<<3)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
//第三步:点亮LED2,其它的灭
ldr r0, = ~(1<<4)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
//第四步:点亮LED3,其它的灭
ldr r0, = ~(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//延时
bl delay
bl flah
delay:
ldr r2, =9000000
ldr r3, =0x00
delay_oop:
sub r2, r2, #1
cmp r2, r3
bne delay_oop
mov pc, lr
示例代码3—加入LED4实现流水灯
#define GPJ0CON 0xE0200240
#define GPJ0DAT 0xE0200244
#define GPD0CON 0xE02000A0
#define GPD0DAT 0xE02000A4
.global _start
_start:
flah:
//第一步:设置GPJ0CON和GPD0CON控制寄存器,设置方法是直接把所有引脚设置为输出模式
ldr r0, =0x11111111
ldr r1, =GPJ0CON
str r0, [r1]
ldr r2, =0x11111111
ldr r3, =GPD0CON
str r2, [r3]
//第二步:点亮LED1,其它的灭
ldr r0, = (0<<3)|(1<<4)|(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//GPD0DAT控制LED4
ldr r2, = 0b1111
ldr r3, =GPD0DAT
str r2, [r3]
//延时
bl delay
//第三步:点亮LED2,其它的灭
ldr r0, = (1<<3)|(0<<4)|(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//GPD0DAT控制LED4
ldr r2, = 0b1111
ldr r3, =GPD0DAT
str r2, [r3]
//延时
bl delay
//第四步:点亮LED3,其它的灭
ldr r0, = (1<<3)|(1<<4)|(0<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//GPD0DAT控制LED4
ldr r2, = 0b1111
ldr r3, =GPD0DAT
str r2, [r3]
//延时
bl delay
//第五步:点亮LED4,其它的灭
ldr r0, = (1<<3)|(1<<4)|(1<<5)
ldr r1, =GPJ0DAT
str r0, [r1]
//GPD0DAT控制LED4
ldr r2, = 0b1101
ldr r3, =GPD0DAT
str r2, [r3]
//延时
bl delay
bl flah
delay:
ldr r2, =9000000
ldr r3, =0x00
delay_oop:
sub r2, r2, #1
cmp r2, r3
bne delay_oop
mov pc, lr