蓝桥杯第三届省赛代码

代码是我当初练习时写的,可供参考,其余各届代码可在我的博客中下载

//主函数代码
#include "iic.h"
#include "keyscan.h"

uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba,guang;

void display1()
{
	P2=0xc0;
	P0=0x01;
	P2=0xff;
	P0=table[yi];
	delay(1);

	P2=0xc0;
	P0=0x02;
	P2=0xff;
	P0=0x40;
	delay(1);
}

void display2()
{
	P2=0xc0;
	P0=0x04;
	P2=0xff;
	P0=table[san];
	delay(1);

	P2=0xc0;
	P0=0x08;
	P2=0xff;
	P0=table[si];
	delay(1);
}

void display3()
{
	P2=0xc0;
	P0=0x10;
	P2=0xff;
	P0=table[wu];
	delay(1);

	P2=0xc0;
	P0=0x20;
	P2=0xff;
	P0=table1[liu];
	delay(1);
}

void display4()
{
	P2=0xc0;
	P0=0x40;
	P2=0xff;
	P0=table[qi];
	delay(1);

	P2=0xc0;
	P0=0x80;
	P2=0xff;
	P0=table[ba];
	delay(1);

	P2=0xc0;
	P0=0xff;
	P2=0xff;
	P0=0xff;
	delay(1);
}

void allinit()
{
	P2=0x80;
	P0=0xff;
	P2=0xa0;
	P0=0x00;
	P2=0xc0;
	P0=0xff;
	P2=0xff;
	P0=0xff;
}

void Timer0Init(void)	
{
	AUXR |= 0x80;	
	TMOD &= 0xF0;	
	TL0 = 0x00;		
	TH0 = 0x28;		
	TF0 = 0;		
	EA=1;
	ET0=1;
}

void timer0() interrupt 1
{
	uchar count;
	count++;
	if(count==20)
	{
		count=0;
		chushui++;
		if(chushui==9999)
			chushui=9999;
	}
}


void main()
{
	allinit();
	Timer0Init();
	while(1)
	{
		keyscan();
		guang=IIC_read(0x90,0x01,0x91);
		yi=11;er=0x40;san=5;si=0;
		if(chu==1)
		{
			wu=chushui/1000;liu=chushui%1000/100;
			qi=chushui%100/10;ba=chushui%10;
		}
		if((chu==0)||(chushui==9999))
		{	
			if(chushui==9999)
			{
				chu=0;
				TR0=0;
				qing=1;
				P2=0xa0;
				P0=0x00;
				P2=0x80;
				P0=0xff;
				dian=0;
				chushui=chushui*0.5;
			}
			wu=chushui/1000;liu=chushui%1000/100;
			qi=chushui%100/10;ba=chushui%10;
		}
		if(guang>97)
		{
			if(dian==1)
			{
				P2=0x80;
				P0=0xfe;
			}
			if(dian==0)
			{
				P2=0x80;
				P0=0xff;
			}
		}
		if(guang<97)
		{
			if(dian==1)
			{
				P2=0x80;
				P0=0xfc;
			}
			if(dian==0)
			{
				P2=0x80;
				P0=0xfd;
			}
		}

		display1();
		display2();
		display3();
		display4();
	}
}
//按键函数
//头文件
#ifndef __KEYSCAN_H
#define __KEYSCAN_H

#include <stc15f2k60s2.h>

#define uchar unsigned char
#define uint unsigned int

extern uint chushui;
extern bit chu,qing,dian;

void delay(uint);
void keyscan();

#endif

//源文件
#include "keyscan.h"

bit chu=0,qing=0,dian;
uint chushui;

void delay(uint x)
{
	uint i,j;
	for(i=x;i>0;i--)
		for(j=845;j>0;j--);
}

void keyscan()
{
	if(P30==0)
	{
		delay(5);
		if(P30==0)
		{
			while(!P30);
			if(qing==1)
				chushui=0;
			P2=0xa0;
			P0=0x10;
			P2=0x80;
			P0=0xfe;
			dian=1;
			chu=1;
			qing=0;
			TR0=1;
		}
	}

	if(P31==0)
	{
		delay(5);
		if(P31==0)
		{
			while(!P31);
			P2=0xa0;
			P0=0x00;
			P2=0x80;
			P0=0xff;
			dian=0;
			chu=0;
			chushui=chushui*0.5;
			TR0=0;
			qing=1;
		}
	}
}
//iic的代码,这块的代码比赛时会给,但还是需要适当修改才可用
//头文件
#ifndef _IIC_H
#define _IIC_H

#include <stc15f2k60s2.h>
#include "intrins.h"

#define uchar unsigned char
#define uint unsigned int
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

sbit SDA = P2^1; 
sbit SCL = P2^0; 

void IIC_Start(void); 
void IIC_Stop(void);  
void IIC_SendByte(unsigned char byt); 
bit IIC_WaitAck(void);  
unsigned char IIC_RecByte(void); 
void IIC_write(uchar,uchar,uchar);
uchar IIC_read(uchar,uchar,uchar);

#endif

//源文件
#include "iic.h"

void IIC_Start(void)
{
	SDA = 1;
	SCL = 1;
	somenop;
	SDA = 0;
	somenop;
	SCL = 0;	
}

void IIC_Stop(void)
{
	SDA = 0;
	SCL = 1;
	somenop;
	SDA = 1;
}

bit IIC_WaitAck(void)
{
	SDA = 1;
	somenop;
	SCL = 1;
	somenop;
	if(SDA)    
	{   
		SCL = 0;
		IIC_Stop();
		return 0;
	}
	else  
	{ 
		SCL = 0;
		return 1;
	}
}

void IIC_SendByte(unsigned char byt)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{   
		if(byt&0x80) 
		{	
			SDA = 1;
		}
		else 
		{
			SDA = 0;
		}
		somenop;
		SCL = 1;
		byt <<= 1;
		somenop;
		SCL = 0;
	}
}

unsigned char IIC_RecByte(void)
{
	unsigned char da;
	unsigned char i;
	
	for(i=0;i<8;i++)
	{   
		SCL = 1;
		somenop;
		da <<= 1;
		if(SDA) 
		da |= 0x01;
		SCL = 0;
		somenop;
	}
	return da;
}
void IIC_write(uchar add,uchar wei,uchar dat)
{
	IIC_Start();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(wei);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

uchar IIC_read(uchar add,uchar wei,uchar add1)
{
	uchar temp;
	IIC_Start();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(wei);
	IIC_WaitAck();

	IIC_Start();
	IIC_SendByte(add1);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_Stop();

	return temp;

}

猜你喜欢

转载自blog.csdn.net/RwinR/article/details/82805994