PDA

View Full Version : giai thich cho minh voi


thanhhai18
29-06-2007, 10:50 AM
minh co chuong trinh nay ma khong tung dong lenh y nghia ra sao


#include <reg52.h>
#include <stdio.h>

void timer0int(void);
char *Puts(char* str);

float read_temp1(void);
float read_temp1_filter(void);


char cputick;
char buffer[24];
char pulseE;

char timer3;
char temp;
char vout;
int x1,x2,x3,x4,x5;

sbit RS=P1^6;
sbit E =P1^4;
sbit RW=P1^5;

sbit CLK=P1^2;
sbit Data=P1^1;
sbit CS=P1^3;

sbit CHG=P3^0;

int readADC(char n)
{
int k;
char i,channel;
k=0;

CS=0;

if(n==0) channel= 0x0d;
else channel=0x0f;

for (i=0;i<4;i++)
{
CLK = 0;

if(channel&8) Data =1;
else Data = 0;
CLK =1;
channel <<=1;
}

Data = 1;
CLK =0;

for(i=0; i<12;i++)
{
k<<=1;
CLK =1;
CLK=0;
if(Data) k|=1;
else k &=~1;
}
CS =1;

return k&=0xfff;
}


void timer0int(void) interrupt 1 using 1

{
TH0 |=0xdc;
cputick++;
}


pause(int j)
{ int i;
for (i=0;i<j;i++)
continue;
}


pulesE()
{
E =1;
;
E =0;
}


void delay(int m)
{
int j;
for(j=0;j<=m;j++)
continue;
}

LCDWI (char n)
{
RW =0;
RS=0;
E =0;
P0=n;
E =1;
E =0;
pause(50);
}


LCDWD(char n)
{
RW=0;
RS=1;
E=0;
P0=n;
E=1;
E=0;
pause(50);
}

print_LCD(char a,char *s)
{
LCDWI(a);
while(*s != 0)
LCDWD(*s++);
}


i_LCD()
{
RS=0;
E=0;
P0=0x30;pulesE();delay(10);pulesE();delay(1);pules E();delay(1);
P0=0x20;pulesE();pulesE();pulesE();
LCDWI(0x38);
LCDWI(0x0c);
LCDWI(0x06);
LCDWI(1);
delay(50);
}

print_ADC()
{
char buffer[20];
if(++timer3<50)
{
timer3=0;
sprintf(buffer,"%0.1fC %0.1fF",read_temp1_filter(),read_temp1_filter()*9/5+32);
print_LCD(0xc0,buffer);
}
}

int low_pass_filter1(void)
{
x5=x4;
x4=x3;
x3=x2;
x2=x1;
x1=readADC(0);
return(x1+x2+x3+x4+x5)/5;
}

float read_temp1_filter(void)
{
return(0.0323*low_pass_filter1()-15.615);
}


void main()
{
i_LCD();
print_LCD(0x80,"LCD Thermometer");
TMOD |= 0x01;
TR0 = 1;
cputick = 0;
EA = 1;
ET0 = 1;

for(;;)
{
while(!TF0)
continue;
TF0=0;
print_ADC();
}
}


giup minh nha

falleaf
29-06-2007, 03:15 PM
Chương trình này là chương trình đọc ADC. Sau khi đọc 5 lần thì lấy giá trị trung bình hiển thị ra LCD. Còn mấy cái chương trình con là để hiển thị lên LCD thôi.

Mấy cái << là dịch bit. Còn lại chả có cái gì khác cả. Nếu bạn chưa biết C thì bạn phải học lại C và tự mình chú thích dần vào chương trình rồi sẽ hiểu chương trình viết gì.

Chúc vui