#include <STC12C5A60S2.h>
#include <intrins.h>
#define HI 1
#define LO 0
#define true 1
#define false 0
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 18432000L
#define BAUD 9600
/*Declare SFR associated with the ADC */
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result 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
#define ADC1 0
#define ADC2 1
#define ADC3 2
#define ADC4 3
#define ADC5 4
#define ADC6 5
#define ADC7 6
#define ADC8 7
unsigned int mSec;
unsigned char SEC,MIN,cx=0;
unsigned int Data =0;
unsigned int ADC_Val = 0;
unsigned int LC=0;
/*----------------------------
Software delay function
----------------------------*/
void delay_ms(unsigned int DxVal) //@18.432MHz
{
unsigned char i, j;
while(DxVal)
{
_nop_();
_nop_();
i = 18;
j = 234;
do
{
while (--j);
} while (--i);
DxVal--;
}
}
/*----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------*/
void SendData( BYTE dat)
{
ES = LO;
SBUF = dat; //Send current data
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
ES = HI;
}
void PROMT(void)
{
SendData('\r'); SendData('\n');
SendData('>'); SendData('>');
}
void Timer0Init(void) //100us@18.432MHz
{
AUXR &= 0x7F; //Timer clock is 12T mode
TMOD &= 0xF0; //Set timer work mode
TMOD |= 0x22; //Set timer work mode
TL0 = 0x66; //Initial timer value
TH0 = 0x66; //Set reload value
TF0 = 0; //Clear TF0 flag
TR0 = 1; //Timer0 start run
}
void IntInit(void) //function Interrupt
{
EX0=HI; IT0 = HI;
ET0=LO;
EX1=HI; IT1 =LO;
ET1=LO;
ES=HI;
EA=HI;
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0xFF; //Set all P1 as analog input port
AUXR1 = 0x74;
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | 0;
delay_ms(2); //ADC power-on delay and Start A/D conversion
}
/*---------------------------
Get ADC result
----------------------------*/
WORD adc_read(BYTE ch)
{
unsigned int ADC_ValBuff;
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
ADC_ValBuff = ADC_RES;
ADC_ValBuff = ADC_ValBuff << 8;
ADC_ValBuff = ADC_ValBuff | ADC_RESL;
return(ADC_ValBuff); //Return ADC result
}
/*----------------------------
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
}
unsigned char RxChar;
void UART_IntService(void) interrupt 4
{
if(TI)
{
TI = LO; return;
}
RxChar = SBUF;
RI = 0;
SendData(RxChar);
}
void Sent_ADC (unsigned int n,WORD ADC_Valx)
{
unsigned int N1;
unsigned char N2;
SendData(n);
N2 = ADC_Valx/1000;
SendData(N2 +'0');
N1 = ADC_Valx % 1000;
SendData((N1/100)+'0');
N2 = N1%100;
SendData((N2/10)+'0');
SendData((N2%10)+'0');
PROMT();
}
void main (void)
{
Timer0Init();
InitUart();
IntInit();
InitADC();
while(1)
{
ADC_Val = adc_read(ADC1);
Sent_ADC('A',ADC_Val);
delay_ms(500);
}
}
评论0