51单片机设计两位计数器C语言

2025-04-05 10:05:34
推荐回答(2个)
回答1:

#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 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:

你好!程序是c语言写吗