蓝桥杯单片机第十届省赛程序题

 

 

 

 

 

 

 

 

这套题总体来说还是比较简单的,我今天用了一个半小时就写出来了,但是还是用点问题,就是当S7按键的功能还有点没有实现,希望各位大佬帮帮忙,指教指教!

#include"reg52.h"
#include"iic.h"

sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;

unsigned char stat=1;    //S4按键状态切换
unsigned char mode=1;     //S5按键状态切换
unsigned char In_LED=1;  //S6按键状态切换
unsigned char In_SMG=1;  //S7按键状态切换


unsigned  char code duanma_int[10]=
{0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,0x80,0x90};  

//0.   1.   2.  3.    4.  5.   6.   7.   8.   9.
unsigned  char code duanma_float[10]=
{0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10};


/*********************************/
void InitHC573(unsigned char n)  //锁存器选通通道
{
    switch(n)
    {
    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;
    case 0:P2=(P2&0x1f)|0x00;break;
    }
}

void Init()  //初始化  
{
    InitHC573(5);
    P0=0x00;  //关闭蜂鸣器和继电器
    InitHC573(4);
    P0=0xff;  //关闭数码管
}

void Delay(unsigned int t)  //延迟函数
{
    while(t--);
}

void SMG_off(unsigned char dat)  //选通全部数码管 来进行消隐
{
    InitHC573(6);
    P0=0xff;
    InitHC573(7);
    P0=dat;
}

void InitSMG(unsigned char dat,unsigned char pos)  //数码管 内容 ,位选
{
    InitHC573(7);
    P0=0xff;
    InitHC573(6);
    P0=0x01<<pos;
    InitHC573(7);
    P0=dat;
}
/*********************************/

/**************NE555_f**************************/
unsigned int dat_f=0;          //频率接收
unsigned int command_f=0;  //频率累加
unsigned char dat_t=0;     //定时


void InitTime()   //定时器初始化函数
{
    //定时器0用来计数
    //定时器1用来定时 :1s
    
    TMOD=0x16;   //0001 0110 
    TH0=0xff;
    TL0=0xff;
    TH1=(65535-50000+1)/256;
    TL1=(65535-50000+1)%256;
    
    ET0=1;
    ET1=1;
    EA=1;
    TR0=1;
    TR1=1;
}

void SeriveT0() interrupt 1
{
    command_f++;
}

void SeriveT1() interrupt 3
{
    TH1=(65535-50000+1)/256; //1ms
    TL1=(65535-50000+1)%256;
    
    dat_t++;
    if(dat_t==20) // 1ms*1000=1s
    {
    dat_f=command_f;
    dat_t=0;
    command_f=0;
    }
}

void SMG_NE555()   //频率显示函数
{
    InitSMG(0x8e,0);Delay(500);
    InitSMG(0xff,1);Delay(500);
    
    if(dat_f>99999)
    {
    InitSMG(duanma_int[dat_f/100000],2);Delay(500);
    }
    
    if(dat_f>9999)
    {
    InitSMG(duanma_int[(dat_f/10000)%10],3);Delay(500);
    }
    
    if(dat_f>999)
    {
    InitSMG(duanma_int[(dat_f/1000)%10],4);Delay(500);
    }
    
    if(dat_f>99)
    {
    InitSMG(duanma_int[(dat_f/100)%10],5);Delay(500);
    }
    
    if(dat_f>9)
    {
    InitSMG(duanma_int[(dat_f/10)%10],6);Delay(500);
    }
    
    InitSMG(duanma_int[(dat_f/1)%10],7);Delay(500);
    
    SMG_off(0xff);
}
/**********************************************/

/*****************ADC__DAC***************************/
unsigned int dat_v=0;

void Read_RB2()  //ADC输入函数
{
    IIC_Start(); 
    IIC_SendByte(0x90); 
    IIC_WaitAck();  
    IIC_SendByte(0x03);  // AIN3通道
    IIC_WaitAck();  
    IIC_Stop();
    
    IIC_Start(); 
    IIC_SendByte(0x91); 
    IIC_WaitAck();
    dat_v=1.96*IIC_RecByte(); 
    IIC_SendAck(1); 
    IIC_Stop();
}

