|
本帖最后由 小开--心 于 2019-7-31 16:11 编辑
原理图如下
main.c文件:
#include <stc12c5a60s2.h>
#include <intrins.h>
//#include<stdio.h>
//#include"zm.h"
#include "delay.h"
#include "lcd12864.h"
#include "ds18b20.h"
#include "ds1302.h"
//#include "process.h"
#include "key.h"
#include "delay.h"
#include "adc.h"
#include "dht11.h"
#include "24c02.h"
#include "menu.h"
void main()
{
lcdinit(); //12864初始化函数
// delay(10);
LCD_clear(); // 显示屏清空显示
while(1)
{
menu();//菜单切换显示
// adc_menu();
// Test();
// display();//ds18b20显示
// key();//时钟按键修改
// LcdDisplay();//时钟显示
// DHT11disply();//DHT11温湿度显示
}
menu.c文件:
#include <menu.h>
//#include <12864.h>
//#include <ds1302.h>
//#include <ds18b20.h>
uint a,b;
//typedef unsigned int u16; //对数据类型进行声明定义
//typedef unsigned char u8;
//void delay(u16 k)
//{
// while(k--);
//}
void menu()
{
add();
if(a==0)
{
function_display();//显示功能主界面
}
if(a==1)
{
Test();//个人信息
}
if(a==2)
{
adc_menu(); //显示ADC电压
// LCD_clear();
}
if(a==3)
{
display(); //显示温度
// key();
}
if(a==4)
{
DHT11disply(); //温湿度显示
}
if(a==5)
{
LcdDisplay();//时钟显示
}
}
void add()
{
if(K4==0)
{
delay1(100);
if(K4==0)
{
a++;
// a--;
LCD_clear();
beep=~beep;
delay1(25);
if(a>=6)
a=0;
}
if(K4==1)
{
beep=1;
delay1(25);
}
//while(!K4);
}
if(K4==1)
{
beep=1;
delay1(25);
}
}
void dec()
{
if(K3==0)
{
delay1(100);
if(K3==0)
{
a--;
beep=~beep;
delay1(25);
LCD_clear();
if(a<=0)
a=6;
}
if(K3==1)
{
beep=1;
delay1(25);
}
//while(!K3);
}
if(K3==1)
{
beep=1;
delay1(25);
}
}
void delay1(uint i) //1us
{
while(i--);
}
LCD12864.c文件
#include "LCD12864.h"
/**************************************
延迟函数
***************************************/
sbit SID=P2^5;
sbit SCLK1=P2^6;
uint temp1;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=210;y>0;y--);
}
/****************************************************
按照液晶的通信协议,发送一个字节数据
*****************************************************/
void sendbyte(uchar zdata)
{
uint i;
for(i=0; i<8; i++)
{
if((zdata << i) & 0x80)
{
SID = 1;
}
else
{
SID = 0;
}
SCLK1 = 0;
delay(2);
SCLK1 = 1;
}
}
/******************************************************
写串口指令
***************************************************/
void write_com(uchar cmdcode)
{
sendbyte(0xf8);//写串口指令
sendbyte(cmdcode & 0xf0);
sendbyte((cmdcode << 4) & 0xf0);
delay(2);
}
/*****************************************************
写串口数据
**************************************************/
void write_data(uchar Dispdata)
{
sendbyte(0xfa);//写串口数据
sendbyte(Dispdata & 0xf0);
sendbyte((Dispdata << 4) & 0xf0);
delay(2);
}
/***************************************************
初始化函数
******************************************************/
void lcdinit() //12864初始化函数
{
delay(200);
write_com(0x30); //8位数据,基本指令操作
delay(10);
write_com(0x0c); //开显示,关光标
write_com(0x01); //清屏
delay(10);
}
/**************************************************
显示字符串
****************************************************/
void hzkdis(uchar code *s)
{
while(*s>0)
{
write_data(*s);
s++;
delay(10);
}
}
/************************************************
显示CGROM里的汉字
*************************************************/
void DisplayCgrom(uchar addr,uchar *hz)
{
write_com(addr);
delay(10);
while(*hz != '\0')
{
write_data(*hz);
hz++;
delay(10);
}
}
/****************************************
液晶显示界面初始化
*****************************************/
void LCD_Desk(void)
{
write_com(0x03);
delay(50);
DisplayCgrom(0x80,"当前的温度是");
DisplayCgrom(0x8A,"摄氏");
DisplayCgrom(0x8F,"℃");
delay(50);
}
//***********************************************************************
// 显示屏清空显示
//***********************************************************************
void LCD_clear(void)
{
write_com(0x01);
delay(5);
}
//void LCD12864lcd_pos(uchar X,uchar Y)
//{
// uchar pos;
// if(X==2)
// {
// X=0x88;
// }
// else if(X==3)
// {
// X=0x98;
// }
// else if(X==0)
// {
// X=0x80;
// }
// else if(X==1)
// {
// X=0x90;
// }
// pos=X+Y;
// write_com(pos); //显示地址
//}
void LCD12864lcd_pos(unsigned char x,unsigned char y) //数据坐标函数
{
unsigned char pos;
if(x == 0)
{
x = 0x80;
}
if(x == 1)
{
x = 0x90;
}
if(x == 2)
{
x = 0x88;
}
if(x == 3)
{
x = 0x98;
}
pos = x+y;
write_com(pos);
}
//***********************************************************************
// 显示屏字符串写入函数
//坐标函数
//***********************************************************************
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
write_com(0x80 + x); //第一行显示
}
if(y == 1)
{
write_com(0x90 + x); //第二行显示
}
if (y == 2)
{
write_com(0x88 + x); //第三行显示
}
if(y == 3)
{
write_com(0x98 + x); //第四行显示
}
delay(2);
while (*s)
{
write_data( *s);
delay(2);
s ++;
}
}
void adc_menu()
{
///**************** ADC数据采集 ************************************/
unsigned int ADC_Data=0;
ADC_Init(0X01); //P10做ADC采集
/*********************************************************************/
ADC_Data=Get_ADC(0); //通道0
LCD12864lcd_pos(1,3);
write_data((ADC_Data/1000)+0X30);
write_data((ADC_Data/100%10)+0X30);
write_data((ADC_Data/10%10)+0X30);
write_data((ADC_Data%10)+0X30);
LCD_write_str(0,0,"内部ADC:");//lcd12864显示
}
///*************************************************
// 显示 个人信息
//****************************************************/
ds12b20.c文件
#include "ds18b20.h"
#define u16 unsigned char
#define u8 unsigned int
uint temp;
uchar A1,A2,A3; //定义的变量,显示数据处理
uchar Temp_Value[6]; //显示用的温度数据,分离用放入数组准备调用
uchar flag;
uchar DisplayData[5];
uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/*******************************************************************************
* 函 数 名 : Delay1ms
* 函数功能 : 延时函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Delay1ms(uint j)
{
uint i;
for(i = 0; i < j; i ++)
{
_nop_( );//@11.0592M晶振(12M晶振:_nop_( );_nop_( );)
_nop_( );
}
}
/*******************************************************************************
* 函 数 名 : Ds18b20Init
* 函数功能 : 初始化
* 输 入 : 无
* 输 出 : 初始化成功返回1,失败返回0
*******************************************************************************/
void Ds18b20Init()
{
DSPORT = 1;
Delay1ms(60);
DSPORT = 0; //将总线拉低480us~960us
Delay1ms(700);
DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
Delay1ms(150);
if(DSPORT == 0)
{
flag = 1;
}
else
{
flag = 0;
}
Delay1ms(200);
}
/*******************************************************************************
* 函 数 名 : Ds18b20WriteByte
* 函数功能 : 向18B20写入一个字节
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Ds18b20WriteByte(uchar dat)
{
uint j;
for(j=0; j<8; j++)
{
DSPORT = 0; //每写入一位数据之前先把总线拉低1us
_nop_( );
DSPORT = dat & 0x01; //然后写入一个数据,从最低位开始
Delay1ms(70);
//延时68us,持续时间最少60us
DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
dat >>= 1;
}
Delay1ms(70);
}
/*******************************************************************************
* 函 数 名 : Ds18b20ReadByte
* 函数功能 : 读取一个字节
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint j;
for(j=8; j>0; j--)
{
DSPORT = 0;//先将总线拉低1us
_nop_( );
DSPORT = 1;//然后释放总线
_nop_( );_nop_( );//延时6us等待数据稳定
bi = DSPORT; //读取数据,从最低位开始读取
/*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
Delay1ms(100);
byte = (bi << 7)|(byte >> 1) ;
//读取完之后等待48us再接着读取下一个数
}
return byte;
}
/*******************************************************************************
* 函 数 名 : Ds18b20ChangTemp
* 函数功能 : 让18b20开始转换温度
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳过ROM操作命令
Ds18b20WriteByte(0x44); //温度转换命令
// Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
}
/*******************************************************************************
* 函 数 名 : Ds18b20ReadTempCom
* 函数功能 : 发送读取温度命令
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc); //跳过ROM操作命令
Ds18b20WriteByte(0xbe); //发送读取温度命令
}
/*******************************************************************************
* 函 数 名 : Ds18b20ReadTemp
* 函数功能 : 读取温度
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
uint Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp(); //先写入转换命令
Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
tmh = Ds18b20ReadByte(); //再读高字节
temp = tmh;
temp <<= 8;
temp |= tml;
temp=temp*0.0625*100+0.5;
return temp;
}
//uchar Read_Temperature( )
//{
// uchar tmh, tm1;
// float num;
// uchar a,b;
//
// Ds18b20Init();
// Ds18b20WriteByte(0xcc); //跳过ROM操作命令
// Ds18b20WriteByte(0xbe); //发送读取温度命令
// tmh = Ds18b20ReadByte();
// tm1 = Ds18b20ReadByte();
// tm1 <<= 4;
//// tm1 += (tmh & 0xf0) >> 4;
//// a = tm1;
// num = (tmh & 0x0f) * 0.0625;
// b = ( char)(num * 100);
// return a;
//}
/*****************************************
显示函数
******************************************/
void display()
{
int temp1;
uchar change[10];
LCD_Desk();
temp1=Ds18b20ReadTemp();
change[0]=temp1/1000;//温度百位
change[1]=temp1%1000/100;//温度百位
change[2]=temp1%1000%100/10;//温度十位
change[3]=temp1%1000%100%10;//温度个位
// change[3]=a/10;//温度个位
// change[4]=a%10;//温度个位
LCD12864lcd_pos(2,4);
write_data(change[0]+0x30);
write_data(change[1]+0x30);
write_data('.');
// LCD12864lcd_pos(2,6);
write_data(change[2]+0x30);
write_data(change[3]+0x30);
// write_data(change[3]+0x30);
// write_data(change[4]+0x30);
//temp1=data_do(temp1); //处理数据,得到要显示的值
// for(j=0;j<30;j++)
// {
//
// LCD_write_str(0x04,2,Temp_Value); //显示温度,温度值在Temp_Value数组里
//
// }
}
//*************************************************************************
// 温度数据处理函数
//*************************************************************************
// void data_do(uint temp_d)
// {
// uint A2t;
// A1=temp_d/100; //分出百,十,和个位
// A2t=temp_d%100;
// A2=A2t/10;
// A3=A2t%10;
// Temp_Value[0]=A1+0x30;
// Temp_Value[1]=A2+0x30;
// Temp_Value[2]='.';
// Temp_Value[3]=A3+0x30;
// Temp_Value[4]='C';
// Temp_Value[5]=' ';
//}
///*******************************************************************************
//* 函 数 名 : datapros()
//* 函数功能 : 温度读取处理转换函数
//* 输 入 : temp
//* 输 出 : 无
//*******************************************************************************/
//uchar datapros(uint temp)
//{
// float tp;
// if(temp< 0) //当温度值为负数
// {
// DisplayData[0] = 0x40; // -
// //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
// temp=temp-1;
// temp=~temp;
// tp=temp;
// temp=tp*0.0625*100+0.5;
// //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
// //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
// //算加上0.5,还是在小数点后面。
//
// }
// else
// {
// DisplayData[0] = 0x00;
// tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
// //如果温度是正的那么,那么正数的原码就是补码它本身
// temp=tp*0.0625*100+0.5;
// //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
// //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
// //算加上0.5,还是在小数点后面。
// }
//
// return temp;
//}
///*******************************************************************************
//* 函数名 :DigDisplay()
//* 函数功能 :数码管显示函数
//* 输入 : 无
//* 输出 : 无
//*******************************************************************************/
//#include "ds18b20.h"
///*******************************************************************************
//* 函 数 名 : Delay1ms
//* 函数功能 : 延时函数
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//void Delay1ms(uint y)
//{
// uint x;
// for( ; y>0; y--)
// {
// for(x=110; x>0; x--);
// }
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20Init
//* 函数功能 : 初始化
//* 输 入 : 无
//* 输 出 : 初始化成功返回1,失败返回0
//*******************************************************************************/
//uchar Ds18b20Init()
//{
// uchar i;
// DSPORT = 0; //将总线拉低480us~960us
// i = 70;
// while(i--);//延时642us
// DSPORT = 1; //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
// i = 0;
// while(DSPORT) //等待DS18B20拉低总线
// {
// Delay1ms(1);
// i++;
// if(i>5)//等待>5MS
// {
// return 0;//初始化失败
// }
//
// }
// return 1;//初始化成功
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20WriteByte
//* 函数功能 : 向18B20写入一个字节
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//void Ds18b20WriteByte(uchar dat)
//{
// uint i, j;
// for(j=0; j<8; j++)
// {
// DSPORT = 0; //每写入一位数据之前先把总线拉低1us
// i++;
// DSPORT = dat & 0x01; //然后写入一个数据,从最低位开始
// i=6;
// while(i--); //延时68us,持续时间最少60us
// DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
// dat >>= 1;
// }
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20ReadByte
//* 函数功能 : 读取一个字节
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//uchar Ds18b20ReadByte()
//{
// uchar byte, bi;
// uint i, j;
// for(j=8; j>0; j--)
// {
// DSPORT = 0;//先将总线拉低1us
// i++;
// DSPORT = 1;//然后释放总线
// i++;
// i++;//延时6us等待数据稳定
// bi = DSPORT; //读取数据,从最低位开始读取
// /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
// byte = (byte >> 1) | (bi << 7);
// i = 4; //读取完之后等待48us再接着读取下一个数
// while(i--);
// }
// return byte;
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20ChangTemp
//* 函数功能 : 让18b20开始转换温度
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//void Ds18b20ChangTemp()
//{
// Ds18b20Init();
// Delay1ms(1);
// Ds18b20WriteByte(0xcc); //跳过ROM操作命令
// Ds18b20WriteByte(0x44); //温度转换命令
// //Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
//
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20ReadTempCom
//* 函数功能 : 发送读取温度命令
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//void Ds18b20ReadTempCom()
//{
// Ds18b20Init();
// Delay1ms(1);
// Ds18b20WriteByte(0xcc); //跳过ROM操作命令
// Ds18b20WriteByte(0xbe); //发送读取温度命令
//}
///*******************************************************************************
//* 函 数 名 : Ds18b20ReadTemp
//* 函数功能 : 读取温度
//* 输 入 : 无
//* 输 出 : 无
//*******************************************************************************/
//int Ds18b20ReadTemp()
//{
// int temp = 0;
// uchar tmh, tml;
// Ds18b20ChangTemp(); //先写入转换命令
// Ds18b20ReadTempCom(); //然后等待转换完后发送读取温度命令
// tml = Ds18b20ReadByte(); //读取温度值共16位,先读低字节
// tmh = Ds18b20ReadByte(); //再读高字节
// temp = tmh;
// temp <<= 8;
// temp |= tml;
// return temp;
//}
|
|