NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Trying to use SPI interface with Maxim 6675 - can't get SCK to output on PORTB2

July 22, 2009
by smilingjack
smilingjack's Avatar

I'm trying to set up the native SPI function on the Atmega168 so I can monitor one of the new Maxim 6675 thermocouple read chips.

I've checked my DDRB and it is 0b00101100 so SS, MOSI, and SCK are set to output like the data sheet says. I've also checked my SPCR and it is 0b01011111 so SPI is enabled and Master is set along with cpol, cpha, spr1 and spr0. I set spr1 and spr0 to 1 (freq/128) so I should be able to see the shift clock on my ole 60 mHz scope. I've also tried all combinations of cpol and cpha but no luck

When I set the SS low, wait a little and then load a char into the SPDR the SCK should send out 8 pulses but it doesn't - and I know it doesn't 'cause I'm watching on a 'scope.

Anyway here's the code. I'm pretty new at C. I'm hoping a fresh pair of eyss might save my older one's

#define F_CPU 14745600
#include <stdio.h>
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

void SPI_MasterInit(void)
{
/* Set MOSI and SCK and SS output, all others input */
DDRB= (1<<DDB2)|(1<<DDB3)|(1<<DDB5);
/* Enable SPI, Master, set clock rate fck/128 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1)|(1<<CPHA)|(1<<CPOL);

printf_P(PSTR("\r\n"));
printf_P(PSTR("%x, DDRB  %x,  SPCR \r\n"),DDRB, SPCR);
}

void SPI_MasterTransmit()
{
unsigned short tmp0=0, tmp1=0;

/* Start transmission */

SPDR = 0xFF;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));

tmp0=SPDR;

SPDR = 0xFF;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));

tmp1=SPDR;
printf_P(PSTR("%x  DDBB  %x   SPCR \r\n"),DDRB, SPCR);
printf_P(PSTR("%x first byte %x  second byte \r\n"), tmp0,tmp1);
}

int main() {

// start up the serial port
uart_init();
FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
stdin = stdout = &uart_stream;
SPI_MasterInit();

while(1) {
PORTB &= ~(1<<PORTB2); //pull SS low
delay_us(50);
SPI_MasterTransmit();
PORTB |= (1<<PORTB2);  //set SS high
}

return 0;
}
July 23, 2009
by smilingjack
smilingjack's Avatar

Update:

The reason I couldn't see the SCK pulses was that one o'scope channel was connected to ground but ....

Then I finally solved the real problem. What is not mentioned in the Maxim 6675 documentation is that the chip takes almost 200 milli-sec to finish a conversion -- so one must wait at least this time - after raising SS - before lowering it, otherwise one will get get a reading but it is garbage.

On a side note for other noobe's, I also found what clears the SPIF bit in the SPSR register after a byte transfer. It takes two actions. One is accessing the SPSR and, the other, is loading a new byte into the SPDR. Until I found this, I couldn't figure out why the !(SPSR & (1<<SPIF)) could signal the end of consecutive bytes of data transfer. It pays to read the documentation.

October 27, 2009
by Keith726
Keith726's Avatar

smilingjack,

I'm a newb also. One question: I see that the Maxim 6675 chip is packaged as surface-mount SOIC, not the DIP through-hole pins that we use with the Nerdkit. How do you connect SOIC chips to your board? Is there an adapter that I can get? Please tell me the secret. I would love to use that chip, and may others that are only available as surface-mount, but I don't know how.

Keith726

October 27, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Keith726,

I'm not sure what smilingjack is using, but in the past I've used little adapter boards like this one from the Surfboards line. You can see the entire product line for various sizes of surface mount ICs. Be very sure to check the dimensions against the chip datasheet before you buy! Particularly the spacing between two adjacent pins, and the spacing between the two rows of pins.

Mike

October 28, 2009
by Keith726
Keith726's Avatar

Excellent! - thank you Mike.

Keith726

October 28, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

I should also mention that if you haven't soldered little surface mount stuff before (or even if you have), buy several extras of both the IC and the adapter boards. You may need them! I remember once soldering some little 8-pin and 10-pin ADCs and DACs, and I needed to get 2 of each working. I think it took ~4 of each, and ~4 of each board to end up with 2 working ones. Especially if you haven't done it before, it's really easy to bridge adjacent pins, or overheat the boards and make the copper peel off, etc.

mike tiny smt soldering

Mike

October 31, 2009
by Keith726
Keith726's Avatar

Thank you Mike - you guys are the best! This will solve many problems for me (plus, as you've described, create a few new ones, but what the heck!)

Keith726

Post a Reply

Please log in to post a reply.

Did you know that a NerdKit can be used to build an iPhone-controlled R/C car? Learn more...