0
\$\begingroup\$

Good afternoon.

I am trying to figure out how to make a PIC16F877A to read the temperature from the DS1620 sensor. Accordingly to the datasheet of the DS1620, I need to send two nibbles A (AAh) to the sensor and may it return the bytes with the temperature. This is what I've tried until now.

However, since in the datasheet isnt specified the protocol (only says 3-wire communication), I am trying some bit-banging, with no success.

This is the code of what I've tried until now:

#define DQ RC0_bit
#define CLK RC1_bit
#define RST RC2_bit

unsigned int resp;
int rcv;

void clock() {
      CLK = ~CLK;
      Delay_ms(10);
}

int readSensor() {
     int cont = 0;
     RST = 0x00;
     CLK = 0;
     Delay_ms(30);
     resp = 0;

     while (cont <= 16) {
       clock();
       resp |= DQ;
       resp = resp<<1;
       clock();
       cont++;
     }
 
     RST = 0x01;
     Delay_ms(30);
     return resp;
}

void writeSensor(char ch) {
     int cont = 0;
     RST = 0x00;
     CLK = 0;
     Delay_ms(30);
     resp = 0;

     while (cont <= 16) {
       DQ = ch & 0b00000001;
       clock();
       ch = ch>>1;
       clock();
       cont++;
     }

     RST = 0x01;
     Delay_ms(30);
     RST = 0x00;
     Delay_ms(30);
     RST = 0x01;
     Delay_ms(30);
}

void main() {

    UART1_Init(9600);               // Initialize UART module at 9600 bps

    UART1_Write_Text("Start");  UART1_Write(10);  UART1_Write(13);

    TRISC = 0b00000000;
    PORTC = 0b00000100;
 
    writeSensor(0xAA);
    Delay_ms(30);
    rcv = readSensor();
 
    Delay_ms(100);                  // Wait for UART module to stabilize

    UART1_Write(rcv);
 
}

Here are the circuit in Proteus:

enter image description here

I am new in bit banging and PIC, but I have some experience with C language.

May you say (or do some approach) what am I missing in order to read the temperature of this thermometer?

\$\endgroup\$
3
  • \$\begingroup\$ "resp = 0;" How do you know resp is 16 bit wide? \$\endgroup\$
    – Rodo
    Jun 10 at 20:32
  • \$\begingroup\$ because unsigned int have 2 or 4 bytes \$\endgroup\$
    – Guilherme
    Jun 10 at 20:43
  • \$\begingroup\$ Maybe I'm missing something, but why are you resetting it after you write the command? It looks like in the datasheet they say to write the command and then clock out the response, no reset in between. \$\endgroup\$
    – Aaron
    Jun 10 at 21:04
1
\$\begingroup\$

I'm not totally familiar with the PIC, but after looking at the code for a DS1820 Arduino library:

https://github.com/msparks/arduino-ds1620/blob/master/DS1620.cpp

I would make sure you are changing the DQ pin to an input when reading and an output when writing.

The other thing I noticed is that the Arduino library follows this procedure for reading each bit:

  1. Bring the clock line LOW
  2. Read the DQ pin
  3. Bring the clock line HIGH

And your code does:

  1. Begin with CLK = 0
  2. Toggle CLK
  3. Read DQ
  4. Toggle CLK
  5. Goto 2

So you are reading DQ when CLK is high.

There's probably no need for such a large delay in your clock() routine.

\$\endgroup\$
3
  • \$\begingroup\$ It's some good observations, but it still seems like in infinite loop when I run in Proteus. \$\endgroup\$
    – Guilherme
    Jun 10 at 19:36
  • \$\begingroup\$ Since you're using a simulator you should be able to tell where it getting stuck. Update your question with ... "this code is getting stuck in (name of routine)" \$\endgroup\$
    – ErikR
    Jun 10 at 19:45
  • \$\begingroup\$ It wasn't an infinite loop. I saw what it was. Just an external clock signal that I had forgotten to take out. Jajaja. By the way, I am not getting to use the Proteus debugger. I don't know why. I load the .cof file but my source code do not appear in the Debug > PIC CPU > "PIC CPU Source Code" window. \$\endgroup\$
    – Guilherme
    Jun 10 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.