
#include <AT89x51.h>
#include <stdio.h>
#include "ADC0831.h"
#include "lcd4bit.h"

void InitSystem()
{
	InitADC0831();
	init_lcd();
	gotoxy_lcd(1, 1);
	print_lcd("ADC0831 To 8051 ");
}

void main()
{
	unsigned char i;
	unsigned char Msg[20];
	unsigned int AverageValue;
	unsigned char Sample[5] = {0, 0, 0, 0, 0};

	InitSystem();
	while(1)
	{
		AverageValue = 0;
		for(i = 4; i > 0; i --)
		{
			Sample[i] = Sample[i - 1];
			AverageValue += Sample[i];
		}
		Sample[0] = ReadADC0831();
		AverageValue += Sample[0];
		AverageValue /= 5;
		sprintf(Msg, "   ADC = %d      \0", AverageValue);
		gotoxy_lcd(1, 2);
		print_lcd(Msg);
	}
}

