
#include <stdio.h>
#include <ADuC814.h>

void daouth(int i) {  // update high byte
 DAC0H = i;
}

void daoutl(int i) {  // update low byte
 DAC0L = i;
}

void main(void) {
 int i, a[5];
 ADCCON1 = 0x80;  // turn on the internal reference
 DACCON = 0x0D; // DAC outputs update as soon as the DAC low byte are written.
 DAC0H = 0x00; // initialize DAC high byte
 DAC0L = 0x00; // initialize DAC low byte
 SCON = 0x52;  // 8bits UART, enable serial receive
 TMOD = 0x22;  // 8bits auto-reload timer/counter
 TH1 = 0xF7;   // F7 for 9600 band
 TR1 = 1;      // run timer1
 PCON = 0x80;  // double UART baud rate

 while(1) {
  i = 0;
  while(i<10) {
   if(RI) {
    if(!(i%2)) a[i/2] = SBUF;
    i++;
    RI = 0;
   }
  }
  if(a[0]&0x0F==a[0] && a[1]&0x0F==a[1] && a[2]&0x0F==a[2] && a[3]&0x0F==a[3] && a[4]&0x0F==a[4]) {
   while(1) {
    if(RI) {
     P1 ^= 0x02;
     daouth(SBUF);
     RI = 0;
    }
    if(RI) {
     P1 ^= 0x02;
     daoutl(SBUF);
     RI = 0;
    }
   }
  }
  else {
   while(1) {
    if(RI) {
     P1 ^= 0x02;
     daoutl(SBUF);
     RI = 0;
    }
    if(RI) {
     P1 ^= 0x02;
     daouth(SBUF);
     RI = 0;
    }
   }	
  }
 }
}
