提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

 

文章目录

 


前言

写的十五届赛题,供大家参考,还有不足的地方。


 

一、赛题题目

二、代码实现

一.main.c
#include    "main.h"
uchar    Key_Down,Key_up,Key_old,KeyNum;
uchar    mode,Para_mode,Ecoh_mode;
int    Para_PF=2000;
int    jiaozhun=0;
uint    fu_jiaozhun;
uchar    ucRtc[3]={13,03,05};
float    DAC;

void SMG_Desk()
{
    if(SMG_Slow_Down<100)return;
    SMG_Slow_Down=0;
    
    DS1302_Read(ucRtc);
    
    switch(mode)
    {
        case 0:
            if(free>=0)
            {
        SMG[0]=15;
        SMG[1]=16;
        SMG[2]=16;
        if(free_flag==1)
        {
        SMG[3]=(free<10000)?16:free/10000;
        SMG[4]=(free<1000)?16:free/1000%10;
        SMG[5]=(free<100)?16:free/100%10;
        SMG[6]=(free<10)?16:free/10%10;
        SMG[7]=free%10;
        free_flag=0;
        }
            }
            else  
            {
                SMG[0]=15;
                SMG[1]=16;
                SMG[2]=16;
                SMG[3]=16;
                SMG[4]=16;
                SMG[5]=16;
                SMG[6]=20;
                SMG[7]=20;//LL
            }
        break;
        
        case 1:
        switch(Para_mode)
        {
            case 0:
            SMG[0]=17;
            SMG[1]=1;
            SMG[2]=16;
            SMG[3]=16;
            SMG[4]=Para_PF/1000;
            SMG[5]=Para_PF/100%10;
            SMG[6]=Para_PF/10%10;
            SMG[7]=Para_PF%10;
            break;
            
            case 1:
            if(jiaozhun>=0)    
            {
            SMG[0]=17;
            SMG[1]=2;
            SMG[2]=16;
            SMG[3]=16;
            SMG[4]=16;
            SMG[5]=(jiaozhun<100)?16:jiaozhun/100%10;
            SMG[6]=(jiaozhun<10)?16:jiaozhun/10%10;
            SMG[7]=jiaozhun%10;
            }
            else
            {
                fu_jiaozhun=-jiaozhun;
            SMG[0]=17;
            SMG[1]=2;
            SMG[2]=16;
            SMG[3]=16;
            SMG[4]=18;
            SMG[5]=(fu_jiaozhun<100)?16:fu_jiaozhun/100%10;
            SMG[6]=(fu_jiaozhun<10)?16:fu_jiaozhun/10%10;
            SMG[7]=fu_jiaozhun%10;
            }
            break;
        }
        break;
        
            case 2:
            SMG[0]=ucRtc[0]/10;
            SMG[1]=ucRtc[0]%10;
            SMG[2]=SMG[5]=18;
            SMG[3]=ucRtc[1]/10;
            SMG[4]=ucRtc[1]%10;
            SMG[6]=ucRtc[2]/10;
            SMG[7]=ucRtc[2]%10;
            break;
            
            case 3:
            switch(Ecoh_mode)
            {
                case 0:
                SMG[0]=19;
                SMG[1]=15;
                SMG[2]=16;
                SMG[3]=(MAX_Free<10000)?16:MAX_Free/10000;
                SMG[4]=(MAX_Free<1000)?16:MAX_Free/1000%10;
                SMG[5]=(MAX_Free<100)?16:MAX_Free/100%10;
                SMG[6]=(MAX_Free<10)?16:MAX_Free/10%10;
                SMG[7]=MAX_Free%10;
                break;
                
                case 1:
                    if(free_flag)
                    {
                SMG[0]=19;
                SMG[1]=10;
                SMG[2]=Ecoh_time[0]/10;
                SMG[3]=Ecoh_time[0]%10;
                SMG[4]=Ecoh_time[1]/10;
                SMG[5]=Ecoh_time[1]%10;
                SMG[6]=Ecoh_time[2]/10;
                SMG[7]=Ecoh_time[2]%10;
                    }
                break;
            }
            break;
    }
}

