Hi Guys
Hope someone can help be out here, I'm going nowhere after a couple of days debugging.
I have an Atmega328p on a custom board with an external clock running at 20MHz
On the board there are two SPI devices, namely MAX7221 and an MCP4132 (rheostat). The board is controlled via TWI
Each slave has their own SS pin on the 328p
All is working fine except the MCP4132.
If I write to the MCP first, it works as do subsequent writes to the MAX. But after writing to the MAX, the MCP doesn't respond anymore. Slowing down the commands doesn't help.
Below is my setup
// Ports for Atmega328P
#define PIN_SCK PORTB5
#define PIN_MOSI PORTB3
#define PIN_SS PORTB2
#define PIN_SSW PORTC1 // Slave select for MCP
//Init SPI interface
// SCK MOSI CS/LOAD/SS as OUTPUT
DDRB |= (1 << PIN_SCK) | (1 << PIN_MOSI) | (1 << PIN_SS);
//Setup SS for MCP as output
DDRC |= (1 << PIN_SSW);
------------------------------------------------
//Write out data for MCP
PORTC &= ~(1 << PIN_SSW); // Start transaction
spiSendByte(0x00); // function
spiSendByte(data); //Set position
PORTC |= (1 << PIN_SSW); //End transaction
--------------------------------------------
// Write out SPI data for MAXPORTB &= ~(1 << PIN_SS)
spiSendByte(0x00); // function
spiSendByte(data); //Set position
PORTB |= (1 << PIN_SS)
void spiSendByte (char databyte)
{
// Copy data into the SPI data register
SPDR = databyte;
// Wait until transfer is complete
while (!(SPSR & (1 << SPIF)));
}
Any pointers as to where I'm going wrong would be appreciated. It's almost as if the SS pin on the MCP isn't working as it should
regards