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
}
}
2 Answers 2
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– kingcobra19862016年10月05日 09:11:04 +00:00Commented Oct 5, 2016 at 9:11 -
You can use portPinB, but why would you?Gerben– Gerben2016年10月05日 09:35:14 +00:00Commented Oct 5, 2016 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– kingcobra19862016年10月05日 09:45:12 +00:00Commented Oct 5, 2016 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– Gerben2016年10月05日 12:52:13 +00:00Commented Oct 5, 2016 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– Majenko2016年10月05日 13:13:14 +00:00Commented Oct 5, 2016 at 13:13
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.2016年10月05日 20:40:31 +00:00Commented Oct 5, 2016 at 20:40
-
I clarified my answer to show exactly what I was responding to.2016年10月05日 20:43:14 +00:00Commented Oct 5, 2016 at 20:43
Explore related questions
See similar questions with these tags.
if( PINB & _BV(PINB4) )
orif( 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|=
to=
, or the bit will only be set, and never cleared.