第十届蓝桥杯单片机省赛题目

本文详细介绍了STM8单片机中的Timer0、Timer1和Timer2的初始化设置、中断模式以及在主函数中的应用,包括频率计数和数码管显示控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一部分

选择题:

1.B

2.可作为高8位地址

3.Timer0:
模式1 16位自動重裝載模式
模式2 16位不可重裝載模式
模式3 8位自動重裝載模式
模式4 不可屏蔽中斷的16位自動重裝載模式

Timer1:
模式1 16位自動重裝載模式
模式2 16位不可重裝載模式
模式3 8位自動重裝載模式
模式4 無效,停止計數

Timer2:
16位自動重裝載模式

4.c

5.D

6.PCON电源管理寄存器,SCON串口寄存器,AUXR辅助寄存器,TCON定时器寄存器

选D

7.A

8.A

9.D

10.A

第二部分

这次的程序题不难

代码如下

#include "STC15.h"
#include "sys.h"
#include "smg.h"
#include "iic.h"
#include "key.h"
long int freq = 0;
uchar V_count=120;
uchar S4_change=0;long int freq_new;
uchar dat,S5_change=1,S6_change=1;
float V_zhen=0;
void pl_display(void)
{
	uchar gaowei=0;
	//频率数据显示使用 6 位数码管,当显示的数据长度不
//足 6 位时,未使用到的数码管位应熄灭。
	if(S4_change==1)
	{
		smg_display(0,15,0);
		smg_display(1,18,0);
		
		if(freq/100000==0&&gaowei==0)
			smg_display(2,18,0);
		else 
		{
			smg_display(2,freq/100000,0);gaowei=1;
		}
			
		
		if(freq%100000/10000==0&&gaowei==0)
			smg_display(3,18,0);
		else 
		{
			smg_display(3,freq%100000/10000,0);gaowei=1;
		}
			
		
		if(freq/1000%10==0&&gaowei==0)
			smg_display(4,18,0);
		else 
		{
			smg_display(4,freq/1000%10,0);gaowei=1;
		}
			
		
		if(freq/100%10==0&&gaowei==0)
			smg_display(5,18,0);
		else 
		{
			smg_display(5,freq/100%10,0);gaowei=1;
		}
			
		
		if(freq%100/10==0&&gaowei==0)
			smg_display(6,18,0);
		else 
		{
			smg_display(6,freq%100/10,0);gaowei=1;
		}
			
		
		if(freq%10==0&&gaowei==0)
			smg_display(7,18,0);
		else 
		{
			smg_display(7,freq%10,0);gaowei=1;
		}
		if(S6_change==1)
		{
			P0 = 0xfd;
			hc573(4);
		}
	}

}
void V_display(void)
{
	if(S4_change==0)
	{
		
		smg_display(0,26,0);
		smg_display(1,18,0);
		smg_display(2,18,0);
		smg_display(3,18,0);
		smg_display(4,18,0);
		
		V_zhen = V_count/48.0;
		//V_xiao = V_count%48/5;
		if(V_count==240)V_zhen=5;
		smg_display(5,(unsigned char)V_zhen,1);
		smg_display(6,(unsigned int)(V_zhen*100)/10%10,0);
		smg_display(7,(unsigned int)(V_zhen*100)%10,0);
		
		if(S6_change==1)
		{
			P0 = 0xfe;
			hc573(4);
		}
	}

}
void S5_cufa(void)
{
	if(dat==5)S5_change++;if(S5_change==2)S5_change=0;
	if(S5_change==0)V_count=read_pc(0x03);
	if(S5_change==1)V_count=100;
	
}
void S6_cufa(void)
{
	if(dat==6)
	{
		P0 = 0xff;
		S6_change++;
	}
	if(S6_change==2)
	{
		
		S6_change=0;Delay1ms(20);
	}
	
	if(S6_change==0)
	{
		P0 = 0xff;hc573(4);
	}
	if(S6_change==1&&S5_change==0)
	{
		if(V_count<72)
		{
			P0 = P0 | 0x04;
		}
		else if(V_count>=72 && V_count<120)
		{
			P0 = P0 & 0xfb;
		}
		else if(V_count>=120 && V_count<168)
		{
			P0 = P0 | 0x04;
		}
		else if(V_count>=168)
		{
			P0 = P0 & 0xfb;
		}
		hc573(4);
	}
	else if(S6_change==1&&S5_change==1)
	{
		P0 = P0 | 0x04;hc573(4);
	}
	if(S6_change==1)//freq
	{
		if(freq>1000)P0 = P0 | 0x80;
		if(freq>=1000&&freq<5000)P0 = P0 & 0xf7;
		if(freq>=5000&&freq<10000)P0 = P0 | 0x80;
		if(freq>=10000)P0 = P0 & 0xf7;
		hc573(4);
	}
}
int a=0;
//计数0.01ms,
//1MS的次数,f = 
void Timer0_Isr(void) interrupt 1
{
	freq_new++;
	
}

void Timer0_Init(void)		//1微秒@12.000MHz
{
	AUXR &= 0x7F;			//定时器时钟12T模式
	TMOD = 0xF4;			//设置定时器模式
	TL0 = 0xFF;				//设置定时初始值
	TH0 = 0xFF;				//设置定时初始值
	TF0 = 0;				//清除TF0标志
	TR0 = 1;				//定时器0开始计时
	ET0 = 1;				//使能定时器0中断
	EA = 1;
}
int time_count=1000;
void Timer1_Isr(void) interrupt 3
{
	time_count--;
	if(time_count==0)
	{
		TR0 = 0;TR1 = 0;freq=freq_new;freq_new=0;
		time_count=1000;
	}
}

void Timer1_Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x40;			//定时器时钟1T模式
	TMOD &= 0x0F;			//设置定时器模式
	TL1 = 0x20;				//设置定时初始值
	TH1 = 0xD1;				//设置定时初始值
	TF1 = 0;				//清除TF1标志
	TR1 = 1;				//定时器1开始计时
	ET1 = 1;				//使能定时器1中断
}
uint time2_count=1000;

void Timer2_Isr(void) interrupt 12
{
	time2_count--;
	while(time2_count==0)
	{
		time2_count=1000;TR0 = 1;TR1 = 1;//freq=0;
	}
}

void Timer2_Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x04;			//定时器时钟1T模式
	T2L = 0x20;				//设置定时初始值
	T2H = 0xD1;				//设置定时初始值
	AUXR |= 0x10;			//定时器2开始计时
	IE2 |= 0x04;			//使能定时器2中断
}

void main()
{
	uint num=0;
	sysinit();
//	P0 = 0x00;
//	hc573(4);
	//smg_display(0,0,1);
	Timer0_Init();
	Timer1_Init();Timer2_Init();
	while(1)
	{
		//V_display();
		dat = key_display();
		if(dat==4)S4_change++;if(S4_change==2)S4_change=0;
		pl_display();
		V_display();
		S5_cufa();
		S6_cufa();
//		if(dat==)
//		{
//			TR0 = 1;
//			TR1 = 1;
//		}//freq = a;
		//num = TH0<<8||TL0;
		//smg_display(1,num,0);
//		dat = read_pc(0x03);
//		smg_display(0,freq/1000,0);
//		smg_display(1,freq%1000/100,0);
//		smg_display(2,freq%100/10,0);
//		smg_display(3,freq%10,0);
//		write_pc(0);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值