eebhoi01
Advanced Member level 4
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.
PS: I replaced << and >> with ,, and .. respectively. I get errors after using <<, all text following this will be cut.
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.