📄 adc.c
字号:
/*******************************************************************************
- Chip : MG24500/55
- Author : RadioPulse Inc, 2007.
- Date : 2007-07-02
- Version : VER 1.0
*******************************************************************************/
#include "INCLUDE_TOP.h"
//- Ref : Reference Voltage
// 0 : Internal BGR Voltage. TOP=1.2V, BOT=0.3V, VMID=0.75V
// 1 : TOP_BGR. TOP=BGR, BOT=0V
// 2 : ACH2, ACH3. TOP=ACH2, BOT=ACH3, VMID=0.75V
// 3 : VDD, GND. TOP=1.5V, BOT=0V
//- AdcChan : ADC Channel
// 0 : ACH0
// 1 : ACH1
// 2 : ACH2
// 3 : ACH3
// 4 : difference of ACH0 and ACH1
// 5 : difference of ACH2 and ACH3
// 6 : Temprature Sensor
// 7 : Battery Monitoring
UINT8 ZHAL_ADC_SET(UINT8 Ena, UINT8 Ref, UINT8 AdcChan)
{
UINT8 ERR;
UINT8 ib;
xSADCCON = AdcChan & 0x0F;
xSADCCON |= (Ref & 0x03) << 4;
ERR = 1;
if(Ena)
{
xSADCCON |= 0x80; // set bit[7]
for(ib=0 ; ib<400 ; ib++)
{
if(xSADCCON & 0x40)
{
ERR = 0;
break;
}
}
xSADCCON &= 0xBF; // clear bit[6]
}
else
{
ERR = 0;
}
return ERR;
}
UINT16 ZHAL_ADC_GET()
{
UINT16 SADC_OUT;
SADC_OUT = (xSADCVALH << 8) | xSADCVALL ;
return SADC_OUT;
}
//- AdcChan : ADC Channel
// 0 : ACH0
// 1 : ACH1
// 2 : ACH2
// 3 : ACH3
// 4 : difference of ACH0 and ACH1
// 5 : difference of ACH2 and ACH3
// 6 : Temprature Sensor
// 7 : Battery Monitoring
//- RETURN : ADC Output
UINT16 ZSYS_ADC_GET(UINT8 AdcChan)
{
UINT16 ADC_READ;
xMEAS2 &= 0xFC; // clear bit[1:0]
xMEAS2 |= 0x02; // set bit[1:0]=2'b10
xCLKON2 |= 0x03; // set bit[1:0] = 2'b11, SADC/SDEC CLK Enable
if(ZHAL_ADC_SET(1, 0, AdcChan))
{
ADC_READ = 0xFFFF;
}
else
{
ADC_READ = ZHAL_ADC_GET();
ZHAL_ADC_SET(0, 0, 0);
}
xCLKON2 &= 0xFC; // set bit[1:0] = 2'b00
return ADC_READ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -