本帖最后由 ltbytyn 于 2011-11-30 12:26 编辑
不喜欢芯唐的函数库,过于臃肿,且不利于初学者深刻掌握芯片内部构架。故除通信部分外未使用函数库,便于真正理解定时器。
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
/*---------------------------------------------------------------------------------------------------------*/
/* Include hear file */
/*---------------------------------------------------------------------------------------------------------*/
#include "NUC1xx.h"
#include "DrvTIMER.h"
#include "DrvUART.h"
#include "DrvSYS.h"
#include "DrvGPIO.h"
static uint32_t volatile uTimer0Tick = 0;
//static uint32_t volatile uTimer0Time = 0;
static uint32_t volatile uTimer0Type = 1;
void DrvIO_Init(void);
void Time0_Period_Open(void);
/*---------------------------------------------------------------------------------------------------------*/
/* Sample Code Menu */
/*---------------------------------------------------------------------------------------------------------*/
static void TestItem (void)
{
printf("\n\n");
printf("+-----------------------------------------------------------+\n");
printf("| Timer Sample Program(One Shot) |\n");
printf("+-----------------------------------------------------------+\n");
printf("+-----------------------------------------------------------+\n");
printf("| 按下KEY1(GPB15)启动定时器0进入One Shot 模式 |\n");
printf("| 执行一次定时器中断后退出 |\n");
printf("| 启动流水灯 |\n");
printf("+-----------------------------------------------------------+\n");
}
void InitUARTDebugPort(void)
{
STR_UART_T param;
/* Select UART Clock Source From 12MHz */
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
param.u32BaudRate = 115200;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
if (DEBUG_PORT == 1)
{
DrvGPIO_InitFunction(E_FUNC_UART1); /* Set UART1 pins */
DrvUART_Open(UART_PORT1, ¶m); /* Set UART1 configuration */
}
else
{
DrvGPIO_InitFunction(E_FUNC_UART0); /* Set UART0 pins */
DrvUART_Open(UART_PORT0, ¶m); /* Set UART0 configuration */
}
}
void DrvIO_Init(void)
{
// volatile uint32_t u32Reg;
GPIO_T * tGPIO;
tGPIO = (GPIO_T *)((uint32_t)GPIOA);
tGPIO->u32PMD &= 0x0FFFFF00F;
tGPIO->u32PMD |= 0x00000550; //GPA_2、GPA_3、GPA_4、GPA_5为输出模式
/************
GPIOA->u32PMD &= 0x0FFFFF00F;
GPIOA->u32PMD |= 0x00000550; //GPA_2、GPA_3、GPA_4、GPA_5为输出模式
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT); //GPA_2为输出模式
DrvGPIO_Open(E_GPA, 3, E_IO_OUTPUT); //GPA_3为输出模式
DrvGPIO_Open(E_GPA, 4, E_IO_OUTPUT); //GPA_4为输出模式
DrvGPIO_Open(E_GPA, 5, E_IO_OUTPUT); //GPA_5为输出模式
**************/
GPIOA->DOUT &= 0x0FFC3; //GPA_2、GPA_3、GPA_4、GPA_5为输出0
/************
DrvGPIO_ClrBit(E_GPA, 2);
DrvGPIO_ClrBit(E_GPA, 3);
DrvGPIO_ClrBit(E_GPA, 4);
DrvGPIO_ClrBit(E_GPA, 5);
**************/
GPIOB->u32PMD &= 0x0FFFFFFF; //GPB_14、GPB_15为输出模式
/*************
GPIOB->MD.PMD14 = 0x00; //GPB_14为输出模式 0x00输入、0x01输出、0x10开漏、0x11准双向
GPIOB->MD.PMD15 = 0x00; //GPB_15为输出模式
DrvGPIO_Open(E_GPB, 14, E_IO_INPUT); //GPB_14为输出模式
DrvGPIO_Open(E_GPB, 15, E_IO_INPUT); //GPB_14为输出模式
**************/
}
void Time0_Period_Open(void)
{
uTimer0Tick = 1;
uTimer0Type = 1;
SYSCLK->APBCLK.TMR0_EN = 1;
TIMER0->TISR.TIF = 1; //定时器比较中断标志,写1清0
TIMER0->u32TCSR = 0; //关闭定时器
TIMER0->TCSR.MODE = 1; //定时器周期模式
TIMER0->TCSR.IE = 1; //定时器中断使能
TIMER0->TCMPR = 6000000;
//NVIC_SetPriority((IRQn_Type)((uint32_t)TMR0_IRQn + (uint32_t)ch), (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(TMR0_IRQn);
printf(" 按KEY2(GPB_14退出流水灯) ...\n");
TIMER0->TCSR.CEN = 1;
}
void DrvTIMER_Init(void)
{
SYSCLK->CLKSEL1.TMR0_S = 0; /* Set all the default TIMER clock sources are from 12MHz */
}
void TMR0_IRQHandler(void)
{
if ((TIMER0->TCSR.IE == 1) && (TIMER0->TISR.TIF == 1))
TIMER0->TISR.TIF = 1;
if ((TIMER0->TEXCON.TEXEN == 1) && (TIMER0->TEXCON.TEXIEN == 1) && (TIMER0->TEXISR.TEXIF == 1))
TIMER0->TEXISR.TEXIF = 1;
uTimer0Tick++;
if(uTimer0Tick>6)
uTimer0Tick =1;
// uTimer0Time = 500000;
uTimer0Type = 1;
// printf("\n\nTIMER (One Shot Mode)Sample Code \n");
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{
UNLOCKREG();
SYSCLK->;PWRCON.XTL12M_EN = 1; /* Enable External 12M Xtal */
DrvSYS_Delay(5000); /* Waiting for 12M Xtal stable */
LOCKREG();
SYSCLK->CLKSEL1.UART_S = 0; /* Select UART Clock Source From 12MHz */
InitUARTDebugPort(); /* Initial UART debug message function */
DrvSYS_Open(50000000);
DrvTIMER_Init();
TestItem ();
DrvIO_Init();
while(1)
{
if(uTimer0Type)
{
uTimer0Type =0;
switch(uTimer0Tick)
{
case 0x00:
GPIOA->DOUT &= 0x0FFC3;
GPIOA->DOUT |= 0x0003C; //GPA_2\3\4\5为输出1
break;
case 0x01:
GPIOA->DOUT &= 0x0FFC3;
GPIOA->DOUT |= 0x00038; //GPA_2为输出0
break;
//DrvSYS_Delay(500000); /* Waiting for 12M Xtal stable */
case 0x02:
GPIOA->DOUT &= 0x0FFC3;
GPIOA->DOUT |= 0x00034; //GPA_3为输出0
break;
//DrvSYS_Delay(500000); /* Waiting for 12M Xtal stable */
case 0x03:
GPIOA->DOUT &= 0x0FFC3;
GPIOA->DOUT |= 0x0002C; //GPA_4为输出0
break;
//DrvSYS_Delay(500000); /* Waiting for 12M Xtal stable */
case 0x04:
GPIOA->DOUT &= 0x0FFC3;
GPIOA->DOUT |= 0x0001C; //GPA_5为输出0
break;
case 0x05:
GPIOA->DOUT &= 0x0FFC3; //GPA_2\3\4\5为输出0
break;
//DrvSYS_Delay(500000); /* Waiting for 12M Xtal stable */
default :
break;
}
}
if(GPIOB->IN & 0x08000); //GPB_15无按键
else
{
DrvSYS_Delay(500); /* Waiting for 12M Xtal stable */
if(GPIOB->IN & 0x08000); //GPB_15无按键
else
{
while((GPIOB->IN & 0x08000) !=0x08000);
while(GPIOB->IN & 0x08000)
{
Time0_Period_Open();
break;
}
}
}
if(GPIOB->IN & 0x04000); //GPB_14无按键
else
{
DrvSYS_Delay(500); /* Waiting for 12M Xtal stable */
if(GPIOB->IN & 0x04000); //GPB_14无按键
else
{
while((GPIOB->IN & 0x04000) !=0x04000);
while(GPIOB->IN & 0x04000)
{
uTimer0Tick = 0;
uTimer0Type = 1;
printf(" 按KEY1(GPB_15进入周期中断) ...\n");
GPIOA->DOUT |= 0x0FFFF; //GPA_2、GPA_3、GPA_4、GPA_5为输出1
TIMER0->TCSR.CEN = 0;
TIMER0->TISR.TIF = 1; //定时器比较中断标志,写1清0
break;
}
}
}
}
}
|
[local]1[/local] |