CH552/projects/examples/ADC/ADC.C

109 lines
4.5 KiB
C++
Raw Normal View History

2018-10-17 10:44:45 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name : ADC.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554 ADC<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>ADCͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD>Ƚ<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
#include "..\Public\CH554.H"
#include "..\Public\Debug.H"
#include "ADC.H"
#include "stdio.h"
#pragma NOAREGS
#define ADC_INTERRUPT 1
/*******************************************************************************
* Function Name : ADCInit(UINT8 div)
* Description : ADC<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϿ<EFBFBD><EFBFBD><EFBFBD>
* Input : UINT8 div ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
1 <EFBFBD><EFBFBD> 384<EFBFBD><EFBFBD>Fosc
0 <EFBFBD><EFBFBD> 96<EFBFBD><EFBFBD>Fosc
* Output : None
* Return : None
*******************************************************************************/
void ADCInit(UINT8 div)
{
ADC_CFG &= ~bADC_CLK | div;
ADC_CFG |= bADC_EN; //ADC<44><43>Դʹ<D4B4><CAB9>
#if ADC_INTERRUPT
ADC_IF = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
IE_ADC = 1; //ʹ<><CAB9>ADC<44>ж<EFBFBD>
#endif
}
/*******************************************************************************
* Function Name : ADC_ChannelSelect(UINT8 ch)
* Description : ADC<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* Input : UINT8 ch <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
* Output : None
* Return : <EFBFBD>ɹ<EFBFBD> SUCCESS
ʧ<EFBFBD><EFBFBD> FAIL
*******************************************************************************/
UINT8 ADC_ChannelSelect(UINT8 ch)
{
if(ch == 0){ADC_CHAN1 =0;ADC_CHAN0=0;P1_DIR_PU &= ~bAIN0;} //AIN0
else if(ch == 1){ADC_CHAN1 =0;ADC_CHAN0=1;P1_DIR_PU &= ~bAIN1;} //AIN1
else if(ch == 2){ADC_CHAN1 =1;ADC_CHAN0=0;P1_DIR_PU &= ~bAIN2;} //AIN2
else if(ch == 3){ADC_CHAN1 =1;ADC_CHAN0=1;P3_DIR_PU &= ~bAIN3;} //AIN3
else return FAIL;
return SUCCESS;
}
/*******************************************************************************
* Function Name : VoltageCMPModeInit()
* Description : <EFBFBD><EFBFBD>ѹ<EFBFBD>Ƚ<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
* Input : UINT8 fo <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD> 0\1\2\3
UINT8 re <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿<EFBFBD> 1\3
* Output : None
* Return : <EFBFBD>ɹ<EFBFBD> SUCCESS
ʧ<EFBFBD><EFBFBD> FAIL
*******************************************************************************/
UINT8 VoltageCMPModeInit(UINT8 fo,UINT8 re)
{
ADC_CFG |= bCMP_EN; //<2F><>ƽ<EFBFBD>Ƚϵ<C8BD>Դʹ<D4B4><CAB9>
if(re == 1){
if(fo == 0) {ADC_CHAN1 =0;ADC_CHAN0=0;CMP_CHAN =0;} //AIN0<4E><30>AIN1
else if(fo == 2) {ADC_CHAN1 =1;ADC_CHAN0=0;CMP_CHAN =0;} //AIN2<4E><32>AIN1
else if(fo == 3) {ADC_CHAN1 =1;ADC_CHAN0=1;CMP_CHAN =0; } //AIN3<4E><33>AIN1
else return FAIL;
}
else if(re == 3){
if(fo == 0) {ADC_CHAN1 =0;ADC_CHAN0=0;CMP_CHAN =0;} //AIN0<4E><30>AIN1
else if(fo == 1) {ADC_CHAN1 =0;ADC_CHAN0=1;CMP_CHAN =0;} //AIN1<4E><31>AIN1
else if(fo == 2) {ADC_CHAN1 =1;ADC_CHAN0=0;CMP_CHAN =0;} //AIN2<4E><32>AIN1
else return FAIL;
}
else return FAIL;
#if ADC_INTERRUPT
CMP_IF = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
IE_ADC = 1; //ʹ<><CAB9>ADC<44>ж<EFBFBD>
#endif
return SUCCESS;
}
#if ADC_INTERRUPT
/*******************************************************************************
* Function Name : ADCInterrupt(void)
* Description : ADC <EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
void ADCInterrupt( void ) interrupt INT_NO_ADC using 1 //ADC<44>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>1
{
if(ADC_IF == 1) //ADC<44><43><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
{
// UserData = ADC_DATA; //ȡ<><C8A1>ADC<44><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ADC_IF = 0; //<2F><><EFBFBD><EFBFBD>ADC<44>жϱ<D0B6>־
}
if(CMP_IF == 1) //<2F><>ѹ<EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
{
// UserData = ADC_CTRL&0x80 >> 7); //<2F><><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CMP_IF = 0; //<2F><><EFBFBD>ձȽ<D5B1><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
}
}
#endif