EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / DS1621 interface with PIC18F4520 using C18 compiler

DS1621 interface with PIC18F4520 using C18 compiler

|

Microcontroller › PIC › DS1621 interface with PIC18F4520 using C18 compiler

  • This topic has 2 replies, 2 voices, and was last updated 12 years, 7 months ago by Jayant Gupta.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • October 27, 2012 at 10:52 am #1988
    Jayant Gupta
    Participant

    hi

    i m working on a project where i need to get the temperature using DS1621 via I2C protocol and display the temperature on LCD.

    as per the updats i have written the I2C protocol & LCD program.

    the question remains the data that i have got from I2C is not being displayed on to the LCD.i.e.the tempHI and tempLO values are not being displayed.

     

    here is my code so far.

     

    #include<p18f4520.h>
    #include<i2c.h>
    #include<delays.h>

    #define _XTAL_FREQ 4e6 // 4MHz
    #define LCD_DATA PORTD
    #define LCD_RS PORTBbits.RB0 //register select
    #define LCD_RW PORTBbits.RB1  //write on lcd
    #define LCD_EN PORTBbits.RB2  //enable on lcd

    void lcdstrobe(void) // Generate the E pulse
    {
        LCD_EN=0; // E = 0
        Delay1KTCYx(100); // 10ms delay for LCD_EN to settle
        LCD_EN=1; // E = 1
    }

    void lcd_write_cmd(unsigned char cmd)
    {
        LCD_RS=0;
        LCD_RW=0;
        PORTD=cmd;    
        lcdstrobe();
    }

    void lcd_write_data(char data)
    {
        LCD_RS=1; // Select LCD for data mode
        LCD_RW=0;
        PORTD =(data);
        lcdstrobe();
    }

    void init_lcd(void)
    {
        lcd_write_cmd(0x38);
        lcd_write_cmd(0x06);
        lcd_write_cmd(0x0C);
        lcd_write_cmd(0x01);
    }

    /*//***********************hex2bcd******************** ***
    unsigned char hex2bcd(unsigned char data)
    {
    unsigned char msb,lsb;
    msb = data/10;
    msb = msb << 4;
    lsb = data % 10;
    data = msb|lsb;
    return data;
    }*/

    void main()
    {
        char tempHI=0;
        char tempLO=0;
        char MSB1=0;
        char LSB1=0;

        TRISC=0x00; // turn on tri-state register and make all output pins
        PORTC=0x00; // output pins are LOW

        TRISD=0x00;
        TRISB=0x00;
        LCD_EN=1;
        init_lcd();
        while(1)
        {
            LCD_RS = 1;
            LCD_EN=1;
            Delay1KTCYx(1);
    // ============== CALL THE DATA FROM I2C SENSOR =================
    //**********************************I2C COMMUNICATION***********************************
            OpenI2C(MASTER, SLEW_OFF);
            SSPADD=0x27; // communication @ 100KHz for 4MHz
        
            StartI2C(); //begin I2C
            IdleI2C();
            WriteI2C(0x90); //send address to the device

            IdleI2C();
            WriteI2C(0xAC); // sends a control byte to the device to acces the configuration register
        
            IdleI2C();
            WriteI2C(0x02);  // sends configuration byte for continuous conversion and active high polarity

            IdleI2C();
            RestartI2C();     // briefly restart I2C communications
            IdleI2C();
            WriteI2C( 0x90 ); // again sending the address to the device
        
            IdleI2C();
            WriteI2C(0xEE); //command for temperature conversion
            IdleI2C();
            StopI2C(); //stop I2C communication

            StartI2C(); //start I2C communication
            IdleI2C();
            WriteI2C(0x90); //Send the address to the device
            IdleI2C();
            WriteI2C(0xAA); //access the temperature register
            IdleI2C();
            RestartI2C(); //initiate restart command
            IdleI2C();
            WriteI2C(0x91); // read from the register
            IdleI2C();
            tempHI=ReadI2C(); //MSB in tempHI

            IdleI2C();
            AckI2C(); // send acknowledge
            IdleI2C();
            tempLO=ReadI2C(); //LSB in tempLO

            IdleI2C();
            NotAckI2C(); // send not acknowledge
            IdleI2C();
            StopI2C(); // Stop all I2C communication
           
    //********************************** LCD DISPLAY OF TEMPERATURE ***********************************
            lcd_write_cmd(0x80); // Move cursor to line 1 position 1
     
            lcd_write_data(‘T’);
            lcd_write_data(‘E’);
            lcd_write_data(‘M’);
            lcd_write_data(‘P’);
            lcd_write_data(‘:’);

             lcd_write_data(48);        // Display the temperature
            lcd_write_data(0xDF);
            lcd_write_data(‘C’);
           
        }
    }

     

     

    thanks

    October 31, 2012 at 4:51 pm #8706
    AJISH ALFRED
    Participant

    I can see that you will get output like the following only;

    TEMP:0#C

    Is it??

    Because from the main function you are only calling the following functions related to lcd

            lcd_write_data(‘T’);
            lcd_write_data(‘E’);
            lcd_write_data(‘M’);
            lcd_write_data(‘P’);
            lcd_write_data(‘:’);

             lcd_write_data(48);        // Display the temperature
            lcd_write_data(0xDF);
            lcd_write_data(‘C’);

     

    You are not accessing the temperature data which you’ve stored in the register using i2c communication.

    November 8, 2012 at 2:20 pm #8727
    Jayant Gupta
    Participant

    yes my abjective is to see the output on the lcd as

     

    TEMP:#degreeC

    # gives the output from the DS1621 sensor.

     

    i have some how manage to work on the I2C protocol but i m not sure is it right or wrong since lcd interfacing in not yet successful

     

    secondly i have tried the lcd part too but i am not able append it properly in te code. i am confused with the delays that i am giving.

    even though i refered to previous suggestion but i m not able to successful in the program.

     

    i haven’t figured a way to call the value in  tempHI  register onto the lcd and finding it quite hard to understanding any other method of projecting the data.

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Fun with AI and swordfish basic June 23, 2025
  • Simple LED Analog Clock Idea June 23, 2025
  • Microinverters and storeage batteries? June 23, 2025
  • PIC KIT 3 not able to program dsPIC June 23, 2025
  • Is AI making embedded software developers more productive? June 23, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise