您的位置:首页 > 其它

Cubieboard2裸机开发之(二)板载LED交替闪烁

2014-03-06 09:31 260 查看
[b]前言[/b]

电路原理在文章/article/5068191.html中已经说明,两个LED的原理图是一样的。要使两个LED交替闪烁,只需要在点亮蓝色LED,熄灭绿色LED之后延时一段时间,然后再点亮绿色LED,熄灭蓝色LED,延时一段时间,如此不断循环即可,过程如图1所示。



图1 LED交替闪烁流程图

[b]一、目的[/b]

通过编写裸机程序,使板载的两个LED交替闪烁。

[b]二、源代码说明[/b]

程序比较简单,已给出详细注释,直接看代码好了,如下所示:

/*
* (C) Copyright 2014 Conan Liang <lknlfy@163.com>
*
* two leds on Cubieboard2 flashing alternately
*
*/

/* GPIO reg addr */
#define SUNXI_PIO_BASE          0x01C20800
#define SUNXI_PIO_PH_CFG2_BASE  (SUNXI_PIO_BASE + 0x104)
#define SUNXI_PIO_PH_DAT_BASE   (SUNXI_PIO_BASE + 0x10C)

/* global entry point */
.globl _start
_start: b    reset

reset:
ldr r0, =SUNXI_PIO_PH_CFG2_BASE
/* before write, read the reg value into r1*/
ldr r1, [r0]
/* blue led is connected to PH21, green led is connected to PH20 */
ldr r2, =((0xf << 20) | (0xf << 16))
mvn r3, r2
/* clear 4 bits for PH21 & PH20*/
and r1, r1, r3
/* set PH21 & PH20 for output function */
orr r1, r1, #((0x1 << 20) | (0x1 << 16))
str r1, [r0]

ldr r0, =SUNXI_PIO_PH_DAT_BASE
loop:
/* read the PH data reg */
ldr r1, [r0]
/* clear PH.20(green led off) */
ldr r2, =(1 << 20)
mvn r3, r2
and r1, r1, r3
/* set PH.21(blue led on) */
orr r1, r1, #(1 << 21)
str r1, [r0]

bl delay

/* read the PH data reg */
ldr r1, [r0]
/* clear PH.21(blue led off) */
ldr r2, =(1 << 21)
mvn r3, r2
and r1, r1, r3
/* set PH.20(green led on) */
orr r1, r1, #(1 << 20)
str r1, [r0]

bl delay
b loop
/* never reach */
f:
b f

/* inaccurate delay */
delay:
mov r5, #0
ldr r4, =0x3ffff
d:
sub r4, r4, #1
cmp r5, r4
bne d
mov pc, lr


[b]三、验证[/b]

使用arm-linux-gnueabihf工具编译后生成led2.b文件,再使用mksunxiboot工具在led2.b文件前面加上一个头部,最终生成led2.bin文件,使用以下命令将led2.bin文件烧写到TF中:

#sudo dd if=./led2.bin of=/dev/sdb bs=1024 seek=8

将TF卡插入Cubieboard2,上电即可看到两个LED交替闪烁。效果不好用图片展示,因此就不上图了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: