demo.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 473 行 · 第 1/2 页

C
473
字号
  uart_test();



while(1)           /* the following tests are repeated forever */
{
//=============================================================
//     Switch demo      ( SW2 and SW3)
//=============================================================

    put_value(PM1,0);           /* set port B as input */
    led_on(LED_5);              /* put "4" on LED display */
    both_switches_flag=1;       /* stay in this test until this flag is cleared */

    while (both_switches_flag)
    {
      if (switch2_state == 1)   /* SW2 pushed? */
      {
        put_value(PO4,6);       /* turn on red LED */
      }
      else
      {
        put_value(PO4,7);       /* turn off red LED */
      }

      if (switch3_state == 1)   /* SW3 pushed? */
      {
        put_value(PO4,3);       /* turn on green LED */
      }
      else
      {
        put_value(PO4,7);       /* turn off green LED */
      }
      both_switches();          /* check if both switches are pushed */
    }


//=============================================================
//     ADC demo     (potentiometer)
//=============================================================

  adc_entry();

//=============================================================
//     RTC demo   (hyper terminal)
//=============================================================

  rtc_entry();

//=============================================================
//     I2C demo     (temperature sensor)
//=============================================================

  i2c_entry();

//=============================================================
//     UART0 demo   (hyper terminal)
//=============================================================

  uart_entry();

}

//    return(0);
}
/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/*  Note : Initialize of IRQ needs to be performed before this process.     */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handlers into handler table */
    IRQ_HANDLER_TABLE[INT_SYSTEM_TIMER] = timer_handler;

    /* setup interrupt level */
    set_wbit(ILC0, ILC0_ILR0 & ILC0_INT_LV1);  /* system timer(nIRQ[0]) -> level1 */

    return;
}
/****************************************************************************/
/*  Setup of timer                                                          */
/*  Function : set_timer                                                    */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_timer(void)
{
    put_wvalue(TMEN,  0x0);     /* disable timer (write '0' in TMEN[0])*/
    put_wvalue(TMOVF, 0x01);    /* clear overflow register (write '1' in TMOVF[0])*/

    put_wvalue(TMRLR, VALUE_OF_TMRLR);  /* set TMRLR */

    return;
}

/*****************************************************************************/
/*  System Timer handler                                                     */
/*  Function : timer_handler                                                 */
/*     Parameters                                                            */
/*          Input   :   Nothing                                              */
/*          Output  :   Nothing                                              */
/*****************************************************************************/
void timer_handler(void)
{
  counter++;                                  /* increment 10ms timer counter */

  switch2[2]=switch2[1];                      /* keep last 3 samples of switch 2 */
  switch2[1]=switch2[0];
  switch2[0]=(get_value(PI1)&0x04) >> 2;      /* read SW2 */

  switch3[2]=switch3[1];                      /* keep last 3 samples of switch 3 */
  switch3[1]=switch3[0];
  switch3[0]=(get_value(PI1)&0x08) >> 3;      /* read SW3 */

  if ((switch2[0]+switch2[1]+switch2[2])==0)  /* if last 3 samples indicate switch was pushed */
  {
    switch2_state = 1;                        /* then set flag to "1" - pushed */
  }
  if ((switch2[0]+switch2[1]+switch2[2])==3)  /* if last 3 samples indicate switch was not pushed */
  {
    switch2_state = 0;                        /* then set flag to "0" - not pushed */
  }
  if ((switch3[0]+switch3[1]+switch3[2])==0)  /* if last 3 samples indicate switch was pushed */
  {
    switch3_state = 1;                        /* then set flag to "1" - pushed */
  }
  if ((switch3[0]+switch3[1]+switch3[2])==3)  /* if last 3 samples indicate switch was not pushed */
  {
    switch3_state = 0;                        /* the set flag to "0" - not pushed */
  }

  put_wvalue(TMOVF, 0x01);                    /* clear TMOVF register (write '1' in TMOVF[0]) */
  return;
}


/****************************************************************************/
/*  Setup of timer                                                          */
/*  Function : set_timer                                                    */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void init_switches(void)
{
  int i;

  for (i=0;i<sample;i++)
  {
    switch2[i]=1;        /* initialize SW2 buffer */
    switch3[i]=1;        /* initialize SW3 buffer */
  }

  switch2_state=0;       /* set SW2 to open */
  switch3_state=0;       /* set SW3 to open */

  return;
}


/****************************************************************************/
/*  Flash LEDs to indicate a test has failed                                */
/*  Function : test_failure                                                 */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void test_failure(void)
{
  int toggle;                /* toggle flag for flashing the LEDs */
  both_switches_flag=1;      /* pushing both switches will exit this routine */
  while (both_switches_flag)                  /* go here if RAM error */
  {
    if (counter>20)          /* wait 200ms */
    {
      counter = 0;           /* reset timer counter */

      if (toggle==0)
      {
        toggle=1;            /* toggle the toggle flag */
        put_value(PO4,on);   /* turn on the LEDs */
      }
      else
      {
        toggle=0;            /* toggle the toggle flag */
        put_value(PO4,off);  /* turn off the LEDs */
      }
    }
    both_switches();         /* are both switches pushed? */
  }
  return;
}


/****************************************************************************/
/*  Both switches pushed                                                    */
/*  Function : both_switches                                                */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
/*
Before entering this routine the user should set "both_switches_flag" = 1.
This routine checks for the condition of both switches (SW2 and SW3) pushed.
If both switches are not pushed then this routine exits and does nothing.
If both switches are pushed then this routine waits until boths switches are released.
After the switches are released the "both_switches_flag" is cleared and then exits.

This flag can be used for exiting "WHILE" loops.  Each test/demo is made up
of a "WHILE" loop and stays in that loop until both switches are pushed.
*/

void both_switches(void)
{
  int j;

  if (switch2_state && switch3_state ) /* check for both switches pushed */
    {
      j=1;
      while(j)
      {
        if (!(switch2_state || switch3_state)) /* check for both switches released */
        {
          j=0;
        }
      }
      both_switches_flag=0;
    }
  return;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?