/*********************************************************************
Author : ADI - Apps www.analog.com/MicroConverter
Date : Sept. 2005
File : ADCtimer.c
Hardware : Applicable to ADuC702x rev H or I silicon
Currently targetting ADuC7026.
Description : Performs an ADC conversion every 100 us using timer0
overflow alternatively on Channel 0 and 1
sending the results through UART at 9600bps
*********************************************************************/
#include <ADuC7026.h> // Include ADuC7026 Header File
void My_IRQ_Function(void); // IRQ Funtion Prototype
void senddata(short to_send);
void ADCpoweron(int);
char hex2ascii(char toconv);
void delay(int);
int main (void) {
//ADC configuration
ADCpoweron(20000); // power on ADC
ADCCP = 0x00;
ADCCON = 0x4E2; // start conversion on timer 0
// ADC Config: fADC/2, acq. time = 2 clocks => ADC Speed = 1MSPS
REFCON = 0x01; // connect internal 2.5V reference to VREF pin
// Setup tx & rx pins on P1.0 and P1.1
GP1CON = 0x011;
// Start setting up UART at 9600bps
COMCON0 = 0x80; // Setting DLAB
COMDIV0 = 0x88;
COMDIV1 = 0x00;
COMCON0 = 0x07; // Clearing DLAB
// for test purposes only
GP0CON = 0x10100000; // enable ECLK output on P0.7, and ADCbusy on P0.5
IRQ = My_IRQ_Function; // Specify Interrupt Service Rountine
IRQEN = ADC_BIT; // Enable ADC IRQ ( 0x80 )
// timer0 configuration
T0LD = 0x1000; // 4096/40.96MHz = 100us
T0CON = 0xC0; // count down
// periodic mode
GP4DAT = 0x04000000; // Configure P4.2 as output
while(1)
{
}
return 0 ;
}
/********************************************************************/
/* */
/* Interrupt Service Rountine */
/* */
/********************************************************************/
void My_IRQ_Function()
{
GP4DAT ^= 0x00040000; // Complement P4.2
ADCCP ^= 1; // change channel
senddata (ADCDAT >> 16);
return ;
}
void senddata(short to_send)
{
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = 0x0A; // output LF
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = 0x0D; // output CR
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii ((to_send >> 8) & 0x0F);
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii ((to_send >> 4) & 0x0F);
while(!(0x020==(COMSTA0 & 0x020))){}
COMTX = hex2ascii (to_send & 0x0F);
}
char hex2ascii(char toconv)
{
if (toconv<0x0A)
{
toconv += 0x30;
}
else
{
toconv += 0x37;
}
return (toconv);
}
void delay (int length)
{
while (length >=0)
length--;
}
void ADCpoweron(int time)
{
ADCCON = 0x20; // power-on the ADC
while (time >=0) // wait for ADC to be fully powered on
time--;
}