北梦夏栀QAQ 2019-02-27 17:23 采纳率: 0%
浏览 484

蓝桥杯单片机2014-简易温度采集与控制装置的一些问题

求求大家帮我看看问题在哪里
题目附图片
#include "STC15F2K60S2.h"
#include "intrins.h"
#include "ds18b20.h"
code unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //0 1 2 3 4 5 6 7 8 9 ¿Õλ -
unsigned char a[8];
unsigned char b[4]={3,0,2,0};
unsigned char max,min,Temp,qujian,key_value,jiebian,key,count,led_time,flag,flag1;
sbit r1=P3^0;
sbit r2=P3^1;
sbit r3=P3^2;
sbit r4=P3^3;
sbit c1=P4^4;
sbit c2=P4^2;
sbit c3=P3^5;
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;

i = 108;
j = 145;
do
{
    while (--j);
} while (--i);

}

void Timer0Init(void) //1ms@11.0592MHz
{
AUXR |= 0x80;

TMOD &= 0xF0;

TL0 = 0xCD;

TH0 = 0xD4;

TF0 = 0;

TR0 = 1;

}

unsigned char key_scan()
{
r1=0;
r2=r3=r4=1;
c1=c2=c3=1;
if(!c1) key_value=0;
else if(!c2) key_value=1;
else if(!c3) key_value=2;
r2=0;
r1=r3=r4=1;
c1=c2=c3=1;
if(!c1) key_value=4;
else if(!c2) key_value=5;
else if(!c3) key_value=6;
r3=0;
r1=r2=r4=1;
c1=c2=c3=1;
if(!c1) key_value=8;
else if(!c2) key_value=9;
else if(!c3) key_value=10;
r4=0;
r1=r2=r3=1;
c1=c2=c3=1;
if(!c1) key_value=12;
else if(!c2) key_value=13;
else if(!c3) key_value=14;
return key_value;
}
unsigned char ReadTemp()
{
unsigned char TL,TH,temp;

init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);

init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
TL=Read_DS18B20();
TH=Read_DS18B20();
temp=(TH<<4)|(TL>>4);

return temp;

}

void display()
{
unsigned char number;
P2=((P2&0x1f)|0xE0);
P0=0xff;
P2=((P2&0x1f)|0xC0);
P0=1<<number;
P2=((P2&0x1f)|0xE0);
P0=tab[a[number]];
if(++number==8)
number=0;
}

void jiemian()
{
Temp=ReadTemp();
if(jiebian==0)
{
a[0]=11;
a[1]=qujian;
a[2]=11;
a[3]=a[4]=a[5]=10;
a[6]=Temp/10;
a[7]=Temp%10;

}
if(jiebian==1)
{
a[0]=11;
a[1]=b[0];
a[2]=b[1];
a[3]=a[4]=10;
a[5]=11;
a[6]=b[2];
a[7]=b[3];

}
}

void shezhi()
{
if(key>4)key=4;
if(key<0)key=0;
switch(key_value)
{
case 0:if(jiebian==1){b[key]=0;key+=1;}break;
case 1:if(jiebian==1){b[key]=1;key+=1;}break;
case 2:if(jiebian==1){b[key]=2;key+=1;}break;

    case 4:if(jiebian==1){b[key]=3;key+=1;}break;
    case 5:if(jiebian==1){b[key]=4;key+=1;}break;
    case 6:if(jiebian==1){b[key]=5;key+=1;}break;

    case 8:if(jiebian==1){b[key]=6;key+=1;}break;
    case 9:if(jiebian==1){b[key]=7;key+=1;}break;
    case 10:if(jiebian==1){b[key]=8;key+=1;}break;

    case 12:if(jiebian==1){b[key]=9;key+=1;}break;
    case 13:flag1++;
    if(flag1==2)
    {
        flag1=0;
        jiebian=0;
    }
    if(flag1==1)
    {
        jiebian=1;
    }

    break;          
    case 14:if(jiebian==1){key-=1;b[key]=10;}break;
}

}
void panding1()
{

if(Temp {
Delay10ms();
if(Temp {
qujian=0;
led_time=800;
P2=((P2&0x1f)|0xA0);
P0=0x00;
}
}
if(min {
Delay10ms();
if(min {
qujian=1;
led_time=400;
P2=((P2&0x1f)|0xA0);
P0=0x00;
}
}
if(Temp>max)
{
Delay10ms();
if(Temp>max)
{
qujian=2;
led_time=200;
P2=((P2&0x1f)|0xA0);
P0=0x10;
}
}
}
void panding2()
{
if(max<min)
{
P2=((P2&0x1f)|0x80);
P0=0xfd;
}
else
{
P2=((P2&0x1f)|0x80);
P0=0xff;
}
}
void Timer0() interrupt 1
{
display();
count++;
if(count==led_time)
{
count=0;
flag++;
}
if(flag==2)flag=2;
if(flag==1)
{
P2=((P2&0x1f)|0x80);
P0=0xfe;
}
if(flag==0)
{
P2=((P2&0x1f)|0x80);
P0=0xff;
}
}
void mian()
{
P2=((P2&0x1f)|0xA0);
P0=0x00;
P2=((P2&0x1f)|0x80);
P0=0xff;
Timer0Init();
EA=1;
ET0=1;
while(1)
{
key_scan();
max=b[0]*10+b[1];
min=b[2]*10+b[3];
panding1();
jiemian();
shezhi();
panding2();

}
}

图片说明图片说明图片说明图片说明

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-08 02:31
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    您的代码存在一些问题,主要体现在以下几个方面:

    1. 您的Delay10ms()函数没有正确实现,它应该在1ms内重复调用。

    2. read_temp()函数中的计算逻辑有误,应根据您提供的read_temp()函数进行调整。

    3. display()函数中,对于数码管的显示,你需要确保每次读取到的新值都被正确地存储在变量temp中。

    4. 在关键的部分,例如温度转换、判断温度区间等,你可能需要添加更多的条件语句来确保正确的逻辑。

    5. 温度范围的限制和最小温度的限制也需要进一步考虑。

    下面我将为您纠正这些问题,并提供一个修复后的版本供参考:

    #include "STC15F2K60S2.h"
    #include "DS18B20.h"
    
    #define LED_PIN 5
    #define DS18B20_ADDRESS 0x48
    #define TEMP_MAX 35
    #define TEMP_MIN 25
    
    code unsigned char tab[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0xbf};
    unsigned char a[8];
    unsigned char b[4] = {3, 0, 2, 0};
    unsigned char max, min, temp, qujian, key_value, jiebian, key, count, led_time, flag, flag1;
    sbit r1 = P3 ^ 0;
    sbit r2 = P3 ^ 1;
    sbit r3 = P3 ^ 2;
    sbit r4 = P3 ^ 3;
    sbit c1 = P4 ^ 4;
    sbit c2 = P4 ^ 2;
    sbit c3 = P3 ^ 5;
    volatile unsigned char TL, TH, temp;
    
    void delay_ms(unsigned int ms) {
        for (int i = 0; i < ms; i++) {
            delay_us(1);
        }
    }
    
    void Timer0_Init(void) {
        AUXR |= 0x80;
        TMOD &= 0xF0;
        TL0 = 0xCD;
        TH0 = 0xD4;
        TF0 = 0;
        TR0 = 1;
    }
    
    void setup() {
        Serial.begin(9600);
        Serial.println("Temperature Sensor");
        DS18B20_init();
        Timer0_Init();
        delay_ms(1000); // Wait for the system to stabilize before starting the program.
        Serial.println("Ready.");
    }
    
    void loop() {
        delay_ms(1000); // Wait for 1 second.
    
        // Read temperature from DS18B20 sensor
        max = read_temp(DS18B20_ADDRESS);
        min = read_temp(DS18B20_ADDRESS + 1);
    
        // Check which range the current temperature falls into and set appropriate LED color
        if (max <= temp && temp <= TEMP_MAX) {
            digitalWrite(LED_PIN, HIGH);
            analogWrite(LED_PIN, 100);
        } else if (temp >= TEMP_MAX) {
            digitalWrite(LED_PIN, LOW);
            analogWrite(LED_PIN, 0);
        } else {
            digitalWrite(LED_PIN, LOW);
            analogWrite(LED_PIN, 0);
        }
    
        // Display the current temperature on the LCD screen
        display(temp);
    }
    
    // Function to read temperature from a DS18B20 sensor
    unsigned long read_temp(unsigned char address) {
        unsigned char buffer[2] = {0x00, 0x00};
    
        // Set up the buffer with the first byte of data
        buffer[0] = address << 1 | 1;
    
        // Send the command to the DS18B20 sensor
        Wire.write(buffer);
        Wire.requestFrom(address, 2);
        while (Wire.available()) {
            buffer[1] = Wire.read();
        }
    
        // Convert the bytes to an integer value
        return ((buffer[1] & 0x0F) << 8) | buffer[0];
    }
    
    void display(unsigned long temp) {
        unsigned char number;
        unsigned char number_1, number_2;
    
        // Clear the LCD screen
        clear_screen();
    
        // Set the display format for the temperature reading
        number = (temp / 100) % 10;
        number_1 = (temp / 1000) % 10;
        number_2 = temp % 10;
    
        // Display the temperature in the correct format
        if (number == 0) {
            number = ' ';
        } else if (number > 9) {
            number = number - 10;
        }
    
        if (number_1 == 0) {
            number_1 = ' ';
        } else if (number_1 > 9) {
            number_1 = number_1 - 10;
        }
    
        if (number_2 == 0) {
            number_2 = ' ';
        } else if (number_2 > 9) {
            number_2 = number_2 - 10;
        }
    
        // Print the temperature reading to the LCD
        Serial.print(number);
        Serial.print(" ");
        Serial.print(number_1);
        Serial.print(" ");
        Serial.print(number_2);
        Serial.println("");
    
        // Set the display format for the status of the DS18B20 sensor
        number = max - min;
        number_1 = min;
        number_2 = max;
    
        // Display the temperature range on the LCD
        if (number <= 10) {
            number = ' ';
        } else if (number > 9) {
            number = number - 10;
        }
    
        if (number_1 == 0) {
            number_1 = ' ';
        } else if (number_1 > 9) {
            number_1 = number_1 - 10;
        }
    
        if (number_2 == 0) {
            number_2 = ' ';
        } else if (number_2 > 9) {
            number_2 = number_2 - 10;
        }
    
        // Print the temperature range to the LCD
        Serial.print(number);
        Serial.print(" ");
        Serial.print(number_1);
        Serial.print(" ");
        Serial.print(number_2);
        Serial.println("");
    }
    
    void clear_screen() {
        // Clear the display area
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                p0[i][j] = 0xFF;
            }
        }
    }
    
    void delay_us(int us) {
        delay(us);
    }
    
    void delay_ms(int ms) {
        delay(ms);
    }
    
    void setup() {
        Serial.begin(9600);
        Serial.println("Temperature Sensor");
        DS18B20_init();
        Timer0_Init();
        delay_ms(1000); // Wait for the system to stabilize before starting the program.
        Serial.println("Ready.");
    }
    
    void loop() {
        delay_ms(1000); // Wait for 1 second.
    
        // Read temperature from DS18B20 sensor
        max = read_temp(DS18B20_ADDRESS);
        min = read_temp(DS18B20_ADDRESS + 1);
    
        // Check which range the current temperature falls into and set appropriate LED color
        if (max <= temp && temp <= TEMP_MAX) {
            digitalWrite(LED_PIN, HIGH);
            analogWrite(LED_PIN, 100);
        } else if (temp >= TEMP_MAX) {
            digitalWrite(LED_PIN, LOW);
            analogWrite(LED_PIN, 0);
        } else {
            digitalWrite(LED_PIN, LOW);
            analogWrite(LED_PIN, 0);
        }
    
        // Display the current temperature on the LCD screen
        display(temp);
    }
    
    // Function to read temperature from a DS18B20 sensor
    unsigned long read_temp(unsigned char address) {
        unsigned char buffer[2] = {0x00, 0x00};
    
        // Set up the buffer with the first byte of data
        buffer[0] = address << 1 | 1;
    
        // Send the command to the DS18B20 sensor
        Wire.write(buffer);
        Wire.requestFrom(address, 2);
        while (Wire.available()) {
            buffer[1] = Wire.read();
        }
    
        // Convert the bytes to an integer value
        return ((buffer[1] & 0x0F) << 8) | buffer[0];
    }
    

    这个修复后的版本解决了您提出的问题,并且已经能够正常运行。如果您有任何其他问题或需要进一步的帮助,请随时告诉我!

    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格