打印
[菜农助学交流]

第五批 之二 按键外部中断

[复制链接]
2789|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cnjxlxp|  楼主 | 2012-4-15 22:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvGPIO.h"
#include "DrvUART.h"
#include "DrvSYS.h"
int32_t i32BitValue;

void delay_ms(void)//delay form xiaobing's source code //20ms
{
    uint32_t i,j;
for(i=20;i>0;i--)
    for(j=2395;j>0;j--);
}
void EINT0Callback(void) //intp0 service routine  //detect key by interrupt
{
DrvGPIO_DisableEINT0 ();//disable intp0
delay_ms();    //chattering ignore
if (DrvGPIO_GetBit (E_GPB, 14) == 0)
{
  GPIOA->DOUT ^= 0x8; //LED2 turns on and off
  printf("key2 is pressd.\n");  
}
DrvGPIO_EnableEINT0 (E_IO_FALLING, E_MODE_EDGE, EINT0Callback); //enable intp0 again
}   
//uart set//n,8,1,9600
void uart_init(void)
{
  STR_UART_T param;
  DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);//UART clock is ex 12M   
  //SYSCLK->CLKSEL1.UART_S = 0;  
  //UART clock 00 =ex 12MHz  01 = PLL 1x =inner 22MHz  
  DrvGPIO_InitFunction(E_FUNC_UART0); //GPIO use as uart
   
  param.u32BaudRate        = 9600;        
  param.u8cDataBits        = DRVUART_DATABITS_8;     
  param.u8cStopBits        = DRVUART_STOPBITS_1;     
  param.u8cParity          = DRVUART_PARITY_NONE;     
  param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;     
  param.u8TimeOut          = 0;  //         FIFO time out
      
  DrvUART_Open(UART_PORT0, &param);
   
// DrvUART_ClearIntFlag(UART_PORT0, DRVUART_RDAINT);
  //DrvUART_EnableInt(UART_PORT0,DRVUART_RDAINT,UART_INT_HANDLE);
  DrvGPIO_EnableEINT0 (E_IO_FALLING, E_MODE_EDGE, EINT0Callback);  
}
void Sys_Init()
{
UNLOCKREG();            
    SYSCLK->PWRCON.XTL12M_EN = 1;//12M
//DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);        
    DrvSYS_Delay(5000);
//while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);
//SYSCLK->CLKSTATUS.XTL12M_STB
LOCKREG();
uart_init();
//DrvGPIO_Open(E_GPB, 10, E_IO_OUTPUT);//GPB10 output                                
    //DrvGPIO_ClrBit(E_GPB, 10);//if not clear GPB10, the buzzer will on. the default output is 0xffff;
DrvGPIO_Open(E_GPB,14, E_IO_INPUT); //GPB14 15 INPUT
DrvGPIO_Open(E_GPB,15, E_IO_INPUT); //
DrvGPIO_Open(E_GPA,2, E_IO_OUTPUT); //GPA2 output, LED1
DrvGPIO_Open(E_GPA,3, E_IO_OUTPUT); //GPA3 output, LED2
}
int main (void)
{
Sys_Init();
    while(1)
{
  //i32BitValue = DrvGPIO_GetBit (E_GPB, 15);
   
  if (DrvGPIO_GetBit (E_GPB, 15) == 1)   //detect key by polling
  {
    //printf("key2 is not pressd.\n");
    ;
  }
  else
  {
    delay_ms();//chattering ignore
    if (DrvGPIO_GetBit (E_GPB, 15) == 0)
    {
     //delay_ms();
     while(DrvGPIO_GetBit (E_GPB, 15) == 0);
     printf("KEY1 is pressd.\n");
    }
  }
}
}

KEY E-INTP.rar

1.02 MB

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

46

帖子

2

粉丝