/************************************************************************* ** ** 宋海村 ** song_haicun@163.com ** **************************************************************************/ #include #include #include //系统设置,Fosc,Fcclk,Fcco,Fpclk的定义; #define FOSC 11059200UL // OSC [Hz] #define FCCLK FOSC*4 // Core clk [Hz] #define FCCO (FCCLK*4) // Per clk [Hz] #define FPCLK (FCCLK/4)*1 // Timer tick per second #define TICK_PER_SEC (4UL) #define TIM_PER_S(Val) (PCCLK/Val) #define MAX_TICK_PER TIM_PER_S(20) #define MIN_TICK_PER TIM_PER_S(5) // Timer Delta period [ms] #define DELTA_PER (50UL) #define TIM_DPER ((PCCLK*DELTA_PER)/1000UL) #define LED_MASK 1<<18 /************************************************************************* * 函数名称:irq_handler * 入口参数:无 * * 返回参数:无 * * 描 述:IRQ handler * *************************************************************************/ #pragma vector=IRQV __irq __arm void irq_handler (void) { void (*interrupt_function)(); unsigned int vector; vector = VICVectAddr; //获得中断向量 interrupt_function = (void(*)())vector; if(interrupt_function != NULL) { interrupt_function(); //调用中断指向的函数 } else { VICVectAddr = 0; //清除在VIC中的中断 } } /************************************************************************* * 函数名称: Timer0Handler * 入口参数: 无 * * 返回参数: 无 * * 说 明: Timer 0 handler * *************************************************************************/ void Timer0Handler (void) { T0IR_bit.MR0INT = 1; // clear interrupt flag if ((IOSET & LED_MASK) == 0) // Change patern IOSET = LED_MASK; //关闭LED else IOCLR = LED_MASK; VICVectAddr = 0; //pNextPattern = pNextPattern->pNextPattern; //调整当前的链表 } /************************************************************************* * 函数名称: VicInit * 入口参数: 无 * * 返回参数: 无 * * 说 明: Init VIC module * *************************************************************************/ void VicInit (void) { VICIntSelect = 0; // Assign all interrupt chanels to IRQ VICIntEnClear = 0xFFFFFFFF; // Diasable all interrupts VICSoftIntClear = 0xFFFFFFFF; // Clear all software interrutps VICProtection = 0; // VIC registers can be accessed in User or privileged mode VICVectAddr = 0; // Clear interrupt VICDefVectAddr = 0; // Clear address of the Interrupt Service routine (ISR) for non-vectored IRQs. // Clear address of the Interrupt Service routine (ISR) for vectored IRQs. VICVectAddr0 = VICVectAddr1 = VICVectAddr2 = VICVectAddr3 =\ VICVectAddr4 = VICVectAddr5 = VICVectAddr6 = VICVectAddr7 =\ VICVectAddr8 = VICVectAddr9 = VICVectAddr10 = VICVectAddr11 =\ VICVectAddr12 = VICVectAddr13 = VICVectAddr14 = VICVectAddr15 = 0; // Disable all vectored IRQ slots VICVectCntl0 = VICVectCntl1 = VICVectCntl2 = VICVectCntl3 =\ VICVectCntl4 = VICVectCntl5 = VICVectCntl6 = VICVectCntl7 =\ VICVectCntl8 = VICVectCntl9 = VICVectCntl10 = VICVectCntl11 =\ VICVectCntl12 = VICVectCntl13 = VICVectCntl14 = VICVectCntl15 = 0; } /************************************************************************* * 函数名称: Init_timer0 * 入口参数: 无 * * 返回参数: 无 * * 说 明: Init tiner0 * *************************************************************************/ void Init_timer0(void) { // Init timer T0TCR = 2; // Reset and stop timer0 T0CTCR = 0; // Set timer counters mode - clock by PCLK T0PR = 0; // Set timer prescaler T0MR0 = PCCLK/TICK_PER_SEC; // Set timer period T0MCR = 3; // Set mack action - interrupt by MACH0 enable, reset counter T0EMR = 0; // No external action VICIntSelect_bit.TIMER0 = 0; // Assign to IRQ // Set interrupt slots VICVectAddr0 = (unsigned int) Timer0Handler; VICVectCntl0_bit.NUMBER = VIC_TIMER0; VICVectCntl0_bit.ENABLED = 1; VICIntEnable_bit.TIMER0 = 1; // Timer 0 interrupt enable T0TCR = 1; // Enable timer0 } /************************************************************************* * 函数名称: Init_Gpio * 入口参数: 无 * * 返回参数: 无 * * 说 明: Init GPIO * *************************************************************************/ void Init_Gpio(void) { PINSEL0 = PINSEL1 = 0; // Init GPIO SCS_bit.GPIO0M = 0; // Disable fast IO IODIR = LED_MASK; // Set pins connect to LEDs as outputs IOCLR = LED_MASK; // All LEDs off } /************************************************************************* * 函数名称: Init_pll * 入口参数: 无 * * 返回参数: 无 * * 说 明: Init PLL * *************************************************************************/ void Init_pll(void) { PLLCON = 0; // Disable PLL PLLFEED = 0xAA; // Write Feed PLLFEED = 0x55; APBDIV_bit.APBDIV = 0; // Set periphery divider /4 MAMCR_bit.MODECTRL = 0; // Set MAM fully enable MAMTIM_bit.CYCLES = 3; MAMCR_bit.MODECTRL = 2; } /************************************************************************* * 函数名称: main * 入口参数: 无 * * 返回参数: 无 * * 描 述: main * *************************************************************************/ void main(void) { Init_pll(); // Memory map init flash memory is maped on 0 address #ifdef FLASH MEMMAP_bit.MAP = 1; #else MEMMAP_bit.MAP = 2; #endif __disable_interrupt(); VicInit(); Init_Gpio(); Init_timer0(); __enable_interrupt(); while(1) {}; }