/*
利用定时器对1秒时间内KEY2键的操作情况进行检测,
并通过串口将测试结果传给PC
*/
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvGPIO.h"
#include "DrvSYS.h"
#include "DrvUART.h"
uint8_t test ;
uint8_t inchar[1];
uint32_t TimerCOUN=0;
void TMR0_IRQHandler(void) // Timer0 interrupt subroutine
{
TIMER0->TISR.TIF =1;
TimerCOUN++;
if(test)
{
if( TimerCOUN&0x0200 )
DrvGPIO_SetBit(E_GPA,5);
else
DrvGPIO_ClrBit(E_GPA,5);
}
}
void UART_INT_HANDLE(uint32_t u32IntStatus)
{
if(u32IntStatus & DRVUART_RDAINT)
{
/* Get all the input characters */
while(UART0->ISR.RDA_IF==1)
{
DrvUART_Read(UART_PORT0,inchar,1); /* 保存输入按键 */
}
}
}
void delay()
{
uint32_t i;
for(i=80000;i;i--);
}
int main (void)
{
uint8_t count;
uint32_t loop=1;
STR_UART_T param; //串口参数
UNLOCKREG(); //解锁
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->PWRCON.XTL12M_EN = 1;
DrvSYS_Delay(5000);
LOCKREG();
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟 UART时钟源选择. 00 =外部12MHz 晶振
DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
param.u32BaudRate = 115200; //波特率为115200
param.u8cDataBits = DRVUART_DATABITS_8; //数据位8
param.u8cStopBits = DRVUART_STOPBITS_1; //停止位 1
param.u8cParity = DRVUART_PARITY_NONE; //校验位 无
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; //FIFO存储深度 1 字节
param.u8TimeOut = 0; //FIFO超时设定
if(DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) // 串口开启、结构体整体赋值
printf("串口0打开失败!\n");
DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE); //使能接收数据中断,回调函数 UART_INT_HANDLE
NVIC_DisableIRQ(TMR0_IRQn); //Disable Timer0 interrupt
outpw(&TIMER0->TCSR ,0 ); //Disable Timer0
SYSCLK->CLKSEL1.TMR0_S = 7; //Select 22Mhz for Timer0 clock source
SYSCLK->APBCLK.TMR0_EN =1; //Enable Timer0 clock source
TIMER0->TCSR.MODE=1; //Select periodic mode for operation mode
TIMER0->TCSR.PRESCALE=0; // Set Prescale [0~255]
TIMER0->TCMPR = 2765; // Set TCMPR [0~16777215]
// (1/22118400)*(0+1)*(2765)= 125.01usec or 7999.42Hz
TIMER0->TCSR.IE = 1;
TIMER0->TISR.TIF = 1; //Write 1 to clear for safty
NVIC_EnableIRQ(TMR0_IRQn); //Enable Timer0 Interrupt
TIMER0->TCSR.CRST = 1; //Reset up counter
TIMER0->TCSR.CEN = 0; //UnEnable Timer0
TIMER0->TCSR.TDR_EN=1; // Enable TDR function
DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT ); //led端口设置为输出
DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
DrvGPIO_ClrBit(E_GPA,2); //程序运行指示
printf("\r\n");
printf("====================================\r\n");
printf("* 菜农250 助学计划 *\r\n");
printf("* NUC120助学板 *\r\n");
printf("* 程 序 参 考 *\r\n");
printf("* 菜农Cortex-M0助学园地各位大侠 *\r\n");
printf("* 2011年08月17日 *\r\n");
printf("* 按键单击、多击、长按检测实验 *\r\n");
printf("====================================\r\n");
printf("\r\n");
printf("请在键盘输入 Y 或(y)健启动测试:\r\n");
printf("\r\n");
while(1)
{
if(inchar[0]=='Y'||inchar[0]=='y')
{
printf("现在进行测试中......\r\n");
printf("你可按下KEY2键进行测试\r\n");
printf("\r\n");
break;
}
}
while(1)
{
TimerCOUN=0 ;
count=0;
test=0;
while(TimerCOUN<8000)
{
while( DrvGPIO_GetBit(E_GPB,14) && TimerCOUN<8000 ) ;
TIMER0->TCSR.CEN = 1;
test=1;
delay();
while ( !DrvGPIO_GetBit(E_GPB,14) && TimerCOUN<8000 ); // 键按下检测
if( TimerCOUN<8000 )
count++;
delay();
}
TIMER0->TCSR.CEN = 0;
DrvGPIO_SetBit(E_GPA,5);
printf("测试结果:\r\n");
if(count==0)
printf("你长按了 KEY2 键!\r\n");
else
printf("你按下了KEY2键 %d 击\r\n",count);
inchar[0]=0;
for(test=400;test;test--)
delay();
printf("\r\n");
printf("你可以按下KEY2键继续进行第 %d 次测试:\r\n",++loop);
}
} |