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

📄 dma.c

📁 DSP关于F240的实例程序
💻 C
字号:
#include "ADuC842.h"
#include "DMA.h"
#include "math.h"
#include "lcd.h"

//*****************thuseday added*************8
unsigned int xdata int_lost; //points lost

//*********************************************

unsigned int xdata DMA_Chanel=0;        //check adc2 or adc3
unsigned int xdata num[2*N];//data[0]~~data[255]
float   monitor_f = 100.0; //存放频率值


//******************init the data,used in DMA modle*********//
//chanel代表通道号
void init_data(unsigned int chanel)

{
  unsigned int xdata j,temp;
 temp=chanel<<12;

 for(j=0; j< 2*N-2; j++)   {num[j]=temp;}//chanel n
 
 num[j]=0xf000;//stop command,now 
}


//*****************init the head address*****************//
//DMA way,define the head address in DMAL,DMAH,DMAP
//将数组的地址赋给 DMAL,DMAH,DMAP
void init_addr( void )
{
  unsigned int addr;
  addr = num;  //num is the address of the data sampled
  DMAL = addr & 0x00ff;
  DMAH = addr >> 8;
  DMAP = 0x00;
}



//******************* init the time loaded T2*********//
// float_time_load代表应该被植入的时间,比如20.4 。
//  int_time_load代表实际被植入的时间,比如20,落掉了0.4 。
// float_time_left 代表实际落掉的的时间,0.4  。
// float_lost 代表采样N个点后总共落掉的的时间与应该被植入的时间的比值,0.4*128/20.4= 2.5,也就是丢失点数。
//  int_lost取整数个丢失点数,即2。然后根据剩下的小数而四舍五入。因为剩余0.5 ,所以int_lost=3。

void init_time(void)
{
 float xdata float_time_load, float_time_left, float_lost;
 int  xdata int_time_load;
 int  xdata  time=0;


 time=(1000000.0 / (N * monitor_f))*JING_ZHEN;



if(time<=256)  {RCAP2L=256-time; RCAP2H=0xff;} 
else if(time%256==0)  {RCAP2L=0x00;  RCAP2H=256-(time/256);}
else {RCAP2L=256-time%256; RCAP2H=255-time/256;}

float_time_load = (1000000.0/N/monitor_f)*JING_ZHEN;
int_time_load   = float_time_load;
float_time_left = float_time_load - int_time_load;
    float_lost  =     (float_time_left*N)/float_time_load;
      int_lost  = float_lost;
      if((float_lost - int_lost)>=0.5)  int_lost  = int_lost +1;// samples lost
}

// 采样通道chanel#然后,显示在液晶上。
void DMA_Start( unsigned int chanel )
{
   ADCCON1=0x00;//turn down adc,this is needed by DMA。关闭adc,是设置dma方式所必需的。
   init_time();
   init_data(chanel);
   init_addr();

   DMA_Chanel=chanel;

   ADCCON2=0x40;//set DMA mode
   ADCCON1=0xA2;//power up the adc,3 divided,timer 2 triger
   EA=1;// allow cpu interupt
   EADC=1;//allow adc interupt
   ET2=0;//forbid T2 interupt
   TL2=0x00; TH2=0xFF;
   TR2=1;//begin timer 2
   while(TR2==1){;}//wait DMA over。等待dma转化的结束。结束会触发中断服务程序。    
}

//dma结束时会转入此中断程序。
void  adc_int() interrupt 6
{
  TR2=0;
}
void DMA_Display(void)
{   unsigned  int xdata ad_result,preResult;
    unsigned int xdata i;
    while(TR2==1);
    preResult =( num[0] & 0x0fff )>>4;
    for(i=1;i<N+1;i++)
     {  ad_result =  (num[i] & 0x0fff) >>4;
      //  plot_point(2,i+1, ad_result,1);
        plot_line(2,i,preResult,i+1, ad_result);
        preResult = ad_result;
      
     }
    
}





⌨️ 快捷键说明

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