Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

MikroC code using SPI Communication

Status
Not open for further replies.

daggerdoi

Newbie level 5
Joined
Mar 27, 2008
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,329
mikroc spi

can anyone help me make a MikroC code for communicating PIC16f877 and SA9904b(an Energy Meter IC) using SPI Communication.

i want to have a code for accessing a 10-bit register address..

i badly need ur help coz im having problems understanding MikroC language.. Coz im just a neophyte in making microcontroller related projects..

plss help me.. please. . .

thanks...
 

spi communication

This should work, according to my understanding of chip operation.
Code:
#define CS PORTC.F0 // just an example
long read_reg(char adr)
{
	union char_long {
		long l,
		char c[4]
	} result;

	CS = 1;
  Spi_Write(1);
  Spi_Write(adr | 0x80);
  result.c[2]=Spi_Read(0);
  result.c[1]=Spi_Read(0);
  result.c[0]=Spi_Read(0);
	CS = 0;
	// sign extension of negative register values
	if (result.c[2] & 0x80)
	  result.c[3]= 0xff;
	return result.l;
}

// CKP = 0, CKE =1, SMP = 0, SSPM = 0010
// MASTER_OSC_DIV16 may also work, depending on clock frequency
Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, HIGH_2_LOW);
TRISC.F0 = 0; // Init CS output
CS = 0;
Hardware SPI interface must be used, cause mikroC Software SPI has wrong clock phase. Documentation regarding SPI communication has drawing errors, so I hope to understand it correct.
 
spi mikroc

thank you so much sir.. this would help a lot..

is this code used for accessing 10-bit register?
 

spi_read mikroc

This code is for reading SA9904 24-Bit data registers as documented. According to the datasheet, the chip has no 10 bit registers. It has 9 bits of address input, that is written as 16 bit zero-padded, as suggested.
 

    daggerdoi

    Points: 2
    Helpful Answer Positive Rating
mikroc

Hi FvM..

Im doing a similar project with the CS5461A energy IC from cirrus logic. I need to read the data registers of this IC using SPI. Im using PIC18F4580 with the MikroC compiler.

I was wondering if the code you posted could be used for this project. I am trying to modify your code for my application but I dont quite understand all of your code. What does the line Spi_Write(adr | 0x80); do? and why are you reading the registers three times ?

I would appreciate any help that you can give

thank you
 

Re: spi communication

I also have a similar kind of problem. i.e. to send data from MAX1132 16bit ADC Chip to PIC 16F877A using SPI communication. can you please send me a sample code since i'm new to this. Thanx
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top