无实物学习stm32f103,对于proteus,有很多bug,滴答时钟不准确,串口仿真不能打印浮点型,自己学的时候,花了好长时间,这里把整理好的代码给大家
运行效果:
主函数代码:
int main()
{
u8 i=0;
u16 temper;
float value;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组 分2组
LED_Init();
USART1_Init(115200);
while(DS18B20_Init())
{
printf("DS18B20检测失败,请插好!\r\n");
delay_ms(500);
}
printf("DS18B20检测成功!\r\n");
while(1)
{
i++;
if(i%20==0)
{
LED1=!LED1;
}
if(i%50==0)
{
temper=DS18B20_Get_Temp();
if((temper&0xf800)==0xf800)
{
temper=(~temper)+1;
printf("检测的温度为:- ");
}
else
{
printf("检测的温度为: ");
}
value=(float)temper*0.0625;
PrintfFloat(value);
printf("\r\n");
}
delay_ms(10);
}
}
DS18B20 函数:
#include "ds18b20.h"
//由于proteus延时函数无效,这里使用机械延时
void delay_1us(int num)
{
while(num--) ;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Rst
* 函数功能 : 复位DS18B20
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PG11 OUTPUT
DS18B20_DQ_OUT=0; //拉低DQ
delay_1us(750); //拉低750us
DS18B20_DQ_OUT=1; //DQ=1
delay_1us(15); //15US
}
/*******************************************************************************
* 函 数 名 : DS18B20_Check
* 函数功能 : 检测DS18B20是否存在
* 输 入 : 无
* 输 出 : 1:未检测到DS18B20的存在,0:存在
*******************************************************************************/
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PG11 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
delay_1us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
delay_1us(1);
};
if(retry>=240)return 1;
return 0;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Read_Bit
* 函数功能 : 从DS18B20读取一个位
* 输 入 : 无
* 输 出 : 1/0
*******************************************************************************/
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();//SET PG11 OUTPUT
DS18B20_DQ_OUT=0;
delay_1us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN();//SET PG11 INPUT
delay_1us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
delay_1us(50);
return data;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Read_Byte
* 函数功能 : 从DS18B20读取一个字节
* 输 入 : 无
* 输 出 : 一个字节数据
*******************************************************************************/
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;
}
/*******************************************************************************
* 函 数 名 : DS18B20_Write_Byte
* 函数功能 : 写一个字节到DS18B20
* 输 入 : dat:要写入的字节
* 输 出 : 无
*******************************************************************************/
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0;// Write 1
delay_1us(2);
DS18B20_DQ_OUT=1;
delay_1us(60);
}
else
{
DS18B20_DQ_OUT=0;// Write 0
delay_1us(60);
DS18B20_DQ_OUT=1;
delay_1us(2);
}
}
}
/*******************************************************************************
* 函 数 名 : DS18B20_Start
* 函数功能 : 开始温度转换
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
/*******************************************************************************
* 函 数 名 : DS18B20_Init
* 函数功能 : 初始化DS18B20的IO口 DQ 同时检测DS的存在
* 输 入 : 无
* 输 出 : 1:不存在,0:存在
*******************************************************************************/
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS18B20_PORT_RCC,ENABLE);
GPIO_InitStructure.GPIO_Pin=DS18B20_PIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(DS18B20_PORT,&GPIO_InitStructure);
DS18B20_Rst();
return DS18B20_Check();
}
/*******************************************************************************
* 函 数 名 : DS18B20_Get_Temp
* 函数功能 : 从ds18b20得到温度值
* 输 入 : 无
* 输 出 : 温度数据
*******************************************************************************/
int DS18B20_Get_Temp(void)
{
u16 temp;
u8 a,b;
DS18B20_Start(); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0xbe);// convert
a=DS18B20_Read_Byte(); // LSB
b=DS18B20_Read_Byte(); // MSB
temp=b;
temp=(temp<<8)+a;
return temp;
}
头文件函数:
#ifndef _ds18b20_H
#define _ds18b20_H
#include "system.h"
/* DS18B20时钟端口、引脚定义 */
#define DS18B20_PORT GPIOB
#define DS18B20_PIN GPIO_Pin_8
#define DS18B20_PORT_RCC RCC_APB2Periph_GPIOB
#define DS18B20_IO_IN() {GPIOB->CRH&=0XFFFFFFF0;GPIOB->CRH|=8<<0;}
#define DS18B20_IO_OUT() {GPIOB->CRH&=0XFFFFFFF0;GPIOB->CRH|=3<<0;}
IO操作函数
#define DS18B20_DQ_OUT PBout(8) //数据端口 PG11
#define DS18B20_DQ_IN PBin(8) //数据端口 PG11
u8 DS18B20_Init(void); //初始化DS18B20
int DS18B20_Get_Temp(void);//获取温度
void DS18B20_Start(void); //开始温度转换
void DS18B20_Write_Byte(u8 dat);//写入一个字节
u8 DS18B20_Read_Byte(void); //读出一个字节
u8 DS18B20_Read_Bit(void); //读出一个位
u8 DS18B20_Check(void); //检测是否存在DS18B20
void DS18B20_Rst(void); //复位DS18B20
#endif