/****************************************************************************
* 文件名:main.c
* 功 能:
*
* 说 明:
****************************************************************************/
#i nclude <AT91SAM7S64.h>
#i nclude <SAM7S.h>
/****************************************************************************
* 文件名:irp_in()
* 功 能:中断函数
*
* 说 明:
****************************************************************************/
//---------------------------------------------------------------
void irp_in(void) __irq
{
*AT91C_PIOA_SODR=0x00000001;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000001;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000001;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000001;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000001;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000001;
delay_1ms(500);
*AT91C_AIC_EOICR = 0x00;
}
//---------------------------------------------------------------
void io_irq_handler(void) __irq//I/O中断函数
{
if(*AT91C_PIOA_ISR&(1<<14|1<<15)) //如果检测到了对应管脚的电平变化
{
if(!(*AT91C_PIOA_PDSR&(1<<15)))
{
*AT91C_PIOA_SODR=0x00000002;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000002;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000002;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000002;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000002;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000002;
delay_1ms(500);
}
else if(!(*AT91C_PIOA_PDSR&(1<<14)))
{
*AT91C_PIOA_SODR=0x00000004;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000004;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000004;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000004;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000004;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000004;
delay_1ms(500);
}
}
*AT91C_AIC_EOICR = 1; /* End of Interrupt */
}
/****************************************************************************
* 文件名:init()
* 功 能:初始化IO、中断
*
* 说 明:
****************************************************************************/
void init(void)
{
unsigned long i;
*AT91C_PIOA_PER = 0xffffffff; //设置为POIO
*AT91C_PIOA_PDR = 1<<20; //PA20设为外设
*AT91C_PIOA_BSR = 1<<20; //PA20设为外设B
*AT91C_PMC_PCER = 1<<20; //打开外设时钟
*AT91C_PIOA_OER = 0x0000000f; //PA01,PA02,PA03,PA04设为输出
*AT91C_PIOA_IFER = 0xfffffff0; //打开虑波
AT91C_AIC_SVR[AT91C_ID_IRQ0]=(unsigned long) *irp_in;
i=*AT91C_AIC_ISR;
AT91C_AIC_SMR[AT91C_ID_IRQ0]=0x02<<6;
*AT91C_PMC_PCER = (unsigned int)1<<AT91C_ID_PIOA; //允许IO口的时钟
*AT91C_AIC_IDCR = 1<<AT91C_ID_PIOA; //系统里面禁止中断
*AT91C_PIOA_PER = AT91C_PIO_PA15 | AT91C_PIO_PA14; //PA16,15作为IO口
*AT91C_PIOA_ODR = AT91C_PIO_PA15 | AT91C_PIO_PA14; //PA16,15作为输入
*AT91C_PIOA_IER = AT91C_PIO_PA15 | AT91C_PIO_PA14; //使能PA16,15的中断
*AT91C_AIC_ICCR = 1<<AT91C_ID_PIOA; //清除中断
AT91C_AIC_SMR[AT91C_ID_PIOA] = AT91C_AIC_SRCTYPE_EXT_HIGH_LEVEL | 1;
AT91C_AIC_SVR[AT91C_ID_PIOA]=(unsigned int)*io_irq_handler;
*AT91C_AIC_IECR = 1<<AT91C_ID_IRQ0; //打开AIC中断
*AT91C_AIC_IECR = 1<<AT91C_ID_PIOA; //打开AIC中断
}
/****************************************************************************
* 文件名:main.c
* 功 能:
*
* 说 明:
****************************************************************************/
void main (void)
{
init();
while(1)
{
*AT91C_PIOA_SODR=0x00000008;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000008;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000008;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000008;
delay_1ms(500);
*AT91C_PIOA_SODR=0x00000008;
delay_1ms(500);
*AT91C_PIOA_CODR=0x00000008;
delay_1ms(500);
}
}
延时子程序:
/*-----------------------------------------------------------------------
延时函数
系统时钟:18.423M
-----------------------------------------------------------------------*/
void delay_nus(unsigned long int n) //N us延时函数
{
unsigned int i;
unsigned int j;
for (j=0;j<n;j++)
{
for (i=0;i<3;i++);
}
}
void delay_1ms(unsigned int n) //nms延时函数
{
unsigned int i;
unsigned int j;
for (j=0;j<n;j++)
{
for (i=0;i<3000;i++);
}
}
文章评论(0条评论)
登录后参与讨论