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

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

  • main.c
  • iic.c

main.c

#include
#include

unsigned char code smgduan[]={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,(~0x3F)&0x7f,(~0x06)&0x7f,(~0x5B)&0x7f,(~0x4F)&0x7f,(~0x66)&0x7f,(~0x6D)&0x7f,(~0x7D)&0x7f,(~0x07)&0x7f,(~0x7F)&0x7f,(~0x6F)&0x7f,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt,ADtt;
unsigned char guang;
unsigned int chushui;
float chushuiliang,jiage;
int jiagee;
	
bit mode;

void display();
void keyscan();

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_keeper, P2_keeper;
	long tt;
	
	P0_keeper=P0; P2_keeper=P2; P2=P2&0x1f;
	
	display();
	keytt++; if(keytt==10) {keytt=0; keyscan();}
	ADtt++;
	if(mode==1){tt++; if(tt==100){tt=0; chushui++;}}
	else if(mode==0) {tt=0;}
		
	P0=P0_keeper; P2=P2_keeper;
}

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()
{
	unsigned char dat,trg,cnt;
	dat=P3^0xff;
	trg=dat&(dat^cnt);
	cnt=dat;
	
	if(trg&0x01)     {chushui=0; mode=1; P0=0x10; P2=P2&0x1f|0xa0; P2=P2&0x1f;}
	else if(trg&0x02){mode=0; P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;}
}

void ADC()
{
	if(ADtt==200){ADtt=0; guang=AD_read(0x01);}
	
	if(guang<63){P00=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
	else if(guang>63){P00=1; P2=P2&0x1f|0x80; P2=P2&0x1f;}	
}

void xianshi()
{
	if(mode==0)
	{
		smgbuffer[0]=20;
		smgbuffer[1]=10;smgbuffer[2]=5;smgbuffer[3]=0;
    chushuiliang=chushui/100+(chushui%100)*0.01;
		jiage=chushuiliang*0.5;
		jiagee=jiage*100;
		smgbuffer[4]=jiagee/1000;
		smgbuffer[5]=jiagee%1000/100+10;
		smgbuffer[6]=jiagee%1000%100/10;
		smgbuffer[7]=jiagee%10;		
	}
	else if(mode==1)
	{
		smgbuffer[0]=20;
		smgbuffer[1]=10;smgbuffer[2]=5;smgbuffer[3]=0;	
		smgbuffer[4]=chushui/1000;
		smgbuffer[5]=chushui%1000/100+10;
		smgbuffer[6]=chushui%1000%100/10;
		smgbuffer[7]=chushui%10;
		if(chushui==9999){mode=0;}
	}
	
}

void main()
{
	P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
	P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
	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

#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;  
		IIC_WaitAck();
}

//从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);
    }
		IIC_WaitAck();
    return da;    
}

void AD_write(unsigned char add,unsigned char dat)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_SendByte(add);
	IIC_SendByte(dat);
  IIC_Stop();
}

unsigned char AD_read(unsigned char add)
{
	unsigned char AD_temp;
	
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_SendByte(add);
	
	IIC_Start();	
	IIC_SendByte(0x91);
  AD_temp=IIC_RecByte();
	
	return AD_temp;
}

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