RT:#include "reg51.h"//ADC
#include "intrins.h"
#define FOSC 18432000L
#define BAUD 9600
#define ag P0
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the ADC */
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
sfr AURX1 = 0xA2; //ADת»»½á¹û´æ´¢·½Ê½¿ØÖÆλ
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //540 clocks
#define ADC_SPEEDL 0x20 //360 clocks
#define ADC_SPEEDH 0x40 //180 clocks
#define ADC_SPEEDHH 0x60 //90 clocks
BYTE table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6d,0X7D,0X07,0X7F,0X6F};
sbit yiwei=P2^0;
sbit erwei=P2^1;
sbit sanwei=P2^2;
sbit siwei=P2^3;
//void InitUart();
//void SendData(BYTE dat);
void Delay(WORD n);
void InitADC();
void display(BYTE shuzi);
void delayms(WORD x);
void ShowResult(BYTE ch);
BYTE GetADCResult(BYTE aa);
BYTE value,volt;
BYTE ch = 0; //ADC channel NO.
WORD ad,value0;
void main()
{
//InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
IE = 0xa0; //Enable ADC interrupt and Open master interrupt switch
//Start A/D conversion
while (1)
{
value=GetADCResult(0);
value0=value*500/255*2;
erwei=value0/100;
sanwei=value0%100/10;
yiwei=value0%100%10;
display(ad);
}
}
void ShowResult(BYTE ch) //ÏÔʾ½á¹û
{
float value;
value=GetADCResult(ch);
value=value/255*4.8;
}
BYTE GetADCResult(BYTE aa) //¶ÁÈ¡ADC½á¹û
{
P1ASF=0X01;
ADC_CONTR=ADC_POWER|ADC_SPEEDLL|ADC_START|aa;
//ADC_CONTR=0X88|aa;
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));
//while(!ADC_FLAG);
//ADC_FLAG=0;
ADC_CONTR &= ~ ADC_FLAG ;
return ADC_RES; //·µ»ØADC½á¹û
}
//Êý¾Ý´¦Àí
void display(BYTE shuzi) //ÊýÂë¹ÜÏÔʾº¯Êý
{
BYTE yiwei,erwei,sanwei,siwei;
yiwei=shuzi/1000;
erwei=shuzi/100%10;
sanwei=shuzi/10%10;
siwei=shuzi%10;
P2=0xFE;
ag=table[yiwei]+0x80;
delayms(1);
ag=0;
P2=0xFD;
ag=table[erwei];
delayms(1);
ag=0;
P2=0xfb;
ag=table[sanwei];
delayms(1);
ag=0;
P2=0xf7;
ag=table[siwei];
delayms(2);
ag=0;
P2=0xef;
ag=table[siwei];
delayms(1);
}
/*----------------------------
ADC interrupt service routine
----------------------------*/
void adc_isr() interrupt 5 using 1 //ÖжϺ¯Êý
{
ADC_CONTR &= !ADC_FLAG; //Clear ADC interrupt flag
// SendData(ch); //Show Channel NO.
// SendData(ADC_RES); //Get ADC high 8-bit result and Send to UART
//if you want show 10-bit result, uncomment next line
// SendData(ADC_LOW2); //Show ADC low 2-bit result
if (++ch > 7) ch = 0; //switch to next channel
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
}
/*----------------------------
Initial ADC sfr //³õʼ»¯ADC
----------------------------*/
void InitADC( )
{
P1ASF = 0x80; //Set all P1 as analog input port
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
Delay(2); //ADC power-on delay and Start A/D conversion
}
/*----------------------------
Initial UART
----------------------------*/
/*---------------------------------
void InitUart()
{
SCON = 0x5a; //8 bit data ,no parity bit
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}
/*----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------*/
/*------------------------------
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
----------------------------------*/
/*----------------------------
Software delay function //ÑÓʱº¯Êý
----------------------------*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
void delayms(WORD x)
{
BYTE i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
为什么我接了电池却无法显示呢也就是说每办法测电压!求大神解答!!!
加急!!!!!
求助!!!!
#include "intrins.h"
#define FOSC 18432000L
#define BAUD 9600
#define ag P0
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*Declare SFR associated with the ADC */
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC hight 8-bit result register
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
sfr AURX1 = 0xA2; //ADת»»½á¹û´æ´¢·½Ê½¿ØÖÆλ
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //540 clocks
#define ADC_SPEEDL 0x20 //360 clocks
#define ADC_SPEEDH 0x40 //180 clocks
#define ADC_SPEEDHH 0x60 //90 clocks
BYTE table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6d,0X7D,0X07,0X7F,0X6F};
sbit yiwei=P2^0;
sbit erwei=P2^1;
sbit sanwei=P2^2;
sbit siwei=P2^3;
//void InitUart();
//void SendData(BYTE dat);
void Delay(WORD n);
void InitADC();
void display(BYTE shuzi);
void delayms(WORD x);
void ShowResult(BYTE ch);
BYTE GetADCResult(BYTE aa);
BYTE value,volt;
BYTE ch = 0; //ADC channel NO.
WORD ad,value0;
void main()
{
//InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
IE = 0xa0; //Enable ADC interrupt and Open master interrupt switch
//Start A/D conversion
while (1)
{
value=GetADCResult(0);
value0=value*500/255*2;
erwei=value0/100;
sanwei=value0%100/10;
yiwei=value0%100%10;
display(ad);
}
}
void ShowResult(BYTE ch) //ÏÔʾ½á¹û
{
float value;
value=GetADCResult(ch);
value=value/255*4.8;
}
BYTE GetADCResult(BYTE aa) //¶ÁÈ¡ADC½á¹û
{
P1ASF=0X01;
ADC_CONTR=ADC_POWER|ADC_SPEEDLL|ADC_START|aa;
//ADC_CONTR=0X88|aa;
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));
//while(!ADC_FLAG);
//ADC_FLAG=0;
ADC_CONTR &= ~ ADC_FLAG ;
return ADC_RES; //·µ»ØADC½á¹û
}
//Êý¾Ý´¦Àí
void display(BYTE shuzi) //ÊýÂë¹ÜÏÔʾº¯Êý
{
BYTE yiwei,erwei,sanwei,siwei;
yiwei=shuzi/1000;
erwei=shuzi/100%10;
sanwei=shuzi/10%10;
siwei=shuzi%10;
P2=0xFE;
ag=table[yiwei]+0x80;
delayms(1);
ag=0;
P2=0xFD;
ag=table[erwei];
delayms(1);
ag=0;
P2=0xfb;
ag=table[sanwei];
delayms(1);
ag=0;
P2=0xf7;
ag=table[siwei];
delayms(2);
ag=0;
P2=0xef;
ag=table[siwei];
delayms(1);
}
/*----------------------------
ADC interrupt service routine
----------------------------*/
void adc_isr() interrupt 5 using 1 //ÖжϺ¯Êý
{
ADC_CONTR &= !ADC_FLAG; //Clear ADC interrupt flag
// SendData(ch); //Show Channel NO.
// SendData(ADC_RES); //Get ADC high 8-bit result and Send to UART
//if you want show 10-bit result, uncomment next line
// SendData(ADC_LOW2); //Show ADC low 2-bit result
if (++ch > 7) ch = 0; //switch to next channel
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
}
/*----------------------------
Initial ADC sfr //³õʼ»¯ADC
----------------------------*/
void InitADC( )
{
P1ASF = 0x80; //Set all P1 as analog input port
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
Delay(2); //ADC power-on delay and Start A/D conversion
}
/*----------------------------
Initial UART
----------------------------*/
/*---------------------------------
void InitUart()
{
SCON = 0x5a; //8 bit data ,no parity bit
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}
/*----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------*/
/*------------------------------
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
----------------------------------*/
/*----------------------------
Software delay function //ÑÓʱº¯Êý
----------------------------*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
void delayms(WORD x)
{
BYTE i,j;
for(i=x;i>0;i--)
for(j=120;j>0;j--);
}
为什么我接了电池却无法显示呢也就是说每办法测电压!求大神解答!!!
加急!!!!!
求助!!!!