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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
📖 第 1 页 / 共 2 页
字号:
        SET_RTC_MS();
      break;
      
      default:
      break;
     }
   } 
   else 
   {
     switch(Samp_Rate) 
     {
      case 12:                /*100Hz, 0.01s*/
        SET_TPM_TIMER(15000);
      break;
      
      case 13:                /*200Hz, 0x005s*/
        SET_TPM_TIMER(7500);
      break;
      
      case 14:                /*500Hz, 0x002s*/
        SET_TPM_TIMER(3000);
      break;
      
      case 15:                /*1000Hz, 0x001s*/
        SET_TPM_TIMER(1500);
      break;
      
      case 16:                /*2000Hz, 0x0005s*/
        SET_TPM_TIMER(750);
      break;
      
      case 17:                /*5000Hz, 0x0002s*/
        SET_TPM_TIMER(300);
      break;
      
      case 18:                /*10000Hz, 0x0001s*/
        SET_TPM_TIMER(150);
      break;
      
      default:
      break;
     }
   }
   return;
}

/**********************************************************************************************
 * Get_ADC_Result: This function is used to fetch the all ADC conversion result
 *
 * Parameters:      Buf : point to the location which save the result
 *
 * Return:     0: failed, the conversion is not finished 
 *             1: success     
 *********************************************************************************************/ 
char Get_ADC_Result(char *DataBuf) 
{      
  char *pDes = DataBuf;
  char *pSrc;
  
  if(Working_Buf) 
  {
    pSrc =  ADC_Cvt_Result_Buf0;
  } 
  else 
  {
    pSrc =  ADC_Cvt_Result_Buf1;
  }

  if(memcpy(pDes,pSrc, 2*MAX_ADC_CH))
    return 1;
  else
    return 0;
}

/**********************************************************************************************
 * Get_ADC_Ch_Result: This function is used to fetch the ADC conversion result of one channel
 *
 * Parameters:  Ch  : the channel number     
 *              Buf : point to the location which save the result
 *
 * Return:     0: failed, the conversion is not finished 
 *             1: success     
 *********************************************************************************************/ 
char Get_ADC_Ch_Result(char Ch, char *DataBuf) 
{
  char *pSrc;
  
  if(Ch >= MAX_ADC_CH)
    return 0;
  
  if(Working_Buf) 
  {
    pSrc =  ADC_Cvt_Result_Buf0;
  } 
  else 
  {
    pSrc =  ADC_Cvt_Result_Buf1;
  }

  *DataBuf = *(pSrc+ 2*Ch);
  *(DataBuf+1) = *(pSrc+ 2*Ch +1);

  return 1;
}

/****************************************************************************************
 * ADC_Start :  start to conversion of one channel
 * input:     None
 *
 * Return : None
 ***************************************************************************************/ 
char ADC_Start(void) 
{
  char Cvt_Ch = 0;
  unsigned long Compare_Val = 0;
  
  Current_Channel = 0;
  Time_Stamp._dword = 0;
  
  if( Total_Channels > 0) 
     Cvt_Ch = Work_Channel[0];
  else
     return 0;
  
  if(ADC_State == ADC_IDLE)
  {
     memset(ADC_Cvt_Result_Buf0, 0x00, 2* MAX_ADC_CH);
     memset(ADC_Cvt_Result_Buf1, 0x00, 2* MAX_ADC_CH);
     ADC_Results_Available = 0;
          
     if(Trig_Type == 0)             /*Software Trigger*/
     {
      ADC_State = ADC_BUSY;
      Config_Samp_Clk();
      ADCSC1 = Cvt_Ch;
     }
     else
      if(Trig_Type == 1)            /*Delay Trigger*/
      {
        //ADCSC2 = 0x40;
        //ADCSC1 =  Cvt_Ch; 
        //Set RTC value
        ADC_State = ADC_WAIT_TRIG;
        SET_RTC_TIME(Trig_Para1);
        SET_RTC_SEC();      
      } 
      else
       if(Trig_Type == 2)           /*Voltage level Trigger */
       {
         if(Trig_Para2)   
          ADCSC2 = 0x30;
         else
          ADCSC2 = 0x20;
          
          Compare_Val = Trig_Para3; 
          if(ADCCFG_MODE == 0b01) 
          {
            Compare_Val *= 4096;
            Compare_Val /= 100;
            ADCCVH = ((char)(Compare_Val>>8)) & 0x0F;
          }
          else
            if(ADCCFG_MODE == 0b10) 
            {
              Compare_Val *= 1024;
              Compare_Val /= 100;
              ADCCVH = ((char)(Compare_Val>>8)) & 0x03;
            }
            else 
            {
              Compare_Val *= 256;
              Compare_Val /= 100;
              ADCCVH = 0x00;
            }
          
            
          
          ADCCVL = (char)(Compare_Val);
          Cvt_Ch = Trig_Para1;
          
          ADC_State = ADC_WAIT_TRIG;
          ADCSC1 = (Cvt_Ch | 0x60);
       }
       
     return 1; 
    } 
    else 
    {
       if(ADC_State == ADC_WAIT_TRIG) 
         {
          ADCSC1 = Cvt_Ch;
          ADC_State = ADC_BUSY;
          return 1;
          } 
      else 
      {
        if(ADC_State == ADC_FINISH) 
         {
          ADC_State = ADC_BUSY;
          ADCSC1 = Cvt_Ch;
          return 1;
          } 
         else
          return 0;
      }
    }
 
}


