?? adc12.c
字號:
# include "ADC12.h"
//設(shè)定序列轉(zhuǎn)換通道,從A0通道開始,共五個(gè)通道
static unsigned int A0_Results[NUM_OF_RESULTS]={0};
static unsigned int A1_Results[NUM_OF_RESULTS]={0};
static unsigned int A2_Results[NUM_OF_RESULTS]={0};
static unsigned int A3_Results[NUM_OF_RESULTS]={0};
static unsigned int A4_Results[NUM_OF_RESULTS]={0};
//序列通道單次轉(zhuǎn)換忙標(biāo)志
static unsigned char ADC12_CON_Flag=1;
static unsigned int ADC12_Index = 0;
void ADC12_Device_INIT(void)
{
//ADC12_INIT();
ADC12_SM_INIT();
ADC12_PORT_INIT();
}
void ADC12_PORT_INIT(void)
{
//P1DIR |= 0xff;
//P1OUT |= 0xff;
}
/****************************************/
//單通道單次轉(zhuǎn)換初始化與獲取轉(zhuǎn)換結(jié)果
void ADC12_INIT(void)
{
ADC12CTL0 = ADC12ON+SHT0_8+ MSC;
ADC12CTL1 = SHP+ CONSEQ_2;
ADC12IE |= 0x01;
ADC12CTL0 |= ENC;
ADC12CTL0 |= ADC12SC;
}
//獲取單通道單次轉(zhuǎn)換的結(jié)果
//相關(guān)寄存器設(shè)定
void ADC12_SS_INIT(void)
{
P6SEL |= BIT0; //初始化默認(rèn)選擇A0通道
ADC12CTL0 &= ~ENC;
ADC12CTL0 = ADC12ON+SHT0_8; //使用采樣保持定時(shí)器,單通道單次模式
ADC12CTL1 |= SHP| CONSEQ_0| CSTARTADD_0;//默認(rèn)存儲地址為ADC12MEM0
ADC12MCTL0 |= INCH_0; //選擇對應(yīng)轉(zhuǎn)換存儲器的采樣通道(A0)
ADC12CTL0 |= ENC; //允許轉(zhuǎn)換
}
//單通道單次轉(zhuǎn)換,通道可選
unsigned int Get_ADC12_SS(unsigned char ch_INCH)
{
//因?yàn)槭菃瓮ǖ绬未无D(zhuǎn)換,所以存儲寄存器不用選擇,
//這里使用ADC12MEM0
unsigned int SS_Result=0;
while(ADC12CTL0 & ADC12BUSY)
;
ADC12CTL0 &= ~ENC;
ADC12MCTL0 &= ~0x0f; //清除上次轉(zhuǎn)換選擇通道
ADC12MCTL0 |= ch_INCH; //選擇本次轉(zhuǎn)換使用通道
P6SEL &= ~0xff;
P6SEL |= BIT(ch_INCH); //選擇復(fù)用通道
ADC12CTL0 |= ENC;
ADC12CTL0 |= ADC12SC; //開始轉(zhuǎn)換
while (!(ADC12IFG & BIT0)); //等待轉(zhuǎn)換結(jié)束
;
SS_Result=ADC12MEM0;
return SS_Result;
}
unsigned int Get_Real_Value(unsigned int Value, unsigned char Is_Real)
{
unsigned long Temp_Value=(unsigned long)Value;
if(Is_Real)
{
Temp_Value = Temp_Value*329/409;//325
}
return (unsigned int)Temp_Value;
}
void Get_Real_ADC12(unsigned int* p_Array, unsigned int* p_Result,
unsigned char ch_Sum)
{
unsigned char i = 0;
for(i = 0; i < ch_Sum; i++)
{
p_Array[i] = Get_Real_Value(p_Result[i], 1);
}
}
/******************************************/
//單通道重復(fù)轉(zhuǎn)換初始化與獲取轉(zhuǎn)換結(jié)果
void ADC12_SM_INIT(void)
{
P6SEL |= BIT0| BIT1| BIT2| BIT3| BIT4; // Enable A/D channel inputs
ADC12CTL0 |= ADC12ON| MSC| SHT0_8; // Turn on ADC12, extend sampling time
// to avoid overflow of results
ADC12CTL1 |= SHP| CONSEQ_3| CSTARTADD_0; // Use sampling timer, repeated sequence
ADC12MCTL0 |= INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 |= INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 |= INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 |= INCH_3; // ref+=AVcc, channel = A3, end seq.
ADC12MCTL4 |= INCH_4| EOS;
ADC12IE |= BIT4; // Enable ADC12IFG.4
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start convn - software trigger
Stop_ADC12_SM();
}
void Clear_ADC_Register(void)
{
ADC12CTL0 = 0;
ADC12CTL1 = 0;
ADC12IE &= ~BIT4;
}
void Begin_ADC12_SM(void)
{
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start convn - software trigger
}
void Stop_ADC12_SM(void)
{
ADC12CTL0 &= ~ENC;
ADC12CTL0 &= ~ADC12SC;
}
void Get_ADC12_SM(unsigned int* p_Result)
{
//通過取平均值增加了精確度
unsigned char i = 0;
unsigned int Temp_Result_Sum=0;
Begin_ADC12_SM(); //啟動單通道序列轉(zhuǎn)換
while(ADC12_CON_Flag) //等待一輪轉(zhuǎn)換結(jié)束
;
//這段代碼看似冗長,但可以節(jié)約一定時(shí)間
//****************************************
//A0通道
for(i=0; i<NUM_OF_RESULTS; i++)
{
Temp_Result_Sum += A0_Results[i];
}
p_Result[0] = Temp_Result_Sum / NUM_OF_RESULTS;
Temp_Result_Sum = 0;
//A1通道
for(i=0; i<NUM_OF_RESULTS; i++)
{
Temp_Result_Sum += A1_Results[i];
}
p_Result[1] = Temp_Result_Sum / NUM_OF_RESULTS;
Temp_Result_Sum = 0;
//A2通道
for(i=0; i<NUM_OF_RESULTS; i++)
{
Temp_Result_Sum += A2_Results[i];
}
p_Result[2] = Temp_Result_Sum / NUM_OF_RESULTS;
Temp_Result_Sum = 0;
//A3通道
for(i=0; i<NUM_OF_RESULTS; i++)
{
Temp_Result_Sum += A3_Results[i];
}
p_Result[3] = Temp_Result_Sum / NUM_OF_RESULTS;
Temp_Result_Sum = 0;
//A4通道
for(i=0; i<NUM_OF_RESULTS; i++)
{
Temp_Result_Sum += A4_Results[i];
}
p_Result[4] = Temp_Result_Sum / NUM_OF_RESULTS;
Temp_Result_Sum = 0;
//Clear_ADC_Buffer();
//*************************************************
}
void Clear_ADC_Buffer(void)
{
unsigned char i = 0;
for(i=0; i<NUM_OF_RESULTS; i++)
{
A0_Results[i]=0;
}
for(i=0; i<NUM_OF_RESULTS; i++)
{
A1_Results[i]=0;
}
for(i=0; i<NUM_OF_RESULTS; i++)
{
A2_Results[i]=0;
}
for(i=0; i<NUM_OF_RESULTS; i++)
{
A3_Results[i]=0;
}
for(i=0; i<NUM_OF_RESULTS; i++)
{
A4_Results[i]=0;
}
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
A0_Results[ADC12_Index] = ADC12MEM0; // Move A0 results, IFG is cleared
A1_Results[ADC12_Index] = ADC12MEM1; // Move A1 results, IFG is cleared
A2_Results[ADC12_Index] = ADC12MEM2; // Move A2 results, IFG is cleared
A3_Results[ADC12_Index] = ADC12MEM3; // Move A3 results, IFG is cleared
A4_Results[ADC12_Index] = ADC12MEM4; // Move A3 results, IFG is cleared
ADC12_Index = (ADC12_Index+1)%NUM_OF_RESULTS; // Increment results index, modulo; Set Breakpoint1 here
if(ADC12_Index==0) //一輪轉(zhuǎn)換完成,數(shù)據(jù)將被讀出
{
ADC12_CON_Flag=0; //清除忙標(biāo)志
Stop_ADC12_SM(); //轉(zhuǎn)換結(jié)束
}
else
{
ADC12_CON_Flag=1; //設(shè)置忙標(biāo)志,正在轉(zhuǎn)換
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -