我正在尝试使用UART在1-Wire上编程数字温度计DS18B20,我遇到了奇怪的问题。当我调试这个没有断点运行的程序(或者只运行程序而没有调试)时,我的读数大约是100°C。当我在接收温度信息之前放置断点时,它给我约50°c。当我打开这个窗口时,当我看到SFR并展开UART4部分时,第三个选项可以让我得到大约25°C的正确读数。我有Saleae逻辑分析仪,在所有这三种情况下,它都会向我发送有效数据(约25°C)。我试图在断点处延迟一些延迟(甚至2秒延迟)并且它没有帮助。它给出了大约50°C的读数(就像断点而不是扩展的SFR窗口一样)。这可能是编程错误(我正在开始),但这个带有扩展SFR窗口的操作超出了我的推理范围。 我真的不知道发生了什么。我希望你能为我解决这个问题。
我正在使用STM32F4-Discovery评估板并在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);
}
}
答案 0 :(得分:0)
Write_1Wire(0xCC);
Write_1Wire(0xBE);
Read_1Wire()
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;
}
答案 1 :(得分:0)
我也遇到了类似的问题,我正在测量电容水位传感器的电容,并且在调试时运行时的值与未使用调试器时在运行模式下的值不同。 (只需将其简单地连接到外部电源即可)。我认为问题在于处理器的体系结构,当您调试断点时,它会暂停cpu而不是外设时钟,并且GPT arent会暂停,这会给出不同的读数,因为我的测量取决于计时器。我是从我的角度讲的,因此在您的情况下可能不会发生,但可以使您对问题有不同的看法。