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);
}
I used the internal oscilator of the microcontrollers.
EDIT:
Thank's p.erasmus,i changed.
Regards,
ducu