ARM裸机程序——跑马灯
先看这样一段C代码
本文引用地址:https://www.eepw.com.cn/article/201611/316635.htm#define GPBCON (*(volatile unsigned long *)0x56000010) //这是寄存器的定义,由于GPB引脚在硬件上连接到了LED上,所以用到GPB引脚,那么就要定义相关寄存器,该寄存器定义了GPB相关引脚的工作方式。
#define GPBDAT (*(volatile unsigned long *)0x56000014) //该寄存器用来给引脚上的数据。
int main()
{
GPBCON=0x00000400;
GPBDAT=0x00000000;
return 0;
}
这是完整的跑马灯程序。GPBUP为上拉使能寄存器,在使用管脚过程中,一般定义为使能无效。
程序开头的地址声明以后可以定义为#include "2440addr.h", 这2440addr.h文件包含了arm中所有的寄存器地址的定义。
/* 地址声明 */
#define GPBCON (*(volatile unsigned long *)0x56000010)
#define GPBDAT (*(volatile unsigned long *)0x56000014)
#define GPBUP (*(volatile unsigned long *)0x56000018)
/* 变量声明 */
#define uint unsigned int
#define LED0_ON() (GPBDAT &= ~(1<<5))
#define LED1_ON() (GPBDAT &= ~(1<<6))
#define LED2_ON() (GPBDAT &= ~(1<<8))
#define LED3_ON() (GPBDAT &= ~(1<<10))
#define LED0_OFF() (GPBDAT |= (1<<5))
#define LED1_OFF() (GPBDAT |= (1<<6))
#define LED2_OFF() (GPBDAT |= (1<<8))
#define LED3_OFF() (GPBDAT |= (1<<10))
/* 函数声明 */
void Delay(uint);
/* 延迟函数 */
void Delay(uint x)
{
uint i,j,k;
for(i=0;i
for(k=0;k<0xff;k++);
}
/* 主函数 */
int Main(void)
{
GPBCON = (1<<(5*2)) | (1<<(6*2)) | (1<<(8*2)) | (1<<(10*2));//将LED0、LED1、LED2、LED3的相关引脚都设置为输出
GPBUP = 0xff;
GPBDAT = (1<<5)| (1<<6) | (1<<8)| (1<<10);
while(1)
{
LED0_ON();
Delay(30);
LED0_OFF();
LED1_ON();
Delay(30);
LED1_OFF();
LED2_ON();
Delay(30);
LED2_OFF();
LED3_ON();
Delay(30);
LED3_OFF();
Delay(30);
}
return 0;
}
这里面还一些软件相关配置的问题就不说了。还有就是我用的启动代码把MMU相关内容全部注销掉了,因为裸机程序还用不到。
评论