/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 8 $
* $Date: 15/07/02 11:18a $
* [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrate how to set GPIO pin mode and use pin data input/output control.
* @note
* Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NUC123.h"
#define HCLK_CLOCK 72000000
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable XT1_OUT(PF0) and XT1_IN(PF1) */
// SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;
/* Enable Internal RC 22.1184MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for Internal RC clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
// /* Enable external XTAL 12MHz clock */
// CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
// /* Waiting for external XTAL clock ready */
// CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
// /* Set core clock as HCLK_CLOCK */
// CLK_SetCoreClock(HCLK_CLOCK);
// /* Enable UART module clock */
// CLK_EnableModuleClock(UART0_MODULE);
// /* Select UART module clock source */
// CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
// /*---------------------------------------------------------------------------------------------------------*/
// /* Init I/O Multi-function */
// /*---------------------------------------------------------------------------------------------------------*/
// /* Set GPB multi-function pins for UART0 RXD(PB.0) and TXD(PB.1) */
// SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
// SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
uint16_t Light = 10 ; //Time lengh of LED lighting. MAX value = PERIOD_LIGHT
typedef void (FUNC_PTR)(void);
__asm spChange(uint32_t _sp)
{
MSR MSP, r0
BX lr
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
FUNC_PTR *func;
volatile uint32_t time=20, a, _sp;
SYS_Init() ;
GPIO_SetMode(PB, BIT4, GPIO_PMD_OUTPUT);
while(time--)
{
PB4 = 0;
for(a=0;a<50000;a++);
PB4 = 1;
for(a=0;a<50000;a++);
}
SYS_UnlockReg();
SYS->IPRSTC2=0xFFFFFFFF; //复位所有外设
SYS->IPRSTC2=0x0;
NVIC->ICER[0] = 0xFFFFFFFF; //关闭中断
CLK->APBCLK |= CLK_AHBCLK_ISP_EN_Msk;
FMC->ISPCON |=1;
FMC->ISPCMD=0x2E;
FMC->ISPADR =0x3000; //设置向量映射的地址
FMC->ISPTRG|=1;
__ISB();
_sp = *(volatile uint32_t *) 0x3000;
spChange(_sp); //设置堆栈
func = (FUNC_PTR *)(*(uint32_t *)(0x3000 + 4)); //函数指针指向第二套代码的复位向量
func(); //跳到第二套代码执行
while(1);
}
|