历史上的今天
今天是:2024年11月13日(星期三)
2019年11月13日 | stm32 驱动DS18B20温度传感器
2019-11-13 来源:eefocus
#include "temp.h"
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
#define DS18B20_PORT GPIOA
#define DS18B20_PIN GPIO_Pin_1
#define DS18B20_CLK RCC_APB2Periph_GPIOA
#define RW1820_DQ_HIGH GPIO_SetBits(DS18B20_PORT, DS18B20_PIN);
#define RW1820_DQ_LOW GPIO_ResetBits(DS18B20_PORT, DS18B20_PIN);
#define RW1820_DQ_VALUE GPIO_ReadInputDataBit(DS18B20_PORT,DS18B20_PIN)
//---------------------------------------------------------------------------//
void _delay_us(uint16_t nCount)
{
nCount *= 3;
while(--nCount);
}
//-----------------------------------------------------------------------------//
static void RW1820_DQ_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS18B20_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);
}
//--------------------------------------------------------
static void RW1820_DQ_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS18B20_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS18B20_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_PORT, &GPIO_InitStructure);
}
//--------------------------------------------------------
void RW1820_Init(void)
{
u8 retry=0;
RW1820_DQ_OUT();
RW1820_DQ_HIGH;
_delay_us(50);
RW1820_DQ_LOW;
_delay_us(500); //480 - 960
RW1820_DQ_HIGH;
_delay_us(40); //15-60
RW1820_DQ_IN(); //
while(RW1820_DQ_VALUE==1&& (retry<100))
{
retry++;
_delay_us(10);
}
RW1820_DQ_OUT();
RW1820_DQ_HIGH;
_delay_us(400);
}
//-------------
void RW1820_WriteByte(unsigned char _data)
{
int i = 0;
RW1820_DQ_OUT();
_delay_us(10);
for (i = 0; i < 8; i++)
{
RW1820_DQ_LOW; //
_delay_us(2); //
if (_data & 0x01)
{
RW1820_DQ_HIGH; //
}
else RW1820_DQ_LOW;
_delay_us(60); //60
RW1820_DQ_HIGH;
_data = _data >> 1;
}
}
//-------------
unsigned char RW1820_ReadByte(void)
{
int i = 0, _data = 0;
_delay_us(10);
for (i = 0; i < 8; i++)
{
RW1820_DQ_OUT();
RW1820_DQ_LOW;
_data >>= 1;
_delay_us(2);
RW1820_DQ_HIGH;
RW1820_DQ_IN();
if(RW1820_DQ_VALUE)
{
_data |= 0x80;
}
_delay_us(60); //60us
}
return _data;
}
int RW1820_ReadTemperature(void)
{
unsigned char temp;
unsigned int t;
RW1820_Init(); //
RW1820_WriteByte(0xcc); //
RW1820_WriteByte(0x44); //
_delay_us(10);
RW1820_Init();
RW1820_WriteByte(0xcc); //
RW1820_WriteByte(0xbe); //
temp = RW1820_ReadByte();
t = (int)(((temp & 0xf0) >> 4) + (temp & 0x07) * 0.125);
temp = RW1820_ReadByte();
t += ((temp & 0x0f) << 4);
return t;
}
史海拾趣
|
本帖最后由 dontium 于 2015-1-23 11:32 编辑 电路如图!!我用的单电源供电,lm324没用的管脚是悬空的!不知道有没影响。问题是输出最大只有1.6V,而我的程序结果应该是0到5v 就算不能满量程也该在5V附近吧!!大侠们 帮忙看下吧!!谢了!!dac0 ...… 查看全部问答> |
|
最近小弟正在做一个利用zigbee协议的无线语音通信的东东,碰到了一些问题,例如在zigbee协议的home_automation_profile已经有了关于light、Closures等的ID,那关于voice的是不是自己定义一个值就行了? 麻烦这一领域里的师兄师姐帮助 ...… 查看全部问答> |
|
前几天,在跑程序的时候遇到一个zbuf的异常,vxworks打印的调用链如下: data storage Exception current instruction address: 0x0050cb60 Machine Status Register: 0x00029230 Data Exception Address Register: 0x0069bf38 Condition Regi ...… 查看全部问答> |
|
Linux的开发软件SldView 谁有啊,能否给我发一下,不胜感激 想做Linux开发,在网上找了下 发现开发包都不是很好找 望大虾有的话能够发一下 先行谢过 qq:175232683… 查看全部问答> |
|
本帖最后由 paulhyde 于 2014-9-15 03:38 编辑 各位大神,能分享一下你们的经验吗,你觉得今年的带赛题,电源类的应该准备那些元器件,最要有具体的型号,谢谢分享 … 查看全部问答> |