void Set_DAC(unsigned char dat)   //DAC输出函数
{
    IIC_Start(); 
    IIC_SendByte(0x90); 
    IIC_WaitAck();  
    IIC_SendByte(0x40);  //0x40   DAC输出 
    IIC_WaitAck();  
    IIC_SendByte(dat); 
    IIC_WaitAck();
    IIC_Stop();
}

void SMG_ADC()  //ADC数码管显示函数
{
    InitSMG(0xc1,0);Delay(500);
    
    InitSMG(0xff,1);Delay(500);
    InitSMG(0xff,2);Delay(500);
    InitSMG(0xff,3);Delay(500);
    InitSMG(0xff,4);Delay(500);
    
    InitSMG(duanma_float[dat_v/100],5);Delay(500);
    InitSMG(duanma_int[(dat_v%100)/10],6);Delay(500);
    InitSMG(duanma_int[dat_v%10],7);Delay(500);
}

void SMG_2v()  //固定2V
{
    InitSMG(0xc1,0);Delay(500);
    
    InitSMG(0xff,1);Delay(500);
    InitSMG(0xff,2);Delay(500);
    InitSMG(0xff,3);Delay(500);
    InitSMG(0xff,4);Delay(500);
    
    InitSMG(duanma_float[2],5);Delay(500);
    InitSMG(duanma_int[0],6);Delay(500);
    InitSMG(duanma_int[0],7);Delay(500);
}
/****************************************************/

/*****************独立按键**********************/

void Scannkey()
{
    if(S4==0)
    {
        Delay(100);
        if(S4==0)
        {
        stat++;  
        if(stat==3) stat=1;
        }
        while(S4==0);
    }
    
    if(S5==0)
    {
        Delay(100);
        if(S5==0)
        {
        mode++;
        if(mode==3) mode=1;
        }
        while(S5==0);
    }
    
    if(S6==0)
    {
        Delay(100);
        if(S6==0)
        {
        In_LED++;
        if(In_LED==3) In_LED=1;
        }
        while(S6==0);
    }
    
    if(S7==0)
    {
        Delay(100);
        if(S7==0)
        {
        In_SMG++;
        if(In_SMG==3) In_SMG=1;
        }
        while(S7==0);
    }
}

/***********************************************/

/*****************状态转换************************/
void In_to_1_2()
{
    if(stat==1)
    {
     if(mode==1)  
     {
     Set_DAC(102);
     SMG_2v();
     
     InitHC573(4);
     P0=0xff;
     InitHC573(0);
     }
     
     if(mode==2)
     {
     Read_RB2();
     SMG_ADC();
     
     if(dat_v<150||(dat_v>=250&&dat_v<350))
     {
     InitHC573(4);
     P0=0xee;
     InitHC573(0);
     }
     
     if((dat_v>=150&&dat_v<250)||(dat_v>=350))
     {
     InitHC573(4);
     P0=0xea;
     InitHC573(0);
     }
     
     }
    }
    
    if(stat==2)
    {
    SMG_NE555();
    
    if(dat_f<10000||(dat_f>=50000&&dat_f<100000))
    {
    InitHC573(4);
    P0=0xfd;
    InitHC573(0);
    }
    
    if((dat_f>=10000&&dat_f<50000)||dat_f>=100000)
    {
    InitHC573(4);
    P0=0xf5;
    InitHC573(0);
    }
    
    }
    
    if(In_SMG==2)
    {
    InitHC573(5);
    P0=0x00;
    InitHC573(0);
    }
    
    if(In_LED==2)
    {
    InitHC573(4);
    P0=0xff;
    InitHC573(0);
    }
}

/************************************************/

void main()
{
    Init();
    InitTime();
    while(1)
    {
    Scannkey();
    In_to_1_2();
    }
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie918294071

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值