第三届蓝桥杯省赛单片机组自动售水机







#include<stc15f2k60s2.h>

#include "intrins.h"


#define somenop {

_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

#define uchar unsigned char
#define uint unsigned int

#ifndef _IIC_H
#define _IIC_H

//函数声明
void IIC_Start(void);
void IIC_Stop(void);  
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);  
unsigned char IIC_RecByte(void);

#endif

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */

//小数点与0x7f相与       P0=tab[yi]&0x7f;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar guang,tt;
uint ML_10;
uint money;


void delay(uchar t)    ;
void display1(uchar yi,uchar er);
void display2(uchar san,uchar si);
void display3(uchar wu,uchar liu);
void display4(uchar qi,uchar ba);
void allinit(void);
void keyscan(void);
uchar IIC_read(uchar add);
void Timer0Init(void);
void main(void)
{
    yi=11;er=0;san=5; si=0;    wu=0;liu=1;qi=0;ba=0;
    allinit() ;
    Timer0Init();
    while(1)
    {    
        guang=IIC_read(0x01);     //0-255    0X01光敏电阻地址
         if(guang<64)         //1.25/5x255=63.75 --> 64
         {
             P2=0x80;
            P0=0xfe;       //第一个灯点亮

         }
        else
        {
         P2=0x80;P0=0xff;
        }        

        
        display1(yi,er);
        display2(san,si);
        display3(wu,liu);
        display4(qi,ba);
        keyscan();
    
    }

}

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

void timer0() interrupt 1
{
    tt++;
    if(tt==20)
    {    
        tt=0;
        ML_10=ML_10+1;
        wu=ML_10/1000;liu=ML_10%1000/100;qi=ML_10%100/10;ba=ML_10%10;
        if(ML_10>=9999)
        {
        ET0=0;EA=0;
        P2=0xa0;P0=0x00;//关闭继电器
        money=ML_10*5;
        wu=money/10000;liu=money%10000/1000;qi=money%1000/100;ba=money%100/10;
        ML_10=0;    
        }
    }    
}



void keyscan(void)
{
    if(P30==0)
    {
        delay(5);
        if(P30==0)
        {
        wu=0;liu=0;qi=0;ba=0;
        P2=0xa0;P0=0x10;//打开继电器
        ET0=1;//打开定时器
        EA=1;
                
        }
            while(!P30);
    }
    


        if(P31==0)
    {
        delay(5);
        if(P31==0)
        {
        ET0=0;EA=0;
        P2=0xa0;P0=0x00;//关闭继电器
        money=ML_10*5;
        wu=money/10000;liu=money%10000/1000;qi=money%1000/100;ba=money%100/10;
        ML_10=0;        
        }
            while(!P31);
    }
    
    
}

void allinit(void)
{
    P2=0xa0;
    P0=0x00;

    P2=0x80;
    P0=0xff;

    P2=0xc0;
    P0=0xff;
    P2=0xff;
    P0=0xff;
}


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

void display1(uchar yi,uchar er)
{
    P2=0xc0;
    P0=0x01;
    P2=0xff;
    P0=tab[yi];
    delay(1);

    P2=0xc0;
    P0=0x02;
    P2=0xff;
    P0=tab[er]&0x7f;
    delay(1);
}

void display2(uchar san,uchar si)
{
    P2=0xc0;
    P0=0x04;
    P2=0xff;
    P0=tab[san];
    delay(1);

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

void display3(uchar wu,uchar liu)
{
    P2=0xc0;
    P0=0x10;
    P2=0xff;
    P0=tab[wu];
    delay(1);

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

void display4(uchar qi,uchar ba)
{
    P2=0xc0;
    P0=0x40;
    P2=0xff;
    P0=tab[qi];
    delay(1);

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




//总线启动条件
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;
    }
}

//通过I2C总线发送数据
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;
    }
}

//从I2C总线上接收数据
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;
}

uchar IIC_read(uchar add)
{
    uchar temp;
    IIC_Start();
    IIC_SendByte(0x90);
    IIC_WaitAck();
    IIC_SendByte(add);
    IIC_WaitAck();
    IIC_Stop();
    
    IIC_Start();
    IIC_SendByte(0x91);
    IIC_WaitAck();
    temp=IIC_RecByte();
    IIC_Stop();

    return temp;
 

}

猜你喜欢

转载自blog.csdn.net/weixin_41215479/article/details/80888889