#define PLL_CLOCK 50000000ul
//CLK_SysTickDelay(1000ul);//delay one ms
void SYS_Init(void)
{
SYS_UnlockReg();
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
CLK_SetCoreClock(PLL_CLOCK);
CLK_EnableModuleClock(TMR0_MODULE|TMR1_MODULE|TMR2_MODULE);
CLK_EnableModuleClock(UART0_MODULE);
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HIRC, CLK_CLKDIV_UART(1));
SYS_LockReg();
}
void SYSctrl_Timer_Init (void)
{
///////============定时器0==========//////////////
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 0);//
TIMER_SET_CMP_VALUE(TIMER0, 221184);//10ms
TIMER_SET_PRESCALE_VALUE(TIMER0, 0);//修改预分频的值
TIMER_EnableInt(TIMER0); // 使能定时器0的中断
NVIC_EnableIRQ(TMR0_IRQn); // 使能定时器0的中断向量
TIMER_Start(TIMER0); // 启动定时器0
////============定时器1==========//////////
TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 0);//
TIMER_SET_CMP_VALUE(TIMER1, 221);//10us
TIMER_SET_PRESCALE_VALUE(TIMER1, 0);
TIMER_EnableInt(TIMER1);
NVIC_EnableIRQ(TMR1_IRQn);
TIMER_Start(TIMER1);
////============定时器2==========//////////
TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 0);//
TIMER_SET_CMP_VALUE(TIMER2, 2212);//100us
TIMER_SET_PRESCALE_VALUE(TIMER2, 0);
TIMER_EnableInt(TIMER2);
NVIC_EnableIRQ(TMR2_IRQn);
TIMER_Start(TIMER2);
//printf("\n\nTIMER2 @ %d Hz\n", TIMER_GetModuleClock(TIMER2));
}
////============定时器0中断============//////////10ms
void TMR0_IRQHandler(void)
{
if(TIMER_GetIntFlag(TIMER0) == 1)
{
TIMER_ClearIntFlag(TIMER0);
PIN_BTN_GREEN_LED=~PIN_BTN_GREEN_LED;
}
}
////============定时器1中断============//////////10us
void TMR1_IRQHandler(void)
{
if(TIMER_GetIntFlag(TIMER1) == 1)
{
TIMER_ClearIntFlag(TIMER1);
}
}
////============定时器2中断============//////////100us
void TMR2_IRQHandler(void)
{
if(TIMER_GetIntFlag(TIMER2) == 1)
{
TIMER_ClearIntFlag(TIMER2);
}
}
////============串口0设置============//////////
void UART0_Init(void)
{
// 设置引脚功能为UART模式
SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);
// 设置UART线路配置
UART_SetLine_Config(UART0, 9600, UART_WORD_LEN_8, UART_PARITY_NONE, UART_STOP_BIT_1);
// 打开UART
UART_Open(UART0, 9600);
// 启用UART中断
UART_EnableInt(UART0, UART_IER_LIN_RX_BRK_IEN_Msk);
NVIC_EnableIRQ(UART0_IRQn);
}
// UART0中断处理函数
void UART0_IRQHandler(void)
{
}
void GPIO_Init(void)
{
GPIO_SetMode(P2, BIT2, GPIO_PMD_OUTPUT);
GPIO_SetMode(P3, BIT5, GPIO_PMD_INPUT);
GPIO_SetMode(P0, BIT6, GPIO_PMD_INPUT);
GPIO_EnableInt(P3, BIT5, GPIO_INT_FALLING);
NVIC_EnableIRQ(GPIO_P0P1_IRQn);
GPIO_EnableInt(P0, BIT6, GPIO_INT_FALLING);
NVIC_EnableIRQ(GPIO_P0P1_IRQn);
GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC,GPIO_DBCLKSEL_8);
GPIO_ENABLE_DEBOUNCE(P0,BIT6);
GPIO_ENABLE_DEBOUNCE(P3,BIT5);
}
void GPIOP0P1_IRQHandler(void)
{
if(GPIO_GET_INT_FLAG(P0,BIT6))
{
GPIO_CLR_INT_FLAG(P0, BIT6);
printf("------------------");
}
else{
P0->ISRC = P0->ISRC;
P1->ISRC = P1->ISRC;
}
}
//中断服务函数
void GPIOP2P3P4_IRQHandler()
{
if(GPIO_GET_INT_FLAG(P3, BIT5))
{
//PIN_BTN_GREEN_LED=~PIN_BTN_GREEN_LED;
GPIO_CLR_INT_FLAG(P3, BIT5);
printf("===============");
}
else//清除其他端口不必要的中断
{
P2->ISRC = P2->ISRC;
P3->ISRC = P3->ISRC;
P4->ISRC = P4->ISRC;
}
}
int main(void)
{
SYS_UnlockReg();
SYS_Init();
SYS_LockReg();
SYSctrl_Timer_Init();
UART0_Init();
GPIO_Init();
printf("===============");
while(1){
} //while循环
} //主循环
|