关于mini2440的一个裸机程序——LED与按键中断

 
/*	
 *File 				: main.c
 *Description		: When you press the key,
 *					  the LED will show the key_value of key by the binary 
 *					  and produce the buzzer.
 *Author			: Cong
 *Copyright			: Cong
 *
 *
 *History
 *---------------------
 *Rev	:0.0
 *Date	:2011/11/26
 *Creat.
 *--------------------
 *
 *
 */
 
 //--------------------Include Files------------------------------------//
#include "2440addr.h"
#include "2440slib.h"


//---------------------Define Valurable-------------------------------//

#define GLOBAL_CLK 1

#define LED1_ON   ~(1<<5)
#define LED2_ON   ~(1<<6)
#define LED3_ON   ~(1<<7)
#define LED4_ON   ~(1<<8)

#define LED1_OFF (1<<5)
#define LED2_OFF (1<<6)
#define LED3_OFF (1<<7)
#define LED4_OFF (1<<8)

#define BEEP 0

#define LED1 10
#define LED2 12
#define LED3 14
#define LED4 16

#define KEY1 0
#define KEY2 6
#define KEY3 10
#define KEY4 12
#define KEY5 14
#define KEY6 22

//--------------------Function Prototype------------------------------//
static void LEDKEY_port_init(void);
static void Show_num(int num);
static void Beep_run(void);
static void delay(unsigned int times);
static void __irq key_handler(void);

/*
 *
 *程序入口
 */
 
 
 void Main(void)
 {
 	LEDKEY_port_init();
 	while(1)
 	{
 		
 	}
 }
 /*
  *===================Function======================
  *	Name			:Port_init
  * Discription		:init the GPIO
  *=================================================
  */
  static void LEDKEY_port_init(void)
 {
 	//初始化LED与BEEP所要用到的IO   用到的这五个端口都配置为输出
 	rGPBCON &= ~((3<<BEEP)|(3<<LED1)|(3<<LED2)|(3<<LED3)|(3<<LED4));
 	rGPBCON |= (1<<BEEP)|(1<<LED1)|(1<<LED2)|(1<<LED3)|(1<<LED4);
 	
 	//初始化KEY所用的IO     所有的KEY端口都初始化为中断方式
 	rGPGCON &= ~((3<<KEY1)|(3<<KEY2)|(3<<KEY3)|(3<<KEY4)|(3<<KEY5)|(3<<KEY6));
 	rGPGCON |= (2<<KEY1)|(2<<KEY2)|(2<<KEY3)|(2<<KEY4)|(2<<KEY5)|(2<<KEY6);
 	
 	//中断初始化EINT8
// 	rEXTINT1 &=~((0xf)|(0xf<<12)|(0xf<<20)|(0xf<<24)|(0xf<<28));			//设置K1的低电平中断
 	rEXTINT1 &=~0xfff0f00f;
 	rEXTINT2 &=~(0xf<<12);
 	rEINTPEND |=(1<<8)|(1<<11)|(1<<13)|(1<<14)|(1<<15)|(1<<19);		//清除K1的中断标志
 	rEINTMASK = ~((1<<8)|(1<<11)|(1<<13)|(1<<14)|(1<<15)|(1<<19));	//打开EINT8的中断使能
 	
 	pISR_EINT8_23 = (U32)key_handler;
 	EnableIrq(BIT_EINT8_23);//外部中断8-23共用的中断位使能,与ClearPending对应	
 }
 
 /*
  *===================Function======================
  *	Name			:Show_num
  * Discription		:init the GPIO of Key
  *=================================================
  */
 static void Show_num(int data)
 {
 	if(data & 0x08)   rGPBDAT = rGPBDAT&(LED4_ON);
    else           rGPBDAT = rGPBDAT|(LED4_OFF);
  	if(data & 0x04)   rGPBDAT = rGPBDAT&(LED3_ON);
    else           rGPBDAT = rGPBDAT|(LED3_OFF);
  	if(data & 0x02)   rGPBDAT = rGPBDAT&(LED2_ON);
    else           rGPBDAT = rGPBDAT|(LED2_OFF);
  	if(data & 0x01)   rGPBDAT = rGPBDAT&(LED1_ON);
    else           rGPBDAT = rGPBDAT|(LED1_OFF);
 }
 
 /*
  *===================Function======================
  *	Name			:Beep_run
  * Discription		:produce the buzzer
  *=================================================
  */
 static void Beep_run(void)
{
    rGPBDAT |= (0x1<<0);
    delay(50);
    rGPBDAT &= (0x0<<0);
    delay(50);
}
 
 /*
  *===================Function======================
  *	Name			:delay
  * Discription		:produce delay ,the time is times.
  *=================================================
  */
static void delay(unsigned int times)
{
	unsigned int i;
	for(;times>0;times--)
	{
		for(i=400;i>0;i--);
	}
}
 /*
  *===================Function======================
  *	Name			:key_handler
  * Discription		:when the key is pressed,
  *					 the IRQ service will be run.
  *=================================================
  */ 
 static void __irq key_handler(void)
 {
 	Beep_run();
 	if(rEINTPEND & (1<<8))
 	{
 		rEINTPEND |=1<<8;
 		Show_num(1);
 	}
 	else if(rEINTPEND & (1<<11))
 	{
 		rEINTPEND |=1<<11;
 		Show_num(2);
 	}
	else if(rEINTPEND & (1<<13))
 	{
 	 	rEINTPEND |=1<<13;
 		Show_num(3);
 	} 	
 	else if(rEINTPEND & (1<<14))
 	{
 		rEINTPEND |=1<<14;
 		Show_num(4);
 	}
 	else if(rEINTPEND & (1<<15))
 	{
 		rEINTPEND |=1<<15;
 		Show_num(5);
 	}
 	else if(rEINTPEND & (1<<19))
 	{
	 	rEINTPEND |=1<<19;
 		Show_num(6);
 	}
 	ClearPending(BIT_EINT8_23);	//用来清除中断标志位;针对全局的中断		
 }
 


实现的功能:当按下mini2440下键盘下的6个按键中的一个时,蜂鸣器发出声音,并且4个发光二极管将以2进制的形式显示出按键的值。其中要比较注意的是EINTPEND的清零是通过置“1”清零的。因为在一些情况下,置一可以1+1=10,可以达到比较高效的目的。

其中以上中断程序是比较有问题的,因为在中断服务程序中还有延时,这样对实时性十分不利,但是在此只是进行ARM裸机程序的简单学习就不深入讨论了,因为我也是才开始学习,只是对一些东西知道,现在也不知道更好的解决方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值