第十五届单片机模拟考试III

发布于:2025-04-06 ⋅ 阅读:(18) ⋅ 点赞:(0)

题目

题目不长 ,功能也不难,一道水题

按键功能

S4界面切换,S5 功能切换,在不同界面转换不同的功能,定义两个标志位记录即可。

S9复位,回到初始状态,记得界面也得回到初始的信号界面(这里算是一个坑点)

/*按键函数区域*/
void Key_Proc(){
	if(Key_Slow) return;
	Key_Slow = 1;
	
	Key_Val = Key_Read();
	Key_Down = Key_Val & (Key_Val ^ Key_Old);
	Key_Up = ~Key_Val & (Key_Val ^ Key_Old);
	Key_Old = Key_Val;
	
	switch(Key_Down){
		case 4:
			Seg_Mode ^= 1;
		break;
		case 5:
			if(Seg_Mode == 0){//在信号界面,频率转换周期
				freq_zq = 1;
				time_start = 1;
			}
			else{//温度界面,小数转换成整数
				float_zs = 1;
			}
		break;
		case 9://复位
			float_zs = 0;
			freq_zq = 0;
			Seg_Mode = 0;
		break;
	}
}

显示功能 

基础的频率显示和温度显示,记得我们的按键S5界面内的模式切换,定义的标志位来对应不同的小界面,记得高位熄灭。

/*数码管函数区域*/
void Seg_Proc(){
	if(Seg_Slow) return;
	Seg_Slow = 1;
	
	temp = rd_tempature();
	if(temp>30) L7_Flag = 1;
	else L7_Flag = 0;
	
	if(Seg_Mode == 0){//频率界面
		Seg_Buf[0] = 11;//P
		Seg_Buf[1] = 10;
		Seg_Buf[2] = 10;
		Point[6] = 0;
		if(freq_zq ==0){//频率
			Seg_Buf[3] = freq>9999?freq/10000:10;
			Seg_Buf[4] = freq>999?freq%10000/1000:10;
			Seg_Buf[5] = freq>99?freq%1000/100:10;
			Seg_Buf[6] = freq>9?freq%100/10:10;
			Seg_Buf[7] = freq%10;
		}
		else{
			Seg_Buf[3] = zq>9999?zq/10000:10;
			Seg_Buf[4] = zq>999?zq%10000/1000:10;
			Seg_Buf[5] = zq>99?zq%1000/100:10;
			Seg_Buf[6] = zq>9?zq%100/10:10;
			Seg_Buf[7] = zq%10;
		}
	}
	else{//温度界面
		Seg_Buf[0] = 12;//C
		Seg_Buf[1] = 10;
		Seg_Buf[2] = 10;
		Seg_Buf[3] = 10;
		Seg_Buf[4] = 10;
		if(float_zs == 0){//小数
			Point[6] = 1;
			Seg_Buf[5] = (unsigned char)temp>10?(unsigned char)temp/10:10;
			Seg_Buf[6] = (unsigned char)temp%10;
			Seg_Buf[7] = (unsigned int)(temp*10)%10;
		}
		else{
			Point[6] = 0;
			Seg_Buf[5] = 10;
			Seg_Buf[6] = (unsigned char)temp>10?(unsigned char)temp/10:10;
			Seg_Buf[7] = (unsigned char)temp%10;
		}
		
	}
	
}

Led指示灯功能

L1,L2是常规的界面指示灯,L3是功能指示灯,L8状态指示灯,都很常规,L3,L8检测标志位即可.

/*Led函数区域*/
void Led_Proc(){
	Led_Buf[0] = (Seg_Mode==0);
	Led_Buf[1] = (Seg_Mode==1);
	Led_Buf[2] = time_start;//点亮1s后熄灭
	Led_Buf[7] = L7_flash;
}

完整主函数 

/*头文件区域*/
#include <STC15F2K60S2.H>
#include <Led.h>
#include <Seg.h>
#include <Init.h>
#include <Key.h>
#include <onewire.h>

