⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ARM7(LPC2124)学习的一些例程
💻 C
字号:
#include <LPC21XX.H>

#define LED1CON 0x00000004	/*LED1接在P0.2上*/
#define LED2CON 0x00000008  /*LED2接在P0.3上*/
#define CR 0x0D

typedef unsigned int uint32;

__irq void IRQ_Eint1(void);/*外部中断1服务子程序声明*/
__irq void IRQ_Eint2(void);/*外部中断2服务子程序声明*/

uint32 times[2] = {30,30};/*循环次数默认为30*/

char *int1Str = "Extern interrupt 1 is activated!";
char *int2Str = "Extern interrupt 2 is activated!";
char *normal = "No extern interrupts!";


void delay30(uint32 ind)  {                         
  unsigned volatile long i,j;
  for(i=0;i<10000;i++)
  for(j=0;j<times[ind];j++)
  ;
  if(times[ind] > 30){
  	times[ind]--;
  }else if(times[ind] <30){
  	times[ind]++;
  }
}

void uart0_init(void)				
{
	U0LCR = 0x83;       			/*UART1初始化,使能DLAB,8bit数据,1个停止位,无校验*/
	U0DLL = 122;						//15MHz,波特率9600
	U0LCR = 0x03;					//DLAB=0
}

void putchar(char ch) 				/*输出一个字符到串口*/
{
	while(!(U0LSR & 0x20));			//等待上次发送结束
	U0THR = ch;
}

void puts(char *string)				/*输出一个字符串到串口*/
{
	while(*string != '\0')
		putchar(*string++);
	putchar(CR);
}

void extInt_init(void)
{
	PINSEL0 |= 0xA0000005;			/*引脚选中EINT1(P0.14),EINT2(P0.15),UART0功能*/
	PINSEL1 = 0x00000000;
	/*以下为中断控制部分*/		
	VICIntSelect = 0;				/*全部中断设置为IRQ,若某位为1是FIQ*/
	VICIntEnable = 0x00018000; 		/*使能EINT1(15),ENT2(16)*/
	VICVectCntl1 = 0x2F;			/*0xF,15号中断*/
	VICVectCntl2 = 0x30;			/*16号中断*/
	VICVectAddr1 = (int)IRQ_Eint1;	/*设置外部中断1服务子程序*/
	VICVectAddr2 = (int)IRQ_Eint2;	/*设置外部中断2服务子程序*/
	EXTINT = 0x07;					/*清除外部中断标志*/
}

int  main(void)
	{
	IO0DIR = LED1CON + LED2CON;
	uart0_init();
	extInt_init();
	while (1){						/*正常状况下两个LED同时缓慢地闪烁*/
	  	IO0CLR = LED1CON + LED2CON;
		delay30(0);
	  	IO0SET = LED1CON + LED2CON;
		delay30(0);
		puts(normal);
	  }
}

void IRQ_Eint1(void)__irq{
  times[0] = 0;	 //更改循环次数,加速LED闪烁
  puts(int1Str);
  while(times[0]!=30){
	IO0CLR = LED1CON;
	delay30(0);
	IO0SET = LED1CON;
	delay30(0);  	
  }

  while((EXTINT&0x02)!=0){
  	EXTINT=0x02;            //清除EINT1中断标志
  }
}

void IRQ_Eint2(void)__irq{
	times[1] = 0;
	puts(int2Str);
	while(times[1]!=30){
		IO0CLR = LED2CON;
		delay30(1);
		IO0SET = LED2CON;
		delay30(1);  	
  	}
	while((EXTINT&0x04) != 0){//清除EINT2中断标志
		EXTINT = 0x04;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -