使用STC89C51单片机进行proteus仿真,包括ADC0832、LCD1602、DS18B20串口发送、单位按键等。效果如下所示。
其中NF表示流度,AL为流量,流量大小是根据流速与时间的乘积得来的。NT为DS18B20测的实时温度,当温度小于按键设计的阈值ST时,电磁阀闭合,打开水泵并发出报警声。
硬件框架如下:
程序设计流程图如下:
部分代码如下:
#include <reg52.h>
#include <stdio.h>
#include "LCD1602.h"
#include "ADC0832.h"
#include "DS18B20.h"sbit Beep = P3^6;
sbit Motor = P3^7;typedef unsigned char uint8_t;
typedef unsigned int uint16_t;#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
#define BAUD 9600 //UART baudratebit busy;
uint16_t count = 0;
uint16_t Per = 0;//设定流量
uint16_t All_Per = 0;//总流量
uint16_t Tem = 0;//当前温度
uint16_t Set_Tem = 28;//设置温度
void Init();
void Keyscan();void main() {
uint8_t Buff[20];
Init();
LCD1602_Init();
Ds18b20Init();
while(1) {
Per = Get_ADC0832(0);
Tem = Ds18b20ReadTemp();
Keyscan();
LCD_pos(0,0);//显示设定流量
sprintf(Buff,"NF:%hdcfm ", (uint16_t)Per);
LCD_Str(Buff);
LCD_pos(1,0);//显示设定温度
sprintf(Buff,"ST:%hd%cC ", (uint16_t)Set_Tem, 0xdf);
LCD_Str(Buff);
LCD_pos(0,10);//显示总的流量
sprintf(Buff,"AF:%hdL ", (uint16_t)All_Per/1000);
LCD_Str(Buff);
LCD_pos(1,8);//显示当前温度
sprintf(Buff,"NT:%hd%cC ", (uint16_t)Tem/100, 0xdf);
LCD_Str(Buff);
if(Tem/100 > Set_Tem) {
Beep = 0;
Motor = 0;
}
else {
Beep = 1;
Motor = 1;
}
}
}/* Timer0 interrupt routine */
void tm0_isr() interrupt 1
{
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
All_Per += Per;
}
}/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
Set_Tem = SBUF; //P0 show UART data
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}void Init() {
//定时器0
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
//串口
SCON = 0x50; //8-bit variable UART
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1;
}void Keyscan() {
P1 = 0x07;
if((P1&0x07) != 0x07) {
switch(P1&0x07) {
case 0x06:Set_Tem++;
break;
case 0x05:if(Set_Tem != 0)Set_Tem--;
break;
}
}while((P1&0x07) != 0x07);
}
文件下载链接:
文件内资源: