Transform Arduino Code into C++ with Atmel Studio

I want to use a RTC from Epson, Model: R4543. I found some Arduino librarys and I want to transform them into Microchip Studio. The original Arduino Code is:

digitalWrite(_dataPin,(data >> i) & 0x01);

and I transformed it in:

PORTC |= (((data>>i)&0x01)<<_dataPin);

is this correct? It does not work, and I'm trying to find the problem. The type of data is uint8_t. Thank you, Markus


It isn't correct. There is wrong two things.

  • digitalWrite setup pin to value 0 or 1. PORTC |= can set pin value only to 1, not to 0 if pin value is 1.
  • _dataPin value can by more then 7. DigitalWrite make transformation to port address and bit position. You can make manual transformation like this. And than set pin to 0 or 1 by simple if construction. Assigned values for DATA_PORT and DATA_PIN is illustrative only. You do not provide value of _dataPin variable.
#define DATA_PORT PORTC //maybe not right port name
#define DATA_PIN 0      //maybe not right pin number
if ((data >> i) & 0x01) {
  DATA_PORT |= (1<<DATA_PIN);
} else {
  DATA_PORT &= ~(1<<DATA_PIN);
}