View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
DS1620 |
Posted: Fri Mar 24, 2006 8:29 am |
|
|
Hi everyone,
I am having some difficulty and i was hoping someone may be able to help.
I am reading a temperature using a DS1620 chip and the PIC16f877 - i am using rs232 to output the value and viewing it using hyperterminal - the output i am getting is quite riduculous - it is saying the temperature is 511???
If anyone can offer any advice this would be great beacause this problem has me stumped!!
My code is listed below:
#include <16f877.h>
#device ADC=10
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
# use delay (clock = 4000000)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7,parity=N,stream=PC)
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP
#byte PORTC = 7
#byte PORTA = 5
#bit DQ = PORTA.3
#bit CLK = PORTA.2
#bit RST = PORTA.1
void DS1620_write(unsigned int Data)
{
unsigned int i;
set_tris_a(0xF1); /*Set TRISA to Pattern 11110001 */
CLK = 1;
for (i=1; i<=8; i++)
{
DQ = (Data & 1);
CLK = 0;
CLK = 1;
Data = Data >> 1;
}
}
long DS1620_read(void)
{
long Data;
long Temp;
int i;
set_tris_a(0xF2); /*Set TRISA to Pattern 11110010 */
CLK = 1;
Data = 0;
Temp = 1;
for (i=1; i<=9; i++)
{
CLK = 0;
if (DQ == 1) Data += Temp;
Temp = Temp * 2;
CLK = 1;
}
RST = 0;
return(Data);
}
void main()
{
while(1)
{
long temperature = 0;
setup_adc_ports(NO_ANALOGS);
RST = 0;
delay_us(10);
RST = 1;
DS1620_write(0xEE);
delay_ms(750);
DS1620_write(0xAA);
temperature = DS1620_read();
delay_ms(750);
fprintf(PC, "\n\rTemperature = %ld", temperature);
delay_ms(5000);
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Mar 24, 2006 3:50 pm |
|
|
I don't know the DS1620 but I had a quick look at your code and noticed some oddities.
Code: | long DS1620_read(void)
{
long Data;
long Temp;
int i;
set_tris_a(0xF2); /*Set TRISA to Pattern 11110010 */
CLK = 1;
Data = 0;
Temp = 1;
for (i=1; i<=9; i++)
{
CLK = 0;
if (DQ == 1) Data += Temp;
Temp = Temp * 2;
CLK = 1;
}
RST = 0;
return(Data);
} |
1) By calling set_tris_a(0xF2) you make the RST line an input, but at the end of the function you set RST=0 which suggests you want it to be an output. In DS1620_write() you use the value 0xF1 which is more likely what you intended.
2) You don't have a set_tris_a() at the start of your main so there is a good chance your reset toggle isn't working. You only need to call set_tris_a() once in your program, calling it multiple times only makes it more difficult to maintain your program (see the error of point 1)
3) Your direction of shifting bits in is the inverse of the direction you are shifting bits out in the DS1620_write function. Most likely the direction used in DS1620 is the correct direction.
4) You set RST-0 at the end of the function but you didn't set it 1 to start with.
This was just to start with, very likely there are more issues. I suggest you give your code another serious inspection. |
|
|
Guest Guest
|
|
Posted: Mon Mar 27, 2006 11:14 am |
|
|
Many Thanks- have got this up and running. |
|
|
|