B4R Question Read/write Arduino ports 8-bits at a time?

paulc91316

Member
Licensed User
Longtime User
Hi Erel,

I have some Arduino code that I'd like to port into B4R without using inline or have to build a library wrapper for it. It reads and writes to the Arduino ports in bytes. I looked in the forums and have not found an equivalent or I'm just doing the wrong search term. Here is the Arduino code:
B4X:
// *** Hardware specific defines ***
#define clear_port_bit(reg, bitmask) *reg &= ~bitmask
#define set_port_bit(reg, bitmask) *reg |= bitmask
#define pulse_high(reg, bitmask) clear_port_bit(reg, bitmask); set_port_bit(reg, bitmask);
#define pulse_low(reg, bitmask) set_port_bit(reg, bitmask); clear_port_bit(reg, bitmask);
#define clear_port(port, data) port &= data
#define set_port(port, data) port |= data

// *** Set Data Direction ***
    DDRC |= 0x40;
    DDRD |= 0x9F;
    DDRE |= 0x40;

// *** Set/Clear Port Data ***
    clear_port(PORTC, 0xBF);        // -->> PORTC &= 0xBF;
    clear_port(PORTD, 0x60);        // -->> PORTD &= 0x60;
    clear_port(PORTE, 0xBF);        // -->> PORTE &= 0xBF;
 
    set_port(PORTC, 0x0F);        // -->> PORTC |= 0x0F;
    set_port(PORTD, 0X40);        // -->> PORTD |= 0x40;
    set_port(PORTE, 0xB0);        // -->> PORTE |= 0xB0;

Is there an equivalent code structure (not using bits) in B4R?

I looked into using an HC595 and a SPI port in the future but the hardware is already built and working so I wanted to make the least amount of H/W changes.

Thanks,
Paul
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please don't limit your questions to a single member.

You will need to use inline C for this.
B4X:
Sub Process_Globals
   Public Serial1 As Serial
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   RunNative("init", Null)
   RunNative("ClearPortC", 0xBF)
End Sub


#if C
#define clear_port_bit(reg, bitmask) *reg &= ~bitmask
#define set_port_bit(reg, bitmask) *reg |= bitmask
#define pulse_high(reg, bitmask) clear_port_bit(reg, bitmask); set_port_bit(reg, bitmask);
#define pulse_low(reg, bitmask) set_port_bit(reg, bitmask); clear_port_bit(reg, bitmask);
#define clear_port(port, data) port &= data
#define set_port(port, data) port |= data

void init (B4R::Object* o) {
   DDRC |= 0x40;
   DDRD |= 0x9F;
   DDRE |= 0x40;
}
void ClearPortC (B4R::Object* o) {
  clear_port(PORTC, o->toULong());
}
void SetPortC (B4R::Object* o) {
  set_port(PORTC, o->toULong());
}
void ClearPortD (B4R::Object* o) {
  clear_port(PORTD, o->toULong());
}
void SetPortD (B4R::Object* o) {
  set_port(PORTD, o->toULong());
}
void ClearPortE (B4R::Object* o) {
  clear_port(PORTE, o->toULong());
}
void SetPortE (B4R::Object* o) {
  set_port(PORTE, o->toULong());
}
 
Upvote 0
Top