本工程实现系统自带的嘀嗒定时器 该定时器可以用来作为RTOS的时钟及测量时间等
main函数- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: 实现系统自带的24为SysTick定时器 以后可以作为RTOS的系统时钟
- 本函数实现 led4每0.1s闪烁一次
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- Set_System(); //封装一些初始化模块
- while(1)
- {}
- }
hw_config函数- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- SysTick_Configuration(); //配置系统滴答定时器SysTick
- }
- /*************************************************************************************
- ** 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, 2, E_IO_OUTPUT ); //led端口设置为输出
- DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- }
- /*************************************************************************************
- ** Function name: SysTick_Configuration
- ** Descriptions: 配置系统滴答定时器SysTick
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void SysTick_Configuration()
- {
- DrvSYS_SelectSysTickSource(0); //0: External 12M clock 1: External 32K clock 2: External 12M clock / 2
- //3: HCLK / 2 4~7: Internal 22M clock / 2
- SysTick->CTRL = 0X03; //[SYST_CSR]向下计数到0将引起SysTick 异常而挂起(进入中断) 计数器运行
- SysTick->VAL = 0x00; //[SYST_CVR]该向SYST_CVR寄存器写入0样确保定时器以SYST_RVR中的值计数,而非任意值
- SysTick->LOAD = 6000000; //[SYST_RVR]6000000/12000000 = 0.5s
- }
- /*************************************************************************************
- ** Function name: SysTick_Handler
- ** Descriptions: 系统滴答定时器SysTick中断函数
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void SysTick_Handler(void)
- {
- static uint8_t count = 0;
- SysTick->CTRL |= (1 << 16);//向SYST_CSR.COUNTFLAG中写1 以清除标志
- count++;
- if(count==3)
- count = 1;
- if(count == 1)
- DrvGPIO_ClrBit(E_GPA,5); //LED4 点亮
- if(count == 2)
- DrvGPIO_SetBit(E_GPA,5); //LED4 熄灭
- }
- /*************************************************************************************
- ** 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头文件- #ifndef __HW_CONFIG_H__
- #define __HW_CONFIG_H__
- void Set_System(void);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void SysTick_Configuration(void);
- void delay_ms(uint32_t count);
- #endif
测试通过 OK
工程
|