STM32VLDiscovery - One wire temerature sensor - UART

Status
Not open for further replies.

HitTive

Newbie level 3
Joined
Oct 12, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,305
Hi, I want connect a one wire temperature sensor with UART. My problem is a two registers of measurement is always equal 255 ( temp_LSB and temp_MSB).
And I have problem that
Code:
int USART2_Read()
{    
    //  while ((USART2->SR & USART_SR_RXNE) == 0);
      return USART2->DR & 0x1FF;
}
while ((USART2->SR & USART_SR_RXNE) == 0); must be comment because program can't exit the loop.
Impulse RESET work, because I can read PRESENCE impuls.
Configuration:
Code:
    //usart2
    GPIOA->CRL |= GPIO_CRL_CNF3_0;
    GPIOA->CRL |= GPIO_CRL_MODE2_1;
    GPIOA->CRL |= GPIO_CRL_CNF2_1 ;
    RCC->APB1ENR|=  RCC_APB1ENR_USART2EN;
    USART2->CR1 |= USART_CR1_UE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
    USART2->CR3|=USART_CR3_HDSEL;
    USART2->BRR = (SystemCoreClock /9600);
    NVIC_EnableIRQ(USART2_IRQn);
Main program:
Code:
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
    const TickType_t xDelay2 = 800 / portTICK_PERIOD_MS;
     int temp_LSB = 0;
    int temp_MSB = 0;
    int temp_celsius = 0;
    char temp[8];
    while(1)
        {
              vTaskDelay(xDelay);
              if(one_wire_reset())
            {
                USART2->BRR = (SystemCoreClock /115200);
                one_wire_write_byte(0xCC);
                one_wire_write_byte(0x44);
                vTaskDelay(xDelay2);
                one_wire_reset();
                USART2->BRR = (SystemCoreClock /115200);
                one_wire_write_byte(0xCC);
                one_wire_write_byte(0xBE);
                temp_LSB = one_wire_read_byte();
                temp_MSB = one_wire_read_byte();
                one_wire_reset();
                USART2->BRR = (SystemCoreClock /115200);
                USART1_PutString("     ");
                USART1_PutString(itoa(temp_LSB,temp,10));
                USART1_PutString(itoa(temp_MSB,temp,10));
                USART1_PutString("     ");
                temp_celsius = ((temp_MSB * 256) + temp_LSB) / 16.0;
                USART1_PutString(itoa(temp_celsius,temp,10));
            }
        }
and functions:
Code:
int one_wire_read_byte()
{
int byte = 0;
int i=0,bit=0;
for (i = 0; i < 8; i++)
{
USART2_PutChar(0xFF);
bit =  USART2_Read()==0xFF ? 1 : 0;
byte = (byte >> 1) + 0x80 * bit;
}
return byte;
}
void one_wire_write_byte(int byte)
{
    int i=0;int bit;
    for (i = 0; i < 8; i++, byte = byte >> 1)
    {
        bit=byte & 0x01;
        bit = bit ? 0xFF : 0x00;
        USART2_PutChar(bit);
    }
}
int one_wire_reset()
{
        USART2->BRR = (SystemCoreClock /9600);
        while(!(USART2-> SR & USART_SR_TXE));
        USART2->DR =0xF0;
        if (USART2_Read()==0xF0) return 0; else return 1;
        return 1;
}
/********************************************
*
*/
void USART1_PutChar(uint8_t ch)
{
    while(!(USART1-> SR & USART_SR_TXE));
    USART1->DR =ch;
}
void USART2_PutChar(uint8_t ch)
{
    while(!(USART2-> SR & USART_SR_TXE));
    USART2->DR =ch;
}
void USART1_Read()
{
      while ((USART1->SR & USART_SR_RXNE) == 0);
      UART_Read=USART1->DR & 0x1FF;
}
int USART2_Read()
{    
    //  while ((USART2->SR & USART_SR_RXNE) == 0);
      return USART2->DR & 0x1FF;
}
void USART1_PutString(uint8_t * str)
{
while(*str != 0)
  {
  USART1_PutChar(*str);
  str++;
}
}
void USART1_IRQHandler(void)
{
     USART1_Read();
}
void USART2_IRQHandler(void)
{
    USART2_Read();
}
I used this sites to learn about one wire:
https://electricimp.com/docs/resources/onewire/
https://www.maximintegrated.com/en/app-notes/index.mvp/id/214
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…