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

📄 adc.c

📁 led,key,i2c,uart,adc,rtc,wdt测SHI 文件代码
💻 C
字号:
#include <string.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\adc.h"

int ReadAdc(int ch);

void Test_Adc(void)
{
    int a5=0;
    int adcpsr;
    char ch='c';
	Uart_Printf("\n********Test the ADC_IN5***********\n");
    Uart_Printf("\nThe ADC_IN are adjusted to the following values.\n");	       
	/*CLKCON: clock control register
	*/
    rCLKCON=0x7ff8;	
    /*ADCCON
      b6:flag,          0-->in process,1-->end
      b5:sys power down,0-->normal operation,1-->sleep mode
    b4-2:clock source select
      b1:a/d conversion start by read,0-->disable,,1-->enable
      b0:start enable
    */
    rADCCON=0x1|(0<<2);		//Enable ADC
    Delay(100);	//delay for 10ms for ADC reference voltage stabilization.
	/*ADCPSR:AD conversion prescaler register
	prescaler:0-255,
	drived=2*(prescaler+1)
	*/
    adcpsr=64;
    rADCPSR=adcpsr;
    Delay(5000);
    Uart_Printf("ADC conv. freq.=%d(Hz)\n",(int)(MCLK/(2.*(adcpsr+1.))/16.) );
    
    while(ch=='c')
    {
	a5=(int)(ReadAdc(5)*2500/1024);
	Uart_Printf("\nadc5:%04d mv\n",a5);
	Uart_Printf("\npress c key to stop test adc,others stop test:");
	ch=Uart_Getch();
    }
    Uart_Printf("\n********Finished Test the ADC_IN5***********\n");
}

int ReadAdc(int ch)
{
    int i;
    static int prevCh=-1;

    if(prevCh!=ch)
    {
    	rADCCON=0x0|(ch<<2);	//setup channel.
    	for(i=0;i<150;i++);	//min. 15us
    }
    rADCCON=0x1|(ch<<2);	//Start A/D conversion
    while(rADCCON &0x1);	//To avoid The first FLAG error case.
				//(The START bit is cleared in one ADC clock.)
    while(!(rADCCON & 0x40));
    for(i=0;i<rADCPSR;i++); 	//To avoid The second FLAG error case
    prevCh=ch;
    return rADCDAT;
}

⌨️ 快捷键说明

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