Go Down

Topic: PORTB problem (Read 494 times) previous topic - next topic

Stipl3x

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?

Robin2

Without seeing your program ... ? ? ?


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Stipl3x

#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.

Stipl3x

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

septillion

Why use an Arduino and the Arduino IDE if you don't want the easy to use HAL to go with it?
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

AWOL

#5
Jun 21, 2019, 10:43 am Last Edit: Jun 21, 2019, 10:43 am by AWOL
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.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Stipl3x

#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
      }
    }
}

septillion

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

Code: [Select]
#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 ;)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

GolamMostafa

#8
Jun 24, 2019, 07:35 pm Last Edit: Jun 24, 2019, 07:38 pm by GolamMostafa
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?

Romonaga

#9
Jun 24, 2019, 07:37 pm Last Edit: Jun 24, 2019, 07:39 pm by Romonaga
What is HAL?

High Level Arduino Language?
or
Heuristically programmed ALgorithmic computer?
or
What else?
HAL Hardware Abstraction Layer. https://en.wikipedia.org/wiki/HAL_%28software%29
The universe exists only because we are aware of it.
We want a few mad people now. See where the sane ones have landed us!

Whandall

Hardware Abstraction Layer.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

GolamMostafa

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

AWOL

How do Arduino, Arduino IDE, and HAL come together -- any example please?
digitalWrite
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

GolamMostafa

#13
Jun 25, 2019, 08:58 am Last Edit: Jun 26, 2019, 08:43 am by GolamMostafa
Quote
digitalWrite
Quote
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.  

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.

Whandall

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?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up