DS18B20温度传感器基于STM32.docx
《DS18B20温度传感器基于STM32.docx》由会员分享,可在线阅读,更多相关《DS18B20温度传感器基于STM32.docx(19页珍藏版)》请在冰豆网上搜索。
DS18B20温度传感器基于STM32
/*----------------------------------------------main.c-------------------------------------------------------*/
#include"stm32f10x.h"
#include"stdio.h"
#include"platform_config.h"
#include"misc.h"
#include"DS18b20.h"
#include"Systick_Delay.h"
/**@addtogroupStdPeriph_Examples
*@{
*/
unsignedcharsign,interger;
unsignedintdecimal;
/**@addtogroupTIM_TimeBase
*@{
*/
voidRCC_Configuration(void);
voidGPIO_Configuration(void);
voidNVIC_Configuration(void);
//intfputc(intch,FILE*f);//重定义串口字符串输出函数
voidUSART_Configuration1(void);
/*Privatetypedef-----------------------------------------------------------*/
/*Privatedefine------------------------------------------------------------*/
/*Privatemacro-------------------------------------------------------------*/
/*Privatevariables---------------------------------------------------------*/
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
__IOuint16_tCCR1_Val=100;
u16counter;
ErrorStatusHSEStartUpStatus;
/*Privatefunctionprototypes-----------------------------------------------*/
/*Privatefunctions---------------------------------------------------------*/
/**
*@briefMainprogram
*@paramNone
*@retval:
None
*/
intmain(void)
{
/*SystemClocksConfiguration*/
RCC_Configuration();
GPIO_Configuration();
USART_Configuration1();
/*NVICConfiguration*/
NVIC_Configuration();
/*GPIOConfiguration*/
/*---------------------------------------------------------------
TIM2Configuration:
OutputCompareTimingMode:
TIM2CLK=36MHz,Prescaler=4,TIM2counterclock=7.2MHz36/(4+1)=7.2MHZ
CC1updaterate=TIM2counterclock/CCR1_Val=146.48Hz
CC2updaterate=TIM2counterclock/CCR2_Val=219.7Hz
CC3updaterate=TIM2counterclock/CCR3_Val=439.4Hz
CC4updaterate=TIM2counterclock/CCR4_Val=878.9Hz
---------------------------------------------------------------*/
TIM_TimeBaseStructure.TIM_Period=65535;//定时器最大值就是65535
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
/*Timebaseconfiguration*/
TIM_TimeBaseStructure.TIM_Period=65535;//定时器最大值就是65535
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
/*Prescalerconfiguration*/
/*OutputCompareTimingModeconfiguration:
Channel1*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Timing;//
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
//TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Disable);
/*TIMITenable*/
//TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
/*TIM2enablecounter*/
//TIM_Cmd(TIM1,ENABLE);
//TIM_Cmd(TIM2,ENABLE);
printf("ok");
Read_Temperature(&sign,&interger,&decimal);
printf("%d,%d,%d",sign,interger,decimal);
while
(1)
{
}
}
/**
*@briefConfiguresthedifferentsystemclocks.
*@paramNone
*@retval:
None
*/
#ifdef__GNUC__
/*WithGCC/RAISONANCE,smallprintf(optionLDLinker->Libraries->Smallprintfsetto'Yes')calls__io_putchar()*/
#definePUTCHAR_PROTOTYPEint__io_putchar(intch)
#else
#definePUTCHAR_PROTOTYPEintfputc(intch,FILE*f)
#endif/*__GNUC__*/
PUTCHAR_PROTOTYPE
{
/*Placeyourimplementationoffputchere*/
/*e.g.writeacharactertotheUSART*/
USART_SendData(USART1,(uint8_t)ch);
/*Loopuntiltheendoftransmission*/
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET)
{}
returnch;
}
voidUSART_Configuration1(void)//串口配置程序
{
USART_InitTypeDefUSART_InitStructure;
USART_ClockInitTypeDefUSART_ClockInitStructure;
/*USART1configuration------------------------------------------------------*/
/*USART1configuredasfollow:
-BaudRate=115200baud
-WordLength=8Bits
-OneStopBit
-Noparity
-Hardwareflowcontroldisabled(RTSandCTSsignals)
-Receiveandtransmitenabled
-USARTClockdisabled
-USARTCPOL:
Clockisactivelow
-USARTCPHA:
Dataiscapturedonthemiddle
-USARTLastBit:
Theclockpulseofthelastdatabitisnotoutputto
theSCLKpin
*/
USART_ClockInitStructure.USART_Clock=USART_Clock_Disable;//时钟低电平活动
USART_ClockInitStructure.USART_CPOL=USART_CPOL_Low;//时钟低时数据有效
USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;//数据在第二个时钟边沿捕获
USART_ClockInitStructure.USART_LastBit=USART_LastBit_Disable;//最后数据位的时钟不输出到SCLK
/*ConfiguretheUSART1synchronousparamters*/
USART_ClockInit(USART1,&USART_ClockInitStructure);//使用以上参数初始化串口1时钟其实可以
//在USART_Init(USART1,&USART_InitStructure)中设置
USART_InitStructure.USART_BaudRate=115200;//设置波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//设置数据长度为8位
USART_InitStructure.USART_StopBits=USART_StopBits_1;//设置一个停止位
USART_InitStructure.USART_Parity=USART_Parity_No;//无校验位
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
//禁止硬件流控制模式
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//使能串口的收发功能
/*ConfigureUSART1basicandasynchronousparamters*/
USART_Init(USART1,&USART_InitStructure);//用以上参数初始化串口1
/*EnableUSART1*/
USART_Cmd(USART1,ENABLE);
}
voidRCC_Configuration(void)
{
/*RCCsystemreset(fordebugpurpose)*/
RCC_DeInit();
/*EnableHSE*/
RCC_HSEConfig(RCC_HSE_ON);
/*WaittillHSEisready*/
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
/*EnablePrefetchBuffer*/
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/*Flash2waitstate*/
FLASH_SetLatency(FLASH_Latency_2);
/*HCLK=SYSCLK*/
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/*PCLK2=HCLK*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/*PCLK1=HCLK/4*/
RCC_PCLK1Config(RCC_HCLK_Div4);
/*PLLCLK=8MHz*9=72MHz*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
/*EnablePLL*/
RCC_PLLCmd(ENABLE);
/*WaittillPLLisready*/
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
{}
/*SelectPLLassystemclocksource*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/*WaittillPLLisusedassystemclocksource*/
while(RCC_GetSYSCLKSource()!
=0x08)
{}
}
/*TIM2clockenable*/
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/*GPIOCclockenable*/
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);//使能由APB2时钟控制的外设中的USART1和PA端口
}
/**
*@briefConfiguretheGPIODPins.
*@paramNone
*@retval:
None
*/
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*ConfigureUSART1Tx(PA.09)asalternatefunctionpush-pull*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//IO端口的第0位
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//端口模式为复用推拉输出方式
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//翻转速度为50M
GPIO_Init(GPIOA,&GPIO_InitStructure);//用以上几个参数初始化PA口
/*ConfigureUSART1Rx(PA.10)asinputfloating*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//端口模式为复用浮点输入方式
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
/**
*@briefConfigurethenestedvectoredinterruptcontroller.
*@paramNone
*@retval:
None
*/
voidNVIC_Configuration(void)
{
NVIC_InitTypeDefNVIC_InitStructure;
/*EnabletheTIM2gloabalInterrupt*/
//NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#ifdefUSE_FULL_ASSERT
/**
*@briefReportsthenameofthesourcefileandthesourcelinenumber
*wheretheassert_paramerrorhasoccurred.
*@paramfile:
pointertothesourcefilename
*@paramline:
assert_paramerrorlinesourcenumber
*@retval:
None
*/
voidassert_failed(uint8_t*file,uint32_tline)
{
/*Usercanaddhisownimplementationtoreportthefilenameandlinenumber,
ex:
printf("Wrongparametersvalue:
file%sonline%d\r\n",file,line)*/
while
(1)
{}
}
#endif
/**
*@}
*/
/**
*@}
*/
/*******************(C)COPYRIGHT2009STMicroelectronics*****ENDOFFILE****/
/*-----------------------------------DS18B20.c--------------------------------------------------*/
#include"stm32f10x.h"
#include"DS18b20.h"
#include"SysTick_Delay.h"
/******************************************
函数名称:
GPIO_DQ_Out_Mode
功能:
设置DQ引脚为开漏输出模式
参数:
无
返回值:
无
*******************************************/
voidGPIO_DQ_Out_Mode(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=DQ_GPIO_Pin;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;//开漏输出
GPIO_Init(DQ_GPIO,&GPIO_InitStructure);
}
/******************************************
函数名称:
GPIO_DQ_Input_Mode
功能:
设置DQ引脚为浮空输入模式
参数:
无
返回值:
无
*******************************************/
voidGPIO_DQ_Input_Mode(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=DQ_GPIO_Pin;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(DQ_GPIO,&GPIO_InitStructure);
}
/******************************************
函数名称:
Tx_ResetPulse
功能:
发送复位脉冲
参数:
无
返回值:
无
*****