我正在尝试使用UART对1线进行数字温度计DS18B20编程,并且我有奇怪的问题。当我在没有断点的情况下运行(或仅运行程序而无需调试)时,我的读数约为100°C。当我在收到温度信息之前放断点时,它给了我约50°C。当我只打开此窗口时,当我看到SFRS并展开UART4部分时,它为我提供了约25°C的适当读数。我有Saleeae Logic Analyzer,在所有这三种情况下,它都会向我发送有效的数据(约25°C)。我试图在断点为(甚至2s延迟)的位置放置一些延迟,这无济于事。它给了我大约50°C的读数(例如有断点,而不是扩展的SFR窗口)。这可能是编程错误(我正在开始),但是使用扩展的SFRS窗口的操作超出了我的推理。我真的不知道发生了什么。希望您能为我介绍这种情况。
我正在使用STM32F4 - 发现评估板,并在Atollic 4.1.0上进行编程。
我的"库"文件:
#include "DS18B20_Lib.h"
void DS18B20_Init(void)
{
//USART4 PA0
GPIO_InitTypeDef GS;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4);
GS.GPIO_Mode = GPIO_Mode_AF;
GS.GPIO_OType = GPIO_OType_PP;
GS.GPIO_PuPd = GPIO_PuPd_UP;
GS.GPIO_Speed = GPIO_Speed_50MHz;
GS.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA,&GS);
USART_InitTypeDef US;
US.USART_BaudRate = 115200;
US.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
US.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
US.USART_Parity = USART_Parity_No;
US.USART_StopBits = USART_StopBits_1;
US.USART_WordLength = USART_WordLength_8b;
USART_Init(UART4,&US);
USART_SetAddress(UART4,0x12);
USART_Cmd(UART4,ENABLE);
USART_HalfDuplexCmd(UART4,ENABLE);
USART_ITConfig(UART4,USART_IT_TXE | USART_IT_RXNE | USART_IT_TC ,ENABLE);
/*NVIC_InitTypeDef NS;
NS.NVIC_IRQChannel = UART4_IRQn;
NS.NVIC_IRQChannelCmd = ENABLE;
NS.NVIC_IRQChannelPreemptionPriority = 1;
NS.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NS);*/
}
uint16_t Reset_1Wire(void)
{
uint16_t Present;
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
UART4->BRR = 0x1117;
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
USART_SendData(UART4,0xF0);
while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
Present = USART_ReceiveData(UART4);
while (USART_GetFlagStatus(UART4, USART_FLAG_TC) == RESET);
UART4->BRR = 0x016D;
return Present;
}
char Read_1Wire(void)
{
char Data=0;
int i;
for(i=0;i<8;i++)
{
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
USART_SendData(UART4,0xFF);
while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);
if(USART_ReceiveData(UART4)==0xFF)
Data|=0x80;
else Data|=0;
if (i!=7) Data=Data>>1;
}
return Data;
}
void Write_1Wire(char Data)
{
char Mask=1;
int i;
for(i=0;i<8;i++)
{
while (USART_GetFlagStatus(UART4, USART_FLAG_TXE) == RESET);
if (Data & Mask)
USART_SendData(UART4,0xFF);
else
USART_SendData(UART4,0x00);
Mask=Mask<<1;
}
}
uint16_t DS18B20_MeasTemp(void)
{
uint16_t Data;
uint16_t LSB;
uint16_t MSB;
Reset_1Wire();
Write_1Wire(0xCC); //SkipRom only 1 device
Write_1Wire(0x44);
while(Read_1Wire()==0x00);
Reset_1Wire();
Write_1Wire(0xCC);
Write_1Wire(0xBE);
/*--------BRAKEPOINT HERE--------*/
LSB=Read_1Wire();
MSB=Read_1Wire();
Data=MSB<<8|LSB;
Reset_1Wire();
return Data;
}
和我的主要功能:
int main(void)
{
char strLine[25];
uint16_t Temperature;
LCD_Init();
LCD_SetColors(GREEN, BLUE);
LCD_Clear(BLUE);
LCD_CharSize(16);
DS18B20_Init();
sprintf(strLine,"%s","Temp: ");
LCD_StringLine(20,50,(uint8_t*) strLine);
//GPIO_Config();
while (1)
{
Presence1Wire();
Temperature = DS18B20_MeasTemp();
if (Temperature&0xF000)
LCD_PutChar(60,50,'-');
else LCD_PutChar(60,50,'+');
LCD_PutInt(68,50,(Temperature&0x0FF0)>>4);
LCD_PutInt(72,64,(Temperature&0xF)*625);
}
}
Write_1Wire(0xCC);
Write_1Wire(0xBE);
- 将字节发送到UART,没有阅读。在FIFO中接收数据。
Read_1Wire()
- 在FIFO中读取旧数据
uint16_t DS18B20_MeasTemp(void)
{
uint16_t Data;
uint16_t LSB;
uint16_t MSB;
Reset_1Wire();
Write_1Wire(0xCC); //SkipRom only 1 device
Write_1Wire(0x44);
while(Read_1Wire() == 0x00);
Reset_1Wire();
Write_1Wire(0xCC);
Write_1Wire(0xBE);
/*--------BRAKEPOINT HERE--------*/
// !!!!!!! Paste this
while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) != RESET)
USART_ReceiveData(UART4);
// !!!!!!!
LSB = Read_1Wire();
MSB = Read_1Wire();
Data = MSB << 8 | LSB;
Reset_1Wire();
return Data;
}
我也遇到了一个类似的问题,我正在测量电容水位传感器的电容,而在调试中运行时,我在运行模式下获得了不同的值,而没有使用调试器(只是debugger)明确将其连接到外部电源)。
我认为问题在于处理器的体系结构,当您调试断点时,它会停止CPU,而不是外围时钟,而GPT不会暂停,因为我的测量值是计时器的,因此会产生不同的读数。
我是从我的角度讲话,因此在您的情况下可能不会发生这种情况,但它可能会使您对问题有所不同。