/****************************************************************************************
 * ADC_Stop :  stop to conversion of one channel
 * input:     none
 *
 * Return : None
 ***************************************************************************************/ 
void ADC_Stop(void) 
{
   ADCSC1 = 0x00;
   ADCSC2 = 0x00;
   STOP_RTC();
   STOP_TPM();
   ADC_State = ADC_IDLE;
   
   return;
}

/****************************************************************************************
 * ADC_ISR: The ADC interrupt service routine which reads the ADC data register and places
 *         the value into global variables 
 *
 * Parameters: None
 *
 * Return : None
 ***************************************************************************************/ 
 interrupt void ADC_ISR (void)
 {
    dword *ptr;
    
    if(ADC_State == ADC_BUSY) 
    {
       if(Working_Buf) 
       {
          ADC_Cvt_Result_Buf1[2*Current_Channel] = ADCRH;
          ADC_Cvt_Result_Buf1[2*Current_Channel + 1] = ADCRL;
       } 
       else
       {
          ADC_Cvt_Result_Buf0[2*Current_Channel] = ADCRH;
          ADC_Cvt_Result_Buf0[2*Current_Channel + 1] = ADCRL;
       }
       
       
       if(Current_Channel < (Total_Channels -1)) 
       {
         Current_Channel ++;
       }
       else 
       {
         Time_Stamp._dword +=1;
         
         if(Working_Buf) 
         {
           ptr = &ADC_Cvt_Result_Buf1[2*Total_Channels];
         } 
         else 
         {
           ptr = &ADC_Cvt_Result_Buf0[2*Total_Channels];
         }
         
         *ptr =  Time_Stamp._dword;
         
         
         Current_Channel = 0;
         ADC_State = ADC_FINISH;
         Working_Buf ^= 0x01;
         ADC_Results_Available = 1;

         return;
       }
       
    }
    else 
    {
      if(ADC_State == ADC_WAIT_TRIG) 
      {
        ADCSC2 = 0x00;
        ADCSC2 = 0x00;
        Current_Channel = 0;
        ADC_State = ADC_BUSY;
        Config_Samp_Clk();
      }
    }
    
    ADCSC1 =  Work_Channel[Current_Channel]; 
    return;
 }


interrupt void  RTC_ISR(void) 
{
   RTCSC_RTIF = 1;
  
   if(ADC_State == ADC_WAIT_TRIG) 
   {
      //ADCSC2 = 0x00;
      STOP_RTC();
      //ADC_Start();
      ADC_State = ADC_BUSY;
      ADCSC1 = Work_Channel[Current_Channel];
      Config_Samp_Clk();
   } 
   else 
   {
       if(ADC_State == ADC_FINISH) 
        {
          ADC_State = ADC_BUSY;
          ADCSC1 = Work_Channel[Current_Channel]; 
          PTFD ^= 0x80;    //for test
        }
   }
   
   return;
}


interrupt void  TPM1_ISR(void) 
{
  if(TPM1SC_TOF)
    TPM1SC_TOF = 0;
  
  if(ADC_State == ADC_FINISH) 
  {
    ADC_State = ADC_BUSY;
    ADCSC1 = Work_Channel[Current_Channel];
    PTFD ^= 0x80;    //for test
  }
  return;
}

⌨️ 快捷键说明

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