Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top