本帖最后由 Swallow_0322 于 2011-4-2 16:52 编辑
定时器1ms定时并中断配置步骤:
① 初始化定时器:对任何定时器操作都要调用该函数,DrvTIMER_Init();
② 设定时钟源:DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0);
/*TIMER0 时钟源选择:000 =外部12MHz 晶振 、001 =外部 32KHz 晶振 、010 = HCLK 、011 = 外部触发时钟 、1xx =内部 22MHz 振荡器 */
③配置定时器tick及模式:DrvTIMER_Open(E_TMR0,1000,E_PERIODIC_MODE);
/*设定定时器 tick 周期并且启动定时器:定时器通道 TMR0 每秒1000次 周期模式*/
④配置定时器中断DrvTIMER_SetTimerEvent(E_TMR0,1,(TIMER_CALLBACK) Timer0_Callback,0);
/*安装一个定时处理事件到 timer0, timer1, timer2, timer3通道*/
⑤使能定时器中断:DrvTIMER_EnableInt(E_TMR0);
⑥启动定时器:
DrvTIMER_Start(E_TMR0);
各函数的相关参数详见程序注释!
工程文件就不上传了,复制源程序添加到工程模板即可,详情参见第一帖!
源程序如下:
/**************************************************
** 文件名称:NUC120_HOT_TIMER.c
** 文件说明:NUC120助学板练习程序
** 创建日期:2011-04-02
** 修改日期:
** 备 注:定时器0 1ms定时并中断练习
**************************************************/
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvTIMER.h"
#define Run_Led 2 //2----LED1 3----LED2 4----LED3 5----LED4
volatile uint32_t u32Timer0Cnt;
/***************
** 函数声明 **
***************/
void Init_System (void);
void Init_Uart (void);
void Init_Timer0(void);
void UART_INT_HANDLE(uint32_t u32IntStatus);
void Timer0_Callback (void);
/*****************************
** Name: UART_INT_HANDLE
** Function: UART Callback function
** Input: u32IntStatus
** OutPut: None
** Data: 2011-03-17
** Note:
****************************/
void UART_INT_HANDLE(uint32_t u32IntStatus)
{
uint8_t bInChar[1]={0xFF};
if(u32IntStatus & DRVUART_RDAINT)
{
/* Get all the input characters */
while(UART0->ISR.RDA_IF==1)
{
/* Get the character from UART Buffer */
DrvUART_Read(UART_PORT0,bInChar,1);
}
}
}
/*****************************
** Name: Timer0_Callback
** Function: Timer0 Callback function
** Input: None
** OutPut: None
** Data: 2011-04-02
** Note:
****************************/
void Timer0_Callback (void)
{
u32Timer0Cnt++;
/* Toggle LED (GPA4) */
//outpw((uint32_t)&GPIOA->DOUT, inpw((uint32_t)&GPIOA->IN) ^ (1 << 4)); //与下面if else语句相同功能
if (DrvGPIO_GetBit(E_GPA,4))
DrvGPIO_ClrBit(E_GPA,4);
else
DrvGPIO_SetBit(E_GPA,4);
}
/*****************************
** Name: Init_System
** Function: 系统初始化函数
** Input: None
** OutPut: None
** Data: 2011-03-17
** Note:
****************************/
void Init_System(void)
{
/* Unlock the locked registers before access */
UNLOCKREG(x); //寄存器锁定键地址寄存器(RegLockAddr) :有些系统控制寄存器需要被保护起来,以防止误操作而影响芯片运行,
//这些寄存器在上电复位到用户解锁定之前是锁定的。用户可以连续依次写入“59h”, “16h” “88h”到0x5000_0100解锁定.
/* Enable the 12MHz oscillator oscillation */
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->WRCON.XTL12M_EN = 1;
/* Waiting for 12M Xtal stable */
//while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1); //SYSCLK->CLKSTATUS.XTL12M_STB
/*eClkSrc - [in] E_SYS_XTL12M / E_SYS_XTL32K / E_SYS_OSC22M / E_SYS_OSC10K / E_SYS_PLL */
// Note: Only some of NuMicro NUC100 Series support this function.
DrvSYS_Delay(5000);
LOCKREG(x);
//向“0x5000_0100”写入任何值,就可以重锁保护寄存器
}
/*****************************
** Name: Init_Uart
** Function: UART初始化函数
** Input: None
** OutPut: None
** Data: 2011-03-17
** Note:
****************************/
void Init_Uart(void)
{
STR_UART_T param;
/*
声明 UART设置的结构体 位于DRVUART.H
结构体如下
typedef struct DRVUART_STRUCT
{
uint32_t u32BaudRate;
E_DATABITS_SETTINS u8cDataBits;
E_STOPBITS_SETTINS u8cStopBits;
E_PARITY_SETTINS u8cParity;
E_FIFO_SETTINGS u8cRxTriggerLevel;
uint8_t u8TimeOut ;
}STR_UART_T;
*/
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); //使能UART时钟
//SYSCLK->CLKSEL1.UART_S = 0; //UART时钟源选择. 00 =外部12MHz 晶振 01 = PLL 1x =内部 22MHz 振荡器
DrvGPIO_InitFunction(E_FUNC_UART0); //GPB_MFP0-1-2-3置位 GPIO使能UART功能
//outpw(&SYS->GPBMFP, inpw(&SYS->GPBMFP) | (0xF<<0));
param.u32BaudRate = 115200; // 波特率
param.u8cDataBits = DRVUART_DATABITS_8; // 数据位
param.u8cStopBits = DRVUART_STOPBITS_1; // 停止位
param.u8cParity = DRVUART_PARITY_NONE; // 校验位
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; // FIFO存储深度 1 字节
param.u8TimeOut = 0; // FIFO超时设定
/* Set UART Configuration */
if(DrvUART_Open(UART_PORT0,¶m) != E_SUCCESS) // 串口开启、结构体整体赋值
printf("UART0 open failed\n");
/* u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 /UART_PORT2 */
/* sParam -[in] the struct parameter to configure UART */
/* Enable Interrupt and install the call back function */
DrvUART_EnableInt(UART_PORT0, DRVUART_RDAINT,UART_INT_HANDLE);
/*u32Port -[in] UART Channel: UART_PORT0 / UART_PORT1 / UART_PORT2 */
/*u32InterruptFlag -[in] DRVUART_LININT/DRVUART_WAKEUPINT/DRVUART_BUFERRINT/DRVUART_RLSINT */
/* DRVUART_MOSINT/DRVUART_THREINT/DRVUART_RDAINT/DRVUART_TOUTINT */
/*pfncallback -[in] A function pointer for callback function */
}
/*****************************
** Name: Init_Timer0
** Function: Timer0初始化函数
** Input: None
** OutPut: None
** Data: 2011-04-02
** Note:
****************************/
void Init_Timer0(void)
{
DrvTIMER_Init(); //初始化定时器
//User must to call this function before any timer operations after system boot up.
DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0); //设定IP 时钟源
/* Set TIMER0 clock source are from 12MHz */
/* Parameters: */
/* eIpClkSrc- [in] E_SYS_WDT_CLKSRC / E_SYS_ADC_CLKSRC / E_SYS_TMR0_CLKSRC */
/* E_SYS_TMR1_CLKSRC / E_SYS_TMR2_CLKSRC / E_SYS_TMR3_CLKSRC */
/* E_SYS_UART_CLKSRC / E_SYS_CAN_CLKSRC / E_SYS_PWM01_CLKSRC */
/* E_SYS_PWM23_CLKSRC / E_SYS_PWM45_CLKSRC/ E_SYS_PWM67_CLKSRC */
/* E_SYS_FRQDIV_CLKSRC/ E_SYS_I2S_CLKSRC */
DrvTIMER_Open(E_TMR0,1000,E_PERIODIC_MODE); //设定定时器 tick 周期并且启动定时器:定时器通道 TMR0 每秒1000次 周期模式
/* Parameters: */
/* ch - [in] E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* uTicksPerSecond - [in] This value means how many timer interrupt ticks in one second */
/* op_mode - [in] E_TIMER_OPMODE, E_ONESHOT_MODE/E_PERIODIC_MODE/E_TOGGLE_MODE/E_CONTINUOUS_MODE */
DrvTIMER_SetTimerEvent(E_TMR0,1,(TIMER_CALLBACK) Timer0_Callback,0); //安装一个定时处理事件到 timer0, timer1, timer2, timer3通道
/* Parameters: */
/* ch - [in] E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
/* uInterruptTicks - [in] Number of timer interrupt occurred */
/* pTimerCallback - [in] The function pointer of the interrupt callback function */
/* parameter - [in] A parameter of the callback function */
DrvTIMER_EnableInt(E_TMR0); //enable the specified timer interrupt
/* Parameters: */
/* ch - [in] E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
DrvTIMER_Start(E_TMR0); //Start to count the specified timer channel
/* Parameters: */
/* ch - [in] E_TIMER_CHANNEL, it could be E_TMR0/E_TMR1/E_TMR2/E_TMR3 */
}
int main (void)
{
uint8_t test = 250;
Init_System();
Init_Uart();
Init_Timer0();
DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT); //程序运行指示
DrvGPIO_ClrBit(E_GPA,Run_Led);
DrvGPIO_Open(E_GPA,4,E_IO_OUTPUT); //定时器中断指示
printf("\n");
printf("/*==========================\n");
printf("======菜农 %d 助学计划======\n",test);
printf("========NUC120助学板========\n");
printf("======程序参考新唐BSP库======\n");
printf("=======2010年04月02日=======\n");
printf("=========Timer0实验=========\n");
printf("==========================*/\n");
while(1)
{
if (u32Timer0Cnt>999)
{
printf("Timer0 1ms intrrupt happend %d!\n",u32Timer0Cnt);
u32Timer0Cnt = 0;
}
}
}
工程结构:
HOTISP串口调试截图:
GPA.4 波形图:
有不对或者不合适的地方望各位高手不吝赐教!:P
听取大家意见,补传工程文件。
工程文件:
NUC120_HOT_TIMER.rar
(903.99 KB)
|