0

This is for a class in which we are not allowed to use the Arduino Library (Not my choice or preference). I know to use DDRB for the B pins. This is the code that I have so far:

//Global pointers
unsigned char *portDDRB = (unsigned char *) 0x24;
unsigned char *portDataB = (unsigned char *) 0x25;
unsigned char *portPinB = (unsigned char *) 0x23;

void setup () {
  //For the led output I know that I would do this
  *portDDRB |= 0x80;
}

void loop () {
   //And here the code would be like this to light up the light:
   if (condition to turn on light) {
     *portDataB |= 0x80;
   }
   // To Turn off the LED:
   if (condition to turn off the light) {
     *portDataB &= 0x7F
   }
}

I would like for the light to turn on when the input of PB4 is HIGH (from a button press). I just am not sure on what to put in the condition statements or the setup functions.

When I set the DDRB to 0x80, is that just making the PB7 an output and the rest as input?

I am using the Arduino Mega ATmega2560 btw.

From the comments I have this:

//Global pointers
unsigned char *portDDRB = (unsigned char *) 0x24;
unsigned char *portDataB = (unsigned char *) 0x25;
unsigned char *portPinB = (unsigned char *) 0x23;

void setup () {
  //For the led output I know that I would do this
  *portDDRB |= 0x80;
}

void loop () {
   //And here the code would be like this to light up the light:
   if (portPinB & 0x10) {
     *portDataB |= 0x80;
   }
   // To Turn off the LED:
   else {
     *portDataB &= 0x7F
   }
}
  • You check if the PB4 bit is set in the PINB register, by bitwise and-ing it with all zeros except for the bit you are interrested in. So if( PINB & _BV(PINB4) ) or if( PINB & (1<<PINB4) ). PS by using setup and loop you are still using the Arduino library. PS2 you need to change DATAB to turn the led on, not DDRB – Gerben Oct 5 '16 at 8:35
  • I guess those are the only two we get to use. What do I initialize PINB and PINB4 to? As for the DataB, thats what I have, just tired. – kingcobra1986 Oct 5 '16 at 8:38
  • PINB is (kind of) an output register. You can only read from it. It reports the HIGH and LOW voltages on the B-register's pins. – Gerben Oct 5 '16 at 8:45
  • I added to my Question does that look right? Is PINB included in the Arduino Library? If you make it an answer, you'll get the upvote btw. – kingcobra1986 Oct 5 '16 at 8:47
  • yes, except you need to change the |= to =, or the bit will only be set, and never cleared. – Gerben Oct 5 '16 at 8:58
1

Assuming the button is connected between GND and pin 10, without any pull-ups or pull-downs.

void setup () {
    //For the led output I know that I would do this
    DDRB = 0;             // set all pins on port B to INPUT
    DDRB |= _BV(DDB7);   // set PB7 to OUTPUT
    PORTB = 0;            // set all pins on port B to LOW
    PORTB |= _BV(PORTB4); // enable the pull-up resistor on PB4
}

void loop () {
    if( PINB & _BV(PINB4) )
    {
        // if bit PINB4 is set, the button is not pressed
        PORTB &= ~_BV(PORTB7); // turn off led
    }
    else
    {
        PORTB |= _BV(PORTB7); // turn off led
    }
}
  • Wait which |= were you telling me to change to =. Also I am good to use my portPinB in place of your PINB right? – kingcobra1986 Oct 5 '16 at 9:11
  • You can use portPinB, but why would you? – Gerben Oct 5 '16 at 9:35
  • The professor's "Blinky" example used it. So I figured that I would need to use it too. I'd rather be safe. He stated that if we use anything from the arduino library we would get a 0 for the entire assignment. I would rather be safe then sorry and declare/initialize everything myself. – kingcobra1986 Oct 5 '16 at 9:45
  • It's part of libc. You could just ask him. He'd be more of an asshole if he didn't allow this. I find it quite odd of him to force you to use the Arduino IDE, but not use any other part of Arduino; well; except for the loop and setup; oh; and the initialization that Arduino does. – Gerben Oct 5 '16 at 12:52
  • @kingcobra1986 PORTB etc come with the compiler, and are nothing to do with the Arduino API. If your "professor" thinks they are anything to do with Arduino then he is a fool. – Majenko Oct 5 '16 at 13:13
1

When I set the DDRB to 0x80, is that just making the PB7 an output and the rest as input?


Clarification

I take that question to mean "When I assign the value to 0x80 to DDRB". For example:

DDRB = 0x80;

In the code above that question, you were not assigning, you were "or"ing the value.


Yes. Typically you would "or" in the value if you want to affect only one pin, eg.

DDRB |= 0x80;

The conventional way to make it an input again would be:

DDRB &= ~0x80;

That is, "and" in the negated bit pattern for that pin.

Remember, you have 3 registers (per port) where "x" is "A", "B", "C" etc.:

  • DDRx - data direction - a 1-bit is output, a 0-bit is input
  • PORTx - output value - a 1-bit is HIGH, a 0-bit is LOW

    However, if the data direction is input, then a 1-bit is input-pullup, and a 0-bit is not input-pullup.

  • PINx - read the input from the port - a 1-bit is HIGH, a 0-bit is LOW

    Note that writing a 1-bit to PINx toggles the output value of that port.


These remarks apply to the AVR chips, not the SAM ones.

  • I was responding to the sentence, not the code. If you set DDRB to 0x80 then it has that effect. I think the words "set a variable" are generally understood to mean "assign a value to a variable". That is, replace the old contents with the new ones. – Nick Gammon Oct 5 '16 at 20:40
  • I clarified my answer to show exactly what I was responding to. – Nick Gammon Oct 5 '16 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.