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

📄 f06x_ad0average.c

📁 c8051f060 ad0 采样程序
💻 C
📖 第 1 页 / 共 2 页
字号:

   SFRPAGE = UART0_PAGE;               // Switch to UART0 page

   TI0    = 1;                         // Indicate TX ready

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE

}

//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
   char old_SFRPAGE = SFRPAGE;
   int i;                                 

   SFRPAGE = CONFIG_PAGE;              // Switch to Configuration Page?

   OSCXCN = 0x67;                      // OSCXCN外部振荡器控制寄存器
                                       // 22.1184MHz crystal on TB

   for (i=0; i <5000; i++) ;           // 在晶体方式,为了防止读到假XTLVLD标志,
                                       //软件在使能外部振荡器和检查XTLVLD之间至少应延时1ms
                                       //XTLVLD为外部晶体稳定振荡标志 (>1ms)

   while (!(OSCXCN & 0x80)) ;          // 等XTLVLD稳定在开始执行下面程序

   RSTSRC = 0x04;                      // RSTSRC:复位源寄存器,使能时钟丢失复位

   CLKSEL = 0x01;                      // 系统使用外部时钟

   OSCICN = 0x00;                      // 禁止内部振荡器



   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}

//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
   char old_SFRPAGE = SFRPAGE;
   int i;

   SFRPAGE = ADC0_PAGE;                // Switch to ADC0 Page

   ADC0CN = 0x44;                      // ADC Disabled, Timer3 start-of-conversion
                                       // track 16 SAR clocks before data conversion
                                       // upon Timer3 OV.  DMA will enable ADC as needed
                                       //

   REF0CN = 0x02;                      // turn on bias generator and external reference.

   for(i=0;i<10000;i++);               // Wait for Vref to settle (large cap used on target board)

   AMX0SL = 0x00;                      // Single-ended mode

   ADC0CF = (SYSCLK/25000000) << 4;    // Select SAR clock frequency =~ 25MHz

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}

/*void ADC1_Init (void)
{
   char old_SFRPAGE = SFRPAGE;
   int i;

   SFRPAGE = ADC1_PAGE;                // Switch to ADC1 Page

   ADC1CN = 0x44;                      // ADC Disabled, Timer3 start-of-conversion
                                       // track 16 SAR clocks before data conversion
                                       // upon Timer3 OV.  DMA will enable ADC as needed
                                       //

   REF1CN = 0x02;                      // turn on bias generator and external reference.

   for(i=0;i<10000;i++);               // Wait for Vref to settle (large cap used on target board)

   AMX0SL = 0x00;                      // Single-ended mode

   ADC1CF = (SYSCLK/25000000) << 4;    // Select SAR clock frequency =~ 25MHz

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}*/

//-----------------------------------------------------------------------------
// DMA0_Init
//-----------------------------------------------------------------------------
void DMA0_Init (void)
{
   char old_SFRPAGE = SFRPAGE;

   SFRPAGE = DMA0_PAGE;                // Switch to DMA0 Page

   DMA0CN = 0x00;                      // Disable DMA interface

   DMA0DA = XRAM_START_ADD;            // Starting Point for XRAM addressing

   DMA0CT = NUM_SAMPLES;               // Get NUM_SAMPLES samples

   DMA0IPT = 0x00;                     // Start writing at location 0

   // Push instructions onto stack in order they will be executed
   DMA0IDT = DMA0_GET_ADC0;            // DMA to move ADC0 and ADC1 data.
   DMA0IDT = DMA0_END_OF_OP;
  
   DMA0BND = 0x00;                     // Begin instruction executions at address 0
   DMA0CN = 0xA0;                      // Mode 1 Operations, Begin Executing DMA Ops
                                       // (which will start ADC)

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
} 

//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate ADC sample rate
// specified by <counts> using SYSCLK as its time base.
//
void Timer3_Init (int counts)
{
   char old_SFRPAGE = SFRPAGE;

   SFRPAGE = TMR3_PAGE;                // Switch to Timer 3 page

   TMR3CN = 0x00;                      // Stop Timer3; Clear TF3;
   TMR3CF = 0x08;                      // use SYSCLK as timebase
   RCAP3   = -counts;                  // Init reload values
   TMR3    = 0xffff;                   // set to reload immediately
   TR3 = 1;                            // start Timer3

   SFRPAGE = old_SFRPAGE;              // restore SFRPAGE
}

//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the external memory interface to use upper port pins in
// non-multiplexed mode to a mixed on-chip/off-chip configuration without
// Bank Select.
//
void EMIF_Init (void)
{
   char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page

   SFRPAGE = EMI0_PAGE;                // Save SFR_PAGE
   EMI0CF = 0x3C;                      // upper ports; non-muxed mode;
                                       // split mode w/o bank select
   EMI0TC = 0x45;                      // timing (7-cycle MOVX)

   SFRPAGE = CONFIG_PAGE;
   P4MDOUT |= 0xFF;                    // all EMIF pins configured as
   P5MDOUT |= 0xFF;                    // push-pull
   P6MDOUT |= 0xFF;
   P7MDOUT |= 0xFF;

   SFRPAGE = SFRPAGE_SAVE;             // restore SFR_PAGE
}

void Sample(void)
{
  
   unsigned int data i;
  
   read_ptr = XRAM_START_ADD;          // Set pointer to beginning of data
    ad00=0;
    //ad10=0;
    ad01=0;
    //ad11=0;
    ad02=0;
    //ad12=0; 
   for (i=0;i<=z-1;i++)
   {
    ad00+=*read_ptr++;
    //ad10+=*read_ptr++;
    ad01+=*read_ptr++;
   // ad11+=*read_ptr++;
    ad02+=*read_ptr++;  
   // ad12+=*read_ptr++;  
   }                              //每组的所有数相加
    
    
      ad00=ad00>>n;
      //ad10=ad10>>n;
      ad01=ad01>>n;
      //ad11=ad11>>n;
      ad02=ad02>>n;
      //ad12=ad12>>n;           //所有数右移n次,相当于除n次取平均;
      ad00_int=ad00;
      //ad10_int=ad10;
      ad01_int=ad01;
      //ad11_int=ad11;
      ad02_int=ad02;
      //ad12_int=ad12;   
 }                                  //将long型数据转化为int型
  
    


//-----------------------------------------------------------------------------
// SendData
//-----------------------------------------------------------------------------
//
//Send data out UART0 
//
/*void SendData(void)
{
   unsigned int i;
   char old_SFRPAGE = SFRPAGE;
   SFRPAGE = UART0_PAGE;               // Switch to UART0 page

   read_ptr = XRAM_START_ADD;          // Set pointer to beginning of data

   for (i=0;i<NUM_SAMPLES;i++)
   {
   printf ("%u\n",*read_ptr);          // Send data as unsigned integers
   read_ptr++;
   }

   SFRPAGE = old_SFRPAGE;

}//End	*/




⌨️ 快捷键说明

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