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,這個變數即計數值