📄 adc.c
字号:
#include <string.h>
#include "..\inc\option.h"
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\adc.h"
/****************************************************************************
【功能说明】模数转换初始化
****************************************************************************/
void ADC_Init(void)
{
rCLKCON = rCLKCON | (1<<12); //控制系统主时钟进入ADC单元模块
rADCCON = 0x1|(0<<2); //ADC转换使能,选择AIN7
Delay(100); //延时若干个100us
rADCPSR = 10; //ADC转换频率为:(int)(MCLK/(2*(rADCPSR+1))/16)
}
//***************************************************************************
/****************************************************************************
【功能说明】ADC某一通道进行转换,返回转换的数据
****************************************************************************/
unsigned short Read_Adc(unsigned char ch)
{
int i;
static int prevCh=-1;
if(prevCh!=ch)
{
rADCCON = 0x1|(ch<<2); //设置AD转换通道
for(i=0;i<150;i++); //最小15uS
}
rADCCON=0x1|(ch<<2); //开始AD转换
while(rADCCON & 0x1); //避免标志FLAG错误
while(!(rADCCON & 0x40)); //等待AD转换结束
for(i = 0; i < rADCPSR; i++); //避免第二次标志FLAG错误
prevCh=ch;
return rADCDAT; //返回AD转换值
}
//***************************************************************************
/****************************************************************************
【功能说明】ADC转换测试程序
****************************************************************************/
void Test_Adc(void)
{
int a0=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0;
Uart_Printf("\n【8通道ADC模数转换测试:】\n");
rCLKCON=0x7ff8;
rADCCON=0x1|(0<<2); //Enable ADC
Delay(100); //delay for 10ms for ADC reference voltage stabilization.
rADCPSR = 10;
Uart_Printf("ADC转换频率 = %d(Hz)\n",(int)(MCLK/(2.*(10+1.))/16.) );
while(1)
{
a0 = Read_Adc(0); //ADC某一通道进行转换,返回转换的数据
a1 = Read_Adc(1); //ADC某一通道进行转换,返回转换的数据
a2 = Read_Adc(2); //ADC某一通道进行转换,返回转换的数据
a3 = Read_Adc(3); //ADC某一通道进行转换,返回转换的数据
a4 = Read_Adc(4); //ADC某一通道进行转换,返回转换的数据
a5 = Read_Adc(5); //ADC某一通道进行转换,返回转换的数据
a6 = Read_Adc(6); //ADC某一通道进行转换,返回转换的数据
a7 = Read_Adc(7); //ADC某一通道进行转换,返回转换的数据
Uart_Printf("ADC 0-7通道输出依次为:%4d %4d %4d %4d %4d %4d %4d %4d\n",a0,a1,a2,a3,a4,a5,a6,a7);
}
}
//***************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -