📄 09.c
字号:
//Program Description: Emulate a Thermostat on UNI-51-SDK using DS18B20 and Relays
//Author: Anurag Chugh (anurag@ucmicrosys.com)
#include"LCD.H"
//This Library is rquired for displaying the Temperature and Relay Status on the LCD
#include"CALCULATOR.H"
//This Library to handle floating point operations. DS18B20 is very accurate and so we need a floating
//variable to store the temperature. See page 4 of DS18B20 Datasheet to see the Temperature Register Format.
//Specially note the number of binary digits of precision (ie 2^6 to 2^-4)
#include"DS1820.H"
//This contains the function to access the DS18B20 over the 1-wire bus.
//The following are the predefined temperatures above which each of the relays would turn on
#define RELAY1_TEMP 36.0000 //Degrees Centigrade
#define RELAY2_TEMP 38.0000 //Degrees Centigrade
//Pins (Register Bits) of the microcontroller to which Relay Drivers and hence the Relay Coils are connected.
#define RELAY1_STATE P2_6
#define RELAY2_STATE P2_7
//This is to make the program more readable.
//Writing a 1 to the pin would turn ON the corresponding relay.
//Writing a 0 to the pin would turn OFF the corresponding relay.
#define ON 1
#define OFF 0
unsigned char MyTemp[9];
//This function reads the temperature from the DS18B20 and returns the temperature in a float variable
//It also converts this float to a String and saves it at the location in the memory to which the passed char pointer points to.
float get_temperature(char *tempstring)
{
unsigned char sign, tempinteger,tempdecimal;
float temperature;
ReadTemp(&MyTemp[0]);
//Conversion to Floating Point -->
//Converting the raw temperature data to floating point variable.
//Refer Page 4 of DS18B20 for the Temperature Registers (TH & TL) bit place values.
sign = ((MyTemp[1]&0xF0) == 0xF0)? '-' : '+';
tempinteger = (MyTemp[0] >> 4) + ( (MyTemp[1]&0x07) << 4 );
tempdecimal = (MyTemp[0]&0x0F);
temperature = (float)tempinteger + ( ((float)tempdecimal) * 0.0625);
if(sign == '-')
temperature = -1*temperature;
//<--Conversion to Floating Point
ftoa(temperature,4,tempstring); //Convert float to char array for display on LCD
return temperature;
}
void main(void)
{
float temperature;
char tempstring[10];
int i;
RELAY1_STATE = OFF; //At startup, both relays are off.
RELAY2_STATE = OFF;
LCD_init(); //Initialize the LCD
//Initially the DS18B20 reports the temperature as 85.0000 degrees Centigrade (See datasheet)
//So we need to put some delay for the temperature to stabilize.
//By stabilize we mean two things:
//1. The DS18S20's temperature output should stabilize (takes a few hundred milliseconds)
//2. The temperature of the environment around the temperature sensor must stabilize. (This depends on the application, for
// example if you are measuring the temperature of exhaust flowing through the vent, then you might need to
// wait for a few more seconds so that the blowers may start and the actual exhaust reaches the temperature sensor,
// and the temperature then recorded is that of the actual exhaust air.)
for(i=0; i<= 25; i++)
{
DelayMs(200);
temperature=get_temperature(tempstring);
LCD_row1();
LCD_gotoxy(0,0);
LCD_putc('T');
LCD_putc('=');
LCD_puts(tempstring);
LCD_putc(' ');
LCD_putc(0xDF); //Degree Symbol
LCD_putc('C');
LCD_row2();
LCD_gotoxy(0,1);
LCD_puts("Initializing...");
}
LCD_init(); //Clear the LCD before going into the Super-Loop
while(1)
{
temperature=get_temperature(tempstring);
LCD_row1();
LCD_gotoxy(0,0);
LCD_putc('T');
LCD_putc('=');
LCD_puts(tempstring);
LCD_putc(' ');
LCD_putc(0xDF); //Degree Symbol
LCD_putc('C');
LCD_row2();
LCD_puts("R1=");
if(temperature >= RELAY1_TEMP) //Actual logic to fire the relays as per the temperature and show the status on LCD
{
RELAY1_STATE = ON;
LCD_puts("ON ");
}
else
{
RELAY1_STATE = OFF;
LCD_puts("OFF");
}
LCD_puts(" R2=");
if(temperature >= RELAY2_TEMP)
{
RELAY2_STATE = ON;
LCD_puts("ON ");
}
else
{
RELAY2_STATE = OFF;
LCD_puts("OFF");
}
DelayMs(200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -