1. 51单片机设计两位计数器C语言
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define dataport P1
sbit s1=P2^0;
sbit s2=P2^1;
sbit s3=P2^2;
sbit wei1=P2^4;
sbit wei2=P2^5;
signed char a=0;
uchar TABLE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//延时子程序
void delay1ms(uint t)
{
uint i;
uint j;
for(i=0;i<t;i++)
for(j=0;j<116;j++);
}
//显示子程序
void display(uchar n)
{
wei1=1;
dataport=TABLE[n/10];
delay1ms(1);
wei1=0;
wei2=1;
dataport=TABLE[n%10];
delay1ms(1);
wei2=0;
}
void main(void)//主程序
{
while(1)
{
if(s1==0)
{
delay1ms(20);
if(s1==0)
{
a++;
}
}
if(a=100)a=0;
if(s2==0)
{
delay1ms(20);
if(s2==0)
{
a--;
}
}
if(a<0)a=99;
if(s3==0)
{
delay1ms(20);
if(s3==0)
{
a=0;
}
}
display(a);
}
}
2. C51单片机设计一个游客流量计数器(00-99),实现对游客人数的统计。
#include<reg51.h>
#defineucharunsignedchar
uchar派握codeledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//0-9
ucharscanled;
uchar顷猜disdat[6]={0,0,0,0,0,0};
uchardatin,datout,dath;
display()
{
disdat[0]=datin/10;
disdat[1]=datin%10;
disdat[2]=datout/10;
disdat[3]=datout%10;
disdat[4]=dath/10;
disdat[5]=dath%10;
}
voidext0isr()interrupt0
{
datin++;
datin%=100;
dath=(datin-datout);
display();
}
voidext1isr()interrupt尘乎庆2
{
if(dath>0)datout++;
dath=(datin-datout);
display();
}
voidt1isr()interrupt3 //显示
{
TH1=0xec;
TL1=0x78;
P2=~(1<<scanled);
P0=ledtab[disdat[scanled]];
scanled++;
scanled%=6;
}
main()
{
TMOD=0x10;
TH1=0xec;
TL1=0x78;
TR1=1;
ET1=1;
EX0=1;
IT0=1;
EX1=1;
IT1=1;
EA=1;
while(1);
}
3. 怎么用单片机设计一个计数器
把T0设为计数器模式,初值为FFFFH,来一个脉冲即中断,定义一个变量放在中断函数里加1,这个变量即计数值