首页 > 其他分享 >14-综合实训案例

14-综合实训案例

时间:2023-01-03 11:23:51浏览次数:40  
标签:P2 break 14 unsigned char 案例 实训 case 0x1f

#include "reg52.h"
#include <stdio.h>
#include <string.h>
sfr AUXR = 0x8e;

sbit L7 = P0^6;
sbit L8 = P0^7;

sbit S5 = P3^2;
sbit S4 = P3^3;

//=========预定义=========
unsigned char t_005ms = 0;
unsigned char shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;
unsigned char stat_led;
//========================

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

unsigned char code table[] = 
{
	0xc0,//0
	0xf9,//1
	0xa4,//2
	0xb0,//3
	0x99,//4
	0x92,//5
	0x82,//6
	0xf8,//7
	0x80,//8
	0x90,//9
	0x88,//A
	0x83,//B
	0xc6,//C
	0xa1,//D
	0x86,//E
	0x8e,//F
	0xbf,//-
	0x7f//.
};

//=======寄存器选择
void SelectHC573(unsigned char n)
{
	switch(n)
	{
		case 4:
			P2 = (P2&0x1f)|0x80;//LED
		break;
		
		case 5:
			P2 = (P2&0x1f)|0xa0;//蜂鸣器
		break;
		case 6:
			P2 = (P2&0x1f)|0xc0;//位选
		break;
		case 7:
			P2 = (P2&0x1f)|0xe0;//段选
		break;
		case 0:
			P2 = (P2&0x1f)|0x00;//段选
		break;
		
	}
}

//=========跑马灯=========
void LEDRunning()
{
	
	unsigned char i;
	for(i = 0;i < 8;i++)
	{
		SelectHC573(4);
		P0 = ~(0x01 << i);
		Delay(60000);
		Delay(60000);
	}
	P0 = 0xff;
	Delay(60000);
	Delay(60000);
}

//======数码管浏览=============
void SMGRunning()
{
	unsigned char i;
	for(i = 0;i < 8;i++)
	{
		SelectHC573(6);
		P0 = 0x01 << i;
		SelectHC573(7);
		P0 = 0x00;
		Delay(60000);
		Delay(60000);
	}
	P0 = 0xff;
	Delay(60000);
	Delay(60000);
}

//==========数码管显示时钟==========
void display(unsigned char val,unsigned char pos)
{
	SelectHC573(6);
	P0 = 0x01<<pos;
	SelectHC573(7);
	P0 = table[val];
}
void showtime()
{
	display(miao%10,7);//个秒
	Delay(500);
	display(miao/10,6);//十
	Delay(500);
	display(16,5);
	Delay(500);
	
	display(fen%10,4);//个分
	Delay(500);
	display(fen/10,3);//十
	Delay(500);
	display(16,2);
	Delay(500);
	
	display(shi%10,1);//个时
	Delay(500);
	display(shi/10,0);//十
	Delay(500);
	
}

//========定时器相关函数==============
void InitTimer0()
{
	TMOD = 0x21;
	TH0 = (65535 - 50000)/256;
	TL0 = (65535 - 50000)%256;
	
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}

void Timer0() interrupt 1
{
	TH0 = (65535 - 50000)/256;
	TL0 = (65535 - 50000)%256;
	
	t_005ms++;
	if(t_005ms == 20)
	{
		miao++;
		t_005ms = 0;
		if(miao == 60)
		{
			fen++;
			miao = 0;
		}
		if(fen == 60)
		{
			shi++;
			fen = 0;
		}
		if(shi == 24)
		{
			shi = 0;
		}
	}
}
//=================串口通信==============
void InitSystem()
{
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(4);
	P0 = 0xff;
	SelectHC573(0);
	stat_led = P0;
	
}


void InitUart()
{
	TMOD = 0x21;
	TH1 = 0xfd;
	TL1 = 0xfd;
	TR1 = 1;
	
	SCON = 0x50;
	AUXR = 0x00;
	ES = 1;
	EA = 1;
}


void SendByte(unsigned char dat)//发送
{
	SBUF = dat;
	while(TI == 0);
	TI = 0;
}

