第八届蓝桥杯单片机省赛

本文介绍了参加第八届蓝桥杯单片机省赛的程序实现,包括STC15F2K60S2单片机的程序编写,涉及到时间显示、温度测量和按键交互等功能。程序中详细解释了如何烧录和运行代码,并展示了与DS1302实时时钟及DS18B20温度传感器的通信过程。

直接复制粘贴然后运行

然后打开stc烧录到开发板上面就能用  程序哪里不懂的话问我,我闲的蛋疼!

#include <STC15F2K60S2.H>
#include <intrins.h>

unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6};
unsigned char yi,er,san,si,wu,liu,qi,ba;
unsigned char shijian[]={50,59,23,0,0,0,0};
unsigned char num[7];
unsigned char shezhi1=0;
unsigned char shezhi2=0;
unsigned char gaibian=0;
unsigned char panding=0;
long a,b;
unsigned char sansuo=0,sansuo1=0,sansuo2=0;
unsigned char naozhong=0;
unsigned char dingshi[]={0,0,0};
unsigned char huan;
unsigned char jia1=0,jia2=0,jia3=0;
unsigned char jian1=0,jian2=0,jian3=0;
unsigned char shi,fen,miao;
unsigned char shi1,fen1,miao1;
unsigned char yanshi=0;
int m;
bit add=0;


bit qiehuan=0;

unsigned char wendu;

sbit DQ = P1^4;  //单总线接口

sbit SCK=P1^7;        
sbit SDA=P2^3;        
sbit RST = P1^3;   // DS1302复位


void Delayms(int ms)
{
    int i,j;
    for(i=0;i<ms;i++)
        for(j=845;j>0;j--);
}

void chushihua()
{
    P2=0XA0;P0=0X00;P2=0X80;P0=0XFF;
    P2=0XC0;P0=0X00;P2=0XFF;P0=0XFF;
}
void shumaguan1(unsigned char yi,unsigned char er)
{
    P2=0XC0;
    P0=0X01;
    P2=0XFF;
    P0=tab[yi];
    Delayms(1);
    
    P2=0XC0;
    P0=0X02;
    P2=0XFF;
    P0=tab[er];
    Delayms(1);
}
void shumaguan2(unsigned char san,unsigned char si)
{
    P2=0XC0;
    P0=0X04;
    P2=0XFF;
    P0=tab[san];
    Delayms(1);
    
    P2=0XC0;
    P0=0X08;
    P2=0XFF;
    P0=tab[si];
    Delayms(1);
}

void shumaguan3(unsigned char wu,unsigned char liu)
{
    P2=0XC0;
    P0=0X10;
    P2=0XFF;
    P0=tab[wu];
    Delayms(1);
    
    P2=0XC0;
    P0=0X20;
    P2=0XFF;
    P0=tab[liu];
    Delayms(1);
}
void shumaguan4(unsigned char qi,unsigned char ba)
{
    P2=0XC0;
    P0=0X40;
    P2=0XFF;
    P0=tab[qi];
    Delayms(1);
    
    P2=0XC0;
    P0=0X80;
    P2=0XFF;
    P0=tab[ba];
    Delayms(1);
}


void Delay500us()        //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    _nop_();
    i = 6;
    j = 93;
    do
    {
        while (--j);
    } while (--i);
}

void Delay80us()        //@11.0592MHz
{
    unsigned char i, j;

    _nop_();
    i = 1;
    j = 217;
    do
    {
        while (--j);
    } while (--i);
}


void chushihua2()
{
    DQ=0;
    Delay500us();
    DQ=1;
    Delay500us();
}

void Write_DS18B20(unsigned char dat)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        DQ = 0;
        DQ = dat&0x01;
        Delay80us();
        DQ = 1;
        dat >>= 1;
    }
    Delay80us();
}


unsigned char Read_DS18B20(void)
{
    unsigned char i;
    unsigned char dat;
  
    for(i=0;i<8;i++)
    {
        DQ = 0;
        dat >>= 1;
        DQ = 1;
        if(DQ)
        {
            dat |= 0x80;
        }        
        Delay80us();
    }
    return dat;
}


unsigned char zhuchengxu()
{
     unsigned char di,gao,temp;
    chushihua2();
    Write_DS18B20(0xcc);
    Write_DS18B20(0x44);
    Delay500us();
    
    chushihua2();
    Write_DS18B20(0xcc);
    Write_DS18B20(0xbe);
    Delay500us();
    
    di=Read_DS18B20();
    gao=Read_DS18B20();
    
    di=di>>4;
    gao=gao<<4;
    temp=di|gao;
    return temp;
    
}

void Write_Ds1302_Byte(unsigned  char temp) 
{
    unsigned char i;
    for (i=0;i<8;i++)         
    { 
        SCK=0;
        SDA=temp&0x01;
        temp>>=1; 
        SCK=1;
    }
}   

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

unsigned char Read_Ds1302 ( unsigned char address )
{
     unsigned char i,temp=0x00;
    unsigned char dat1,dat2;
     RST=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     SCK=0;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     RST=1;
    _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
     Write_Ds1302_Byte(address);
     for (i=0;i<8;i++)     
     {        
        SCK=0;
        temp>>=1;    
         if(SDA)
         temp|=0x80;&

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值