DIRECT_MODE_OUTPUT in OneWire not working?

Status
Not open for further replies.

DrOldies

Active member
Using some of the OneWire Teensy3 code in my project, DIRECT_MODE_OUTPUT is not working. I'm using a Teensy 3.1 and OneWire from the Arduino 1.65 release, but the DIRECT_MODE_OUTPUT macro has not changed in quit a while. To test I extracted the macros into a simple sketch and tested against pinMode (pin, OUTPUT). Here is my test sketch. flag controls the 3 scenarios // 0 = digital write, 1 = OneWire w/DIRECT_MODE_OUTPUT, 2 = OneWire w/pinMode. Is it me or the macro?

Code:
/*
 * I extracted the Onewire code line for the Teensy3 and included them here. 
 * If pinMode OUTPUT is used in OneW, pin 13 lights the LED
 * DIRECT_MODE_OUTPUT the LED does not light
 */

#define flag 1      // 0 = digital write, 1 = OneWire w/DIRECT_MODE_OUTPUT, 2 = OneWire w/pinMode 
uint8_t pinA = 13;

//oneWire.h extracted to here
#define PIN_TO_BASEREG(pin)             (portOutputRegister(pin))
#define PIN_TO_BITMASK(pin)             (1)
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask)         (*((base)+512))
#define DIRECT_MODE_INPUT(base, mask)   (*((base)+640) = 0)
#define DIRECT_MODE_OUTPUT(base, mask)  (*((base)+640) = 1)
#define DIRECT_WRITE_LOW(base, mask)    (*((base)+256) = 1)
#define DIRECT_WRITE_HIGH(base, mask)   (*((base)+128) = 1)

IO_REG_TYPE bitmask;
volatile IO_REG_TYPE *baseReg;
IO_REG_TYPE mask = bitmask;

void setup() {
    baseReg = PIN_TO_BASEREG(pinA);
    bitmask = PIN_TO_BITMASK(pinA);
}

void loop() {

  #if flag == 1
    DIRECT_MODE_OUTPUT(baseReg, bitmask);     //1
    OneW();
  #elif flag == 2
    pinMode (pinA,OUTPUT);  //2
    OneW();
  #else
    dig();
  #endif
  
}

void dig(void){
  pinMode (pinA,OUTPUT);
  digitalWrite(pinA,HIGH);
  delay(2);
  digitalWrite(pinA,LOW);
}

void OneW(void){
    DIRECT_WRITE_HIGH(baseReg, bitmask);
    delay(500);
    DIRECT_WRITE_LOW(baseReg, bitmask);
    delay(500);
}
 
By default, pins on Teensy 3.1 are disabled. Those macros only work on pins after they've been configured. You can use pinMode() or you can write directly to the port config register.
 
Status
Not open for further replies.
Back
Top