利用ds18b20,lcd1602,蜂鸣器,还有两个按键控制定时时间和温度阈值。后面又加了蓝牙的发送信息提示定时时间到了的课设,看代码
主函数:
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"
#include "Key.h"
#include "Buzzer.h"
#include "DS18B20.h"unsigned char T=20;
unsigned char Buzzer_flag=0,uart_flag=0;
unsigned char Sec=000,Min=20,Hour=00,tempe=20;
unsigned char KeyNum,timer_1;
static unsigned int T0Count;
char asss='A';void key_action(void);
void temperature_view(void);
void temperature_action(void);
void spi_send( unsigned char *ch);
void Buzzer_action(void);
void uart_action(void);void main()
{ DS18B20_ConvertT(); //上电先转换一次温度,防止第一次读数据错误Delay(1000);//等待转换完成LCD_Init();Timer1Init(); while(1){Buzzer_action();temperature_view();key_action();temperature_action();uart_action();}
}void uart_action(void)
{if(uart_flag){uart_flag=0;TR0=0;TR1=1;spi_send("ok");TR1=0;TR0=1;}
}void spi_send( unsigned char *ch)
{while(*ch){SBUF=*ch;while(TI==0);TI=0;ch++;}
}
void Buzzer_action(void)
{if(Buzzer_flag){Buzzer_flag=0;Buzzer_Time(1000);}
}void temperature_view(void)
{DS18B20_ConvertT(); //转换温度T=DS18B20_ReadT(); //读取温度LCD_ShowString(1,1,"set:");LCD_ShowString(1,8,"tempe:");LCD_ShowNum(1,5,Min,3);LCD_ShowNum(2,5,Sec,3);LCD_ShowNum(1,14,tempe,3);LCD_ShowNum(2,14,T,3);
}void temperature_action(void)
{if(T>=tempe) timer_1=1;else timer_1=0;if(timer_1==0){Sec=0;T0Count=0;}
}
void key_action(void)
{KeyNum=Key(); //获取独立按键键码if(KeyNum) //如果按键按下{if(KeyNum==4){Min=Min+20;if(Min>=121) Min=20;}if(KeyNum==3) //如果K1按键按下{tempe=tempe+10;if(tempe>=51) tempe=20;}}
}
void timer0_isr() interrupt 1
{
// static unsigned int T0Count;TL0 = 0x67; //设置定时初值TH0 = 0xFC; //设置定时初值if(timer_1){T0Count++;if(T0Count>=1000) //定时器分频,1s{T0Count=0;Sec++; //1秒到,Sec自增if(Sec>=Min){Sec=0; //60秒到,Sec清0,Min自增timer_1=0;uart_flag=1;Buzzer_flag=1;}}}
}
定时器加上串口初始化:
void Timer1Init(void)
{TMOD &= 0xF0;TMOD |= 0x01; // 定时器0工作在模式1,16位定时器模式TH0 = 0xFC;TL0 = 0x67;ET0 = 1; // 使能定时器0中断TR0 = 1; // 启动定时器0// 串口的初始化TMOD &= 0x0F;TMOD |= 0x20; // 定时器1工作在模式2,8位自动重装载定时器模式SCON = 0x50; // 串口工作在模式1,允许接收TH1 = 0xFD; // 波特率为9600bpsFDTL1 = 0xFD;ES = 0; // 使能串口中断TR1 = 0; // 启动定时器1// 使能全局中断EA = 1;
}
ds18b20:
#include <REGX52.H>
#include "OneWire.h"//DS18B20指令
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE/*** @brief DS18B20开始温度变换* @param 无* @retval 无*/
void DS18B20_ConvertT(void)
{OneWire_Init();OneWire_SendByte(DS18B20_SKIP_ROM);OneWire_SendByte(DS18B20_CONVERT_T);
}/*** @brief DS18B20读取温度* @param 无* @retval 温度数值*/
float DS18B20_ReadT(void)
{unsigned char TLSB,TMSB;int Temp;float T;TR0=0;TR1=0;OneWire_Init();OneWire_SendByte(DS18B20_SKIP_ROM);OneWire_SendByte(DS18B20_READ_SCRATCHPAD);TLSB=OneWire_ReceiveByte();//先读低位再读高位,共16位TMSB=OneWire_ReceiveByte();Temp=(TMSB<<8)|TLSB;T=Temp/16.0;//自动转化为16进制的TR0=1;TR1=1;return T;
}
ds18b20总线:
#include <REGX52.H>
#include "OneWire.h"
//引脚定义/*** @brief 单总线初始化* @param 无* @retval 从机响应位,0为响应,1为未响应*/
unsigned char OneWire_Init(void)
{unsigned char i;unsigned char AckBit;OneWire_DQ=1;OneWire_DQ=0;i = 247;while (--i); //Delay 500usOneWire_DQ=1;i = 32;while (--i); //Delay 70usAckBit=OneWire_DQ;i = 247;while (--i); //Delay 500usreturn AckBit;
}/*** @brief 单总线发送一位* @param Bit 要发送的位* @retval 无*/
void OneWire_SendBit(unsigned char Bit)
{unsigned char i;OneWire_DQ=0;i = 4;while (--i); //Delay 10usOneWire_DQ=Bit;i = 24;while (--i); //Delay 50usOneWire_DQ=1;
}/*** @brief 单总线接收一位* @param 无* @retval 读取的位*/
unsigned char OneWire_ReceiveBit(void)
{unsigned char i;unsigned char Bit;OneWire_DQ=0;i = 2;while (--i); //Delay 5usOneWire_DQ=1;i = 2;while (--i); //Delay 5usBit=OneWire_DQ;i = 24;while (--i); //Delay 50usreturn Bit;
}/*** @brief 单总线发送一个字节* @param Byte 要发送的字节* @retval 无*/
void OneWire_SendByte(unsigned char Byte)
{unsigned char i;for(i=0;i<8;i++){OneWire_SendBit(Byte&(0x01<<i));}
}/*** @brief 单总线接收一个字节* @param 无* @retval 接收的一个字节*/
unsigned char OneWire_ReceiveByte(void)
{unsigned char i;unsigned char Byte=0x00;for(i=0;i<8;i++){if(OneWire_ReceiveBit()){Byte|=(0x01<<i);}}return Byte;
}
按键
unsigned char Key()
{unsigned char KeyNumber=0;if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;}if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;}if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;}if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;}return KeyNumber;
}
lcd1602
#include <REGX52.H>//引脚配置:
sbit LCD_RS=P2^7;
sbit LCD_RW=P2^6;
sbit LCD_EN=P2^5;
//sbit LCD_RS=P2^6;
//sbit LCD_RW=P2^5;
//sbit LCD_EN=P2^7;
#define LCD_DataPort P0//函数定义:
/*** @brief LCD1602延时函数,12MHz调用可延时1ms* @param 无* @retval 无*/
void LCD_Delay()
{unsigned char i, j;i = 2;j = 239;do{while (--j);} while (--i);
}/*** @brief LCD1602写命令* @param Command 要写入的命令* @retval 无*/
void LCD_WriteCommand(unsigned char Command)
{LCD_RS=0;LCD_RW=0;LCD_DataPort=Command;LCD_EN=1;LCD_Delay();LCD_EN=0;LCD_Delay();
}/*** @brief LCD1602写数据* @param Data 要写入的数据* @retval 无*/
void LCD_WriteData(unsigned char Data)
{LCD_RS=1;LCD_RW=0;LCD_DataPort=Data;LCD_EN=1;LCD_Delay();LCD_EN=0;LCD_Delay();
}/*** @brief LCD1602设置光标位置* @param Line 行位置,范围:1~2* @param Column 列位置,范围:1~16* @retval 无*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{if(Line==1){LCD_WriteCommand(0x80|(Column-1));}else if(Line==2){LCD_WriteCommand(0x80|(Column-1+0x40));}
}/*** @brief LCD1602初始化函数* @param 无* @retval 无*/
void LCD_Init()
{LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动LCD_WriteCommand(0x01);//光标复位,清屏
}/*** @brief 在LCD1602指定位置上显示一个字符* @param Line 行位置,范围:1~2* @param Column 列位置,范围:1~16* @param Char 要显示的字符* @retval 无*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{LCD_SetCursor(Line,Column);LCD_WriteData(Char);
}/*** @brief 在LCD1602指定位置开始显示所给字符串* @param Line 起始行位置,范围:1~2* @param Column 起始列位置,范围:1~16* @param String 要显示的字符串* @retval 无*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{unsigned char i;LCD_SetCursor(Line,Column);for(i=0;String[i]!='\0';i++){LCD_WriteData(String[i]);}
}/*** @brief 返回值=X的Y次方*/
int LCD_Pow(int X,int Y)
{unsigned char i;int Result=1;for(i=0;i<Y;i++){Result*=X;}return Result;
}/*** @brief 在LCD1602指定位置开始显示所给数字* @param Line 起始行位置,范围:1~2* @param Column 起始列位置,范围:1~16* @param Number 要显示的数字,范围:0~65535* @param Length 要显示数字的长度,范围:1~5* @retval 无*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{unsigned char i;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');}
}/*** @brief 在LCD1602指定位置开始以有符号十进制显示所给数字* @param Line 起始行位置,范围:1~2* @param Column 起始列位置,范围:1~16* @param Number 要显示的数字,范围:-32768~32767* @param Length 要显示数字的长度,范围:1~5* @retval 无*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{unsigned char i;unsigned int Number1;LCD_SetCursor(Line,Column);if(Number>=0){LCD_WriteData('+');Number1=Number;}else{LCD_WriteData('-');Number1=-Number;}for(i=Length;i>0;i--){LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');}
}/*** @brief 在LCD1602指定位置开始以十六进制显示所给数字* @param Line 起始行位置,范围:1~2* @param Column 起始列位置,范围:1~16* @param Number 要显示的数字,范围:0~0xFFFF* @param Length 要显示数字的长度,范围:1~4* @retval 无*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{unsigned char i,SingleNumber;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){SingleNumber=Number/LCD_Pow(16,i-1)%16;if(SingleNumber<10){LCD_WriteData(SingleNumber+'0');}else{LCD_WriteData(SingleNumber-10+'A');}}
}/*** @brief 在LCD1602指定位置开始以二进制显示所给数字* @param Line 起始行位置,范围:1~2* @param Column 起始列位置,范围:1~16* @param Number 要显示的数字,范围:0~1111 1111 1111 1111* @param Length 要显示数字的长度,范围:1~16* @retval 无*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{unsigned char i;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');}
}
对于ds18b20和蓝牙(hc05)因为定时器冲突所以我们要用哪个就把另一个关了。这样好些。