51单片机多路测温报警系统(AT89C51、多个DS18B20、LCD1602)
想要更多项目私wo!!!
一、简介
此系统主要由AT89C51、多个DS18B20温度模块和LCD1602组成。
大致的原理是多个DS18B20按顺序采集温度到的数据传送给AT89C51的P2.7,最后通过LCD1602显示当前的实时温度,按键可以设置上下限温度,根据温度判断是否需要报警。
二、运行效果图
模拟仿真运行
三、部分代码
/*想要更多项目私wo!!!*/
#include <reg52.h>
#include <intrins.h>unsigned int iTempDataH;
unsigned int SetTemp = 30;
int SetTemp1 = 1;
int num = 0;//??????????
void Busy()
{
DATA = 0xff;RS = 0;RW = 1;while(DATA & 0x80){
E = 0;E = 1;}E = 0;
}//?????????ò
void WriteCommand(unsigned char btCommand)
{
Busy();RS = 0;RW = 0;E = 1;DATA = btCommand;E = 0;
}//?????????ò
void WriteData(unsigned char btData)
{
Busy();RS = 1;RW = 0;E = 1;DATA = btData;E = 0;
}//????????
void Clear()
{
WriteCommand(1);
}//??????
void Init()
{
WriteCommand(0x0c); //??????,????±ê????WriteCommand(0x06); //??×?????????±ê×???????WriteCommand(0x38); //?è??????????:8??2??5x7???ó
}//????????×?·?
void DisplayOne(bit bRow, unsigned char btColumn, unsigned char btData, bit bIsNumber)
{
if (bRow) WriteCommand(0xc0 + btColumn);else WriteCommand(0x80 + btColumn);if (bIsNumber) WriteData(btData + 0x30);else WriteData(btData);
}//????×?·???????
void DisplayString(bit bRow, unsigned char btColumn, unsigned char *pData)
{
while (*pData != '\0'){
if (bRow) WriteCommand(0xc0 + btColumn); //????????1??else WriteCommand(0x80 + btColumn); //????????0??WriteData(*(pData++)); //????????????btColumn++; //????????}
}//???±1ms×?????
void Delayms(unsigned int x)
{
unsigned char i;while(x--){
for(i=0;i<200;i++);}
}//???±16us×?????
void Delay16us()
{
unsigned char a;for (a = 0; a < 4; a++);
}//???±60us×?????
void Delay60us()
{
unsigned char a;for (a = 0; a < 18; a++);
}//???±480us×?????
void Delay480us()
{
unsigned char a;for (a = 0; a < 158; a++);
}//???±240us×?????
void Delay240us()
{
unsigned char a;for (a = 0; a < 78; a++);
}//???±500ms×?????
void Delay500ms()
{
unsigned char a, b, c;for (a = 0; a < 250; a++)for (b = 0; b < 3; b++)for (c = 0; c < 220; c++);
}//??????????
void Initialization()
{
while(1){
DQ = 0;Delay480us(); //???±480usDQ = 1;Delay60us(); //???±60usif(!DQ) //????ds18b20??????????{
DQ = 1;Delay240us(); //???±240usbreak; }}
}//??????×???(????????????)
void WriteByte(unsigned char btData)
{
unsigned char i, btBuffer;for (i = 0; i < 8; i++){
btBuffer = btData >> i;if (btBuffer & 1){
DQ = 0;_nop_();_nop_();DQ = 1;Delay60us();}else{
DQ = 0;Delay60us();DQ = 1; }}
}//??????×???(????????????)
unsigned char ReadByte()
{
unsigned char i, btDest;for (i = 0; i < 8; i++){
btDest >>= 1;DQ = 0;_nop_();_nop_();DQ = 1;Delay16us();if (DQ) btDest |= 0x80; Delay60us();}return btDest;
}//?ò????????
void MatchROM(const unsigned char *pMatchData)
{
unsigned char i;Initialization();WriteByte(MATCH_ROM);for (i = 0; i < 8; i++) WriteByte(*(pMatchData + i)); }//???????í×????ò
void DataProcess()
{
m_TempData = ReadTemperature();if (m_TempData.btNegative) DisplayOne(1, 6, '-', 0);else DisplayOne(1, 6, m_TempData.btThird, 1);DisplayOne(1, 7, m_TempData.btSecond, 1);DisplayOne(1, 8, m_TempData.btFirst, 1);DisplayOne(1, 10, m_TempData.btDecimal, 1);
}int main(void)
{
Clear();Init();DisplayString(0, 0, "T1: T2: ");DisplayString(1, 0, "CurT:");DisplayOne(1, 9, '.', 0);while (1) DataProcess();
}