CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

18f2550 and Lm92 Problems

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Cheick



Joined: 07 Feb 2005
Posts: 12

View user's profile Send private message

18f2550 and Lm92 Problems
PostPosted: Mon Nov 16, 2009 11:49 am     Reply with quote

Hi all,

I am trying to do a program with 18F2550 and Lm92 but it did not work and I do not know where is problem. The address to write is 0x90 and 0x91 for read, because A0 and A1 is connected to ground. Thank you for your help.
Code:

#opt 9
//#include <18F2550.h>
#device PIC18F2550 adc=10
//#include <sk4x2_i2c_temp.h>
#include <18F2550.h>
 #fuses  HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
//#use i2c (Master,fast,sda=PIN_C0,scl=PIN_C1, force_hw)
//#use i2c(master,sda=SDA_pin,scl=SCL_pin)

#define LEDV         PIN_B2
#define LED_ON       output_high
#define LED_OFF      output_low

//********************************************************/
//
// définitions de variables
//
// définition de la structure pour une variable de type mesure de température sur le lm92
// le lm92 nous donne une température sur 16 bits codée comme suit:
      // S D D D D D D D D D D D D C H L
      // I 1 1 0 0 0 0 0 0 0 0 0 0 R I O
      // G 1 0 9 8 7 6 5 4 3 2 1 0 I G W
      // N                         T H
// comme on lit cela par i2c, il faut pouvoir y accéder par 2 bytes
// comme on doit manipuler les bits, il faut également pouvoir y accéder par un word
// enfin, le format est en entier signé, if faut donc également pouvoir y accéder par un signed int
typedef union {
                 struct
       {
         byte lsb,msb;
       } b;
         signed int16 signed_temp;
         int16 int16_temp;
              } t_temp;
t_temp temp;           //Stocke la temperature mesurée
signed int16 temperature_mesuree; // officialisation de la température mesurée
//
// définition de la structure pour une variable de type manufacturer id
// le lm92 nous donne le manufacturer ID sur 16 bits codé comme suit:
      // D D D D D D D D D D D D D D D D
      // 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
      // 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
      //
      // 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
// comme on lit cela par i2c, il faut pouvoir y accéder par 2 bytes
// comme on doit manipuler les bits, il faut également pouvoir y accéder par un word
typedef union {
                 struct
       {
         byte lsb,msb;
       } b;
         int16 int16_ID;
              } t_man_ID;
t_man_ID Manufacturer_ID;           //Stocke le manufacturer ID

byte aff_temp[8]; //température à afficher
byte var_trav; // variable à tout faire

// définitions des constantes
const byte conv_16_10[16]  = {'0', // 0/16 = 0
                              '1', // 1/16 = 0,0625 -> 1/10
                              '1', // 2/16 = 0,125 -> 1/10
                              '2', // 3/16 = 0,1875 -> 2/10
                              '3', // 4/16 = 0,25 -> 3/10
                              '3', // 5/16 = 0,3125 -> 3/10
                              '4', // 6/16 = 0,375 -> 4/10
                              '4', // 7/16 = 0,4375 -> 4/10
                              '5', // 8/16 = 0,5 -> 5/10
                              '6', // 9/16 = 0,5625 -> 6/10
                              '6', // 10/16 = 0,625 -> 6/10
                              '7', // 11/16 = 0,6875 -> 7/10
                              '8', // 12/16 = 0,75 -> 8/10
                              '8', // 13/16 = 0,8125 -> 8/10
                              '9', // 14/16 = 0,875 -> 9/10
                              '9'}; // 15/16 = 0,9375 -> 9/10

 /********************************************************/
void lm92_read_temp()  {
    i2c_start();
    i2c_write(lm92_wr);
    i2c_write(lm92_temp_ptr);
    i2c_start();
    i2c_write(lm92_rd);
    temp.b.msb = i2c_read(1); // ack
    temp.b.lsb = i2c_read(0); // no ack
    i2c_stop();
}

/******************************************************/
/******************************************************/
void lm92_read_manufacturer_ID()  {
    i2c_start();
    i2c_write(lm92_wr);
    i2c_write(lm92_manufacturer_id);
    i2c_start();
    i2c_write(lm92_rd);
    Manufacturer_ID.b.msb = i2c_read(1); // ack
    Manufacturer_ID.b.lsb = i2c_read(0); // no ack
    i2c_stop();
}

/******************************************************/
void main(void) {

   while (TRUE)
   {
//lm92_read_manufacturer_ID();
//LED_ON(LEDV);
delay_ms(1000);
lm92_read_temp();

         }
      }


In 18f2550 I added the following line
Code:

/////////////////////////////// i2c
//
#use i2c(master,sda=SDA_pin,scl=SCL_pin)
#define lm92_rd    0x91  // lm92 address for read
#define lm92_wr    0x90  // lm92 address for write

#define lm92_temp_ptr  0x00
#define lm92_config_ptr 0x01
#define lm92_thyst_ptr 0x02
#define lm92_tcrit_ptr 0x03
#define lm92_tlow_ptr 0x04
#define lm92_thigh_ptr 0x05
#define lm92_manufacturer_id 0x07

#list
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 16, 2009 1:42 pm     Reply with quote

Quote:

#opt 9
//#include <18F2550.h>
#device PIC18F2550 adc=10
//#include <sk4x2_i2c_temp.h>
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)



If you have a 20 MHz crystal, your fuse settings will create an oscillator
frequency of 48 MHz. Get rid of your code above, and use this:
Code:

#include <18F2550.h>
#device adc=10
#fuses  HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
 

Also, make sure you have pull-up resistors on the SDA and SCL pins.
You can use 4.7K ohms (4700 ohms).
Cheick



Joined: 07 Feb 2005
Posts: 12

View user's profile Send private message

PostPosted: Mon Nov 23, 2009 1:13 pm     Reply with quote

Ok I have a problem. When I use this code with the pic 18F452 it works but with the pic 18F2550 does not work. I'll display on the screen with the port usb. Look at this if its well my code or not. Thanks for all.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 23, 2009 1:34 pm     Reply with quote

Quote:
#use i2c(master,sda=SDA_pin,scl=SCL_pin)

Your code is not complete or compilable. The #define statements for the
constants in bold are not shown in your posts. Post a complete,
compilable test program. Test it before you post it and verify that it
shows the problem.

Also, your code is made more difficult to read because you have not
followed C standards. For example, your constants are in lower case.
Example:
Code:
#define lm92_temp_ptr  0x00

They should be in upper case:
Code:
#define LM92_TEMP_PTR  0x00

Indian Hill C style guide says:
Quote:

11. Naming Conventions

#define constants should be in all CAPS.

http://www.psgd.org/paul/docs/cstyle/cstyle11.htm

Wikipedia article on Naming conventions says:
Quote:

This is related to the convention in many programming languages of
using all-upper-case identifiers for constants.

http://en.wikipedia.org/wiki/Naming_conventions_(programming)#C_and_C.2B.2B_languages

I'm not trying to beat up on you. I'm trying to help you to understand
why people might not want to look at your code, if you don't use standard
methods. Everyone on the forum is a volunteer. Try to make it easy
for us to help you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group