void KEY_Desk()
{
    if(KEY_Slow_Down<10)return;
    KEY_Slow_Down=0;
    
    KeyNum=Key();
    Key_Down=KeyNum&(KeyNum^Key_old);
    Key_up=~KeyNum&(KeyNum^Key_old);
    Key_old=KeyNum;
    
    if(Key_Down==4)
    {
        if(++mode==4)mode=0;
        Para_mode=0;
        Ecoh_mode=0;
    }
    
    if(Key_Down==5)
    {
        if(mode==1)
        {
            Para_mode^=1;
        }
        
        if(mode==3)
        {
            Ecoh_mode^=1;
        }
    }
    
    if(Key_Down==8)
    {
        if(mode==1&&Para_mode==0)
        {
            Para_PF=(Para_PF>=9000)?9000:Para_PF+1000;
        }
        
        if(mode==1&&Para_mode==1)
        {
            jiaozhun=(jiaozhun>=900)?900:jiaozhun+100;
        }
    }
    
    if(Key_Down==9)
    {
        if(mode==1&&Para_mode==0)
        {
            Para_PF=(Para_PF<=1000)?1000:Para_PF-1000;
        }
        
        if(mode==1&&Para_mode==1)
        {
            jiaozhun=(jiaozhun<=-900)?-900:jiaozhun-100;
        }
    }
}

void UZ_Desk()
{
    if(UZ_Slow_Down<100)return;
    UZ_Slow_Down=0;
    if(free_flag_dat==1)
        DA_Write(0);
    
    if(free>=0&&free<=500)
        DA_Write(51);
    if(free>Para_PF)
        DA_Write(255);
    if(free>500&&free<=Para_PF)
        DA_Write(((4.0*(free-500.0))/(Para_PF-500)+1.0)*51);
}
/*主函数*/
void main()
{
    SYS_Desk();
    Timer1_Init();
    Timer0_Init();
    DS1302_Write(ucRtc);
    while(1)
    {
        SMG_Desk();
        KEY_Desk();
        UZ_Desk();
    }
}

//

一.main.h
#ifndef __MAIN_H__
#define    __MAIN_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int    
#include    "sys.h"
#include    "timer.h"
#include    "key.h"
#include    "ds1302.h"
#include    "iic.h"
extern    uchar    mode,Para_mode,Ecoh_mode;
extern int    Para_PF;
#endif

//

二.sys.c

#include    "sys.h"

code unsigned char Seg_Table[] = 
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e, //F
0xff,    //Ãð 16
0x8c,    //P    17
0xbf,    //-    18
0x89,    //H    19
0xc7,    //L    20
};

void hc573(uchar    chane)
{
    switch(chane)
    {
        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;
    }
    P2=P2&0x1f;
}

void SYS_Desk()
{
    P0=0x00;
    hc573(5);
    P0=0xff;
    hc573(4);
}

void led(uchar    *LED,uchar    pos)
{
    static uchar    temp=0xff;
    if(LED[pos])
        temp&=~(0x01<<pos);
    else
        temp|=0x01<<pos;
    P0=temp;
    hc573(4);
}

void smg(uchar    *temp,uchar    *dot,uchar    pos)
{
    P0=0xff;
    hc573(7);
    P0=0x01<<pos;
    hc573(6);
    if(dot[pos])
        P0=Seg_Table[temp[pos]]    &    0x7f;
    else
        P0=Seg_Table[temp[pos]];
    hc573(7);
}

//

二.sys.h

#ifndef __SYS_H__
#define    __SYS_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int
void hc573(uchar    chane);
void SYS_Desk();
void led(uchar    *LED,uchar    pos);
void smg(uchar    *temp,uchar    *dot,uchar    pos);

#endif

//

三.timer.c

#include    "timer.h"
#include    "main.h"
#include    "sys.h"

uchar    SMG[]={16,16,16,16,16,16,16,16};
uchar    LED[]={0,0,0,0,0,0,0,0};
uchar    dot[]={0,0,0,0,0,0,0,0};
uchar    pos;
uchar    SMG_Slow_Down;//减速函数
uchar    KEY_Slow_Down;
uchar    UZ_Slow_Down;

/*数据变量*/
uint    freetime;
int    free;
int    MAX_Free;
bit    free_flag=0;
int    free_old;
uchar    Ecoh_time[3];
uchar    time200ms;
uchar    time200ms1;
bit    free_flag_dat;
//
void Timer1_Init(void)       //1毫秒@12.000MHZ
{
    AUXR &= 0xBF;       
    TMOD &= 0x0F;         
    TL1 = 0x18;          
    TH1 = 0xFC;             
    TF1 = 0;                
    TR1 = 1;                
    ET1=1;
    EA=1;
}

void Timer0_Init(void)         //1毫秒@12.000MHZ
{
    AUXR &= 0x7F;            
    TMOD &= 0xF0;          
    TMOD    |=0x05;
    TL0 = 0x00;             
    TH0 = 0x00;               
    TF0 = 0;            
    TR0 = 1;         
}
void timer1()    interrupt 3
{
    if(++freetime==1000)
    {
        free_flag=1;
        freetime=0;
        free=((TH0<<8)|TL0)+jiaozhun;
        TL0=0;
        TH0=0;
    }
    //
    if(free_flag)
    {
        if(free_old<free)
        {
            MAX_Free=free;
            free_old=free;
            DS1302_Read(Ecoh_time);
        }
    }
    
    //
    if(mode==0)
    {
    if(++time200ms==200)
    {
        time200ms=0;
        LED[0]=~LED[0];
    }
    }
    else
    {
        LED[0]=0;
    }
    //
    if(free>Para_PF)
    {
        if(++time200ms1==200)
    {
        time200ms1=0;
        LED[1]=~LED[1];
    }    
    }
    else if(free>=0&&free<Para_PF)
    {
        LED[1]=0;
    }
    else if(free<0)
    {
        LED[1]=1;
    }
    //
    if(free<=0)
    {
        free_flag_dat=1;
    }
    else
    {
        free_flag_dat=0;
    }
    SMG_Slow_Down++;
    KEY_Slow_Down++;
    UZ_Slow_Down++;
    smg(SMG,dot,pos);
    led(LED,pos);
    if(++pos==8)pos=0;
}

//

三.timer.h

#ifndef __TIMER_H__
#define    __TIMER_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int
void Timer1_Init(void);        
void Timer0_Init(void);   

extern uchar    SMG[];
extern uchar    LED[];
extern uchar    dot[];
extern uchar    pos;
extern uchar    SMG_Slow_Down;
extern uchar    KEY_Slow_Down;
extern uchar    UZ_Slow_Down;    

extern uint    freetime;
extern int    free;
extern int    MAX_Free;
extern int    free_old;
extern bit    free_flag;
extern uchar    Ecoh_time[3];
extern int    jiaozhun;
extern uchar    time200ms;
extern uchar    time200ms1;
extern bit    free_flag_dat;

#endif

//

四.key.c

#include    "key.h"

uchar    Key()
{
    uchar    temp=0;
    P44=0;P42=1;P35=1;
    if(P33==0)temp=4;
    if(P32==0)temp=5;
    if(P31==0)temp=6;
    if(P30==0)temp=7;
    P44=1;P42=0;P35=1;
    if(P33==0)temp=8;
    if(P32==0)temp=9;
    if(P31==0)temp=10;
    if(P30==0)temp=11;
        P44=1;P42=1;P35=0;
    if(P33==0)temp=12;
    if(P32==0)temp=13;
    if(P31==0)temp=14;
    if(P30==0)temp=15;
        P44=1;P42=1;P35=1;
    //P34=0;
    if(P33==0)temp=16;
    if(P32==0)temp=17;
    if(P31==0)temp=18;
    if(P30==0)temp=19;
    return temp;
}

//

四.key.h

#ifndef __KEY_H__
#define    __KEY_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int
uchar    Key();

#endif

//

五.ds1302.c

//官方底层代码                    
#include    "ds1302.h"
#include    "intrins.h"
sbit    SDA=P2^3;
sbit    SCK=P1^7;
sbit    RST=P1^3;
//
void Write_Ds1302(unsigned  char temp) 
{
    unsigned char i;
    for (i=0;i<8;i++)         
    { 
        SCK = 0;
        SDA = temp&0x01;
        temp>>=1; 
        SCK=1;
    }
}   

//
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
     RST=0;    _nop_();
     SCK=0;    _nop_();
     RST=1;     _nop_();  
     Write_Ds1302(address);    
     Write_Ds1302(dat);        
     RST=0; 
}

