【蓝桥杯单片机笔记】11-频率测量

朴实无华,没有笔记
频率测量比较简单:将定时器 0 初始化为计数模式,对输入到 P34(T0)的脉冲进行计数,1s 的计数值即为频率值。
频率测量时需要将 J3-15(SIGNAL)与 J3-16(P34)短接。

练习:数码管显示测量的频率

#include"reg52.h"
sfr AUXR = 0x8e;
code unsigned char code duanma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
code unsigned char code duanmadot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

void SelectHC573(unsigned char n){
	
	switch(n)
	{
		case 4:
			P2 = (P2 & 0x1f) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1f) | 0xa0;
			break;
		case 6:
			P2 = (P2 & 0x1f) | 0xc0;
			break;
		case 7:
			P2 = (P2 & 0x1f) | 0xe0;
			break;
	} 
	
}
void SystemInit(){
	
	SelectHC573(4);
	P0=0xff;
	SelectHC573(5);
	P0=0x00;
	
}

void delay(unsigned int t){
	
	while(t--);
	
}

void Display(unsigned char pos,unsigned int value){
	
	SelectHC573(7);
	P0=0xff;
	SelectHC573(6);
	P0=0x01<<pos;
	SelectHC573(7);
	P0=value;

} 

void InitTimer()
{	
	TMOD = 0x04;  
	TH0 = 0xff;        
	TL0 = 0xff;
	TH1 = (65536 - 1000) / 256;        
	TL1 = (65536 - 1000) % 256;

  	ET0 = 1;
  	ET1 = 1;
	TR0 = 1;
	TR1 = 1;
	EA = 1;
}

unsigned int k;
void Service_T0() interrupt 1
{
	k++;
}

unsigned int count,fre; 
void Service_T1() interrupt 3
{
	count++;
	if(count == 1000)
	{
		fre = k;
		count = 0;
		k = 0;
	}
}

unsigned char a,b,c,d,e;
void show(){

	a=fre/10000;
	b=(fre/1000)%10;
	c=(fre/100)%10;
	d=(fre/10)%10;
	e=fre%10;

	Display(3,duanma[a]);
	delay(100);
	
	Display(4,duanma[b]);
	delay(100);
	
	Display(5,duanma[c]);
	delay(100);
	
	Display(6,duanma[d]);
	delay(100);
	
	Display(7,duanma[e]);
	delay(100);
	
	Display(0,0xff);
	delay(100);

	Display(1,0xff);
	delay(100);

	Display(2,0xff);
	delay(100);
}
	
void main()
{
	SystemInit();
	InitTimer();
	while(1)
	{
	
	show();		
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沅_Yuan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值