蓝桥杯单片机第六周

蓝桥杯单片机第六周

目录
1、蓝桥杯大模板
2、DS18B20模块

学习收获:
蓝桥杯大模板
组成:头文件声明区、变量声明区、按键处理函数、信息处理函数、其他处理函数、定时器初始化、定时器中断服务程序、主函数

/* 头文件声明区 */
#include "STC15F2K60S2.h"
#include "Init.h"
#include "Key.h"
#include "Seg.h"
#include "Led.h"
/* 变量声明区 */
unsigned char Key_Val,Key_Down,Key_Old,Key_Up;//按键扫描变量
unsigned char Key_Slow_Down;//按键减速变量
unsigned char Seg_Slow_Down;//数码管减速变量
unsigned char Seg_Pos;//数码管Led位选变量
unsigned char Seg_Buf[] = {10,10,10,10,10,10,10,10};//数码管显示数组
unsigned char Seg_Point[] = {0,0,0,0,0,0,0,0};//小数点使能数组
unsigned char ucLed[] = {1,0,0,0,0,0,0,0};//Led显示控制数组
float r;//温度
/* 按键处理函数 */
void Key_Proc()
{
	if(Key_Slow_Down) return;
	Key_Slow_Down = 1;
	
	Key_Val = Key_Read();
	Key_Down = Key_Val & (Key_Old ^ Key_Val); 
	Key_Up = ~Key_Val & (Key_Old ^ Key_Val);
	Key_Old = Key_Val;
	
	
}
/* 信息处理函数 */
void Seg_Proc()
{
	if(Seg_Slow_Down) return;
	Seg_Slow_Down = 1;
	
	r=rd_temperature();
	Seg_Buf[0]
	Seg_Buf[1]
	Seg_Buf[2]
}
/* 其他处理函数 */
void Led_Proc()
{
	
}
/* 定时器初始化函数 */
void Timer0_Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0x7F;			//定时器时钟12T模式
	TMOD &= 0xF0;			//设置定时器模式
	TL0 = 0x18;				//设置定时初始值
	TH0 = 0xFC;				//设置定时初始值
	TF0 = 0;				//清除TF0标志
	TR0 = 1;				//定时器0开始计时
	ET0 = 1;				//定时器0中断开启
	EA = 1;				//全局中断开关开启
}
/* 中断服务函数 */
void Timer0_Server() interrupt 1
{
	if(++Key_Slow_Down==10) Key_Slow_Down = 0;
	if(++Seg_Slow_Down==500) Seg_Slow_Down = 0;
	if(++Seg_Pos==8) Seg_Pos = 0;
	Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
	Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
}
/* Main */
void main()
{
	rd_temperature();
	
	System_Init();
	Timer0_Init();
	while(1)
	{
		Key_Proc();
		Seg_Proc();
		Led_Proc();
	}
}

DS18B20模块
使用教程:

float rd_temperature(void)
{
	unsigned char low,high;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	low = Read_DS18B20();
	high = Read_DS18B20();
	
	return((high<<8)|low) /16.0;
}

按键长按、短按:

if(Seg_Disp_Mode == 1)
	{
		if(Key_Down == 14 || Key_Down == 15)//产生下降沿
			Key_Flag = 1;//拉高标志位 开始计时
	}
	if(Key_Time < 500) //在五百毫秒之内
	{
		if(Key_Up == 14) //短按
		{
			Key_Flag = Key_Time = 0;
			if(++P_Dat[P_Dat_Index] == 71)
				P_Dat[P_Dat_Index] = 10;
		}
		if(Key_Up == 15) //短按
		{
			Key_Flag = Key_Time = 0;
			if(--P_Dat[P_Dat_Index] == 9)
				P_Dat[P_Dat_Index] = 70;
		}
	}
	else //在五百毫秒之外
	{
		if(Key_Old == 14)//长按
		{
			if(++P_Dat[P_Dat_Index] == 71)
				P_Dat[P_Dat_Index] = 10;
		}
		if(Key_Old == 15)//长按
		{
			if(--P_Dat[P_Dat_Index] == 9)
				P_Dat[P_Dat_Index] = 70;
		}
		if(Key_Up == 14 || Key_Up == 15)//S14、S15抬起
		{
			Key_Flag = Key_Time = 0;
		}
	}

错误总结:
1、P_Dat、P_Ctrol的关系类似于草稿纸和正式文件,参数设置界面对参数进行改变并没有改变上下限,参数设置界面对草稿纸上的数据改变退出后,需要对这次改变的参数进行上下限判断,如果数据没问题就保存修改正式文件中的温度参数上下限。

case 12://界面切换
			if(++Seg_Disp_Mode == 2) Seg_Disp_Mode = 0;
			if(Seg_Disp_Mode == 0)//从参数设置界面跳转到温度显示界面
			{
				P_Dat_Index = 0;
				if(P_Dat[0] > P_Dat[1])
				{
					P_Ctrol[0] = P_Dat[0];
					P_Ctrol[1] = P_Dat[1];
				}
			}
			if(Seg_Disp_Mode == 1)//从温度显示界面跳转到参数设置界面
			{
				P_Dat[0] = P_Ctrol[0];
				P_Dat[1] = P_Ctrol[1];
			}

2、Led模块:led分别在四种情况下会被单独点亮,分别有以下几种情况:
1、T > TMAX,指示灯 L1 点亮
2、TMIN ≤ T ≤ TMAX,指示灯 L2 点亮
3、足 T < TMIN,指示灯 L3 点亮
4、出现错误的参数设置操作,指示灯 L4 点亮

	ucLed[0] = (int)Temperature / Parameter_control[0]; //L1点亮
	ucLed[1] = (!((int)Temperature / Parameter_control[0])) & ((int)Temperature / Parameter_control[1]);//L2点亮
	ucLed[2] = !((int)Temperature / Parameter_control[1]);//L3点亮
	ucLed[3] = Error_Flag;//L4点亮