本帖最后由 strang 于 2011-8-21 01:31 编辑
/**************************************************
** 文件名称:NUC120_HOT_TIMER.c
** 文件说明:NUC120助学板练习程序
** 作 者:21IC_strang
** 创建日期:2011-08-20
** 修改日期:
** 备 注:定时器0 1S定时并中断练习
** 实现功能:定时器0 每秒加1,在数码管上显示,大于99S之后自动清零,循环显示
** 接线方式: 主板 显示板
PA.0----La
PA.1----Lb
PA.2----Lc
PA.3----Ld
PA.4----Le
PA.5----Lf
PA.6----Lg
PA.7----Ldp
PA.8----S1
PA.9----S2
VDD ----VCC
**************************************************/
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvTIMER.h"
unsigned char Table[18]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0xff
};
volatile uint32_t u32Timer0Cnt;
volatile uint32_t count;
/***************
** 函数声明 **
***************/
void Init_System (void);
void Init_Timer0(void);
void Timer0_Callback (void);
/*****************************
** Name: Timer0_Callback
** Function: Timer0 Callback function
** Input: None
** OutPut: None
** Data: 2011-08-20
** Note:
****************************/
void Timer0_Callback (void)
{
if(++u32Timer0Cnt>999)//定时1S
{
if(++count>99)//大于99S清零
{
count=0;
}
u32Timer0Cnt = 0;
}
}
/******************************************************************
** 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->PWRCON.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”写入任何值,就可以重锁保护寄存器
count=0;
}
/******************************************************************
** 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 */
}
/******************************************************************
** Name: disp(unsigned int data,unsigned char weixuan)
** Function: 数码管选通
** Input: None
** OutPut: None
** Data: 2011-08-20
** Note:
*******************************************************************/
void disp(unsigned int data,unsigned char weixuan)
{
GPIOA->DOUT|=(0xff<<0);
GPIOA->DOUT&= ~(Table[data]<<0);
switch(weixuan)
{
case 1:
GPIOA->DOUT&=~(1<<9);
GPIOA->DOUT|=(1<<8);
break;
case 2:
GPIOA->DOUT&=~(1<<8);
GPIOA->DOUT|=(1<<9);
break;
default:break;
}
}
/******************************************************************
** Name: dispsiwei(unsigned int temp)
** Function:
** Input: None
** OutPut: None
** Data: 2011-08-20
** Note:
*******************************************************************/
void dispsiwei(unsigned int temp)
{
unsigned int i;
for(i=0;i<=1000;i++);
disp(temp/10,2);
for(i=0;i<=1000;i++);
disp(temp%10,1);
for(i=0;i<=1000;i++);
}
/******************************************************************
** Name: main (void)
** Function:
** Input: None
** OutPut: None
** Data: 2011-08-20
** Note:
*******************************************************************/
int main (void)
{
Init_System();
Init_Timer0();
while(1)
{
dispsiwei(count);
}
}
[local]1[/local]
[local]2[/local]
[local]3[/local]
定时器0每秒加1,在数码管上显示,大于99S之后自动清零,循环显示 |