⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 ATMEL ARM AT91SAM9RL64 seminar handson tutorial
💻 C
字号:
/* ----------------------------------------------------------------------------
*         ATMEL Microcontroller Software Support 
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <board.h>
#include <pio/pio.h>
#include <pio/pio_it.h>
#include <pit/pit.h>
#include <aic/aic.h>
#include <tc/tc.h>
#include <utility/led.h>
#include <utility/trace.h>

//TODO
//#include <rtc/rtc.h>
//TODO
//#include <pwmc/pwmc.h>

#include <stdio.h>

//------------------------------------------------------------------------------
//         Internal variables
//------------------------------------------------------------------------------

/// Delay for pushbutton debouncing (in milliseconds).
#define DEBOUNCE_TIME       500

/// PIT period value in 祍econds.
#define PIT_PERIOD          1000


// TODO A2-Step 1.1 
/// List of button pins that must be configured for use by the application.
static const Pin pPins[] = {PINS_DBGU};
static const Pin pinPB1 = PIN_PUSHBUTTON_1;
static const Pin pinPB2 = PIN_PUSHBUTTON_2;

/// Indicates the current state (on or off) for each LED.
static unsigned char pLedStates[2] = {1, 1};

/// Global timestamp in milliseconds since start of application.
volatile unsigned int timestamp = 0;

//------------------------------------------------------------------------------
//         Local functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Handler for PIT interrupt. Increments the timestamp counter.
//------------------------------------------------------------------------------
static void ISR_Pit(void)
{
  unsigned int status;
  
  // Read the PIT status register
  status = PIT_GetStatus() & AT91C_PITC_PITS;
  if (status != 0) {
    
    // Read the PIVR to acknowledge interrupt and get number of ticks
    timestamp += (PIT_GetPIVR() >> 20);
  }
}

//------------------------------------------------------------------------------
/// Configure the periodic interval timer to generate an interrupt every
/// millisecond.
//------------------------------------------------------------------------------
static void ConfigurePit(void)
{
  // Initialize the PIT to the desired frequency
  PIT_Init(PIT_PERIOD, BOARD_MCK / 1000000);
  
  // Configure interrupt on PIT
  AIC_DisableIT(AT91C_ID_SYS);
  AIC_ConfigureIT(AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, ISR_Pit);
  AIC_EnableIT(AT91C_ID_SYS);
  PIT_EnableIT();
  
  // Enable the pit
  PIT_Enable();
}

//------------------------------------------------------------------------------
/// Interrupt handler for pushbutton #1. Starts or stops LED#1.
//------------------------------------------------------------------------------
static void ISR_Bp1(void)
{
  static unsigned int lastPress = 0;
  
  // Check if the button has been pressed
  if (!PIO_Get(&pinPB1)) {
    
    // Simple debounce method: limit push frequency to 1/DEBOUNCE_TIME
    // (i.e. at least DEBOUNCE_TIME ms between each push)
    if ((timestamp - lastPress) > DEBOUNCE_TIME) {
      
      lastPress = timestamp;
      
      // Toggle LED state
      pLedStates[0] = !pLedStates[0];
      if (!pLedStates[0]) {
        
        LED_Clear(0);
      }
    }
  }
}

//------------------------------------------------------------------------------
/// Interrupt handler for pushbutton #2. Starts or stops LED#2 and TC0.
//------------------------------------------------------------------------------
static void ISR_Bp2(void)
{
  static unsigned int lastPress = 0;
  
  // Check if the button has been pressed
  if (!PIO_Get(&pinPB2)) {
    
    // Simple debounce method: limit push frequency to 1/DEBOUNCE_TIME
    // (i.e. at least DEBOUNCE_TIME ms between each push)
    if ((timestamp - lastPress) > DEBOUNCE_TIME) {
      
      lastPress = timestamp;
      
      // Disable LED#2 and TC0 if there were enabled
      if (pLedStates[1]) {
        
        pLedStates[1] = 0;
        LED_Clear(1);
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
      }
      // Enable LED#2 and TC0 if there were disabled
      else {
        
        pLedStates[1] = 1;
        LED_Set(1);
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
      }
    }
  }
}

//------------------------------------------------------------------------------
/// Configure the pushbuttons to generate interrupts.
//------------------------------------------------------------------------------
static void ConfigureButtons(void)
{
#if defined(at91sam7lek) || defined(at91cap9dk)
  const Pin pinCol0 = PIN_KEYBOARD_COL0;
  PIO_Configure(&pinCol0, 1);
#endif
  
  // Configure pios
  PIO_Configure(&pinPB1, 1);
  PIO_Configure(&pinPB2, 1);
  
  // Initialize interrupts
  PIO_InitializeInterrupts(AT91C_AIC_PRIOR_LOWEST);
  PIO_ConfigureIt(&pinPB1, (void (*)(const Pin *)) ISR_Bp1);
  PIO_ConfigureIt(&pinPB2, (void (*)(const Pin *)) ISR_Bp2);
  PIO_EnableIt(&pinPB1);
  PIO_EnableIt(&pinPB2);
}

//------------------------------------------------------------------------------
/// Configure LEDs #1 and #2 (cleared by default).
//------------------------------------------------------------------------------
static void ConfigureLeds(void)
{
  LED_Configure(0);
  LED_Configure(1);
}

//------------------------------------------------------------------------------
/// Interrupt handler for TC0 interrupt. Toggles the state of LED #2
//------------------------------------------------------------------------------
static void ISR_Tc0(void)
{
  // Clear status bit to acknowledge interrupt
  AT91C_BASE_TC0->TC_SR;
  
  // Toggle LED state
  LED_Toggle(1);
}

//------------------------------------------------------------------------------
/// Configure Timer Counter 0 to generate an interrupt every 250ms.
//------------------------------------------------------------------------------
static void ConfigureTc(void)
{
  unsigned int div;
  unsigned int tcclks;
  
  // Enable peripheral clock
  AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_TC0;
  
  // Configure TC for a 4Hz frequency and trigger on RC compare
  TC_FindMckDivisor(4, BOARD_MCK, &div, &tcclks);
  TC_Configure(AT91C_BASE_TC0, tcclks | AT91C_TC_CPCTRG);
  AT91C_BASE_TC0->TC_RC = (BOARD_MCK / div) / 4; // timerFreq / desiredFreq
  
  // Configure and enable interrupt on RC compare
  AIC_ConfigureIT(AT91C_ID_TC0, AT91C_AIC_PRIOR_LOWEST, ISR_Tc0);
  AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;
  AIC_EnableIT(AT91C_ID_TC0);
  
  // Start the counter if LED is enabled.
  if (pLedStates[1]) {
    
    TC_Start(AT91C_BASE_TC0);
  }
}

//------------------------------------------------------------------------------
/// Waits for the given number of milliseconds (using the timestamp generated
/// by the PIT).
/// \param delay  Delay to wait for, in milliseconds.
//------------------------------------------------------------------------------
static void Wait(unsigned long delay)
{
  volatile unsigned int start = timestamp;
  unsigned int elapsed;
  do {
    elapsed = timestamp;
    elapsed -= start;
  }
  while (elapsed < delay);
}

//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Main function
//------------------------------------------------------------------------------
int main()
{
  
  //TODO 
  //unsigned int DutyCycle;
  
  //TODO 
  //unsigned char hour, minute, second;    
  
  PIO_Configure(pPins, PIO_LISTSIZE(pPins));
  DBGU_Configure(DBGU_STANDARD, 115200, BOARD_MCK);
  printf("-- Getting Started Project 1.4 --\n\r");
  printf("Board : %s, Chip ID : 0x%08X\n\r", BOARD_NAME, AT91C_BASE_DBGU->DBGU_CIDR);
  
  // Configuration
  ConfigurePit();
  ConfigureTc();
  ConfigureButtons();
  ConfigureLeds();
  
  // TODO A2-Step 1.2 
  // Enable PWMC peripheral clock
  
  // TODO A2-Step 1.3 
  // Set clock A to run at 30000Hz (clock B is not used)
  
  // Configure PWMC channel #1
  
  // Period is CLK divided by the Period
  
  // Duty cycle of 10% - Period/ Duty cycle value
  
  
  // TODO A2-Step 2.1 
  // Duty cycle of 100% - Period/ Duty cycle value
  
  
  // TODO A2-Step 1.4 
  // Enable channel #1
  
  
  // TODO A3-Step 1.1 
  // Set time
  
  
  
  // Main loop
  while (1) {
    
    // TODO A2-Step 3.1 Optional
    
    // Wait for LED to be active
    while (!pLedStates[0]);
    
    // TODO A3-Step 1.2
    // Get time
    
    // Display Time
    
    
    
    // Toggle LED state if active
    if (pLedStates[0]) {
      LED_Toggle(0);
    }
    
    // Wait for 500ms
    Wait(500);
    
    //TODO A3-Step 2.1 Optional
    //Wait for the next second
  }
}




⌨️ 快捷键说明

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