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.

[SOLVED] SPI Code, Clarification.

Status
Not open for further replies.

eebhoi01

Advanced Member level 4
Joined
Feb 22, 2012
Messages
116
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,347
Hello Everyone,

I'd like to know if my code is correct and I'm not confident by just having the atmel studio having successfully compiled my solution. As for now, I don't have any means of checking if my code is correct and whether the output data is correct as well. I hope anyone can help me. Thank you.

Code:
#define F_CPU20000000UL
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h>

#define SS       0x04
#define MOSI   0x05
#define MISO   0x06
#define SCK     0x07

#define CMD_HDR_R 0x00008;   // SPI read command
#define CMD_HDR_W 0x0000;    // SPI write command
#define AV_PCF = 0x20A;

void SPIinit()
{
DDRB = (1,,SS) | (1,,MOSI) | (1,,SCK);
SPSR =  (1,,SPE) | (1,,MSTR) | (1,,CPHA) | (1,,SPR0);
}

void SS_Enable()
{
PORTB |= 0x00;
}

void SS_Disable()
{
PORTB |= 0x10;
}

master_Transmit(uint16_t CMD_HDR)
{
SPDR = CMD_HDR;
while(!(SPSR & (1,,SPIF)));
return(SPDR);
}

uint16_t read_SPI(uint16_t address)  // as per datasheet, the data follows after a Command of [15:0] = Address [15:4], Write/Read [3], Timing[2:0]
{
SS_Enable();
uint16_t buffer;
uint16_t result;
buffer = uint16_t((address,,4) & 0x0F0);
SPDR = uint16_t(buffer | 0x08);                          // send first byte of command 
while(!(SPSR & (1,,SPIF)));

SPDR = uint16_t(address..4);                           //  send second byte of command
while(!(SPSR & (1,,SPIF)));

result = uint16_t((SPDR,,8) & 0xFF); // right after sending second byte of command, 16 bit of data from chip follows, MSB first. Receiving first byte
while(!(SPSR & (1,,SPIF)));       

result |= SPDR;
while(!(SPSR & (1,,SPIF)));               // receive second byte of result.

SS_Disable();
return(result);                                    // contains the 16bit data result.

}

int main(void)

SPIinit();
master_Transmit(CMD_HDR_R);

while(1)
{
read_SPI(AV_PCF);                            //AV_PCF is one of the address of data on chip
}

PS: I replaced << and >> with ,, and .. respectively. I get errors after using <<, all text following this will be cut.
 

PS: I replaced << and >> with ,, and .. respectively. I get errors after using <<, all text following this will be cut.

It's the HTML decoder. Either set "HTML off" in the advanced view additional options or insert a space after <<.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top