Digital thermometer with lcd Help!!!

Please check the frequently asked questions before posting to the forum.
Post Reply
Author
Message
Fse
Posts: 59
Joined: 02 Aug 2012 02:01

Digital thermometer with lcd Help!!!

#1 Post by Fse » 19 Aug 2012 05:44

Hello all.
I'm new to building codes for PIC. My second test, I tried using the LM35 to measure temperature digitally with PIC16F877A. The result is what appears on the LCD. More in the original design, is used a DS1820. I modified the pins so that the output of LM35 was against the PIC. More there were some errors. First, the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes

ANSEL = 0; / / Configure AN pins to Digital I / O
     ANSELH = 0;
     C1ON_bit = 0 / / Disable comparators
     C2ON_bit = 0;

Second: In Lcd appeared only temperature reset "000,000 ° C".

From what I read about the PIC16F877A, the pin 10 is a comparator A / D number or RE2 AN7 pin that needs to be disabled to do the reading. Someone help me? Also that for the temperature reading work, you need a reference voltage.
The code follows below:

Code: Select all

/*Header******************************************************/

// LCD module connections
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;
// End LCD module connections

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;
    
    // 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
    
    // Display temperature on LCD
    Lcd_Out(2, 5, text);
}

void main() {
    ANSEL = 0;    // Configure AN pins as digital I/O >>>>>> the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    ANSELH = 0; // the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    C1ON_bit = 0; // Disable comparators>>>>>>>>>>>>>>>>the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    C2ON_bit = 0; // the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    
    Lcd_Init();   // Initialize LCD
    Lcd_Cmd(_LCD_CLEAR);      // Clear LCD
    Lcd_Cmd(_LCD_CURSOR_OFF); // Turn the cursor off
    Lcd_Out(1, 1, " Temperature: ");    
    
    // Print degree character, 'C' for Centigrades
    Lcd_Chr(2,13,223);        // different LCD displays have different char code for degree
    // if you see greek alpha letter try typing 178 instead of 223
    
    Lcd_Chr(2,14,'C');
    
    //--- main loop
    do {
        //--- perform temperature reading
        Ow_Reset(&PORTE, 2);       // Onewire reset signal
        Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
        Ow_Write(&PORTE, 2, 0x44); // Issue command CONVERT_T
        Delay_us(120);
        Ow_Reset(&PORTE, 2);
        Ow_Write(&PORTE, 2, 0xCC); // Issue command SKIP_ROM
        Ow_Write(&PORTE, 2, 0xBE); // Issue command READ_SCRATCHPAD
        temp = Ow_Read(&PORTE, 2);
        temp = (Ow_Read(&PORTE, 2) << 8) + temp;
        
        //--- Format and display result on Lcd
        Display_Temperature(temp);
        Delay_ms(500);
    } while (1);
}
Imagem from http://www.mikroe.com/eng/chapters/view ... ples/#c4v2
My diagram
http://img846.imageshack.us/img846/6139/capturar1g.png

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Digital thermometer with lcd Help!!!

#2 Post by hexreader » 19 Aug 2012 07:28

I can see several problems:

1 You have posted in a Pascal forum, but this is clearly C code.

2 LM35 is an analogue sensor, and is no substitute for DS1820, which is digital. The two work in completely different ways.

3 The code is written for PIC16F887. It will not work with PIC16F877A unless you make a lot of changes to the code.

Change back to DS1820 and PIC16F887 and your code is more likely to work.


If you must use PIC16F877A, then there is a DS1820 example that can be downloaded here:

http://www.mikroe.com/eng/products/view ... nt-system/

I do not know of any LM35 example that works with PIC16F877A
Start every day with a smile...... (get it over with) :)

Fse
Posts: 59
Joined: 02 Aug 2012 02:01

Re: Digital thermometer with lcd Help!!!

#3 Post by Fse » 20 Aug 2012 01:56

somebory have a suggestion?

MrB
Posts: 30
Joined: 09 Aug 2012 06:15
Location: Perth Western Australia

Re: Digital thermometer with lcd Help!!!

#4 Post by MrB » 20 Aug 2012 06:53

hexreader wrote:I can see several problems:
Fse wrote:somebory have a suggestion?
LOL!

Hex... do you ever get the feeling doing the following is more productive? :lol:
Image
Image
EasyMx PRO v7 for STM32 | F407VG MCU card | STM32F4-Discovery | FriendlyARM mini2440

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: Digital thermometer with lcd Help!!!

#5 Post by hexreader » 20 Aug 2012 07:18

@MrB

I am kind of used to those who's definition of "please help me with my code" is actually "I want my code written for me for free, as long as it involves no effort on my part"

I see you have an STM32 board. Mine should be here in a week or two. We must swap ideas.

Did you also buy a "...407" MCU card by any chance? ...and/or Mikromedia Board for STM32 M4?
Start every day with a smile...... (get it over with) :)

MrB
Posts: 30
Joined: 09 Aug 2012 06:15
Location: Perth Western Australia

Re: Digital thermometer with lcd Help!!!

#6 Post by MrB » 20 Aug 2012 07:27

Yes I got the '407 too. I'm hoping to process a video signal with it so need the 210 MIPS.
I have lots of idea's for my end product, but absolutely zero experience with the ARM processors, a very steep learning curve ahead :(

EDIT: Way off-topic now ;) I'm sure we will have plenty to share in the ARM sub-fora
Image
EasyMx PRO v7 for STM32 | F407VG MCU card | STM32F4-Discovery | FriendlyARM mini2440

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Digital thermometer with lcd Help!!!

#7 Post by filip » 20 Aug 2012 09:44

Hi,

First of all, the code below :

Code: Select all

    ANSEL = 0;    // Configure AN pins as digital I/O >>>>>> the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    ANSELH = 0; // the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    C1ON_bit = 0; // Disable comparators>>>>>>>>>>>>>>>>the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
    C2ON_bit = 0; // the mikroC PRO PIC FOR v: 5.6.1 does not accept the following codes
is specific for one type of the microcontroller (in this case 16F887) and it can't be compiled using some other MCU (like 16F877A),
because the internal registers of the MCUs don't have the same name in general.

So, you must use proper registers when using a microcontroller.
To do this, you should consult the MCU's data sheet.

Secondly, as hexreader said, the LM35 is an analog sensor, so you should read the ADC measurement from the pin on which the LM35 is connected,
rather than using One Wire protocol, which is used for the digital communication.

You can see the LM35 example for 18F45K22 and EasyPIC7 from the following location :
http://www.mikroe.com/eng/downloads/get ... s_v100.zip

It will be a good starting point for your example.

Regards,
Filip.

Fse
Posts: 59
Joined: 02 Aug 2012 02:01

Re: Digital thermometer with lcd Help!!!

#8 Post by Fse » 20 Aug 2012 16:16

Thanks for your help. Your help were very important. I have to much to learn.

Fse
Posts: 59
Joined: 02 Aug 2012 02:01

Re: Digital thermometer with lcd Help!!!

#9 Post by Fse » 20 Aug 2012 16:29

I just want to learn this. after, I want learn how to make a buttom with buttom to apper a mensage in the LCD. After this, I want to make my first project where I use PIC16F877A, LCD, and buttons to talk with PT2258 via I2C. Thanks for all help.

Fse
Posts: 59
Joined: 02 Aug 2012 02:01

Re: Digital thermometer with lcd Help!!!

#10 Post by Fse » 20 Aug 2012 16:51

knowledge is only considered when it is shared knowledge.

Post Reply

Return to “mikroPascal FAQ”