木槿呀 2024-03-27 09:55 采纳率: 72.2%
浏览 4
已结题

蓝桥杯第十一届,温度读出来是07,为什么?

蓝桥杯第十一届我在读温度的时候,读出来显示在数码管上的是07,而不是27,为什么啊?
main.c

#include<STC15F2K60S2.H>
#include"hardware.h"
#include"onewire.h"
bit mode=0;//S4切换
unsigned char Tmax=30;
unsigned char Tmin=20;
unsigned char temp=0;
//---------------------------------------------------------------------
void Workingkey()
{
  if(timer10ms>=10)
  {
    timer10ms=0;
    Scankey();
    if(key_value==4 && key_stat==2)
    {
       mode=~mode; 
    }
    
  }
}
//---------------------------------------------------------------------
void SMG_display()
{
   if(mode==0)//数据显示
   {
     SMG_buf[0]=12;
     SMG_buf[1]=16;
     SMG_buf[2]=16;
     SMG_buf[3]=16;
     SMG_buf[4]=16;
     SMG_buf[5]=16;
     SMG_buf[6]=temp/10;
     SMG_buf[7]=temp%10;
   }
   else if(mode==1)
   {
     SMG_buf[0]=24;
     SMG_buf[1]=16;
     SMG_buf[2]=16;
     SMG_buf[3]=Tmax/10;
     SMG_buf[4]=Tmax%10;
     SMG_buf[5]=16;
     SMG_buf[6]=Tmin/10;
     SMG_buf[7]=Tmin%10;
   }
}
//---------------------------------------------------------------------
void Delay(unsigned int t)
{
   while(t--);
}
//---------------------------------------------------------------------
void Read_temp()
{
   unsigned char MSB,LSB;
   init_ds18b20();
   Write_DS18B20(0xcc);
   Write_DS18B20(0x44);

   Delay(1000);

   init_ds18b20();
   Write_DS18B20(0xcc);
   Write_DS18B20(0xbe);
   LSB=Read_DS18B20();
   MSB=Read_DS18B20();
   temp=MSB;
   temp=(temp<<8)|LSB;
   if((temp&0xf800)==0x0000)
   {
      temp>>=4;
   }
   else if((temp & 0xf800)==0xf800)
   {
     temp=(~temp+1)&0x07ff;
     temp>>=4;
   }
}
//---------------------------------------------------------------------
void main()
{
   led_output();
   ws_output();
   Timer1Init();
   Read_temp();
   while(1)
   {
     Workingkey();
     SMG_display();
     if(timer400ms>=400)
     {
        timer400ms=0;
        Read_temp();
     }
      
   }
}


hardware.c

#include<STC15F2K60S2.H>
#include"hardware.h"
/*************  本地常量声明    **************/
unsigned char code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码
unsigned char led_buf=0xff;
unsigned char ws_buf=0x00;
unsigned char SMG_buf[]={16,16,16,16,16,16,16,16};
unsigned int timer10ms=0;
unsigned int timer10ms2=0;
unsigned int timer400ms=0;
unsigned char key_stat=0;
unsigned char key_value=0;
//----------------------------------------------------------------------

void Timer1Init()        //1毫秒@12.000MHz
{
    AUXR |= 0x40;        //定时器时钟1T模式
    TMOD &= 0x0F;        //设置定时器模式
    TL1 = 0x20;        //设置定时初值
    TH1 = 0xD1;        //设置定时初值
    TF1 = 0;        //清除TF1标志
    TR1 = 1;        //定时器1开始计时
    ET1=1;
    EA=1;
}
//---------------------------------------------------------------------
void SelectHC573(unsigned char channel)
{
    switch(channel)
    {
       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 led_output()
{          
    P0=led_buf;
    SelectHC573(4);
    SelectHC573(0);
}
//---------------------------------------------------------------------
void ws_output()
{
       
    P0=ws_buf;
    SelectHC573(5);
    SelectHC573(0);
}
//---------------------------------------------------------------------
void SMG_output()
{
   static unsigned char SMG_com=0;

       
    P0=0x00;
    SelectHC573(6);
    SelectHC573(0);

    
    P0=T_COM[SMG_com];
    SelectHC573(6);
    SelectHC573(0);

    
    P0=~t_display[SMG_buf[SMG_com]];
    SelectHC573(7);
    SelectHC573(0);

    if(++SMG_com>=8)
    {
      SMG_com=0;
    }
}
//---------------------------------------------------------------------
void ServiceTimer()interrupt 3
{
   SMG_output();
   timer10ms++;
    timer10ms2++;
    timer400ms++;
}
//----------------------------------------------------------------------


//----------------------------------------------------------------------
void Scankey()
{
  P3|=0x0f;
  if(~P3&0x0f)
  {
     if(key_stat<255)
       key_stat++;
     if(P33==0)
       key_value=4;
     if(P32==0)
       key_value=5;
     if(P31==0)
       key_value=6;
     if(P30==0)
       key_value=7;
  }
  else 
  {
    key_stat=0;
    key_value=0;
  }
}

onewire.c

#include<STC15F2K60S2.H>
#include "onewire.h"
sbit DQ=P1^4;
//
void Delay_OneWire(unsigned int t)  
{
    unsigned char i;
    while(t--){
        for(i=0;i<12;i++);
    }
}

//
void Write_DS18B20(unsigned char dat)
{
    
    unsigned char i;

    for(i=0;i<8;i++)
    {
        DQ = 0;
        DQ = dat&0x01;
        Delay_OneWire(5);
        DQ = 1;
        dat >>= 1;
    }
    Delay_OneWire(5);
    
}

//
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;
        }        
        Delay_OneWire(5);
    }

    return dat;
}

//
bit init_ds18b20(void)//延时需要自己扩大12倍!!!!!!!!!!!!!!!!
{
      bit initflag = 0;
      
      DQ = 1;
      Delay_OneWire(144);
      DQ = 0;
      Delay_OneWire(960);
      DQ = 1;
      Delay_OneWire(120); 
    initflag = DQ;     
      Delay_OneWire(60);
  
      return initflag;
}

  • 写回答

3条回答 默认 最新

  • 专家-皆知 2024-03-27 09:55
    关注

    引自免费微信小程序:皆我百晓生

    根据你提供的代码,我发现在你的代码中,温度数据是通过DS18B20温度传感器读取的。读取到的温度数据是0x07,这可能是从DS18B20传感器中读取到的原始数据,而不是实际的温度值。

    首先,我们需要将0x07转换为实际的温度值。根据DS18B20传感器的数据手册,0x07对应的温度范围是-40°C至+125°C。因此,我们需要根据这个范围和0x07的值计算出实际的温度值。

    具体计算方法如下:

    实际温度 = (0x07 - 0x44) * 0.0625 + 273.15

    将这个公式应用到你的代码中,可以得到实际的温度值。然后将这个温度值显示在数码管上。

    另外,关于你提到的“为什么读出来显示在数码管上的是07,而不是27”,可能是因为你读取到的温度数据0x07在经过计算后,其值在0-255范围内,而数码管显示的是0-9的数字,所以显示的是07,而不是27。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月27日

悬赏问题

  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3
  • ¥20 Tesla 特斯拉K80显卡 如果需要使用该设备,你需要禁用系统上的另一个设备。
  • ¥30 QT调用百度智能云千帆模型无法取得返回文本
  • ¥50 CCD工业视觉相机检测出现光边
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现