#include <stc8a8k.h>
#include <math.h>
#include <stdlib.h>
sbit LED=P1^7;
sbit clk=P1^0;
sbit oe=P1^1;
void Delay300ms() //@24.000MHz
{
unsigned char i, j, k;
i = 37;
j = 135;
k = 138;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void delay5us() //@24.000MHz
{
unsigned char i;
i = 38;
while (--i);
}
void delay500us() //@24.000MHz
{
unsigned char i, j;
i = 16;
j = 147;
do
{
while (--j);
} while (--i);
}
unsigned char ReadAdc(void)
{
unsigned char i,Voltage;
oe=1;
for(i=8; i>0; i--)
{
clk=1;
delay5us();
delay5us();
clk=0;
delay5us();
// delay5us();
}
oe=0;
delay500us(); //??????????
Voltage=P2;
return(Voltage);
}
void Timer0Init(void) //0.5??@24.000MHz
{
AUXR |= 0x80; //?????1T??
TMOD &= 0xF0; //???????
TL0 = 0xE8; //???????
TH0 = 0xFF; //???????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
ET0 = 1; //???????
}
void portmode()
{
P0M0=0x00;P0M1=0x00;
P1M0=0x00;P1M1=0x00;
P2M0=0x00;P2M1=0x00;
P3M0=0x00;P3M1=0x00;
P4M0=0x00;P4M1=0x00;
P5M0=0x00;P5M1=0x00;
P6M0=0x00;P6M1=0x00;
P7M0=0x00;P7M1=0x00;
}
void UartInit(void) //9600bps@24.000MHz
{
SCON = 0x50; //8???,?????
AUXR |= 0x40; //?????1T??
AUXR &= 0xFE; //??1?????1???????
TMOD &= 0x0F; //???????
TL1 = 0x8F; //???????
TH1 = 0xFD; //???????
ET1 = 0; //?????%d??
TR1 = 1; //???1????
}
void UartSend(char dat)
{
SBUF = dat;
while (!TI);
TI = 0;
}
void main()
{
unsigned char dat;
portmode();
LED=0;
Delay300ms();
LED=1;
Delay300ms();
LED=0;
Delay300ms();
LED=1;
Delay300ms();
LED=0;
Delay300ms();
LED=1;
Delay300ms();
LED=0;
Delay300ms();
LED=1;
Delay300ms();
LED=0;
Delay300ms();
LED=1;
Delay300ms();
oe=1;
UartInit();
Delay300ms();
UartSend(0x85);
UartSend(0x0d);
UartSend(0x0a);
UartSend('a');
UartSend(0x0d);
UartSend(0x0a);
LED=0;
Delay300ms();
LED=1;
Delay300ms();
// Timer0Init();
// ET0=1;
// EA=1;
while(1)
{
dat = ReadAdc();
// P2=dat;
UartSend(dat);
UartSend(0x0d);
UartSend(0x0a);
Delay300ms();
Delay300ms();
Delay300ms();
}
}
void TM0_Isr() interrupt 1
{
static unsigned int aaa=0;
static unsigned int bbb=0;
aaa++;
if(aaa>65530)
{
aaa=0;
bbb++;
if(bbb>10)
{
bbb=0;
LED=!LED;
}
}
// clk = !clk; //????
}
基准电压是2.6V;
所以在2.6V–0xFF;
但是,后面电压,就不对,感觉误差有点大。
原因后面查找下。
视频电平2V。。不是基准电压2V.