蓝桥杯单片机组第十届省赛

main.c

#include"stc15f2k60s2.h"
#include"iic.h"

unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,~0x3e,~0x73,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,ADtt;
unsigned char Rb2;
bit mode;

unsigned int flag,NE555tt,zheng,fan,zheng_1,fan_1,T,f;

void display();
void keyscan();

void Timer0Init(void)		//5微秒@12.000MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xC4;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 0;		//定时器0开始计时
}

void Timer0() interrupt 1
{
	if(P34==0)
	{
		fan++;
		if(flag==0){flag=1; zheng_1=zheng; zheng=0;}
	}	
	if(P34==1)
	{
		zheng++;
		if(flag==1){flag=0; fan_1=fan; fan=0;}
	}
}

void Timer1Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x20;		//设置定时初值
	TH1 = 0xD1;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1=1;
	EA=1;
}

void Timer1() interrupt 3    
{
	unsigned char P0_kepper=0,P2_kepper=0;
	P0_kepper=P0; P2_kepper=P2; P2=P2&0x1f;
	
	display();
	keytt++; if(keytt==10) {keytt=0; keyscan();}
	ADtt++;

	NE555tt++;
	{
		if(NE555tt==969){TR0=1; ET0=1;}
		if(NE555tt==999){NE555tt=0; TR0=0; ET0=0; T=(zheng_1+fan_1)*5; f=1000000/T;}
	}


	P0=P0_kepper; P2=P2_kepper;
}

void display()
{
	P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
	P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
	if(smgcom==8) smgcom=0;
}

void keyscan()    //独立键盘:普通按键用trg  长按键用dat(松手变为0) cnt有时会失效
{
	unsigned char dat,trg,cnt;
	dat=P3^0xff;
	trg=dat&(dat^cnt);
	cnt=dat;
	
	if(trg&0x01){P0=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	else if(trg&0x02){P0=0x55; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	else if(trg&0x04){P0=0xaa; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	else if(trg&0x08){mode=~mode;}
}

void ADC()
{
	if(ADtt==200)
	{
	  ADtt=0;
	  Rb2=AD_read(0x03);	  //初始化???????
	}
}

void xianshi()
{
	if(mode==0)
	{
		smgbuffer[0]=10; smgbuffer[1]=smgbuffer[2]=smgbuffer[3]=smgbuffer[4]=12;
		smgbuffer[5]=(int)(Rb2*1.96+0.5)/100; smgbuffer[6]=(int)(Rb2*1.96+0.5)%100/10; smgbuffer[7]=(int)(Rb2*1.96+0.5)%10;
	}
	else if(mode==1)
	{
		smgbuffer[0]=11;
		smgbuffer[3]=f/10000; smgbuffer[4]=f%10000/1000; smgbuffer[5]=f%10000%1000/100; smgbuffer[6]=f%10000%1000%100/10; smgbuffer[7]=f%10;

	}
}

void main()
{
	P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	Timer0Init();
  Timer1Init();
	while(1)
	{
		ADC();
		xianshi();

	}
}

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

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

#define DELAY_TIME 5     //扩大8倍

#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 char AD_read(unsigned char add)
{
	unsigned char AD_temp;
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();

	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
  AD_temp=IIC_RecByte();
	IIC_Stop();
	
	return AD_temp;
}

void AD_write(unsigned char add,unsigned char dat)  //注意没有返回值 是void!!!!!
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
                    
	IIC_Stop();
}

unsigned char EEPROM_read(unsigned char add)
{
	unsigned char EEPROM_temp;
	
	IIC_Start();
	IIC_SendByte(0xA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xA1);
	IIC_WaitAck();
  EEPROM_temp=IIC_RecByte();
	IIC_Stop();
	
	return EEPROM_temp;
}

void EEPROM_write(unsigned char add,unsigned char dat)
{
	IIC_Start();
	IIC_SendByte(0xA0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

你可能感兴趣的:(蓝桥杯,单片机,蓝桥杯,嵌入式硬件)