Reading and Writing on MCP3561R ADC from Microchip

Hi ALL,
I am using Atmega328pb to read and write on MCP3561R ADCs but I get nothing
Here it is my read function for example:
long Read_MCP_Reg(byte Address, byte Address_reg, byte Command)
{
byte IRQ_Status = 0;
long Reg_val = 0;
byte uC_2_MCP = ((Address & 0x03) << 6 )|((Address_reg & 0x0F) << 2)|(Command & 0x03);
digitalWrite(RTS,HIGH);
RS485.print(uC_2_MCP, BIN);//serial.print(78, BIN) gives "1001110"
digitalWrite(RTS,LOW);

digitalWrite(CS,LOW);
delay(1);
// ******* Start ******* //
// **** Send command form uC to MCP3561 and receive the Status byte **** //
for(int i = 0; i < 7; i++)
{
digitalWrite(SDI,bitRead(uC_2_MCP,7-i)); // Device Latches SDI on Rising Edge (page70)
delayMicroseconds(20);
digitalWrite(SCLK, HIGH);
delayMicroseconds(20);
IRQ_Status = (IRQ_Status << 1) + digitalRead(SDO); // Device Latches SDO on Falling Edge (page70)
digitalWrite(SCLK,LOW);
delayMicroseconds(20);
}

digitalWrite(RTS,HIGH);
RS485.print(IRQ_Status, BIN);//serial.print(78, BIN) gives "1001110"
digitalWrite(RTS,LOW);
RS485_println("\n");
delay(1);
if(bitRead(IRQ_Status,4)!= bitRead(IRQ_Status,3))
{
// The command is verified
Verified_Cmd = true;
RS485_println("ACK_MCP+ \n");
if(bitRead(IRQ_Status,2))
{
Valid_ADC = true;
}
else
{
Valid_ADC = false;
RS485_println("ADC DATA is not available");
}
}
else
{
RS485_println("ACKMCPcmd--");
digitalWrite(CS,HIGH);
Valid_ADC = false;
Verified_Cmd = false;
}
if ((Address_reg == 0) || (Address_reg > 6 && Address_reg < 0xC))
{
for(int i = 0; i < 24; i++)
{
digitalWrite(SCLK, HIGH);
Reg_val = (Reg_val << 1) + digitalRead(SDO);
delayMicroseconds(50);
digitalWrite(SCLK,LOW);
delayMicroseconds(50);
}
Reg_val = (Reg_val ); //& 0x00FFFFFF
digitalWrite(RTS,HIGH);
RS485.print(Reg_val, HEX);//serial.print(78, BIN) gives "1001110"
delay(1);
digitalWrite(RTS,LOW);
digitalWrite(CS,HIGH);
return Reg_val;
}
else if((Address_reg > 0 && Address_reg < 7) || (Address_reg == 0xD)|| (Address_reg == 0xC))
{
for(int i = 0; i < 7; i++)
{
digitalWrite(SCLK, HIGH);
Reg_val = (Reg_val << 1) + digitalRead(SDO);
delayMicroseconds(50);
digitalWrite(SCLK,LOW);
delayMicroseconds(50);
}
digitalWrite(RTS,HIGH);
Reg_val = (Reg_val );// & 0x000000FF
RS485.print(Reg_val, HEX);//serial.print(78, BIN) gives "1001110"
delay(1);
digitalWrite(RTS,LOW);
digitalWrite(CS,HIGH);
return Reg_val;
}
else if ((Address_reg == 0xE) || (Address_reg == 0xF))
{
for(int i = 0; i < 16; i++)
{
digitalWrite(SCLK, HIGH);
Reg_val = (Reg_val << 1) + digitalRead(SDO);
delayMicroseconds(50);
digitalWrite(SCLK,LOW);
delayMicroseconds(50);
}
Reg_val = (Reg_val);// & 0x0000FFFF
digitalWrite(RTS,HIGH);
RS485.print(Reg_val, HEX);//serial.print(78, BIN) gives "1001110"
delay(1);
digitalWrite(RTS,LOW);
digitalWrite(CS,HIGH);
return Reg_val;
}
else
{
RS485_println("Wrong Address to MCP");
digitalWrite(CS,LOW);
return 0;
}
}

Thanks and regards

Edit your post and insert code tags!

That chip uses SPI for the MCU communication. Why do you use an async serial channel for the communication? If you think RS485 is the correct interface why do you set RTS (an RS232 signal)?

Post complete code, the error is usually in the parts of the code that people are hiding from us.

I'm missing a wiring diagram in your post.

Welcome to the forum.

Can you please modify your original post to ensure the source code is in a single box? It should look like this.

// Your example source code

You need three ' at the beginning and end in the edit window? When you click on this icon </> you get.

```
type or paste code here
```

This ensures we can get all the source code.

The Arduino IDE can format your code. Tools -> Auto Format
This help readability and sometimes shows you errors that are not C/C++ errors.

If you use any libraries, please let us know which ones. Provide a link to GitHub if they are not available in the library manager.

Hi,
I figured out the error.
The error is in this part

the counter i should be (i <8) to complete sending command to MCU.

The chip use SPI is correct. but i am using RS485 protocol to interface with MCU So RTS is the pin configuration to select between RS485 Transmit Or Receive.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.