|
本帖最后由 小開--心 于 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;
//}
|
|