#include "stc12c5a56s2.h"
#include "intrins.h"
#define FOSC 11059200L
#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 high 8-bit result register
//sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result 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 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
extern void InitADC();
extern BYTE GetADCResult(BYTE ch);
void Delay1(WORD n);
/*----------------------------
Get ADC result
----------------------------*/
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
return ADC_RES; //Return ADC result
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0xff; //Open 8 channels ADC function
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
Delay1(2); //ADC power-on and Delay1
}
/*----------------------------
Software Delay1 function
----------------------------*/
void Delay1(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
具体的寄存器,参考PDF文档。这种应用的,仔细看文档就行了
看起来很难得样子。。。。我不会。。。。