unsigned char command = 0x00;
void ServiceUart() interrupt 4 //接收
{
	if(RI == 1)
	{
		RI = 0;
		command = SBUF;
	}
	
}
//=======================
unsigned char code byte[] = {10};
void Working()
{
	
	if(command != 0x00)
	{
		switch(command & 0xf0)
		{
			
			case 0xa0:
				SelectHC573(4);
				stat_led = (stat_led | 0x0f) & (~command | 0xf0);
				P0 = stat_led;
				SelectHC573(0);
				command = 0x00;
			break;
			
			case 0xb0:
				SendByte((shi /10 << 4) | (shi %10));
				SendByte((fen /10 << 4) | (fen %10));
				SendByte((miao /10 << 4) | (miao %10));
				command = 0x00;
			break;
		
			
		}
	}
}

void ScanKey()
{
	if(S5 == 0)
	{
		Delay(100);
		while(S5 == 0)
		{
			Working();
			showtime();
		}
		SelectHC573(4);
		P0 = stat_led;
		L7 = 0;
	}
	if(S4 == 0)
	{
		Delay(100);
		while(S4 == 0)
		{
			Working();
			showtime();
		}
		SelectHC573(4);
		P0 = stat_led;
		L8 = 0;
	}
}

void main()
{
	InitSystem();
	LEDRunning();
	SMGRunning();
	InitUart();
	InitTimer0();
	while(1)
	{
		Working();
		showtime();
		ScanKey();
	}
}

 

标签:P2,break,14,unsigned,char,案例,实训,case,0x1f
From: https://www.cnblogs.com/Unclesundada/p/17021538.html

相关文章

  • 【链表】LeetCode 141. 环形链表
    题目链接141.环形链表思路设置fast指针和slow指针,分别走两步和一步,如果链表有环的话,那么两个指针一定会在某一时刻相遇。可以想象成速度不同的两个人跑圈,只要时间足够......
  • Educational Codeforces Round 114 D
    D.TheStrongestBuildtilian发现n只有10啊m也是1e5我们考虑最好的状态肯定就是大家都选最大的时候但是如果被禁用掉了的话咋办呢我们肯定贪心的去减少一个最小的地......
  • navicat 连接Oracle报错ORA-12514:TNS:listener does not currently know of service
     首先检查服务名是否正确,排除改原因后,定位原因为OCI环境和Oracle版本不匹配,Oracle官网下载高版本instantclient依赖解压到本地目录后,navicat-工具-选项-环境重新指定下oc......
  • NC14352 旅行
    题目链接题目题目描述小z放假了,准备到R城市旅行,其中这个城市有N个旅游景点。小z时间有限,只能在三个旅行景点进行游玩。小明租了辆车,司机很善良,说咱不计路程,只要你一次性......
  • Java【双色球案例】
    题目:1.投注是从红色球号码中选择6个号码(不能重复),从蓝色球号码中选择1个号码,组合为一注投注号码的投注2.红色球号码为[1-33],蓝色球号码为[1-16]3.开奖号码和购彩者号码......
  • 可视化 — D3学习笔记小小案例记录一下
    D3全称是Data-DrivenDocuments数据驱动文档,是一个开源的javascript库,可以用于数据可视化图形的创建,该库更接近底层,与g2、echarts不同,d3能直接操作svg,所以拥有极大的自......
  • 基于ThingsBoard二次开发的案例
    ThingsKit物联网平台,底层基于ThingsBoard开发:https://www.thingskit.com/......
  • Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例
    一、为啥整合Dubbo实现SOADubbo不单单只是高性能的RPC调用框架,更是SOA服务治理的一种方案。核心:1.远程通信,向本地调用一样调用远程方法。2.集群容错3.服务自动发......
  • centos9安装docker-compose2.14.2
    Linux上我们可以从Github上下载它的二进制包来使用,最新发行的版本地址:​​https://github.com/docker/compose/releases​​sudocurl-L"https://github.com/docker/co......
  • 【LeeCode】14. 最长公共前缀
    【题目描述】编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ​​""​​。​​​​https://leetcode.cn/problems/longest-common-prefix......