main.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 157 行

C
157
字号
/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2006
 *
 *    File name   : main.c
 *    Description : main module
 *
 *    History :
 *    1. Data        :
 *       Author      : Stanimir Bonev
 *       Description : 27, April 2006
 *
 *  This example project shows how to use the IAR Embedded Workbench for ARM
 * to develop code for the IAR LM3S102-SK boards.
 * It shows basic use of parallel I/O, timer and the interrupt controller.
 *
 * It starts by blinking the red STAT LED.
 * 
 * Jumpler:
 *  R/W-LED - 1-2
 *
 *    $Revision: 20128 $
 **************************************************************************/
#include <Luminary/iolm3s102.h>
#include <intrinsics.h>

#define SET_INTR_HANDLER(Func,Ind) \
__root const unsigned long Func ## _vec @ (4*Ind) = (unsigned long)&Func

#define SYS_CLK         20000000
#define TICK_PER_SEC    10

#define SYSTEM_DIVIDER  10    // Pll divider
#define MAIN_OSC_TYPE   0x0B  // 6MHz Main OSC

void SysTickIntrHandler (void);
void PortBIntrHandler (void);

#ifdef MCU_REV_A
// Writing RCC with a new XTAL value must be done from
// code running in SRAM.
__ramfunc void WriteIt(unsigned long volatile * ulAddress, unsigned long ulValue)
{
  *ulAddress = ulValue;
  for(ulValue = 0x100; ulValue; ulValue--);
}
#endif

/*************************************************************************
 * Function Name: SysTickIntrHandler
 * Parameters: none
 *
 * Return: none
 *
 * Description: SysTick interrupt handler
 *
 *************************************************************************/
void SysTickIntrHandler (void)
{
  GPIOBDATA ^= 2;
}

/*************************************************************************
 * Function Name: main
 * Parameters: none
 *
 * Return: none
 *
 * Description: Main
 *
 *************************************************************************/
void main (void)
{
unsigned long ulRCC, ulDelay;

  // Init Clock
#ifdef MCU_REV_A
unsigned long * pPLLCFG = (unsigned long *)&PLLCFG;
const unsigned long g_pulPLLCfg[] =
{
  0x0da0, 0x0d40, 0x0c40, 0x0be0, 0x0f01, 0x09c0,
  0x0980, 0x0c41, 0x07e0, 0x0680, 0x0600, 0x0f03
};
#endif

  ulRCC  = RCC;               // Get the current value of the RCC register.
  ulRCC |= (1UL << 11);       // Bypass the PLL and system clock dividers for now.
  ulRCC &= ~(1UL << 22);
  // Write the new RCC value.
#ifdef MCU_REV_A
  WriteIt(&RCC, ulRCC);
#else
  RCC = ulRCC;
#endif
  ulRCC &= 0xF07FC003;
  ulRCC |= ((SYSTEM_DIVIDER-1) << 23) | // System divider
           (1UL << 22)                | // Use the system clock divider
           (0UL << 12)                | // PLL Output Enable
           (1UL << 11)                | // PLL Bypass
           (1UL << 9 )                | // Enable Main OSC
           (MAIN_OSC_TYPE << 6)       | // Main OSC Type
           (0UL << 4 );                 // Main oscillator

  MISC_bit.PLLLMIS = 1;           // Clear the PLL lock interrupt.

  // Write the new RCC value.
#ifdef MCU_REV_A
  WriteIt(&RCC, ulRCC);
  // Write PLLCFG on Rev A.
  *pPLLCFG = g_pulPLLCfg[MAIN_OSC_TYPE-4];
#else
  RCC = ulRCC;
#endif

  // Wait for a bit so that new crystal value and oscillator source can take effect.
  for(ulDelay = 0; ulDelay < 16; ulDelay++);
  // Wait until the PLL has locked.
  for(ulDelay = 32768; ulDelay > 0; ulDelay--)
  {
    if(RIS_bit.PLLLRIS)
    {
      break;
    }
  }
  ulRCC &= ~(1UL << 11);    			// Enable use of the PLL.
  // Write the new RCC value.
#ifdef MCU_REV_A
  WriteIt(&RCC, ulRCC);
#else
  RCC = ulRCC;
#endif
  // Delay for a little bit so that the system divider takes effect.
  for(ulDelay = 0; ulDelay < 16; ulDelay++);

  // Init IO - PORTB.1
  RCGC2_bit.PORTB    = 1;         // PORTB clock enable
  GPIOBAFSEL_bit.no1 = 0;         // PORTB.1 GPIO
  GPIOBDATA_bit.no1  = 1;         // PORTB.1 - High
  GPIOBDIR_bit.no1   = 1;         // PORTB.1 - Output
  GPIOBODR_bit.no1   = 0;         // PORTB.1 - outpur is push-pull
  GPIOBDEN_bit.no1   = 1;         // PORTB.1 - digital input enable

  // Init Sys tick timer
  // Set the timer's period
  SYSTICKRVR = (SYS_CLK / TICK_PER_SEC) - 1;
  SYSTICKCVR = 0;                 // reset the counter
  SYSTICKCSR_bit.CLKSOURCE = 1;   // Core clock
  SYSTICKCSR_bit.TICKINT   = 1;   // enable interrupt
  SYSTICKCSR_bit.ENABLE    = 1;   // Enable the system tick timer

  __enable_interrupt();
  // loop forever
  while(1);
}

⌨️ 快捷键说明

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