各位大侠!请指点。
以下时我的main.c
/**************************************************************************//**
* main.c
* V2.10
* $Date: 13/10/07 3:56p $
* MINI51 Series General Purpose I/O Driver Sample Code
*
* @note
* Copyright (C) 2012 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include "stdio.h"
#include "Mini51Series.h"
#include "GPIO.h"
#include "custom.h"
void WDT_IRQHandler(void)
{
// Clear WDT interrupt flag
WDT_CLEAR_
tiMEOUT_INT_FLAG();
// Check WDT wake up flag
if(WDT_GET_RESET_FLAG()) {
WDT_CLEAR_RESET_FLAG();
}
}
void UART_IRQHandler(void)
{
vu32 uart_isr_back;
vu32 uart_fsr_back;
uart_isr_back=UART->ISR;
uart_fsr_back=UART->FSR;
if(UART->ISR&UART_ISR_RDA_INT_Msk)
{
while(UART->ISR&UART_ISR_RDA_IF_Msk)
while(!(UART->FSR&UART_FSR_RX_EMPTY_Msk))
{
if(++uart_rxcnt>uart_buffer_size)
{
uart_rxcnt=uart_buffer_size;
return;
}
uart_data_buffer[uart_rxin]=UART->RBR;
if(++uart_rxin>=uart_buffer_size)
uart_rxin=0;
}
}
}
void TMR0_IRQHandler(void)
{
vu8 i;
///20ms
TIMER_ClearIntFlag(TIMER0);
flag_check_key=1;
M62429_SDA=~M62429_SDA;
}
void TMR1_IRQHandler(void)
{
///190us
TIMER_ClearIntFlag(TIMER1);
M62429_SCL=~M62429_SCL;
}
/**
* @brief Port0/Port1 IRQ
*
* @param None
*
* @return None
*
* @details The Port0/Port1 default IRQ, declared in startup_Mini51.s.
*/
void GPIO01_IRQHandler(void)
{
}
/**
* @brief Port2/Port3/Port4 IRQ
*
* @param None
*
* @return None
*
* @details The Port2/Port3/Port4 default IRQ, declared in startup_Mini51.s.
*/
void GPIO234_IRQHandler(void)
{
P3->ISRC = 0x10;
}
/**
* @brief External INT0 IRQ
*
* @param None
*
* @return None
*
* @details The External INT0(P3.2) default IRQ, declared in startup_Mini51.s.
*/
void EINT0_IRQHandler(void)
{
}
/**
* @brief External INT1 IRQ
*
* @param None
*
* @return None
*
* @details The External INT1(P5.2) default IRQ, declared in startup_Mini51.s.
*/
void EINT1_IRQHandler(void)
{
}
void SYS_Init(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
#if 1
/* Set P5 multi-function pins for XTAL1 and XTAL2 */
SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
SYS->P5_MFP |= (SYS_MFP_P50_XTAL1 | SYS_MFP_P51_XTAL2);
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable external 12MHz XTAL, internal 22.1184MHz */
CLK->PWRCON |= CLK_PWRCON_XTL12M |CLK_PWRCON_IRC22M_EN_Msk|CLK_PWRCON_IRC10K_EN_Msk;
/* Waiting for clock ready */
// CLK_WaitClockReady(CLK_CLKSTATUS_XTL_STB_Msk | CLK_CLKSTATUS_IRC22M_STB_Msk|CLK_CLKSTATUS_IRC10K_STB_Msk);
CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk|CLK_CLKSTATUS_IRC10K_STB_Msk);
/* Switch HCLK clock source to XTL */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_IRC22M,CLK_CLKDIV_HCLK(1));
/* STCLK to XTL STCLK to XTL */
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLK_S_HCLK_DIV2);
#endif
/* Enable IP clock */
CLK_EnableModuleClock(UART_MODULE);
CLK_EnableModuleClock(PWM01_MODULE);
// CLK_EnableModuleClock(WDT_MODULE);
/* Select IP clock source */
CLK_SetModuleClock(UART_MODULE,CLK_CLKSEL1_UART_S_IRC22M,CLK_CLKDIV_UART(1));
// CLK_SetModuleClock(WDT_MODULE,CLK_CLKSEL1_WDT_S_IRC10K,CLK_CLKDIV_UART(1));
CLK_SetModuleClock(TMR0_MODULE,CLK_CLKSEL1_TMR0_S_HCLK,0);
CLK_SetModuleClock(TMR1_MODULE,CLK_CLKSEL1_TMR1_S_HCLK,0);
// CLK_SetModuleClock(TMR0_MODULE,CLK_CLKSEL1_TMR0_S_HCLK,0);
#if 1
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set P0 multi-function pins for UART RXD and TXD */
SYS->P1_MFP &= ~(SYS_MFP_P12_Msk | SYS_MFP_P13_Msk);
SYS->P1_MFP |= (SYS_MFP_P12_RXD | SYS_MFP_P13_TXD);
/* Set P2 multi-function pins for PWM Channel 0~4 */
SYS->P2_MFP = (SYS_MFP_P23_PWM1 );
SYS->P4_MFP &= ~(SYS_MFP_P46_Msk | SYS_MFP_P47_Msk);
SYS->P4_MFP |= (SYS_MFP_P46_GPIO | SYS_MFP_P47_GPIO);
#endif
/* To update the variable SystemCoreClock */
SystemCoreClockUpdate();
/* Lock protected registers */
SYS_LockReg();
}
void UART_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(SYS_IPRSTC2_UART_RST_Msk);
/* Configure UART and set UART Baudrate */
UART_Open(UART, 9600);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{
// int32_t i32Err;
/* Init System, IP clock and multi-function I/O */
SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.
/* Init UART for printf */
UART_Init();
CONTROL4=0;
CONTROL3=0;
UNLOCK=0;
V5_CTRL=0;
V9_CTRL=0;
GPIO_SetMode(P0, BIT0, GPIO_PMD_OUTPUT);
GPIO_SetMode(P0, BIT1, GPIO_PMD_OUTPUT);
GPIO_SetMode(P0, BIT5, GPIO_PMD_OUTPUT);
GPIO_SetMode(P0, BIT6, GPIO_PMD_OUTPUT);
GPIO_SetMode(P0, BIT7, GPIO_PMD_OUTPUT);
MASTER_DETECT=0;
GPIO_SetMode(P0, BIT4, GPIO_PMD_INPUT);
CONTROL2=0;
DOOR2_SEL=0;
DOOR1_SEL=0;
GPIO_SetMode(P1, BIT0, GPIO_PMD_OUTPUT);
GPIO_SetMode(P1, BIT4, GPIO_PMD_OUTPUT);
GPIO_SetMode(P1, BIT5, GPIO_PMD_OUTPUT);
RING_ON=0;
MIC_CTRL=0;
GPIO_SetMode(P2, BIT2, GPIO_PMD_OUTPUT);
GPIO_SetMode(P2, BIT6, GPIO_PMD_OUTPUT);
DOOR1_CALL=0;
DOOR2_CALL=0;
GPIO_SetMode(P2, BIT4, GPIO_PMD_INPUT);
GPIO_SetMode(P2, BIT5, GPIO_PMD_INPUT);
PORT1=0;
PORT2=0;
PORT3=0;
GPIO_SetMode(P3, BIT0, GPIO_PMD_OUTPUT);
GPIO_SetMode(P3, BIT1, GPIO_PMD_OUTPUT);
GPIO_SetMode(P3, BIT2, GPIO_PMD_OUTPUT);
AUDIO12_CTRL=0;
AUDIO_CRTL=0;
GPIO_SetMode(P3, BIT5, GPIO_PMD_INPUT);
GPIO_SetMode(P3, BIT6, GPIO_PMD_INPUT);
M62429_SCL=0;
M62429_SDA=0;
GPIO_SetMode(P4, BIT6, GPIO_PMD_OUTPUT);
GPIO_SetMode(P4, BIT7, GPIO_PMD_OUTPUT);
MUSIC_DATA=0;
MUSIC_RST=0;
CONTROL1=0;
LED_CTRL=0;
GPIO_SetMode(P5, BIT0, GPIO_PMD_OUTPUT);
GPIO_SetMode(P5, BIT1, GPIO_PMD_OUTPUT);
GPIO_SetMode(P5, BIT3, GPIO_PMD_OUTPUT);
GPIO_SetMode(P5, BIT4, GPIO_PMD_OUTPUT);
MUSIC_BUSY=0;
GPIO_SetMode(P5, BIT4, GPIO_PMD_INPUT);
MCU_DATA=0;
GPIO_SetMode(P3, BIT4, GPIO_PMD_INPUT); ////data-in
GPIO_EnableInt(P3, 4, GPIO_INT_RISING);
/* Enable interrupt de-bounce function and select de-bounce sampling cycle time */
GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK, GPIO_DBNCECON_DBCLKSEL_1);
GPIO_ENABLE_DEBOUNCE(P3, BIT4);
#if 1
CLK_EnableModuleClock(TMR0_MODULE);
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 30);
// Update prescale and compare value to what we need in event counter mode.
TIMER_SET_PRESCALE_VALUE(TIMER0, 40);
TIMER_SET_CMP_VALUE(TIMER0, 10000); ////1000-50us 2000-100us
// Counter increase on falling edge
// TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_FALLING_EDGE);
// Start Timer 0
TIMER_Start(TIMER0);
// Enable timer interrupt
TIMER_EnableInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
SYS_ResetModule(SYS_IPRSTC2_TMR1_RST_Msk);
CLK_EnableModuleClock(TMR1_MODULE);
#if 1
TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 100);
// TIMER_SET_PRESCALE_VALUE(TIMER1, 40);
// Update prescale and compare value to what we need in event counter mode.
TIMER_SET_CMP_VALUE(TIMER1, 1900);
// Counter increase on falling edge
// TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_FALLING_EDGE);
#endif
// Start Timer 0
TIMER_Start(TIMER1);
// Enable timer interrupt
TIMER_EnableInt(TIMER1);
NVIC_EnableIRQ(TMR1_IRQn);
UART_ENABLE_INT(UART, (UART_IER_RDA_IEN_Msk));
NVIC_EnableIRQ(UART_IRQn);
// PWM2 frequency is 300Hz, duty 50%
PWM_ConfigOutputChannel(PWM, 1, 2360, 50);
// Enable output of all PWM channels
PWM_EnableOutput(PWM, 0x02);
// PWM_Start(PWM, 0x02);
#endif
init_custom();
/* Waiting for interrupts */
while (1)
{
;
}
}
只要我的外部晶振不启动,
/* Enable external 12MHz XTAL, internal 22.1184MHz */
CLK->PWRCON |=CLK_PWRCON_IRC22M_EN_Msk|CLK_PWRCON_IRC10K_EN_Msk;
我的timer0和timer1就只能启动一个, 然后我只要后定义的是timer1,那就只能timer1启动。
CLK_SetModuleClock(TMR0_MODULE,CLK_CLKSEL1_TMR0_S_HCLK,0);
CLK_SetModuleClock(TMR1_MODULE,CLK_CLKSEL1_TMR1_S_HCLK,0);
找了很久找不到问题!!!求助!!
0