Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

SPI comm with 16f877a

Status
Not open for further replies.

Zwilorg

Member level 5
Joined
Oct 13, 2010
Messages
89
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,906
Hello,

I am trying to connect a max6675 to the pic16f887.

The max6675 is a read only IC and it uses the SPI standarts.

Can someone give me an idea on how to start working with this?

Some code using SPI comm would be nice i would use it to my purpose


Best regards,
Zwi
 

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
Initialize the registers in your pic.

Be careful with the clock source you select in CFP and CFQ of the SSPCON and SSPSTAT registers. Because I made mistake when doing this with maxq3180. Be sure about the edge triggered or level trigerred clock.
 

Zwilorg

Member level 5
Joined
Oct 13, 2010
Messages
89
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,906
I have found some information regarding my problem on this thread:


HI-TECH Software Forums: MAX6675 SPI code not working on 16F917

But i have another problem in this subject they convert a 12bit reading from 6675... and in the 1st post i made a mistake i am trying to read a max31855 temperature wich is 14bit.


Code:
 SPI MAX6675
	RB0		- SPI_DATA - SO - Pin 7
	RD7		- SPI_CLOCK - SCK - Pin 5
	RD6		- SPI_CS - CS - Pin 6
			- T- (alumel) - Pin 2
			- T+ (chromel)- Pin 3
*/

#include 
#include 

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);

int i, j, k, n;                 //  Use Global Variables for Debug

//                         1234567890123456
const char TopMessage[] = "Current:  ";
const char BotMessage[] = "Setpoint: ";
const char Units[] = "F  ";

#define E  RA4                  //  Define the LCD Control Pins
#define RS RA5
#define SPI_CLOCK RD7			//  Define the Max6675 pins
#define SPI_CS RD6				
#define SPI_DATA RB2
#define Out RB5					//  Define the output pin to control the SSR

const int Twentyms = 1250;      //  Declare a Constants for delays
const int Fivems = 300;
const int TwoHundredus = 10;
int SetPoint = 240;				//  Initialize the SetPoint
static unsigned int	relay_timer;	// timer value for relay driver

int GetTemp(void)
{
	int CurrentTemp = 0;		//  Reset value of Current Temp
	SPI_CS = 0;					//  Force CS low to stop conversion
	
	SPI_CLOCK = 1;				//  discard bit 15
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 512;
	SPI_CLOCK = 1;				//  bit 14
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 256;
	SPI_CLOCK = 1;				//  bit 13
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 128;
	SPI_CLOCK = 1;				//  bit 12
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 64;
	SPI_CLOCK = 1;				//  bit 11
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 32;
	SPI_CLOCK = 1;				//  bit 10
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 16;
	SPI_CLOCK = 1;				//  bit 9
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 8;
	SPI_CLOCK = 1;				//  bit 8
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 4;
	SPI_CLOCK = 1;				//  bit 7
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 2;
	SPI_CLOCK = 1;				//  bit 6
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 1;
	SPI_CLOCK = 1;				//  bit 5
	SPI_CLOCK = 0;

	SPI_CLOCK = 1;				//  discard bits 4 and 3
	SPI_CLOCK = 0;				//  decimal values of temp
	SPI_CLOCK = 1;				
	SPI_CLOCK = 0;

	SPI_CLOCK = 1;				//  bit 2 -- fault bit
	SPI_CLOCK = 0;

	SPI_CLOCK = 1;				//  bit 1 -- always 0
	SPI_CLOCK = 0;

	SPI_CLOCK = 1;				//  bit 0 -- tri-state
	SPI_CLOCK = 0;

	SPI_CS = 1; 				// SPI bus is OFF

	return(CurrentTemp);
}

main()
{
	int CurrentTemp = 0;	

	//  Initialize the I/O ports
    PORTA = 0;          			//  Start with Everything Low
    CMCON0 = 7;         			//  Turn off Comparators
    ANSEL = 0;         				//  Turn off ADC
    TRISA = 0;          			//  All of PORTA are Outputs
	TRISB = 0b00011111;				// 	Port B bits 7, 6, 5, 4, 3 are output
	TRISD = 0; 
	T0IE = 1;						// Enable interrupt on TMR0 overflow
	INTEDG = 1;						// falling edge trigger the interrupt
	INTE = 1;						// enable the external interrupt
	GIE = 1;						// Global interrupt enable

	SetPoint = 240;					// Reset the Setpoint

	//  Initialize LCD
    j = Twentyms;
    for (i = 0; i < j; i++);    	//  Wait for LCD to Power Up
    PORTA = 3;                  	//  Start Initialization Process
    E = 1;  E = 0;              	//  Send Reset Command
    j = Fivems;
    for (i = 0; i < j; i++);
    E = 1;  E = 0;              	//  Repeat Reset Command
    j = TwoHundredus;
    for (i = 0; i < j; i++);
    E = 1;  E = 0;              	//  Repeat Reset Command Third Time
    j = TwoHundredus;
    for (i = 0; i < j; i++);
    PORTA = 2;                  	//  Initialize LCD 4 Bit Mode
    E = 1;  E = 0;
    j = TwoHundredus;
    for (i = 0; i < j; i++);

	//  Main control loop
	while (1 == 1)              	
	{	
    	j = Twentyms*10;					// Wait 200ms between loops
    	for (i = 0; i < j; i++);

		CurrentTemp = GetTemp() * 1.8 + 32;	// Get current temp and convert to F
	}
}  //  End Main

i see that the conversion is very easy to make in a 12bit and i am wondering if to a 14 bit i would only have to add this:


Code:
	CurrentTemp = CurrentTemp + SPI_DATA * 2048;
	SPI_CLOCK = 1;				//  bit 14
	SPI_CLOCK = 0;

	CurrentTemp = CurrentTemp + SPI_DATA * 1024;
	SPI_CLOCK = 1;				//  bit 13
	SPI_CLOCK = 0;

in the datasheet they say that from D[31:18] is the 14-bit thermocouple data.

i removed the LCD_data code lines because i am not interested in them... already got mine working in a different way. i only trying to save all the data in a variable like this fellow did on "CurrentTemp"

Hope someone helps! ^^
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top