這是數(shù)據(jù)采樣的程序
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define ADC_FLAG 0X10
#define VCC 220
uchar code SMGduan[]= {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,}; //數(shù)碼管位選碼
uchar code SMGwei[] = {0xfe, 0xfd, 0xfb};
unsigned char pdata bufRxd[64]; //接收字節(jié)緩沖區(qū)
/***********************延時函數(shù)*******************************/
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
/************************采用器初始化******************************/
void InitADC(void)
{
P1M1=0xff; //1^012開啟高阻態(tài)
P1M0=0x00; //其余端口當普通端口使用
P1ASF=0Xff; //P^1 DC_V
ADC_RES=0;
ADC_CONTR=0X80; //1000 0000 開電源,最低速
delay(50);
}
/**************************采樣函數(shù)******************************/
uint GetADC(uchar ch)
{
uint result=0;
ADC_CONTR=0x88|ch; //選擇通道 開始AD轉(zhuǎn)換e8 ADC最快,88,ADC最慢
_nop_(); _nop_();
_nop_(); _nop_();
while(!(ADC_CONTR&ADC_FLAG)); //查詢ADC_FLOG是否置位1 轉(zhuǎn)換結(jié)束
ADC_CONTR&=!ADC_FLAG; //清除ADC中斷標志
result=ADC_RES; //(ADC_RES<<2)+ADC_RESL (<<2)+ADC_RESL;
aa[ss++]= ADC_RES;
if(ss==6)ss=2;
return(result);
}
/**********************定時器0初始化****************************/
void timer0Init()
{
EA = 1; //打開總中斷
ET0 = 1;//打開定時器0中斷
TR0 = 1; //啟動定時器0
REN = 1;//允許串口接收
TMOD |= 0X01; //定時器工作模式1,16位定時模式
TH0 = 0xED;
TL0 = 0xFF; //定時5ms
}
/***********************串口初始化*********************************/
void UARTInit()
{
EA = 1; //打開總中斷
ES = 1; //打開串口中斷
SM0 = 0; SM1 = 1;//串口工作方式1,8位UART波特率可變
}
void main()//main函數(shù)自身會循環(huán)
{
InitADC(); //采樣初始化
timer0Init();//定時器0初始化
UARTInit();//串口初始化
bb=0;
while(mode)
{
}
}
while(mode==0)
{
EA=0; //關(guān)總中斷
bb=0;
while(sw_up==1&sw_down==1&sw_mune0==1&sw_mune1==1&sw_mune2==1)if(mode)break;
if(sw_mune1==0) aa[9]=4;
if(sw_mune2==0) aa[9]=5;
while(sw_up==0|sw_down==0|sw_mune0==0|sw_mune1==0|sw_mune2==0)if(mode)break;
if(mode)break;
else
EA=1; //開總中斷
delay(300);
}
}
/****************************串口通信中斷*****************************/
void UART() interrupt 4
{
SBUF=0xff;
if(TI)//判斷是否發(fā)送完成
{
bb++;
if(bb>=10&&aa[9]==0xff)
{ bb=0; delay(1000); }
}
if(RI==1)//判斷接收是否完成
{
int nu;
nu =SBUF ;//讀SBUF,讀出串口接收到的數(shù)據(jù)
//第0號數(shù)據(jù)不是幀頭,跳過
switch(nu)
{
case 1:{LED2=1;LED1=0;LED0=1;wmq=0;JDQ=0;}break; //過壓
case 2:{LED2=1;LED1=0;LED0=1;wmq=0;JDQ=0;}break; //過流
case 3:{LED2=0;LED1=1;LED0=1;wmq=1;JDQ=1;}break; //低速
case 0:{LED2=1;LED1=1;LED0=0;wmq=1;JDQ=1;}break; //正常
}
RI = 0;//軟件清零接收標志位
cc++;
if(cc==1)cc=0;
}
}