//
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
     unsigned char i,temp=0x00;
     RST=0;    _nop_();
     SCK=0;    _nop_();
     RST=1;    _nop_();
     Write_Ds1302(address);
     for (i=0;i<8;i++)     
     {        
        SCK=0;
        temp>>=1;    
         if(SDA)
         temp|=0x80;    
         SCK=1;
    } 
     RST=0;    _nop_();
     SCK=0;    _nop_();
    SCK=1;    _nop_();
    SDA=0;    _nop_();
    SDA=1;    _nop_();
    return (temp);            
}

void DS1302_Write(uchar    *ucRtc)
{
    uchar    i;
    Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
    for(i=0;i<3;i++)
    {
        Write_Ds1302_Byte(0x84-2*i,ucRtc[i]/10%10<<4|ucRtc[i]%10);
    }
    Write_Ds1302_Byte(0x8e,0x80);//打开
}

void DS1302_Read(uchar    *ucRtc)
{
    uchar    i;
    for(i=0;i<3;i++)
    {
    ucRtc[i]=Read_Ds1302_Byte(0x85-2*i);
    ucRtc[i]=((ucRtc[i]/16)*10)+(ucRtc[i]%16);
    }
}

//

五.ds1302.h

#ifndef __DS1302_H__
#define    __DS1302_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int
    
void DS1302_Write(uchar    *ucRtc);
void DS1302_Read(uchar    *ucRtc);

#endif

//

六.iic.c

//官方底层代码
#include    "iic.h"
#include    "intrins.h"
sbit    scl=P2^0;
sbit    sda=P2^1;
#define DELAY_TIME    10

//
static void I2C_Delay(unsigned char n)
{
    do
    {
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();        
    }
    while(n--);          
}

//
void I2CStart(void)
{
    sda = 1;
    scl = 1;
    I2C_Delay(DELAY_TIME);
    sda = 0;
    I2C_Delay(DELAY_TIME);
    scl = 0;    
}

//
void I2CStop(void)
{
    sda = 0;
    scl = 1;
    I2C_Delay(DELAY_TIME);
    sda = 1;
    I2C_Delay(DELAY_TIME);
}

//
void I2CSendByte(unsigned char byt)
{
    unsigned char i;
    
    for(i=0; i<8; i++){
        scl = 0;
        I2C_Delay(DELAY_TIME);
        if(byt & 0x80){
            sda = 1;
        }
        else{
            sda = 0;
        }
        I2C_Delay(DELAY_TIME);
        scl = 1;
        byt <<= 1;
        I2C_Delay(DELAY_TIME);
    }
    
    scl = 0;  
}

//
unsigned char I2CReceiveByte(void)
{
    unsigned char da;
    unsigned char i;
    for(i=0;i<8;i++){   
        scl = 1;
        I2C_Delay(DELAY_TIME);
        da <<= 1;
        if(sda) 
            da |= 0x01;
        scl = 0;
        I2C_Delay(DELAY_TIME);
    }
    return da;    
}

//
unsigned char I2CWaitAck(void)
{
    unsigned char ackbit;
    
    scl = 1;
    I2C_Delay(DELAY_TIME);
    ackbit = sda; 
    scl = 0;
    I2C_Delay(DELAY_TIME);
    
    return ackbit;
}

//
void I2CSendAck(unsigned char ackbit)
{
    scl = 0;
    sda = ackbit; 
    I2C_Delay(DELAY_TIME);
    scl = 1;
    I2C_Delay(DELAY_TIME);
    scl = 0; 
    sda = 1;
    I2C_Delay(DELAY_TIME);
}

void DA_Write(uchar    addr)
{
    I2CStart();
    I2CSendByte(0x90);
    I2CWaitAck();
    I2CSendByte(0x41);
    I2CWaitAck();
    I2CSendByte(addr);
    I2CWaitAck();
    I2CStop();
}

 

//

六.iic.h

#ifndef __IIC_H__
#define    __IIC_H__
#include <STC15F2K60S2.H>
#define    uchar    unsigned char
#define    uint    unsigned int
    
void DA_Write(uchar    addr);

#endif

//如有侵权请联系私信。

 


总结

总体来说难度还是可以的,主要注意题目细节上的问题。

 

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