51单片机按键计数器C语言编程

51单片机按键计数器C语言编程,第1张

#include<reg51.h>

#define uchar unsigned char

uchar distab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00}  //0到f

uchar number,dat,dis[4]

void t0isr() interrupt 1

{

TH0=(65536-5000)/256

TL0=(65536-5000)%256

number++

number%=3

switch(number)

P1=0x20<<number

P0=distab[dis[number]]

}

void int0isr() interrupt 0

{

dat++

dat%=1000

dis[0]=dat%10

dis[1]=dat%100/10

dis[2]=dat/100

}

main()

{

TMOD=0x01

TH0=(65536-5000)/256

TL0=(65536-5000)%256

TR1=1

ET1=1

EX0=1

IT0=1

EA=1

while(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=0i<ti++)

for(j=0j<116j++)

}

//显示子程序

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)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11365806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存