#include "stm32f10x.h"
#include "uctsk_DS18B20.h"
#include <includes.h>
//#define EnableINT()
//#define DisableINT()
#define DS_PORT GPIOA
#define DS_DQIO GPIO_Pin_1
#define DS_RCC_PORT RCC_APB2Periph_GPIOA
#define DS_PRECISION 0x7f //精度配置寄存器 1f=9位; 3f=10位; 5f=11位; 7f=12位;
#define DS_AlarmTH 0x64
#define DS_AlarmTL 0x8a
#define DS_CONVERT_TICK 1000
#define ResetDQ() GPIO_ResetBits(DS_PORT,DS_DQIO)
#define SetDQ() GPIO_SetBits(DS_PORT,DS_DQIO)
#define GetDQ() GPIO_ReadInputDataBit(DS_PORT,DS_DQIO)
OS_CPU_SR cpu_sr;
static OS_STK App_TaskDS18B20Stk[APP_TASK_DS18B20_STK_SIZE];
static void uctsk_DS18B20(void)
{ //OS_ENTER_CRITICAL();
ds18b20_start();
ds18b20_read();
//OS_EXIT_CRITICAL();
OSTimeDlyHMSM(0, 0, 0, 500);
}
void App_DS18B20TaskCreate (void)
{
CPU_INT08U os_err;
os_err = os_err; /* prevent warning... */
os_err = OSTaskCreate((void (*)(void *)) uctsk_DS18B20,
(void * ) 0,
(OS_STK * )&App_TaskDS18B20Stk[APP_TASK_DS18B20_STK_SIZE - 1],
(INT8U ) APP_TASK_DS18B20_PRIO );
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_DS18B20_PRIO, "Task DS18B20", &os_err);
#endif
}
static unsigned char TempX_TAB[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//void Delay_us(u32 Nus)
/****************************************************************************
* 函 数:Delay(uint16_t Nus);
* 功 能:延时
* 参 数:uint8_t Nus
* 返回值:无
* 更 新:无
* 备 注:Delay(8)大概是1US
****************************************************************************/
void Delay_us(uint16_t Nus)
{
while(Nus--){}
}
unsigned char ResetDS18B20(void)
{
unsigned char resport=0;
SetDQ();
Delay_us(400);
ResetDQ();
Delay_us(4000); //500us (该时间的时间范围可以从480到960微秒)
SetDQ();
Delay_us(320); //40us
resport = GetDQ();
//while(GetDQ());
Delay_us(4000); //500us
SetDQ();
return resport;
}
void DS18B20WriteByte(unsigned char Dat)
{
unsigned char i=0;
for(i=8;i>0;i--)
{
ResetDQ(); //在15u内送数到数据线上,DS18B20在15-60u读数
Delay_us(40); //5us
if(Dat & 0x01)
SetDQ();
else
ResetDQ();
Delay_us(520); //65us
SetDQ();
Delay_us(16); //连续两位间应大于1us
Dat >>= 1;
}
}
unsigned char DS18B20ReadByte(void)
{
unsigned char i=0,Dat=0;
SetDQ();
Delay_us(40);
for(i=8;i>0;i--)
{
Dat >>= 1;
ResetDQ(); //从读时序开始到采样信号线必须在15u内,且采样尽量安排在15u的最后
Delay_us(40); //5us
SetDQ();
Delay_us(40); //5us
if(GetDQ())
Dat|=0x80;
else
Dat&=0x7f;
Delay_us(520); //65us
SetDQ();
}
return Dat;
}
void ReadRom(unsigned char *Read_Addr)
{
unsigned char i=0;
DS18B20WriteByte(ReadROM);
for(i=8;i>0;i--)
{
*Read_Addr=DS18B20ReadByte();
Read_Addr++;
}
}
void DS18B20Init(unsigned char Precision,unsigned char AlarmTH,unsigned char AlarmTL)
{
//DisableINT();
//OS_ENTER_CRITICAL();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(WriteScratchpad);
DS18B20WriteByte(AlarmTL);
DS18B20WriteByte(AlarmTH);
DS18B20WriteByte(Precision);
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(CopyScratchpad);
//EnableINT();
while(!GetDQ()); //等待复制完成 ///////////
// OS_EXIT_CRITICAL();
}
void DS18B20StartConvert(void)
{
//DisableINT();
//OS_ENTER_CRITICAL();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(StartConvert);
//EnableINT();
//OS_EXIT_CRITICAL();
}
void DS18B20_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(DS_RCC_PORT, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS_DQIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //开漏输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //2M时钟速度
GPIO_Init(DS_PORT, &GPIO_InitStructure);
}
void ds18b20_start(void)
{
DS18B20_Configuration();
DS18B20Init(DS_PRECISION, DS_AlarmTH, DS_AlarmTL);
DS18B20StartConvert();
}
unsigned short ds18b20_read(void)
{
unsigned char TemperatureL=0,TemperatureH=0;
unsigned int Temperature=0;
//DisableINT();
// OS_ENTER_CRITICAL();
ResetDS18B20();
DS18B20WriteByte(SkipROM);
DS18B20WriteByte(ReadScratchpad);
TemperatureL=DS18B20ReadByte();
TemperatureH=DS18B20ReadByte();
ResetDS18B20();
//EnableINT();
if(TemperatureH & 0x80)
{
TemperatureH=(~TemperatureH) | 0x08;
TemperatureL=~TemperatureL+1;
if(TemperatureL==0)
TemperatureH+=1;
}
TemperatureH=(TemperatureH<<4)+((TemperatureL&0xf0)>>4);
TemperatureL=TempX_TAB[TemperatureL&0x0f];
//bit0-bit7为小数位,bit8-bit14为整数位,bit15为正负位
Temperature=TemperatureH;
Temperature=(Temperature<<8) | TemperatureL;
// OS_EXIT_CRITICAL();
DS18B20StartConvert();
return Temperature;
} |