嵌入式——按键灯控制

实验结果:按左键led灯向左亮,按右键led灯向右亮

void  __irq int_int(void);             //声明中断服务函数
     int flag=0;                         //定义一个标志位


void led_Y(void)                    //小灯自左向右依次亮灭
{
	int i,nOut;	
	nOut = 0xF0;	
	rCPLDLEDADDR = nOut | 0x0F;
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x07; 
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x03; 
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x01; 
	for(i = 0; i < 100000; i++);
	
	nOut = 0xFF;
	rCPLDLEDADDR = nOut & 0xF0;
	for(i = 0; i < 100000; i++);
	rCPLDLEDADDR = nOut & 0xF8;  
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut & 0xFC;   
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut & 0xFE;  
	for(i = 0; i < 100000; i++);	
}

void led_Z(void)                     //小灯自右向左依次亮灭
{
	int i,nOut;
	nOut = 0xFF;
	rCPLDLEDADDR = nOut & 0xFE;
	for(i = 0; i < 100000; i++);
	rCPLDLEDADDR = nOut & 0xFC;  
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut & 0xF8;   
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut & 0xF0;  
	for(i = 0; i < 100000; i++);

	nOut = 0xF0;	
	rCPLDLEDADDR = nOut | 0x01;
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x03; 
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x07; 
	for(i = 0; i < 100000; i++);	
	rCPLDLEDADDR = nOut | 0x0F; 
	for(i = 0; i < 100000; i++);
}

void led_m(void)
{
	int i;	
	rCPLDLEDADDR = 0xFF;
	for(i = 0; i < 100000; i++);		
}


中断程序  
void  __irq int_int(void)
{
 	unsigned char Status;

	Status = rCPLDIntStatus;    //中断状态是低有效
	Status = ~(Status & 0x6);    //将状态取反
  
    if(Status & 0x2)         //查询键和控制处理
	{
		flag=0;
		uart_printf(" Eint0 interrupt occurred.\n");
		rCPLDIntControl |= (1<<1);
		rCPLDIntControl &= ~(1<<1);
	}
	else if(Status & 0x4)
	{
		flag=1;
		uart_printf(" EINT1 interrupt occurred.\n");
		rCPLDIntControl |= (1<<2);
		rCPLDIntControl &= ~(1<<2);
		
	}
	 rEINTPEND=(1<<9);       //禁止外部中断EINT8
    ClearPending(BIT_EINT8_23);
}


按键初始化
void int_init(void) 
{
    rSRCPND = rSRCPND;               					// Clear all interrupt
    rINTPND = rINTPND;                					// Clear all interrupt
                                                       // nIntMode='3';
	 rGPGCON |= (0xf<<0);			
    rGPGCON &= (0xa<<0);		

	rCPLDIntControl = 0xFF;                        //控制寄存器 中断允许控制
	rCPLDIntControl = 0xF9;

	pISR_EINT8_23=(UINT32T)int_int;            //中断服务地址填到中断向量表
	rEINTPEND = 0xffffff;                       //外部中断请求设置全1
	rSRCPND = BIT_EINT8_23;	     			// Clear the previous pending states
	rINTPND = BIT_EINT8_23;                   //记录CPU正在处理的中断源
	rEXTINT1 &= ~((0x7<<4)|(0x7<<0));           //电平  控制EINT8为下降沿触发
	rEXTINT1 |= ((0x2<<4)|(0x2<<0));

	rEINTMASK &= ~(3<<8);                   //外部中断屏蔽寄存器,允许EINT8,9
	rINTMSK   &= ~(BIT_EINT8_23);
}

主程序
void int_test(void)
{

	uart_printf("\n External Interrupt Test Example\n");
	int_init();
	while(1)                //等待中断,循环
	{
		if(flag==0)          //标志位为0,灯向左亮;为1,灯向右亮
			led_Z();
		else
			led_Y();
	}
}


你可能感兴趣的:(嵌入式——按键灯控制)