View previous topic :: View next topic |
Author |
Message |
halibatsuiba
Joined: 12 Aug 2009 Posts: 30
|
MAX6675 and 18F2580, temp always zero. Why? |
Posted: Sun Aug 08, 2010 12:40 pm |
|
|
I took MAX6675 codes from this thread http://ccsinfo.com/forum/viewtopic.php?t=21676&highlight=max6675 and added just a simple lcd printout.
Connection is like this:http://2.bp.blogspot.com/_CvxN91KM3Rc/TASwrd-qn3I/AAAAAAAAApI/27K1IPY9pss/s1600/Microcontroller+PIC+thermocoupler+interface.JPG except CS, it is now A1.
Circuit uses internal 4MHz oscillator. I tried also with external 20MHz crystal.
Problem is: 6675 returns 0 (zero).
I can't figure out what the problem is, any ideas?
Here is complete code:
Code: |
#include <18F2580.h>
#fuses INTRC, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP
#use delay(clock=4000000)
#include<flex_lcd.c>
#include<max6675.c>
#define MAX_CS PIN_A1
#define MAX_DO PIN_C4
#define MAX_CLK PIN_C3
void init_temp()
{
output_low(MAX_CLK);
output_low(MAX_DO);
output_low(MAX_CS);
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
output_high(MAX_CS);
}
int16 read_temp()
{
BYTE datah, datal=0;
int16 data=0;
output_low(MAX_CS);
delay_cycles(1);
datah=SPI_READ(0);
datal=SPI_READ(0);
output_high(MAX_CS);
if( bit_test(datal,2))
{
bit_set(data,15);
return(data);
}
data = datah<<8;
data = data | datal;
return(data);
}
void main()
{
int16 temp;
setup_oscillator(OSC_4MHZ);
lcd_init();
init_temp();
while (1)
{
temp = read_temp();
lcd_gotoxy(1,1);
printf(lcd_putc,"TEMP: %x ", temp);
delay_ms(500);
}
}
|
|
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Aug 08, 2010 12:51 pm |
|
|
In the INIT, I don't think you need the OUTPUT_LOW statements for SDO and SCK lines. Enabling the SPI module negates the TRIS states for those pins (although it's best to check the datasheet as I think some PIC's are different)
Otherwise, do you have a scope to verify you are seeing any clocking/data on the SPI lines?
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
halibatsuiba
Joined: 12 Aug 2009 Posts: 30
|
|
Posted: Sun Aug 08, 2010 2:35 pm |
|
|
PCM programmer wrote: |
See Figure 1b in the Max6675 data sheet. It shows the data changes
on the falling edge of SCLK. Therefore, it's sampled on the rising edge.
Also, it shows SCLK idles at a low level. Figure 1b is on page 6: |
But in page 5 section "Serial interface" datasheet says "Read the 16 output bits on the falling edge of the clock."?
Clock is idle low and data is read on falling edge, that is mode 1.
bkamen wrote: |
Otherwise, do you have a scope to verify you are seeing any clocking/data on the SPI lines? |
Yes, I have an old HP16500b but unfortunately it is in garage and without thinking too much I powered it up and after a while it died. Too hot in here, over 100F out there and at least 115F in my garage. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Aug 08, 2010 3:09 pm |
|
|
halibatsuiba wrote: |
Yes, I have an old HP16500b but unfortunately it is in garage and without thinking too much I powered it up and after a while it died. Too hot in here, over 100F out there and at least 115F in my garage. |
Tsk tsk. That's a nice logic analyzer.
I have an even older hp1660cs that works great -- because it's cared for like a piece of test instrumentation.
I realize "garage" can be interpreted like most of us think of a garage.. I actually work out of mine and before moving all my equipment in, I cleaned it up, epoxied the floor and the upstairs is like a dry-bar turned office space. So my garage is more like an office with a shop now. The door is almost never opened and it essentiall fully funished/insulated minus the running water/bathroom. So I realize your garage might be like mine.
Sounds like maybe it's time for an A/C....
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 08, 2010 3:11 pm |
|
|
That's true, but to me, the timing diagram shows mode 0.
Now I'm not sure what mode they really use. Maybe keep it
at Mode 1 for a while.
1. What is the Vdd voltage of the PIC, and the Max6675 ? Are they
running at different Vdd voltages ?
2. Post a list of the connections between the PIC and the Max6675. |
|
|
halibatsuiba
Joined: 12 Aug 2009 Posts: 30
|
|
Posted: Sun Aug 08, 2010 3:20 pm |
|
|
PCM programmer wrote: | That's true, but to me, the timing diagram shows mode 0.
Now I'm not sure what mode they really use. Maybe keep it
at Mode 1 for a while.
1. What is the Vdd voltage of the PIC, and the Max6675 ? Are they
running at different Vdd voltages ?
2. Post a list of the connections between the PIC and the Max6675. |
PIC-------------------MAX6675
RA1(3)--------------- /CS (6)
RC3/SCK(14)-------SCK (5)
RC4/SDI(15)--------SO(7)
Both are running the same Vdd: 5.01V.
Thermocouple negative is connected to MAX6675s GND and there are filter capacitors in power pins as specified in datasheets. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Aug 08, 2010 3:43 pm |
|
|
halibatsuiba wrote: |
Thermocouple negative is connected to MAX6675s GND and there are filter capacitors in power pins as specified in datasheets. |
Uhhh, it's supposed to be connected to 'T-' as well.
(pin 2)
Please verify that connection for us.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
halibatsuiba
Joined: 12 Aug 2009 Posts: 30
|
|
Posted: Sun Aug 08, 2010 3:47 pm |
|
|
bkamen wrote: | halibatsuiba wrote: |
Thermocouple negative is connected to MAX6675s GND and there are filter capacitors in power pins as specified in datasheets. |
Uhhh, it's supposed to be connected to 'T-' as well.
(pin 2)
Please verify that connection for us.
-Ben |
Ah, sorry about confusion.
That should be: "Thermocouple negative is connected also to MAX6675 GND as specified in typical application circuit diagram in datasheet".
It is conncted to both T- and GND. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Aug 08, 2010 3:48 pm |
|
|
I think they are very unclear about what mode to use. The sheet lists as one of the timing parameters 'serial clock fall to output data valid', which would make no sense at all if you are samplng on the falling edge of the clock...
If you look online, the Stamp examples talking to this chip, use MSBPRE', which implies Mode0, not Mode1.
So I think given Mode1 is not working, I'd try Mode0, and see if this works....
Best Wishes |
|
|
halibatsuiba
Joined: 12 Aug 2009 Posts: 30
|
|
Posted: Sun Aug 08, 2010 3:56 pm |
|
|
Ttelmah wrote: | I think they are very unclear about what mode to use. The sheet lists as one of the timing parameters 'serial clock fall to output data valid', which would make no sense at all if you are samplng on the falling edge of the clock...
If you look online, the Stamp examples talking to this chip, use MSBPRE', which implies Mode0, not Mode1.
So I think given Mode1 is not working, I'd try Mode0, and see if this works....
Best Wishes |
For the sake of it I have tried all of these (these are from post http://www.ccsinfo.com/forum/viewtopic.php?t=42270&start=1 )
None of them work.
Code: |
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
|
I wonder if I am doing something wrong with this:
Code: | setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16); |
CCS documentation is quite vague in some points. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Aug 08, 2010 3:57 pm |
|
|
I have found another example, doing it in assembler, and this is again using Mode0.
I think the 'comment' in the data sheet is simply wrong, and the diagram is right. It appears the data bits go valid, 100nSec _after_ the clock falling edge, with the first bit (dummy anyway), being output 100nSec after the CS line goes low (without needing a clock at all).
It looks like the data sheet line should be something like 'read the 16 bits after the falling edge of the clock', rather than 'on'.....
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 08, 2010 4:04 pm |
|
|
Quote: | void main()
{
int16 temp;
.
.
.
printf(lcd_putc,"TEMP: %x ", temp);
|
I noticed another problem in your program. You're trying to display
an 'int16' with "%x". That's not correct. You must use "%lx". That's
a lower-case L in front of the x. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sun Aug 08, 2010 4:10 pm |
|
|
I'd really have to say you have a problem in your hardware. Probably something like a little solder 'whisker' shorting one of the lines. You need to monitor the clock line, and verify that a clock does appear on this. Then check the CS, does go low, and high, then monitor the SDI line, and verify that this also changes. The 'most likely' fault is a poor connection, or a whisker, so the data line is not going high.....
Best Wishes |
|
|
|