?? ad.c
字號(hào):
//------------------------函數(shù)聲明,變量定義---------------------------------
#include<reg51.h>
#include<intrins.h>
sbit sle_A=P3^0;
sbit sle_B=P3^1;
sbit sle_C=P3^2;
sbit comp_OUT=P3^6;
sbit comp_IN1=P1^0;
sbit comp_IN2=P1^1;
unsigned char ABC;
unsigned int Tref;
unsigned int Tx;
unsigned int Vx;
unsigned int Vref=25000;//實(shí)際參考電壓值的1000倍,以免出現(xiàn)小數(shù)
void delay(unsigned int N);//延時(shí)子程序,實(shí)現(xiàn)(16xN+24)US的延時(shí)
void timer_set();//定時(shí)器初始化函數(shù)
void discharge();//電容放電
void measure_Vref();//測(cè)量參考電壓值
void measure_Vx(unsigned char channels);//測(cè)量指定通道的電壓
void calculate_Vx();//計(jì)算待測(cè)電壓值
#define delayNOP();{_nop_();_nop_();_nop_();_nop_();};
//------------------------------------------------------------------
//函數(shù)名稱(chēng):DELAY
//入口參數(shù):N
//函數(shù)功能:延時(shí)子程序,實(shí)現(xiàn)(16xN+24)us的延時(shí)
//系統(tǒng)采用11。0592的時(shí)鐘時(shí),延時(shí)滿(mǎn)足要求,其他情況需要改動(dòng)
//--------------------------------------------------------------------
void delay(unsigned int N)
{
int i;
for(i=0;i<N;i++);
}
//-----------------------------------------------------------------------
//函數(shù)名稱(chēng):TIMER-SET()
//功能說(shuō)明:設(shè)置定時(shí)器工作狀態(tài),與硬件有關(guān)、
//-----------------------------------------------------------------------
void timer_set()
{
TMOD=0X01;//設(shè)定定時(shí)器工作模式
}
//-----------------------------------------------------------------------
//函數(shù)名稱(chēng):DISCHARGE()
//函數(shù)功能:電容放電,定時(shí)器清零
//-----------------------------------------------------------------------
void discharge()
{
comp_IN2=0;//P1,1置零,cr放電
delay(0);//延時(shí)24us
TH0=0;
TL0=0;
}
//--------------------------------------------------------------------------
//函數(shù)名稱(chēng):MEASURE-Vref
//函數(shù)功能:測(cè)量參考電壓
//--------------------------------------------------------------------------
void measure_Vref()
{
sle_A=0;
sle_B=0;
sle_C=0;//選通道0,Vref輸入
delayNOP();//定時(shí)消除抖動(dòng)
TR0=1;//啟動(dòng)計(jì)數(shù)器
comp_IN2=1;//p1.1置1,開(kāi)始積分。
while(comp_OUT);//檢測(cè)p3,6是否出現(xiàn)跳變
TR0=0;//停止計(jì)數(shù)器記數(shù)
Tref=TH0;
Tref=(Tref<<8)||TL0;
discharge();//放電
}
//-------------------------------------------------------------------------------------
//函數(shù)名稱(chēng):MEASURE-Vx
//入口參數(shù):CHANNELS選通通道(1-7)
//功能說(shuō)明:測(cè)量指定通道的電壓
//-------------------------------------------------------------------------------------
void measure_Vx(unsigned char channels)
{
sle_A=channels&0x01;
sle_B=channels&0x02;
sle_C=channels&0x04;
delayNOP();//定時(shí)消除抖動(dòng)
TR0=1;//啟動(dòng)計(jì)數(shù)器
comp_IN2=1;//置p1,1為1,開(kāi)始積分
while(comp_OUT);
TR0=0;
Tx=TH0;
Tx=(Tx<<8)||TL0;
discharge();//放電
}
//---------------------------------------------------------------------------------------------
//函數(shù)名稱(chēng):CALCULATE-VX
//函數(shù)公式VX=Vref *Tx/Tref計(jì)算結(jié)果
//功能說(shuō)明:計(jì)算Vx
//---------------------------------------------------------------------------------------------
void calculte_Vx()
{
}
//--------------------------------------------------------------------------------------------
//函數(shù)名稱(chēng):MAIN
//函數(shù)功能:主函數(shù)
//--------------------------------------------------------------------------------------------
void main()
{
timer_set();
discharge();
measure_Vref();
measure_Vx(1);
calculte_Vx();//計(jì)算待測(cè)電壓
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -