本函数实现led4每0.5s闪烁一次。
main- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: timer0 (led4每0.5是闪烁一次)
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- Set_System(); //封装一些初始化模块
- while(1)
- {}
- }
hw_config.c- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- 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_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 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_GPA, 5, E_IO_OUTPUT );
- }
- /*************************************************************************************
- ** Function name: TIMER_Configuration
- ** Descriptions: TIMER配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void TIMER_Configuration()
- {
- DrvTIMER_Init(); //初始化定时器
- DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0); //设定TIMER0的时钟源为外部12MHZ
- DrvTIMER_Open(E_TMR0,1000,E_PERIODIC_MODE); //设定定时器timer0的tick周期,并且启动定时器:定时器通道 TMR0 每秒1000次 周期模式
- DrvTIMER_SetTimerEvent(E_TMR0,500,(TIMER_CALLBACK) Timer0_Callback,0); //安装一个定时处理事件到 timer0通道
- DrvTIMER_EnableInt(E_TMR0); //使能定时器中断 //TIMER0->TCSR.IE = 1
- DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
- }
- /*************************************************************************************
- ** Function name: Timer0_Callback
- ** Descriptions: 定时处理事件,小灯每0.5秒闪烁一次
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void Timer0_Callback (void)
- {
- if (DrvGPIO_GetBit(E_GPA,5))
- DrvGPIO_ClrBit(E_GPA,5);
- else
- DrvGPIO_SetBit(E_GPA,5);
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- 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- 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);
includes.h- #include <stdio.h>
- #include "NUC1xx.h"
- #include "variables.h"
- #include "hw_config.h"
- #include "Driver\DrvSYS.h"
- #include "Driver\DrvGPIO.h"
- #include "Driver\DrvTIMER.h"
工程
|