0

I have some confusion about the #define thing in Arduino. Normally I use #define to select the pin I want to use. But here the code has lines like

#define LTC_CS 2
#define LTC_MISO 4
#define LTC_SCK 5

But on the Arduino Uno I connect pins 10, 12, and 13 respectively? What does it mean? Why do I not connect pins 2, 4, and 5? LTC 2400 You can check the connection from the figure. Please help I am stuck. I can not connect it to Arduino Leonardo. When I connect it to pins 10, 12, and 13, it is not working properly.

1 Answer 1

1

If you include the comments from the code you linked, it actually explains exactly what those #defines are doing.

#define LTC_CS 2         // LTC2400 Chip Select Pin  on Portb 2
#define LTC_MISO  4      // LTC2400 SDO Select Pin  on Portb 4
#define LTC_SCK  5       // LTC2400 SCK Select Pin  on Portb 5

This seems to explain that the physical IO pin corresponding to IO port B pin 2 of the Arduino shall connect to the CS (Chip Select) pin on the LTC2400, and similarly the other two pins connect to other named pins on the LTC2400.

The reason these numbers are different from the actual physical pin numbers is that they don't refer to the pin number, they refer to the position of the bit that controls the pin in the PORTB register. sbi(PORTB,LTC_CS) means "Set the 2nd bit of the PORTB register to 1" and will raise the pin to logic high.

#define itself is not code that runs on the Arduino; it is a preprocessor directive. What #define is doing here is defining constants. #define LTC_CS 2 means "In this code file, when I write LTC_CS, pretend I wrote 2 instead". Later these macros are used in the code like sbi(PORTB,LTC_CS);. In that function call you could instead write sbi(PORTB,2), and since LTC_CS has been defined with a value of 2, it means the same thing and compiles to the same code.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for help but I mean instead of #define LTC_SCK 5, why ı do not use #define LTC_SCK 13. On the board it looks like pin 13. Okey I check the mapping portb 5 is equal to 13. But why I do not write there 13 instead of 5 ?
@user2908225 Edited again.
I am using it as defined pins for a long time and it works fine but I just changed the number of pins as you say it is not working proporly ? it is unexpected ! but you can check the <a href="ebay.com/itm/…"> image </a> from the page it is uno also the code for the uno ?
As far as I can tell, that code should work if the LTC2400 is connected exactly as shown in the picture in the eBay listings. Double-check all your connections; you may have a bad LTC2400.
PORTB is probably automatically defined by the compiler since the address of PORTB is sort of a universal constant inherent to the hardware. If it wasn't, you'd be getting a compile-time error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.