#include "reg51.h"
#include "intrins.h"
#define FOSC 18432000L
#define BAUD 9600
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 rusult register
sfr P1ASF =0x9D; //P1 secondary function control register
/*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_SPEEDLH 0x60 //90 clocks
void InitUart();
void SendData(BYTE dat);
void Delay(WORD n);
void InitADC();
BYTE ch=0; //ADC channel NO.
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/Dconversion
while(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--------*/
void InitADC()
{
P1ASF=0xff; //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;
TMOD=0x20;
TH1=TL1=-(FOSC/12/32/BAUD);
TR1=1;
}
/*-----------------
Send one byte data to PC
Input:dat(UART data)
output:---------*/
void SendData(BYTE dat)
{
while(!TI);
TI=0;
SBUF=dat;
}
/*--------------
Software delay funtion
---------------*/
void Delay(WORD n)
{
WORD x;
while(n--)
{
x=5000;
while(x--);
}
}
#define FOSC 18432000L 是指晶振频率吗? 那后面的L是什么意思啊
ADC_CONTR=ADC_POWER|ADC_SPEEDLL|ADC_START|ch 这个有啥用啊 |