51单片机制作脉冲发生器

设计一个秒脉冲发生器

任务描述:应用单片机定时/计数器设计一个秒脉冲发生器,并用2位数码管显示脉冲数


要求

1.在Keil IDE(u Vision2)中完成应用程序设计,并编译
2.在ISIS 7 Professional 中完成电路设计、调试与仿真


/** 
	I use a timer in my code to control the time
**/
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char

uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar second=0,counter=0,second_1=0,second_0=0,time=0;

/******************************Timer******************************/
void init_timer()
{
	TMOD=0x01;
	TH0=(65535-50000)/256;     
	TL0=(65535-50000)%256;
	EA=1;
	ET0=1;
	TR0=1;
}

void timer0() interrupt 1
{
	TH0=(65535-50000)/256;
	TL0=(65535-50000)%256;
	counter++;
	time++;
}
/******************************Display_Tube******************************/
void display_Tube()
{
	if(counter==20)
	{
		counter=0;
		second++;
		if((second>60)||(second==60))
		{
			second=0;
		}
		second_1=second/10;
		second_0=second%10;
	}
}

void display()
{
	if(time==5)
	{
		time=0;
		P2=tab[second_1];
		P3=0x01;
	}
	P3=0x00;

	if(time==5)
	{
		time=0;
		P2=tab[second_0];
		P3=0x02;
	}
	P3=0x00;
}		
/******************************Main******************************/	
void main()
{
	init_timer();
	while(1)
	{
		display_Tube();
		display();
	}
}
	
/**
	Writer: Terence
	Email: 3117807944@qq
	Date: 2020-10-24
	etc: If there is a problem with the code, you can call me.
		 I will solve the problem in the first time
**/

总结

在此代码中我使用了定时器来进行计数,这样可以保证每一次加1都为1秒 如果此代码有错误,请大佬多多指教

在这里插入图片描述

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值