Các bác giúp e giải thích chương trình dưới với. e chưa hiểu đoạn hàm main và đoạn từ đâu cho đến phần khai báo hàm Preset. ai giải thích hết hộ em được thì càng tốt ạ hihi.e cảm ơn nhìu.
#include <reg52.h>
#include <stdio.h>
#include <absacc.h>
unsigned char flg=0;
unsigned int tg;
unsigned char tick=0;
unsigned int prt=10, count;
/* code unsigned char table[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 };
sbit SMG_q = P1^0;
sbit SMG_b = P1^1;
sbit SMG_s = P1^2;
sbit SMG_g = P1^3;
unsigned char l_posit=0; */
//************************************************** ************************************************** ***********************
void getline (char *line)
{
while ((*line++ = getchar()) != '\n');
}
//************************************************** ************************************************** ***********************
int atoi (char *line)
{
bit sign;
int number;
/* skip white space */
for ( ; *line == ' ' || *line == '\n' || *line == '\t'; line++);
/* establish sign */
sign = 1;
if (*line == '+' || *line == '-') sign = (*line++ == '+');
/* compute decimal value */
for (number=0; *line >= '0' && *line <= '9'; line++)
number = (number * 10) + (*line - '0');
return (sign ? number : -number);
}
//************************************************** ************************************************** ***********************
unsigned int getnumber (void)
{
char line [4];
getline (line);
return (atoi (line));
}
void preset(void)
{
printf("Input Preset time pt=\n");
prt=getnumber();
}
void start(void)
{
printf("Start Counter\n");
TR0=1;
TR2=1;
TH0=0; TL0=0;
count=0; tg=0;
flg=1;
}
void stop(void)
{
printf("Stop Counter\n");
TR0=0;
TR2=0;
flg=0;
printf("counter number in %u second is %u\n", tg-1, count);
}
/* void display(unsigned int da)
{
P0=0XFF;
da=da%10000;
switch(l_posit){
case 0:
SMG_q=0;
SMG_b=1;
SMG_s=1;
SMG_g=1;
P0=table[da/1000];
break;
case 1:
SMG_q=1;
SMG_b=0;
SMG_s=1;
SMG_g=1;
P0=table[da%1000/100];
break;
case 2:
SMG_q=1;
SMG_b=1;
SMG_s=0;
SMG_g=1;
P0=table[da%100/10];
break;
case 3:
SMG_q=1;
SMG_b=1;
SMG_s=1;
SMG_g=0;
P0=table[da%10];
break;
}
l_posit++;
if(l_posit>3)
l_posit=0;
} */
void timer2(void) interrupt 5
{
TF2=0; //CLEAR INT
TR2=0; //STOP TIMER
TH2 = 0X4E;
TL2 = 0X20;
TR2=1;
if(tick++==20)
{
tick=0; if(tg++==prt) stop();
count=256*TH0+TL0;
}
}
void init(void)
{
/*--------------------------------------
Set Timer0 for 16-bit counter mode.
Set the Timer0 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x05;
TR0 = 0;
/*--------------------------------------
Set serial port for 9600 baud at
11.0592 MHz. Note that we use Timer 1
for the baud rate generator.
--------------------------------------*/
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON |= 0x80;
/*--------------------------------------
Timer2 as Timer
--------------------------------------*/
TH2 = 0X4E; //LOAD COUNTER 20000 SET 50 MS
TL2 = 0X20;
TR2 = 0 ; //TR2=1 START TIMER2
ET2 = 1; //ENABLE INTERRUPT
EA = 1;
}
void main (void)
{
init();
while (1)
{
if(RI)
{
RI=0;
if(SBUF=='s') start();
if(SBUF=='t') stop();
if(SBUF=='p') preset();
}
if((flg==1)&(tick==0)) printf("%u\r", count);
// display(count);
}
}
#include <reg52.h>
#include <stdio.h>
#include <absacc.h>
unsigned char flg=0;
unsigned int tg;
unsigned char tick=0;
unsigned int prt=10, count;
/* code unsigned char table[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 };
sbit SMG_q = P1^0;
sbit SMG_b = P1^1;
sbit SMG_s = P1^2;
sbit SMG_g = P1^3;
unsigned char l_posit=0; */
//************************************************** ************************************************** ***********************
void getline (char *line)
{
while ((*line++ = getchar()) != '\n');
}
//************************************************** ************************************************** ***********************
int atoi (char *line)
{
bit sign;
int number;
/* skip white space */
for ( ; *line == ' ' || *line == '\n' || *line == '\t'; line++);
/* establish sign */
sign = 1;
if (*line == '+' || *line == '-') sign = (*line++ == '+');
/* compute decimal value */
for (number=0; *line >= '0' && *line <= '9'; line++)
number = (number * 10) + (*line - '0');
return (sign ? number : -number);
}
//************************************************** ************************************************** ***********************
unsigned int getnumber (void)
{
char line [4];
getline (line);
return (atoi (line));
}
void preset(void)
{
printf("Input Preset time pt=\n");
prt=getnumber();
}
void start(void)
{
printf("Start Counter\n");
TR0=1;
TR2=1;
TH0=0; TL0=0;
count=0; tg=0;
flg=1;
}
void stop(void)
{
printf("Stop Counter\n");
TR0=0;
TR2=0;
flg=0;
printf("counter number in %u second is %u\n", tg-1, count);
}
/* void display(unsigned int da)
{
P0=0XFF;
da=da%10000;
switch(l_posit){
case 0:
SMG_q=0;
SMG_b=1;
SMG_s=1;
SMG_g=1;
P0=table[da/1000];
break;
case 1:
SMG_q=1;
SMG_b=0;
SMG_s=1;
SMG_g=1;
P0=table[da%1000/100];
break;
case 2:
SMG_q=1;
SMG_b=1;
SMG_s=0;
SMG_g=1;
P0=table[da%100/10];
break;
case 3:
SMG_q=1;
SMG_b=1;
SMG_s=1;
SMG_g=0;
P0=table[da%10];
break;
}
l_posit++;
if(l_posit>3)
l_posit=0;
} */
void timer2(void) interrupt 5
{
TF2=0; //CLEAR INT
TR2=0; //STOP TIMER
TH2 = 0X4E;
TL2 = 0X20;
TR2=1;
if(tick++==20)
{
tick=0; if(tg++==prt) stop();
count=256*TH0+TL0;
}
}
void init(void)
{
/*--------------------------------------
Set Timer0 for 16-bit counter mode.
Set the Timer0 Run control bit.
--------------------------------------*/
TMOD = (TMOD & 0xF0) | 0x05;
TR0 = 0;
/*--------------------------------------
Set serial port for 9600 baud at
11.0592 MHz. Note that we use Timer 1
for the baud rate generator.
--------------------------------------*/
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON |= 0x80;
/*--------------------------------------
Timer2 as Timer
--------------------------------------*/
TH2 = 0X4E; //LOAD COUNTER 20000 SET 50 MS
TL2 = 0X20;
TR2 = 0 ; //TR2=1 START TIMER2
ET2 = 1; //ENABLE INTERRUPT
EA = 1;
}
void main (void)
{
init();
while (1)
{
if(RI)
{
RI=0;
if(SBUF=='s') start();
if(SBUF=='t') stop();
if(SBUF=='p') preset();
}
if((flg==1)&(tick==0)) printf("%u\r", count);
// display(count);
}
}
Comment