Page 1 of 1

pic16f887+LCD +18s20 [little bug]

Posted: 04 Jul 2010 12:22
by ducu
Hello people,
I recently did a test with this microcontroller [pic16f887], LCD [2x16] and sensor [18s20] temperature, everything works flawlessly, except display the symbol "-" [minus] when it detects below zero temperatures.
Mention that the test consisted of simulation in [Proteus] and physically on a breadboard.Conclusion?, Same result.
I failed to figure out what's wrong.
Can you give a helping hand in this regard?
Program:

Code: Select all

TERMOMETRU ELECTRONIC CU PIC16F887 LCD 2X16 SI SENZOR DE TEMPERATURA DS18S20
// declara conexiunea lcd
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// sfarsit de declaratie

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

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

// verificare pentru temperaturi negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
}
// extrage temp_whole
temp_whole = temp2write >> RES_SHIFT ;

// converteste temp_whole in caractere
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
text[0] = '0';

text[1] = (temp_whole/10)%10 + 48; // extrage zecimi
text[2] = temp_whole%10 + 48; // extrage unitati

// extrage temp_fraction si converteste in unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;

// converteste temp_fraction in caractere
text[4] = temp_fraction/1000 + 48; // extrage miile
text[5] = (temp_fraction/100)%10 + 48; // extrage sutele
text[6] = (temp_fraction/10)%10 + 48; // extrage zecile
text[7] = temp_fraction%10 + 48; // extrage unitatile

// afisare temp. pe LCD
Lcd_Out(2, 5, text);
}

void main() {
ANSEL = 0; // Configureaza pinii AN ca i/o digitali
ANSELH = 0;
C1ON_bit = 0; // comparator dezactivat
C2ON_bit = 0;

Lcd_Init(); // Initializeaza LCD
Lcd_Cmd(_LCD_CLEAR); // curata LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Out(1, 1, " Temperatura: ");

// aviseaza caracterul, 'C' pentru grade Celsius
Lcd_Chr(2,13,223); // char code pentru lcd 178 sau 223 [afiseaza grade]

Lcd_Chr(2,14,'C');

//--- bucla principala
do {
//--- citire temperatura
Ow_Reset(&PORTA, 1); // Onewire semnal de reset
Ow_Write(&PORTA, 1, 0xCC); // comanda SKIP_ROM
Ow_Write(&PORTA, 1, 0x44); // comanda CONVERT_T
Delay_us(120);
Ow_Reset(&PORTA, 1);
Ow_Write(&PORTA, 1, 0xCC); // comanda SKIP_ROM
Ow_Write(&PORTA, 1, 0xBE); // comanda READ_SCRATCHPAD
temp = Ow_Read(&PORTA, 1);
temp = (Ow_Read(&PORTA, 1) << 8) + temp;

//--- format si afisare pe lcd
Display_Temperature(temp);
Delay_ms(500);
} while (1);
}
After this inconvenience i have made a test on 16f628a, same result, the code is taken from the theoretical support of MikroElektronika.
I used the internal oscilator of the microcontrollers.

EDIT:
Thank's p.erasmus,i changed.

Regards,
ducu

Re: pic16f887+LCD +18s20 [little bug]

Posted: 04 Jul 2010 13:55
by p.erasmus
Please use code tags when posting code !!

Re: pic16f887+LCD +18s20 [little bug]

Posted: 04 Jul 2010 14:13
by Acetronics
Hi,

just add a "marker" to flag negative temps and use it NOT to overwrite " text[0] if temp is positive AND < 100°C ... here

Code: Select all

// converteste temp_whole in caractere
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
[b]text[0] = '0';[/b]
Alain

Re: pic16f887+LCD +18s20 [little bug]

Posted: 04 Jul 2010 16:12
by ducu
Hi, Acetronics,
can you argue a little, i'm not a connoisseur in programming.

Re: pic16f887+LCD +18s20 [little bug]

Posted: 05 Jul 2010 14:13
by anikolic
Hello,

Try replacing your Display_Temperature() function with following:

Code: Select all

void Display_Temperature(unsigned int temp2write) {
  const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
  char temp_whole;
  unsigned short isNegative = 0x00;
  unsigned int temp_fraction;
  
  // verificare pentru temperaturi negative
  if (temp2write & 0x8000) {
    text[0] = '-';
    temp2write = ~temp2write + 1;
    isNegative = 1;
  }
  // extrage temp_whole
  temp_whole = temp2write >> RES_SHIFT ;

  if (!isNegative){
    // converteste temp_whole in caractere
    if (temp_whole/100)
      text[0] = temp_whole/100 + 48;
    else
      text[0] = '0';
  }

  text[1] = (temp_whole / 10) % 10 + 48; // extrage zecimi
  text[2] = temp_whole % 10 + 48; // extrage unitati

  // extrage temp_fraction si converteste in unsigned int
  temp_fraction = temp2write << (4-RES_SHIFT);
  temp_fraction &= 0x000F;
  temp_fraction *= 625;

  // converteste temp_fraction in caractere
  text[4] = temp_fraction/1000 + 48; // extrage miile
  text[5] = (temp_fraction/100)%10 + 48; // extrage sutele
  text[6] = (temp_fraction/10)%10 + 48; // extrage zecile
  text[7] = temp_fraction%10 + 48; // extrage unitatile

  // afisare temp. pe LCD
  Lcd_Out(2, 5, text);
}
This way, if the temperature is negative, the minus sign will not be overwritten in the next block. I have did just what Acetronics said: I have used a flag that indicates this, in the first block:

Code: Select all

  // verificare pentru temperaturi negative
  if (temp2write & 0x8000) {
    text[0] = '-';
    temp2write = ~temp2write + 1;
    isNegative = 1;  // SET FLAG
  }
and then this flag is checked in the next block:

Code: Select all

  if (!isNegative){
    // converteste temp_whole in caractere
    if (temp_whole/100)
      text[0] = temp_whole/100 + 48;
    else
      text[0] = '0';
  }
Best regards,
Aleksandar

Re: pic16f887+LCD +18s20 [little bug]

Posted: 08 Jul 2010 12:24
by ducu
Thanks so much for promptly, Aleksandar,the program works perfectly in this manner.
Thanks for your help.

Regards,
ducu