PORTB problem

Hi!
I have bought an Arduino UNO REV 3 board to do some projects in C. I have made a small program to test the input of a button and turn a LED based on that. The thing is that PORTC is working as input, but I cannot read data from PORTB and D (PINB and PIND are not working). Maybe only PORTC is working as input. Can you help me with an answer please?

Without seeing your program ... ? ? ?

...R

#define F_CPU 20000000

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB |= (1 << 0) | (1 << 1);
DDRC &= ~(1 << 0);
PORTC |= (1 << 0);

/* Replace with your application code */
while (1)
{
if(!PINC)
{
PORTB |= (1 << 0);
PORTB &= ~(1 << 1);
_delay_ms(5000);
}
else
{
PORTB &= ~(1 << 0);
PORTB |= (1 << 1);
}
}
}

If I change PORTB as input or PORTD it is not working.

Yeah...figured it out...it was a software problem when I was checking PINB :))

Why use an Arduino and the Arduino IDE if you don't want the easy to use HAL to go with it?

A useful technique when you go off the Arduino piste (where function names and meaningful variable names aid the narrative) is to comment your code.
Then, we can see your intent, and see if the code matches your expectations.

#define F_CPU 20000000

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRB |= (1 << 2) | (1 << 1);
DDRB &= ~(1 << 3);
PORTB |= (1 << 3);

/* Replace with your application code */
while (1)
{
if(!(PINB & (1 << 3))) // when button is pressed the value of the PIN is 0 and the result of the op is 0
{
PORTB |= (1 << 2); // turn LED1 on
PORTB &= ~(1 << 1); // turn LED2 off
}
else
{
PORTB &= ~(1 << 2); // turn LED1 off
PORTB |= (1 << 1); //turn LED2 on
}
}
}

Variables are not forbidden when working with direct port manipulation :wink: And they can make code more self explaining.

#define  F_CPU 20000000

#include <avr/io.h>
#include <util/delay.h>

const uint8_t LedBMasks[] = {_BV(2), _BV(1)};
const uint8_t ButtonBMask = _BV(3);


int main(void)
{
  //Set outputs
  DDRB |= LedBMasks[0] | LedBMasks[1];
  //set inputs with pullup enabled
  DDRB &=  ~ButtonBMask;
  PORTB |= ButtonBMask;

  while (1)
  {
    //Check if button is pressed
    if (!(PINB & ButtonBMask))
    {
      PORTB |= LedBMasks[0]; // turn on
      PORTB &= ~LedBMasks[1]; // turn off
    }
    else
    {
      PORTB &= ~LedBMasks[0]; // turn off
      PORTB |= LedBMasks[1]; //turn on
    }
  }
}

PS Code tags please :wink:

septillion:
Why use an Arduino and the Arduino IDE if you don't want the easy to use HAL to go with it?

What is HAL?

High Level Arduino Language?
or
Heuristically programmed ALgorithmic computer?
or
What else?

GolamMostafa:
What is HAL?

High Level Arduino Language?
or
Heuristically programmed ALgorithmic computer?
or
What else?

HAL Hardware Abstraction Layer. HAL (software) - Wikipedia

Hardware Abstraction Layer.

How do Arduino, Arduino IDE, and HAL come together -- any example please?

GolamMostafa:
How do Arduino, Arduino IDE, and HAL come together -- any example please?

digitalWrite

digitalWrite

Why use an Arduino and the Arduino IDE if you don't want the easy to use HAL to go with it?

Now, it is clear to me that the @septillion's post (quoted above) has not welcomed the cryptic codes of Post#2 which has not even used the setup() and loop() functions of Arduino Platform. Following @septillion's recommendation, the OP may proceed to practice the functional checks of the IO lines of Port-B/Port-C/Port-D Registers based on the hardware structure (Fig-1) of a typical port line (bit-3 of Port-D) and using popular commands like: digitalRead(), digitalWrite(), pinMode() etc.
pd31.png
Figure-1: Internal details (conceptual level) of bit-3 of Port-D Register

1. The symbolic name Port-D may be adopted when the directions of the IO lines are not yet configured; the IO lines may be referred to as: pd0 - pd7.

2. The symbolic name PORTD is used when the directions of the IO lines are configured to work as output lines; the output lines are referred by these symbolic names: PD0 - PD7.

3. The symbolic name PIND is used when the directions of the IO lines are programmed to work as input lines; the input lines are referred by these symbolic names: PIND0 - PIND7.

Edited: In view of the comments that follow.

pd31.png

What a bunch of misinformation.

I'm tired to point all the nonsense.

Is there a mechanism to hide messages by 'special' members to me?

Whandall:
What a bunch of misinformation.

I prefer "alternative reality'

Whandall:
What a bunch of misinformation.

I'm tired to point all the nonsense.

Yes he has “previous” on this sort of thing.

It requires courage and hard labor to present information by defying disinformation.

Much less so spreading it, it seems.

Whandall:
Much less so spreading it, it seems.

The path to the acquisition of knowledge begins with ignorance, doubt, and fallacy.