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

📄 main.c

📁 freescale 协处理器应用相关实例
💻 C
字号:
/******************************************************************************
													            Copyright (c) Freescale 2006
File Name    : $RCSfile: main.c,v $

Current Revision :	$Revision: 1.1 $

PURPOSE: Example 4: Using Semaphores
                                                                          
                                                                       
DESCRIPTION:  In this example you will use semaphores to control access to
              a single resource.
                          
                                                                                                                   
UPDATE HISTORY                                                            
REV  AUTHOR    DATE        DESCRIPTION OF CHANGE                          
---  ------    --------    ---------------------                          
1.0                        - initial coding
1.1  b06321    13/12/06    - ported to S12XE

     *******************************************************************
     * File created by: Freescale East Kilbride MCD Applications Group *
     *******************************************************************

******************************************************************************/
#include <hidef.h>      /* common defines and macros */
#include <mc9s12xep100.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xep100"


#include <string.h>
#include "xgate.h"

#define CPU_Request 0x01;
#define XGate_Request 0x81;

// Global variables
unsigned char PotValue;
unsigned char FlashCount,XFlashCount;
char diff;
unsigned char latestValue;

/******************************************************************************
Function Name  : SetupXGATE
Engineer       : r32151	
Date           : 01/03/2005
Parameters     : NONE
Returns        : NONE
Notes          : initialize the XGATE vector block
******************************************************************************/
static void SetupXGATE(void) {
  /* initialize the XGATE vector block and
     set the XGVBR register to its start address */
  XGVBR= (unsigned int)(void*__far)(XGATE_VectorTable - XGATE_VECTOR_OFFSET);

  /* enable XGATE mode and interrupts */
  XGMCTL= 0xFBC1; /* XGE | XGFRZ | XGIE */
}


/******************************************************************************
Function Name  : main
Engineer       : r32151	
Date           : 01/03/2005
Parameters     : NONE
Returns        : NONE
Notes          : main routine called by Startup.c, Sets up required ports, PIT and ADC.
                 Allocates events between CPU and XGATE.
                 Triggers PIT if ADC value changes by > 5
******************************************************************************/
void main(void)
{
  /* put your own code here */
  SetupXGATE();
  
    /* Set up Port A */
  PORTA = 0x00;
  DDRA = 0x0F;
  PUCR = 0x42;

  /* Enable PIT channels 0 & 1 */
  PITCFLMT = 0x80;    /* Enable PIT */
  PITCE = 0x03;       /* Enable channel 0 & 1 */
  PITMUX = 0x00;      /* Use microtimer 0 for all channels */
  PITMTLD0 = 100;     /* Microtimer count of 100 */
  PITLD0 = 5000;      /* Channel 0 counter of 5000 */
  PITLD1 = 4000;      /* Channel 1 counter of 4000 */
  
  //Configure ADC00
  ATD0CTL1 = 0x00;    /* Trigger source AD0 */
  ATD0CTL2 = 0x40;    /* Fast Flag Clear */
  ATD0CTL3 = 0x8B;    /* Right-just, 1 conv/seq, freeze immed. (debug) */
  ATD0CTL4 = 0xA1;    /* 16 clocks/sample ,f(atd) = f(bus)/4 */
  ATD0CTL5 = 0x20;    /* SCAN, AD0 */
  
  //Configure PP0 & PP1 buttons Key wake up
  PERP = 3;
  PPSP = 0;
  PIFP = 3;
  PIEP = 3;
  
  //Initialise variables
  PotValue = 0;

  /* Allocate events to CPU or XGate */
  INT_CFADDR = 0x70;  /* Set interrupt control page to PIT channels */
  INT_CFDATA4 = XGate_Request;  /* Send interrupt to XGate (level 1) */
  INT_CFDATA5 = CPU_Request;  /* Send interrupt to CPU (level 1) */
  INT_CFADDR = 0x80;  /* Set interrupt control page to Port P channel */
  INT_CFDATA7 = XGate_Request;  /* Send interrupt to XGate (level 1) */

  //IRQ pin is floating on DEMO boards
  INT_CFADDR = 0xF0;  /* Set interrupt control page to IRQ channels */
  INT_CFDATA1 = 0;  /* Block IRQ from causing interrupt (dynamic alternative to IRQCR bit) */
  
  EnableInterrupts;

  while(1)
  {
    while (ATD0STAT2L_CCF0 == 0);       // Conversion complete flag    
    
    //Get ADC value (& clear flag)
    latestValue = ATD0DR0L;
    
    //Get difference from last reading
    diff = PotValue - latestValue;
    
    //If difference is greater than 5 then enable PIT interrupt
    if (diff < 0) diff = -diff;
    if (diff > 4)
    {
      //Initialise 5s sequence
      if (FlashCount == 0) FlashCount = 9;

      //Reset starting point
      PotValue = latestValue;
      
      //Clear any pending interrupt & re-enable
      PITTF = 0x01;
      PITINTE |= 0x01;
    }
  }
  /* please make sure that you never leave this function */
}

#pragma CODE_SEG __NEAR_SEG NON_BANKED 
/******************************************************************************
Function Name  : PIT0_ISR
Engineer       : r32151	
Date           : 01/03/2005
Parameters     : NONE
Returns        : NONE
Notes          : Interrupt Service Routine for PIT0. Flashes lights then turns
                 off PIT0. Should use semaphores to ensure no conflicts occur.
******************************************************************************/
interrupt void PIT0_ISR(void)
{
    XGSEM = 0x0101;  // Try to grab semaphore 0 by writing both bits
    if (XGSEM_XGSEM & 0x0001) {  //Check if semaphore was locked

       if(FlashCount%2==0)
           PORTA = 0x09;
       else 
           PORTA = 0x06;

  
       if (FlashCount != 0) 
           FlashCount--;
       else  {   
           PITINTE ^= 0x01;  //Turn off PIT0 interrupt
           XGSEM = 0x0100;   //Clear semaphore 0 by writing mask=1 and flag=0
       }
  
       PITTF = 0x01;  //Clear interrupt flag

    } // semaphore
}



⌨️ 快捷键说明

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