|
#include <reg52.h> /* special function register declarations */
#include <absacc.h> /* for the intended 8051 derivative */
#include <stdio.h> /* prototype declarations for I/O functions */
sbit ADCLK=P4^2;
sbit ADCS=P4^0;
sbit ADDO=P4^1;
unsigned int i,j;
unsigned char timer1=0;
unsigned char read_ADC(void);
void main (void)
{ /* execution starts here after stack init */
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x21; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xfd; /* TH1: reload value for 19200 baud */
TR1 = 1; /* TR1: timer 1 run */
TH0 = 0xff;
TR0 = 1;
TI = 1; /* TI: set TI to send first char of UART */
IE = 0x82;
while (1) {
while(1){
i=read_ADC();
printf("Current light is %d\r\n",i);
i=0;
while(i<5){
timer1=0;
while(timer1<200){}
i++;
}
}
}
}
dir() interrupt 1
{
static unsigned char i;
TL0=0xcd;
TH0=0xf8;
timer1++;
}
unsigned char read_ADC(void)
{
unsigned char i,j;
ADCLK=0;
ADCS=0;
for(i=0;i<8;i++){
ADCLK=1;
ADCLK=1;
ADCLK=0;
ADCLK=0;
}
ADCS=1;
for(i=0;i<20;i++);
ADCS=0;
j=0;
for(i=0;i<8;i++){
ADCLK=1;
ADCLK=1;
ADCLK=0;
ADCLK=0;
j<<=1;
if(ADDO){
j+=1;
}
}
ADCS=1;
return j;
}
|