蓝桥杯单片机第十届省赛

蓝桥杯单片机第十届省赛,第1张

蓝桥杯单片机第十届省赛 如果有用请点赞,还会继续更新的

题目:



好久没有更新了,今天又写了一个第十届的,感觉这次就是555的频率计数时那个定时器的使用有点小注意事项,要用定时器0来计数,定时器1来计时,同时定时器1要计数555和模拟电压,话不多说,直接上源代码
main.c

#include"reg52.h"
#include"intrins.h"
#include"iic.h"
void SMG(int wei,int dat);
sfr AUXR=0x8e;
sbit S4=P3^3;int K4=0; //mode
sbit S5=P3^2;int K5=0; //choose_V
sbit S6=P3^1;int K6=1; //led
sbit S7=P3^0;int K7=1; //smg
unsigned int v_temp;
char code xianshi[15]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf,0x7F,0xC1,0x8e};
void get_v()
{
	v_temp=read_v();
	v_temp=v_temp*1.96+0.5;}

 
void choose_573(int n)
{
	switch(n)
	{
		case(0):P2=(P2&0x1f)|0x00;break;
		case(4):P2=(P2&0x1f)|0x80;break;
		case(5):P2=(P2&0x1f)|0xa0;break;
		case(6):P2=(P2&0x1f)|0xc0;break;
		case(7):P2=(P2&0x1f)|0xe0;break;
	}
}
void init_system()
{
	choose_573(4);
	P0=0xff;
	choose_573(5);
	P0=0x00;
	choose_573(0);
	P0=0xff;
}
//---------------------------- 中断
void Timer0Init(void)		//100微秒@11.0592MHz
{
	AUXR |= 0x80;		
	TMOD = 0x04;		
	TL0 = 0xff;		
	TH0 = 0xff;		
	TF0 = 0;		
	TR0 = 1;		
	ET0=1;
	EA=1;

}
int five_f;
void Timer1_server() interrupt 1
{
	five_f++;
}


void Timer1Init(void)		//5毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x00;		//设置定时初始值
	TH1 = 0x28;		//设置定时初始值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1=1;
	EA=1;
}
int Timer0_n=0;
int Timer0_m=0;
int dat_of_555;
void Timer0_server() interrupt 3
{
	Timer0_n++;
	Timer0_m++;
	if(Timer0_n>=100)
	{
		Timer0_n=0;
		get_v();
	}
	if(Timer0_m>=200)
	{
		dat_of_555=five_f;
		five_f=0;
		Timer0_m=0;	
	}
}

