本帖最后由 hotpower 于 2012-9-22 12:38 编辑
上代码
main文件- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: TIMER0 0.5定时
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- Set_System(); //调用系统初始化函数
- DrvTIMER_Start(E_TMR0); //启动定时器TIMER0开始计数
- while(1)
- {
- if(ID == 0)
- DrvGPIO_ClrBit(E_PORT3, 1); //LED0-ON
- else
- DrvGPIO_SetBit(E_PORT3, 1); //LED0-OFF
- }
- }
hw_config.c- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- TIMER_Configuration(); //配置TIMER
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88
- DrvSYS_Open(XTL_CLK); // Enable high external clock and use it as system clock (HCLK)
- while (DrvSYS_GetChipClockSourceStatus(XTL_CLK) != 1); //等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open(E_PORT3, E_PIN1, E_IO_OUTPUT); //LED0
- DrvGPIO_Open(E_PORT3, E_PIN6, E_IO_OUTPUT); //LED1
- DrvGPIO_Open(E_PORT5, E_PIN2, E_IO_OUTPUT); //LED2
- DrvGPIO_Open(E_PORT2, E_PIN6, E_IO_OUTPUT); //LED3
- }
- /*************************************************************************************
- ** Function name: TIMER_Configuration
- ** Descriptions: TIMER配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void TIMER_Configuration()
- {
- UNLOCKREG(); //对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88
- DrvSYS_SelectIPClockSource(TMR0_CLK_SET, 0); //设置TIMER0时钟源为:12MHZ
- LOCKREG(); //向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- DrvTIMER_Init(); //初始化一次定时器.
- DrvTIMER_Open(E_TMR0, 2, E_PERIODIC_MODE); //E_TMR0:指定定时器通道为TIMER0 2:定时器每秒中断多少次 E_PERIODIC_MODE: 周期模式
- DrvTIMER_SetTimerEvent(E_TMR0, 1, (TIMER_CALLBACK)TIMER0_Callback, 0);//E_TMR0:在TMR0通道上安装 中断回调函数 1:进入一次中断调用一次回调函数
- DrvTIMER_EnableInt(E_TMR0);
- }
- /*************************************************************************************
- ** Function name: TIMER0_Callback
- ** Descriptions: WDT中断回调函数
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void TIMER0_Callback()
- {
- DrvTIMER_ClearIntFlag (E_TMR0); //清除超时标志
- if(ID == 0)
- ID = 1;
- else ID = 0;
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
hw_config.h- #ifndef __HW_CONFIG_H__
- #define __HW_CONFIG_H__
- void Set_System(void);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void TIMER_Configuration(void);
- void TIMER0_Callback(void);
- void delay_ms(uint32_t count);
- #endif
工程包
|