main.c

来自「NXP LPC系列AMR7的开发程序源码(LCD」· C语言 代码 · 共 572 行 · 第 1/2 页

C
572
字号
/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2006
 *
 *    File name   : main.c
 *    Description : Main module
 *
 *    History :
 *    1. Date        :
 *       Author      : Stanimir Bonev
 *       Description : 3, January 2006
 *
 * Jumpers:
 *  EXT/JLINK - the correct jumper position depends on the power source used
 *  DBG       - install the jumper
 *  INT0      - install the jumper
 *  INT1      - install the jumper
 *  INT2      - install the jumper
 * Buttons:
 *  INT0      - Decrement/Backlight On
 *  INT1      - Correct time/Backlight On
 *  INT2      - Increment/Backlight On
 *
 *    $Revision: 13147 $
 **************************************************************************/
#include "includes.h"

#define DLY_100US       100
#define STARTUP_DLY     2
#define LIGHT_DLY       8
#define CORRECT_TO_DLY  5

#define KEY_DOWN        16
#define KEY_UP          15
#define KEY_SET         14

#define CURSOR_X_HOUR   6
#define CURSOR_Y_HOUR   1
#define CURSOR_X_MINUTE 9
#define CURSOR_Y_MINUTE 1
#define CURSOR_X_YEAR   8
#define CURSOR_Y_YEAR   2
#define CURSOR_X_MONTH  12
#define CURSOR_Y_MONTH  2
#define CURSOR_X_DAY    15
#define CURSOR_Y_DAY    2

typedef enum
{
  CLOCK_IDLE = 0, SET_HOUR, SET_MINUTE, SET_YEAR, SET_MONTH, SET_DAY,
} ClockState_t;

volatile Boolean KeyUpPress, KeyDownPress, KeySetPress;

/*************************************************************************
 * Function Name: irq_handler
 * Parameters: none
 *
 * Return: none
 *
 * Description: IRQ handler
 *
 *************************************************************************/
__irq __arm void irq_handler (void)
{
void (*interrupt_function)();
unsigned int vector;

  vector = VICVectAddr;     // Get interrupt vector.
  interrupt_function = (void(*)())vector;
  if(interrupt_function != NULL)
  {
    interrupt_function();  // Call vectored interrupt function.
  }
  else
  {
    VICVectAddr = 0;      // Clear interrupt in VIC.
  }
}

/*************************************************************************
 * Function Name: KeyUpIntHandler
 * Parameters: none
 *
 * Return: none
 *
 * Description: Key UP interrupt handler
 *
 *************************************************************************/
void KeyUpIntHandler (void)
{
  EXTINT = 4;
  KeyUpPress = TRUE;
  VICVectAddr = 0;
}

/*************************************************************************
 * Function Name: KeySetIntHandler
 * Parameters: none
 *
 * Return: none
 *
 * Description: Key SET interrupt handler
 *
 *************************************************************************/
void KeySetIntHandler (void)
{
  EXTINT = 2;
  KeySetPress = TRUE;
  VICVectAddr = 0;
}

/*************************************************************************
 * Function Name: KeyDownIntHandler
 * Parameters: none
 *
 * Return: none
 *
 * Description: Key DOWN interrupt handler
 *
 *************************************************************************/
void KeyDownIntHandler (void)
{
  EXTINT = 1;
  KeyDownPress = TRUE;
  VICVectAddr = 0;
}

/*************************************************************************
 * Function Name: KeysInit
 * Parameters: none
 *
 * Return: none
 *
 * Description: Init Keys
 *
 *************************************************************************/
void KeysInit (void)
{
  // Init variables
  KeyUpPress = KeyDownPress = KeySetPress = FALSE;
  // Set to EXT Interrupt
  PINSEL0_bit.P0_14 = 1;
  PINSEL0_bit.P0_15 = 1;
  PINSEL1_bit.P0_16 = 1;
  // Set ext. interrupt edge to sensitive mode
  EXTMODE = 0x7;
  // Falling Edge
  EXTPOLAR = 0;
  // Set interrupts
  // Assign to IRQ
  VICIntSelect_bit.EINT0 =\
  VICIntSelect_bit.EINT1 =\
  VICIntSelect_bit.EINT2 = 0;
  // Set slots
  VICVectAddr0 = (Int32U) KeyDownIntHandler;
  VICVectCntl0_bit.NUMBER = VIC_EINT0;
  VICVectCntl0_bit.ENABLED = TRUE;
  VICVectAddr1 = (Int32U) KeySetIntHandler;
  VICVectCntl1_bit.NUMBER = VIC_EINT1;
  VICVectCntl1_bit.ENABLED = TRUE;
  VICVectAddr2 = (Int32U) KeyUpIntHandler;
  VICVectCntl2_bit.NUMBER = VIC_EINT2;
  VICVectCntl2_bit.ENABLED = TRUE;
  // Clear pending interrupts
  EXTINT = 0x7;
  // Enable interrupts
  VICIntEnable_bit.EINT0 =\
  VICIntEnable_bit.EINT1 =\
  VICIntEnable_bit.EINT2 = 1;
}

/*************************************************************************
 * Function Name: Dly100us
 * Parameters: void *arg
 *
 * Return: none
 *
 * Description: Delay ~ (arg * 100us)
 *
 *************************************************************************/
void Dly100us (void *arg)
{
  for(volatile Int32U i = (Int32U)arg; i; i--)
  {
    for(volatile Int32U j = DLY_100US; j; j--);
  }
}

/*************************************************************************
 * Function Name: main
 * Parameters: none
 *
 * Return: none
 *
 * Description: main
 *
 *************************************************************************/
int main(void)
{
Rtc_DateTime_t Rtc_DateTime;      // Hold Time and Date
ClockState_t ClockState;          // Watch state
Boolean LcdUpdate;                // LCD text update flag
Boolean ClockUpdate;              // Clock and Data change flag
Boolean CursorOn;                 // Cursor state flag
HD44780_XY_DEF CursorX, CursorY;  // Hold cursor position
Int8S Line1[17], Line2[17];       // Line 1,2
Int32U Dly;
Int32U LightOnDly;                // Backlight time out
Int32U CorrectToDly;              // Correct mode time out
Int8U DayOfMonthMax;              // Hold max day of month
Int32U CheckDly;

  // Init VIC
  __disable_interrupt();
  // Disable PLL
  PLLCON = 0;
  // Write Feed
  PLLFEED = 0xAA;
  PLLFEED = 0x55;
  // Set periphery divider /4
  APBDIV_bit.APBDIV  = 0;
  // Memory map init flash memory is mapped on 0 address
  MEMMAP_bit.MAP = 1;
  // Assign all interrupt channels to IRQ
  VICIntSelect  =  0;
  // Disable all interrupts
  VICIntEnClear = 0xFFFFFFFF;
  // Clear all software interrupts
  VICSoftIntClear = 0xFFFFFFFF;
  // VIC registers can be accessed in User or privileged mode
  VICProtection = 0;
  // Clear interrupt
  VICVectAddr = 0;
  // Clear address of the Interrupt Service routine (ISR) for non-vectored IRQs.
  VICDefVectAddr = 0;
  // 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 GPIO
  KeysInit();
  // Init RTI
  RtcInit();
  // Init LCD
  __enable_interrupt();
  HD44780_PowerUpInit();
  // Init variables
  HD44780SetBackLight(TRUE);
  ClockUpdate = LcdUpdate = CursorOn = FALSE;
  ClockState  = CLOCK_IDLE;
  // Show text on LCD
  HD44780_StrShow(1,1," IAR Systems AB ");
  HD44780_StrShow(1,2,"LPC2103 KS Card ");
  for(Dly = STARTUP_DLY; Dly; Dly--)
  {
    CheckDly = 0x100000;
    while(!RtcSecIntFlag())
    {
      if(--CheckDly == 0)
      {
        RtcReset();
        break;
      }
    }
  }
  LightOnDly = LIGHT_DLY;
  // main loop
  while(1)
  {
    // Check for time change
    if(RtcSecIntFlag())
    {
      ClockUpdate = TRUE;
      // Back light control
      if(LightOnDly)
      {
        if(--LightOnDly == 0)

⌨️ 快捷键说明

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