Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[ARM] 18B20 not read, on tft only show 000.0625 C

Status
Not open for further replies.

betuse

Junior Member level 3
Junior Member level 3
Joined
Oct 17, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
173
Hi for all,
I use EasyMx Pro for Stellaris board with LM4F232 (Cortex M4) mcu,
I need to read temp from 18B20, but on TFT I only have 000.0625 C,
sensor in connected on PD4 port
the code is
Code:
// TFT module connections
char TFT_DataPort at GPIO_PORTJ_DATA;
sbit TFT_RST at GPIO_PORTH_DATA5_bit;
sbit TFT_RS at GPIO_PORTG_DATA7_bit;
sbit TFT_CS at GPIO_PORTH_DATA6_bit;
sbit TFT_RD at GPIO_PORTC_DATA5_bit;
sbit TFT_WR at GPIO_PORTH_DATA4_bit;
sbit TFT_BLED at GPIO_PORTA_DATA3_bit;
char TFT_DataPort_Direction at GPIO_PORTJ_DIR;
sbit TFT_RST_Direction at GPIO_PORTH_DIR5_bit;
sbit TFT_RS_Direction at GPIO_PORTG_DIR7_bit;
sbit TFT_CS_Direction at GPIO_PORTH_DIR6_bit;
sbit TFT_RD_Direction at GPIO_PORTC_DIR5_bit;
sbit TFT_WR_Direction at GPIO_PORTH_DIR4_bit;
sbit TFT_BLED_Direction at GPIO_PORTA_DIR3_bit;
// End TFT module connections

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12

const unsigned short TEMP_RESOLUTION = 12;
char *text = "000.0000  C";
unsigned temp;

void DrawScr() {
  TFT_Init_ILI9341_8bit(320, 240);
  TFT_Fill_Screen(CL_WHITE);
  TFT_Set_Pen(CL_Black, 2);
  TFT_Line(20, 220, 300, 220);
  TFT_LIne(20,  46, 300,  46);
  TFT_Set_Font(&HandelGothic_BT21x22_Regular, CL_RED, FO_HORIZONTAL);
  TFT_Write_Text("Temperature sensor (18B20)", 20, 14);
  TFT_Set_Font(&Verdana12x13_Regular, CL_RED, FO_HORIZONTAL);
  TFT_Write_Text("Cortex M4", 19, 223);
  TFT_Set_Font(&Verdana12x13_Regular, CL_RED, FO_HORIZONTAL);
  TFT_Write_Text("LM4F232", 256, 223);
  TFT_Set_Font(&TFT_defaultFont, CL_BLACK, FO_HORIZONTAL);
}

void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned int temp_fraction;

  // Check if temperature is negative
  if (temp2write & 0x8000) {
    text[0] = '-';
    temp2write = ~temp2write + 1;
  }

  // Extract temp_whole
  temp_whole = temp2write >> RES_SHIFT ;

  // Convert temp_whole to characters
  if (temp_whole/100)
    text[0] = temp_whole/100  + 48;
  else
    text[0] = '0';

  text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
  text[2] =  temp_whole%10     + 48;             // Extract ones digit

  // Extract temp_fraction and convert it to unsigned int
  temp_fraction  = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // Convert temp_fraction to characters
  text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
  text[5] = (temp_fraction/100)%10 + 48;         // Extract hundreds digit
  text[6] = (temp_fraction/10)%10  + 48;         // Extract tens digit
  text[7] =  temp_fraction%10      + 48;         // Extract ones digit
  text[9] = 176;

  // Print temperature on TFT
  TFT_Set_Font(&tahoma29x29_Bold, CL_RED, FO_HORIZONTAL);
  TFT_Write_Text(text, 85, 100);
}

void main() {
  TFT_Set_Default_Mode();
  DrawScr();

  TFT_Set_Font(&tahoma29x29_Bold, CL_BLACK, FO_HORIZONTAL);
  TFT_Write_Text("Temperature:  ", 75, 65);
  GPIO_Digital_Input(&GPIO_PORTD, _GPIO_PINMASK_ALL);     // all pins on PD is digital input

  // Main loop
  do {
    // Perform temperature reading
    Ow_Reset(&GPIO_PORTD_AHB_DATA, 4);                         // Onewire reset signal
    Ow_Write(&GPIO_PORTD_AHB_DATA, 4, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&GPIO_PORTD_AHB_DATA, 4, 0x44);                   // Issue command CONVERT_T
    
    Delay_ms(750);

    Ow_Reset(&GPIO_PORTD_AHB_DATA, 4);
    Ow_Write(&GPIO_PORTD_AHB_DATA, 4, 0xCC);                   // Issue command SKIP_ROM
    Ow_Write(&GPIO_PORTD_AHB_DATA, 4, 0xBE);                   // Issue command READ_SCRATCHPAD
    
    temp =  Ow_Read(&GPIO_PORTD_AHB_DATA, 4);
    temp = (Ow_Read(&GPIO_PORTD_AHB_DATA, 4) << 8) + temp;
    
    // Format and display result on TFT
    Display_Temperature(temp);
    Delay_ms(1000);

  } while (1);
}

can somebody advice?
regards
 

Test the code step by step.
Firstly DS18b20 can give us the temperarure 9 to 12 bits,
you selected 12 bits in these code lines

Code:
//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12

const unsigned short TEMP_RESOLUTION = 12;

so you can start your test with sending temperature value
to the value converting and displaying algorithm by hand,
for example in the main loop only write these lines;
Code:
    // Format and display result on TFT
   for(temp=0; temp<= binary 0000 1111 1111 1111 ; temp+100)
{
    Display_Temperature(temp);
    Delay_ms(100);
 }
so you can check if converting and displaying algoritm works alright or not.

Then you will check other parts of the algorithm likewise.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top