/*参数变量区域*/
unsigned char Seg_Pos;
unsigned char Key_Down,Key_Val,Key_Old,Key_Up;
unsigned char Seg_Slow,Key_Slow;
unsigned int Time_1s;
//数组
unsigned char Seg_Buf[] = {10,10,10,10,10,10,10,10};
unsigned char Led_Buf[] = {0,0,0,0,0,0,0,0};
unsigned char Point[] = {0,0,0,0,0,0,0,0};
//数据
unsigned char Seg_Mode;//0信号1温度
unsigned int freq;
unsigned int zq;
float temp;
bit freq_zq;//默认0频率。1周期
bit float_zs;//默认0小数,1整数
bit time_start;
bit L7_Flag;
bit L7_flash;
unsigned int Time_L3_1s;
unsigned char Time_100ms;
/*按键函数区域*/
void Key_Proc(){
	if(Key_Slow) return;
	Key_Slow = 1;
	
	Key_Val = Key_Read();
	Key_Down = Key_Val & (Key_Val ^ Key_Old);
	Key_Up = ~Key_Val & (Key_Val ^ Key_Old);
	Key_Old = Key_Val;
	
	switch(Key_Down){
		case 4:
			Seg_Mode ^= 1;
		break;
		case 5:
			if(Seg_Mode == 0){//在信号界面,频率转换周期
				freq_zq = 1;
				time_start = 1;
			}
			else{//温度界面,小数转换成整数
				float_zs = 1;
			}
		break;
		case 9://复位
			float_zs = 0;
			freq_zq = 0;
			Seg_Mode = 0;
		break;
	}
}
/*数码管函数区域*/
void Seg_Proc(){
	if(Seg_Slow) return;
	Seg_Slow = 1;
	
	temp = rd_tempature();
	if(temp>30) L7_Flag = 1;
	else L7_Flag = 0;
	
	if(Seg_Mode == 0){//频率界面
		Seg_Buf[0] = 11;//P
		Seg_Buf[1] = 10;
		Seg_Buf[2] = 10;
		Point[6] = 0;
		if(freq_zq ==0){//频率
			Seg_Buf[3] = freq>9999?freq/10000:10;
			Seg_Buf[4] = freq>999?freq%10000/1000:10;
			Seg_Buf[5] = freq>99?freq%1000/100:10;
			Seg_Buf[6] = freq>9?freq%100/10:10;
			Seg_Buf[7] = freq%10;
		}
		else{
			Seg_Buf[3] = zq>9999?zq/10000:10;
			Seg_Buf[4] = zq>999?zq%10000/1000:10;
			Seg_Buf[5] = zq>99?zq%1000/100:10;
			Seg_Buf[6] = zq>9?zq%100/10:10;
			Seg_Buf[7] = zq%10;
		}
	}
	else{//温度界面
		Seg_Buf[0] = 12;//C
		Seg_Buf[1] = 10;
		Seg_Buf[2] = 10;
		Seg_Buf[3] = 10;
		Seg_Buf[4] = 10;
		if(float_zs == 0){//小数
			Point[6] = 1;
			Seg_Buf[5] = (unsigned char)temp>10?(unsigned char)temp/10:10;
			Seg_Buf[6] = (unsigned char)temp%10;
			Seg_Buf[7] = (unsigned int)(temp*10)%10;
		}
		else{
			Point[6] = 0;
			Seg_Buf[5] = 10;
			Seg_Buf[6] = (unsigned char)temp>10?(unsigned char)temp/10:10;
			Seg_Buf[7] = (unsigned char)temp%10;
		}
		
	}
	
}

/*Led函数区域*/
void Led_Proc(){
	Led_Buf[0] = (Seg_Mode==0);
	Led_Buf[1] = (Seg_Mode==1);
	Led_Buf[2] = time_start;//点亮1s后熄灭
	Led_Buf[7] = L7_flash;
}

/*定时器0初始化函数*/
void Timer0_Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0x7F;			//定时器时钟12T模式
	TMOD &= 0xF0;			//设置定时器模式
	TMOD |= 0x05;
	TL0 = 0x00;				//设置定时初始值
	TH0 = 0x00;				//设置定时初始值
	TF0 = 0;				//清除TF0标志
	TR0 = 1;				//定时器0开始计时
}

/*定时器1初始化函数*/
void Timer1_Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0xBF;			//定时器时钟12T模式
	TMOD &= 0x0F;			//设置定时器模式
	TL1 = 0x18;				//设置定时初始值
	TH1 = 0xFC;				//设置定时初始值
	TF1 = 0;				//清除TF1标志
	TR1 = 1;				//定时器1开始计时
	ET1 = 1;
	EA = 1;
}

/*定时器1中断服务函数*/
void Time1_Service() interrupt 3
{
	if(++Seg_Slow == 200) Seg_Slow = 0;
	if(++Key_Slow == 50) Key_Slow = 0;
	
	if(++Seg_Pos == 8) Seg_Pos = 0;
	Led_Disp(Seg_Pos,Led_Buf[Seg_Pos]);
	Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Point[Seg_Pos]);
	
	if(++Time_1s == 1000){
		Time_1s = 0;
		TR0 = 0;
		freq = TH0<<8|TL0;
		zq = 1000000/freq;
		TH0 = TL0 = 0;
		TR0 = 1;
	}
	
	if(time_start){
		if(++Time_L3_1s == 1000){
			Time_L3_1s = 0;
			time_start = 0;//停止计时
		}
	}
	else Time_L3_1s = 0;
	
	if(L7_Flag){
		if(++Time_100ms == 100){
			Time_100ms = 0;
			L7_flash ^= 1;
		}
	}
	else{
		Time_100ms = 0;
		L7_flash = 0;
	}
}

void main(){
	Timer0_Init();
	Timer1_Init();
	Sys_Init();
	while(1){
		Key_Proc();
		Seg_Proc();
		Led_Proc();
	}
}

一道水的不能再水的题目


网站公告

今日签到

点亮在社区的每一天
去签到