单片机大作业,包含电路图、源代码和实验报告(除总结)

电路图

电路图提供两个版本,一开始我是用Proteus8画的,但是老师说之后要在她的电脑上运行,所以花了几分钟画了个低版本的:

链接:https://pan.baidu.com/s/1OET1wenZcMJAtf0aHUSkjw
提取码:6666

实验代码

#include<reg51.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int

uchar code numtab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code postab[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};


uchar count;
uchar hour = 13, minute = 14, second = 15;
int year = 2021, month = 6, day = 4;

sbit ShowInfo  = P0 ^ 0;  //显示年月日
sbit SetModel  = P0 ^ 1;  //显示时分秒 
sbit SetYear   = P0 ^ 2;  //设置年份
sbit SetMonth  = P0 ^ 3;  //设置月份 
sbit SetDay    = P0 ^ 4;  //设置天
sbit SetHour   = P0 ^ 5;  //设置小时
sbit SetMinute = P0 ^ 6;  //设置分钟 
sbit SetSecond = P0 ^ 7;  //设置秒钟

uchar model = 0;  

void init(){  //定时器初始化
	TMOD = 0x01;
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	EA = 1;
	ET0=1;
	EX0 = 1; IT0 = 1;
	EX1 = 1; IT1 = 1;
	TR0 = 1;	
}

void delay(uint times) { //延时函数
	uint a,b;
	for(a = times; a > 0; a--)
		for(b = 100; b > 0; b--);
}

void DisTime() { //显示时分秒
	P1 = postab[7];
	P2 = numtab[second % 10];
	delay(3);
	P1 = postab[6];
	P2 = numtab[second / 10];
	delay(3);
	
	
	P1 = postab[5];
	P2 = 0xBF;
	delay(3);

	P1 = postab[4];
	P2 = numtab[minute % 10];
	delay(3);
	P1 = postab[3];
	P2 = numtab[minute / 10];
	delay(3);
	
	
	P1 = postab[2];
	P2 = 0xBF;
	delay(3);
	
	P1 = postab[1];
	P2 = numtab[hour % 10];
	delay(3);
	
	P1 = postab[0];
	P2 = numtab[hour / 10];
	delay(3);
}

void DisDate() {	//显示年月日
	P1 = postab[7];
	P2 = numtab[day % 10];
	delay(3);
	P1 = postab[6];
	P2 = numtab[day / 10];
	delay(3);

	P1 = postab[5];
	P2 = numtab[month % 10];
	delay(3);
	P1 = postab[4];
	P2 = numtab[month / 10];
	delay(3);
	
	P1 = postab[3];
	P2 = numtab[year % 10];
	delay(3);
	
	P1 = postab[2];
	P2 = numtab[year / 10 % 10];
	delay(3);
	
	P1 = postab[1];
	P2 = numtab[year / 100 % 10];
	delay(3);
	
	P1 = postab[0];
	P2 = numtab[year / 1000];
	delay(3);
}

int IsLeapYear() { //判断是否为闰年
	if((year % 4 != 0)||(year % 100 == 0 && year % 400 != 0))
		return 0;
	else
		return 1;
}
void AdjustDate() {
	day++;
	if((day == 29 && month == 2 && !IsLeapYear()) || (day == 30 && month == 2 && IsLeapYear())) {  //è?1?ê?2??29
		month = 3; day = 1;
	}
	
	if(day == 31 && (month == 4 ||month == 6 || month == 9 || month ==11)){
		day = 1;
		month ++;
	}
	
	if(day == 32 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
		day = 1;
		month ++;
	}
	
	if(month == 13) {
		year++;
		month = 1;
		day = 1;
	}
}

void AdjustTime() {
	 second++;
	 if(second == 60) {
		second = 0;
		minute++;
		if(minute == 60) {
			minute = 0;
			hour ++;
			if(hour == 24) {
				hour = 0;
				AdjustDate(); //调整日期
			}
		}
	}
}

void main(){
	init();	
	while(1) {		
		if(model == 0) { //模式零显示日期,模式一显示时间
			DisDate();
		} else {
			DisTime();
		} 		
	}	
}

void timer0() interrupt 1 { 
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	if(++count == 18) {
		count = 0;
		AdjustTime();
	}
}

void int0() interrupt 0 { 
	if(ShowInfo == 0) { //设置显示模式,显示年月日
		model = 0;
	} else if(SetModel == 0) { //设置显示时分秒
		model = 1;
	} else if(SetHour == 0) { //设置小时
		hour++;   //小时增加
		if(hour == 24) {  //如果溢出则进位,并且调整日
			hour = 0;
			AdjustDate();
		}
	} else if(SetMinute == 0) { //设置分钟
		minute++;
		if(minute == 60) {   //分钟溢出则进位
			minute = 0;
			hour++;    
			if(hour == 24) { //小时溢出进位,调整日
				hour = 0;
				AdjustDate(); 
			} 
		}
	} else if(SetSecond == 0){ //设置秒
		AdjustTime();
	} else if(SetDay == 0) {
		AdjustDate();
	} else if(SetMonth == 0) { //设置月
		month++;
		if(month == 13) { //月溢出则进位
			month = 1;
			year++;
		}	
	} else if(SetYear == 0) { //设置年
		year++;
	}		
}

实验报告

供大家参考,不要复制粘贴

链接:https://pan.baidu.com/s/1uLRZxO9cUokQsaAHDb6CFw
提取码:6666

立志成为一名攻城狮
最后更新于 2021-06-04