//----------------------------
//----------------------------按键
void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}
int x;
void key_board()
{
	if(S4==0)
	{
		Delay5ms();
		if(S4==0)
		{
			K4++;
			if(K4==2)
			{
				K4=0;
			}
		}
		while(!S4);
	}
	if(S5==0)
	{
		Delay5ms();
		if(S5==0)
		{
			K5++;
			if(K5==2)
			{
				K5=0;
				v_temp=200;
			}
		}
		while(!S5);
	}
	if(S6==0)
	{
		Delay5ms();
		if(S6==0)
		{
			K6++;
			if(K6==2)
			{
				K6=0;
				choose_573(4);
				P0=0xff;
				choose_573(0);
				P0=0xff;
			}
		}
		while(!S6);
	}
	if(S7==0)
	{
		Delay5ms();
		if(S7==0)
		{
			K7++;
			if(K7==2)
			{
				K7=0;
				
				for(x=1;x<9;x++)
				{
					SMG(x,10);
				}
			}
		}
		while(!S7);
	}
}
//----------------------------
//----------------------------数码管
void Delay300us()		//@11.0592MHz
{
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 4;
	j = 54;
	do
	{
		while (--j);
	} while (--i);
}
void SMG(int wei,int dat)
{
	choose_573(6);
	P0=0x80>>(wei-1);
	choose_573(7);
	P0=xianshi[dat];
	choose_573(0);
	P0=0xff;
}
void smg_display()
{
	if(K4==0&&K5==0&&K7==1)
	{
		SMG(1,0);
		Delay300us();
		SMG(2,0);
		Delay300us();
		SMG(3,2);
		Delay300us();
		SMG(3,12);
		Delay300us();
		SMG(4,10);
		Delay300us();
		SMG(5,10);
		Delay300us();
		SMG(6,10);
		Delay300us();
		SMG(7,10);
		Delay300us();
		SMG(8,13);
		Delay300us();	
	}

	if(K4==0&&K5==1&&K7==1)
	{
		SMG(1,v_temp%10);
		Delay300us();
		SMG(2,(v_temp%100)/10);
		Delay300us();
		SMG(3,v_temp/100);
		Delay300us();
		SMG(3,12);
		Delay300us();
		SMG(4,10);
		Delay300us();
		SMG(5,10);
		Delay300us();
		SMG(6,10);
		Delay300us();
		SMG(7,10);
		Delay300us();
		SMG(8,13);
		Delay300us();	
	}
	if(K4==1&&K7==1)
	{
		SMG(1,dat_of_555%10);
		Delay300us();
		if(dat_of_555>=10)
		{
			SMG(2,(dat_of_555%100)/10);
		}
		else 
		{
			SMG(2,10);
		}
		Delay300us();
		if(dat_of_555>=100)
		{
			SMG(3,(dat_of_555%1000)/100);
		}
		else
		{
			SMG(3,10);
		}
		Delay300us();	
		if(dat_of_555>1000)
		{
			SMG(4,(dat_of_555%10000)/1000);
		}
		else
		{
			SMG(4,10);
		}
		Delay300us();
		if(dat_of_555>10000)
		{
			SMG(5,(dat_of_555%100000)/10000);
		}
		else
		{
			SMG(5,10);
		}
		Delay300us();
		if(dat_of_555>100000)
		{
			SMG(6,(dat_of_555%1000000)/100000);
		}
		else
		{
			SMG(6,10);
		}
		Delay300us();
		SMG(7,10);
		Delay300us();
		SMG(8,14);
		Delay300us();
	}
}
//----------------------------
//----------------------------led工作
void led_work()
{
	if(K6==1&&K4==0)
	{
		if(v_temp<150)
		{
			choose_573(4);
			P0=0xfe;
			choose_573(0);
			P0=0xff;
		}
		if(v_temp<350&&v_temp>=250)
		{
			choose_573(4);
			P0=0xfe;
			choose_573(0);
			P0=0xff;
		}
		if(v_temp>=150&&v_temp<250)
		{
			choose_573(4);
			P0=0xfa;
			choose_573(0);
			P0=0xff;
		}
		if(v_temp>=350)
		{
			choose_573(4);
			P0=0xfa;
			choose_573(0);
			P0=0xff;
		}
	}
	if(K6==1&&K4==1)
	{
		if(dat_of_555<1000)
		{
			choose_573(4);
			P0=0xfd;
			choose_573(0);
			P0=0xff;
		}
		if(dat_of_555>=1000&&dat_of_555<5000)
		{
			choose_573(4);
			P0=0xf5;
			choose_573(0);
			P0=0xff;
		}
		if(dat_of_555>=5000&&dat_of_555<10000)
		{
			choose_573(4);
			P0=0xfd;
			choose_573(0);
			P0=0xff;	
		}
		if(dat_of_555>=10000)
		{
			choose_573(4);
			P0=0xf5;
			choose_573(0);
			P0=0xff;	
		}
	}
}
//----------------------------
void main()
{
	init_system();
	Timer0Init();
	Timer1Init();
	while(1)
	{
		key_board();
		smg_display();
		led_work();
	}
}

iic.c


#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  
sbit SCL = P2^0;  

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//发送应答
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					// 0:应答,1:非应答
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}
unsigned int read_v()
{
	unsigned int temp;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_WaitAck();
	IIC_Stop();
	
	return temp;
}

iic.h

#ifndef _IIC_H
#define _IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 
unsigned char read_v();
#endif

**

码字不容易,点个赞再走!!!!

**

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713891.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存