仿真圖proteus 8.9
程序編譯器:keil 5
編程語言:C語言
設(shè)計(jì)編號(hào):C0070
資料下載:https://docs.qq.com/doc/DS2lwY0tjWFlQd1pO
主要功能
STM32F103R6+DS18B20溫度傳感器+LCD1602顯示,溫度精度0.1℃,將采集到的六路溫度同時(shí)顯示在lcd1602顯示屏上。
帶報(bào)警器版本具有上下限報(bào)警功能,其中一路低于下限或高于上限均會(huì)報(bào)警。報(bào)警值可在程序更改。
重要說明:由于STM32仿真的不穩(wěn)定性問題,顯示數(shù)據(jù)存在延遲,讀取溫度值需要等待10秒左右才能穩(wěn)定,穩(wěn)定性問題非我所能改,請知悉,如介意請勿下載。
以下為本設(shè)計(jì)資料展示圖:
無報(bào)警Proteus仿真
帶報(bào)警Proteus仿真
程序
//復(fù)位DS18B20
//等待DS18B20的回應(yīng)
//返回1:未檢測到DS18B20的存在
//返回0:存在
//從DS18B20讀取一個(gè)字節(jié)
//返回值:讀到的數(shù)據(jù)
//寫一個(gè)字節(jié)到DS18B20
//dat:要寫入的字節(jié)
//從ds18b20得到溫度值
//精度:0.1C
//返回值:溫度值 (-550~1250)
#include "ds18b20.h"
#include "lcd1602.h"
void DS18B20_IO_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void DS18B20_IO_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//復(fù)位DS18B20
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PA0 OUTPUT
DS18B20_DQ_OUT(0); //拉低DQ
lcd_delay_us(750); //拉低750us
DS18B20_DQ_OUT(1); //DQ=1
lcd_delay_us(15); //15US
}
//等待DS18B20的回應(yīng)
//返回1:未檢測到DS18B20的存在
//返回0:存在
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PA0 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
lcd_delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
lcd_delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
//從DS18B20讀取一個(gè)位
//返回值:1/0
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();//SET PA0 OUTPUT
DS18B20_DQ_OUT(0);
lcd_delay_us(2);
DS18B20_DQ_OUT(1);
DS18B20_IO_IN();//SET PA0 INPUT
lcd_delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
lcd_delay_us(50);
return data;
}
//從DS18B20讀取一個(gè)字節(jié)
//返回值:讀到的數(shù)據(jù)
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
//寫一個(gè)字節(jié)到DS18B20
//dat:要寫入的字節(jié)
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PA0 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT(0);// Write 1
lcd_delay_us(2);
DS18B20_DQ_OUT(1);
lcd_delay_us(60);
}
else
{
DS18B20_DQ_OUT(0);// Write 0
lcd_delay_us(60);
DS18B20_DQ_OUT(1);
lcd_delay_us(2);
}
}
}
//開始溫度轉(zhuǎn)換
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
//初始化DS18B20的IO口 DQ 同時(shí)檢測DS的存在
//返回1:不存在
//返回0:存在
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DS18B20_Rst();
return DS18B20_Check();
}
//從ds18b20得到溫度值
//精度:0.1C
//返回值:溫度值 (-550~1250)
short DS18B20_Get_Temp(void)
{
u8 temp;
u8 TL,TH;
short tem;
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
// DS18B20_Write_Byte(0x55);// match rom
DS18B20_Write_Byte(0xbe);// convert
TL=DS18B20_Read_Byte(); // LSB
TH=DS18B20_Read_Byte(); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0;//溫度為負(fù)
}
else
temp=1;//溫度為正
tem=TH; //獲得高八位
tem<<=8;
tem+=TL;//獲得底八位
tem=(float)tem*0.625;//轉(zhuǎn)換
if(temp)
return tem; //返回溫度值
else
return -tem;
}
short DS18B20_Get_Temp_WithID(uint8_t * ds18b20_id)
{
u8 temp, i;
u8 TL,TH;
short tem;
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0x55);// match rom
for(i=0;i<8;i++)
DS18B20_Write_Byte(ds18b20_id[ i ]);
DS18B20_Write_Byte(0X44);/* 開始轉(zhuǎn)換 */
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0x55);// match rom
for(i=0;i<8;i++)
DS18B20_Write_Byte(ds18b20_id[ i ]);
DS18B20_Write_Byte(0xbe);// convert
TL=DS18B20_Read_Byte(); // LSB
TH=DS18B20_Read_Byte(); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0;//溫度為負(fù)
}
else
temp=1;//溫度為正
tem=TH; //獲得高八位
tem<<=8;
tem+=TL;//獲得底八位
tem=(float)tem*0.625;//轉(zhuǎn)換
if(temp)
return tem; //返回溫度值
else
return -tem;
}
LCD1602顯示
#include "lcd1602.h"
#include "delay.h"
#define DELAY_2N 0
void lcd_delay_us(unsigned int t)
{
unsigned int i, j;
for(i = 10; i > 0; i--)
for(j = t; j > 0; j--);
}
void lcd_delay_ms(unsigned int t)
{
unsigned int i;
for(i = t; i > 0; i--)
lcd_delay_us(10);
}
//==================================================
void LCD_init(void)
{
/*********************液晶使用的I/O口初始化**************************/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_0| GPIO_Pin_3
| GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7
| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
LCD_RW(0); //讀寫位直接低電平,只寫不讀
/*********************液晶初始化**************************/
lcd_delay_us(340);
LCD_RS(0);
LCD_write_cmd(0x38); // 8bit顯示模式,2行,5x7字體
lcd_delay_ms(4);
LCD_write_cmd(0x08); // 顯示關(guān)閉
lcd_delay_ms(4);
LCD_write_cmd(0x01); // 顯示清屏
lcd_delay_ms(4);
LCD_write_cmd(0x06); // 顯示光標(biāo)移動(dòng)設(shè)置
lcd_delay_ms(4);
LCD_write_cmd(0x0c); // 顯示開,光標(biāo)開,光標(biāo)閃爍
lcd_delay_ms(4);
LCD_write_cmd(0x01); //清屏
lcd_delay_ms(4);
}
/*--------------------------------------------------
函數(shù)說明:寫命令到液晶
---------------------------------------------------*/
void LCD_write_cmd(unsigned char cmd)
{
LCD_RS(0);
LCD_Write_byte(cmd);
lcd_delay_us(340);
}
/*--------------------------------------------------
函數(shù)說明:寫數(shù)據(jù)到液晶
---------------------------------------------------*/
void LCD_write_data(unsigned char w_data)
{
LCD_RS(1);
LCD_Write_byte(w_data);
lcd_delay_us(340);
}
/*--------------------------------------------------
函數(shù)說明:寫4bit到液晶
--------------------------------------------------*/
void LCD_Write_byte(unsigned char num)
{
if (num&0x01)
data0(1);
else
data0(0);
if (num&0x02)
data1(1);
else
data1(0);
if (num&0x04)
data2(1);
else
data2(0);
if (num&0x08)
data3(1);
else
data3(0);
if (num&0x10)
data4(1);
else
data4(0);
if (num&0x20)
data5(1);
else
data5(0);
if (num&0x40)
data6(1);
else
data6(0);
if (num&0x80)
data7(1);
else
data7(0);
lcd_delay_us(340);
LCD_EN(1);
lcd_delay_us(340);
LCD_EN(0);
lcd_delay_us(340);
}
/*----------------------------------------------------
LCD_set_xy : 設(shè)置LCD顯示的起始位置
輸入?yún)?shù):x、y : 顯示字符串的位置,X:0-15,Y:0-1
-----------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address = 0;
if (y==0)
{
address=0x80+x;
}
else
{
address=0xc0+x;
}
// y ? (address=0xc0+x): (address=0x80+x) ;
LCD_write_cmd(address);
}
/*---------------------------------------------------
LCD_write_string : 英文字符串顯示函數(shù)
輸入?yún)?shù):*s :英文字符串指針;
X、Y : 顯示字符串的位置
---------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y, char *s)
{
LCD_set_xy(X,Y);
while (*s)
{
LCD_write_data(*s);
s++;
}
}
//=======================================================
void LCD_wstring(unsigned char X,unsigned char *s)
{
LCD_write_cmd(X);
while (*s)
{
LCD_write_data(*s);
s++;
}
}