ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Понедельник
13 мая
39391 Топик полностью
Abo (19.09.2005 11:56, просмотров: 1) ответил Ken@t на Код в студию
Ответ: Код в студии. Функция прерывания от INT0 Устанавливается аналогично таймерному прерыванию.
static void timerISR(void)
{
  ctl_increment_tick_from_isr();
  /* Clear the timer 0 interrupt */
  T0IR = 0xFF;
}

void SetTimer(unsigned int Period)
{
  ctl_mask_isr(TIMER0_INT);
  PCONP |= (1<<1); // Включим питание таймера
  T0TCR = 0; /* Reset timer 0 */
  T0PR = 0; /* Set the timer 0 prescale counter */
  T0MR0 = Period; /* Set timer 0 match register */
  T0MCR = 3; /* Generate interrupt and reset counter on match */
  T0TCR = 1;   /* Start timer 0 */
  ctl_set_isr(TIMER0_INT, TIMER0_INT_PRIORITY, CTL_ISR_TRIGGER_FIXED, timerISR, 0);
  ctl_unmask_isr(TIMER0_INT);
}

void InitController(void){
  unsigned int temp;
#ifdef _DEBUG_
  debug_printf("Entering into \"void InitController(void)\"\n");
  debug_printf("Seting Up EMC Bank0.\n");
#endif
    // Запрещаем прерывания от всех внутренних устройств
  VICIntEnClr = 0xffffffff;
  
  BCFG0 = (0 << BCFG0_IDCY_BIT) | // 3 WS bw RW cycle
          (0 << BCFG0_WST1_BIT) | // Read len - 3 cyclse
          (1 << BCFG0_RBLE_BIT) | // WE need 
          (1 << BCFG0_WST2_BIT) | // Write len - 2 cycle
          (1 << BCFG0_MW_BIT);
  PINSEL2 = (1<<2)  |             // Разрешим использование JTAG
            (1<<4)  |             // Переведем P2 в режим шины данных, P1.0 - CS0, P1.1 - RD
                                  // Задействуем только младшие 16 бит шины данных, BLS0, BLS1
            (1<<8)  |             // Сигнал WE на P3.27
            (1<<13) |             // Выведем сигнал VPBCLK = 1/2 от CCLK на ногу 40 (P3.27)
            (1<<24) |             // Разрешим сигнал A1 на P3.1
            (3<<25);              // Разрешим сигналы A2-A7 на P3.2-P3.7

#ifdef _DEBUG_
  debug_printf("\tBCFG0.IDCY = %d (Cycles bw transactions)\n",
                (BCFG0 & BCFG0_IDCY_MASK)>>BCFG0_IDCY_BIT);
  debug_printf("\tBCFG0.WST1 = %d (Len of Read access)\n",
                (BCFG0 & BCFG0_WST1_MASK)>>BCFG0_WST1_BIT);
  debug_printf("\tBCFG0.RBLE = %d (1 for WE signal generated)\n",
                (BCFG0 & BCFG0_RBLE_MASK)>>BCFG0_RBLE_BIT);
  debug_printf("\tBCFG0.WST2 = %d (Len of Write access)\n",
                (BCFG0 & BCFG0_WST2_MASK)>>BCFG0_WST2_BIT);
  debug_printf("\tBCFG0.BUSERR = %d (Must be 0)\n",
                (BCFG0 & BCFG0_BUSERR_MASK)>>BCFG0_BUSERR_BIT);
  debug_printf("\tBCFG0.WPERR = %d (Write protect Err Count)\n",
                (BCFG0 & BCFG0_WPERR_MASK)>>BCFG0_WPERR_BIT);
  debug_printf("\tBCFG0.WP = %d (Is Write Protected)\n",
                (BCFG0 & BCFG0_WP_MASK)>>BCFG0_WP_BIT);
  debug_printf("\tBCFG0.BM = %d (Is a Burst Rom Bank)\n",
                (BCFG0 & BCFG0_BM_MASK)>>BCFG0_BM_BIT);
  debug_printf("\tBCFG0.MW = %d (Data Bus Wisth 0-8bit, 1-16bit, 2- 32bit, 3-Reserved)\n",
                (BCFG0 & BCFG0_MW_MASK)>>BCFG0_MW_BIT);
  debug_printf("\tPINSEL2 = 0x%8.8X\n",PINSEL2);
#endif

  // Настраиваем внешнее прерывание от контроллера USB - INT0
  // Как прерывание по спаду
#ifdef _DEBUG_
  debug_printf("Setting INT0 as low level sens. \n");
#endif
  VPBDIV = 0;           // В соответствии с ERRATASEET EXTINT.1 перед обращением
                        // к регистрам EXTMODE и EXTPOLAR 
  
  EXTMODE = 1;          // Прерывание по спаду
  VPBDIV  = 1;          // как написано в ErrataSheet 1.8 EXTINT.2
  VPBDIV = 0;
  temp = EXTMODE;

#ifdef _DEBUG_
  debug_printf("EXTMODE = 0x%2.2X\n",(char)temp);
#endif
  VPBDIV = 0;           // В соответствии с ERRATASEET EXTINT.1 перед обращением
                        // к регистрам EXTMODE и EXTPOLAR 
  EXTPOLAR = 0;         // Прерывание активно при переходе из 1 в 0 
  VPBDIV = 0;

  VPBDIV = VPBDIV_VAL;

#ifdef _DEBUG_
  debug_printf("EXTPOLAR = 0x%2.2X\n", EXTPOLAR);
#endif

  PINSEL1 &= ~(0x3);        // Подключаем вывод P0.16(100 нога)
  PINSEL1 |= 1;             // к входу контроллера прерываний

#ifdef _DEBUG_
  debug_printf("Connecting INT0 to P1.16 (Pin 100). \n");
  debug_printf("PINSEL1 = 0x%8.8X\n", PINSEL1);
#endif
}


int main(void)
{
  ctl_task_init(&main_task, 0, "main");
  ctl_global_interrupts_enable();
#ifdef _DEBUG_
  debug_printf("We are in debug mode.\n");
  debug_printf("Initializing controller.\n");
#endif
  InitController();
  SetTimer(VPB_FREQ / 1000);
  Usb_Initiate();
  while(1){
    CTL_TIME_t CTime = ctl_get_current_time()+1;
    if ((unsigned long)CTime % 5000 == 0){
      debug_printf("Working %d ms.\n",CTime);
      debug_printf("Sof Count %d\n",SOF_Count);
    };
  };

  debug_exit(0);
  return 0;
}