欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

《单片机实践项目》之《电子DIY》之基于51单片机的60秒倒计时设计

程序员文章站 2022-03-02 10:12:00
程序#include "reg51.h"//视频讲解:B站搜索:“逗比小憨憨”#define LED_PORT P0#define uchar unsigned char#define uint unsigned intsbit dula=P2^0;sbit wela=P2^1;sbit LED1=P1^0;sbit LED2=P1^1;sbit START=P3^2;sbit STOP=P3^3;uchar counter=0;uchar displ...

程序

							
#include "reg51.h"
//视频讲解:B站搜索:“逗比小憨憨”
#define LED_PORT P0

#define uchar unsigned char
#define uint  unsigned int

sbit dula=P2^0;
sbit wela=P2^1;
sbit LED1=P1^0;
sbit LED2=P1^1;
sbit START=P3^2;
sbit STOP=P3^3;


uchar  counter=0;
uchar  display_value=0;
uchar  START_STOP_Flag=0;

uchar code dis[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay_ms(uint a);
void display(uchar i,uchar j);


void delay_ms(uint a)
{
	uint i=0,j=0;
	for(i=0;i<a;i++)
		for(j=0;j<118;j++);// 1ms
}

void display(uchar i,uchar j)
{
   dula=wela=0;
   LED_PORT=0X00;
   dula=1;
   LED_PORT=dis[i];
   dula=0;
   LED_PORT=0XFF;
   wela=1;
   LED_PORT=0xfe;
   wela=0;
   delay_ms(5);
   LED_PORT=0X00;
   dula=1;
   LED_PORT=dis[j];
   dula=0;
   LED_PORT=0Xff;
   wela=1;
   LED_PORT=0xfd;
   wela=0;
   delay_ms(5);
}

void Init(void)
{
	LED1=0;
	LED2=1;
	TMOD=0x01;		 			
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	ET0=1;
	IT0=1;
	IT1=1;
	EX0=1;
	EX1=1;
	PX0=1;
	PX1=1;			 
	EA=1;
}
int main(void)
{
	uchar cnt=60;
	 
	Init();	
	while(1)
	{
		if(START_STOP_Flag==1)
		{
			if(counter==20)//50*20=1S
			{	
				counter=0;
				LED1=!LED1;
				LED2=!LED2;
				display_value++;//1
				cnt=60-display_value;  //59
				if(display_value==60)
				{
					display_value=0;	   //若到了60s,则归零
				}
			}
			display(cnt/10,cnt%10);	   //显示
		}
		else
		{
			display(6,6);	   //显示	
		}
	}
	return 0;
}

void Timer0(void) interrupt 1 using 0
{
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	counter++;
}

void Int0(void)interrupt 0 using 0
{
	START_STOP_Flag=1;
	TR0=1;	
}

void Int1(void)interrupt 2 using 1
{
	START_STOP_Flag=0;	
	TR0=0;
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;
	counter=0;
}

					

本文地址:https://blog.csdn.net/liuxianfei0810/article/details/107297864