Port Bits als Makro / Port Bits as macro

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hallo Leute,

ich möchte einzelne Port-Bits mit Hilfe als Verknüpfung setzen oder auslesen.

Mit Bascom-AVR kann ich mit Alias verknüpfen, aber C/C++ habe ich noch nicht raus gefunden.

 

---------------- ENGLISH ---------------------------

Hi guys,

I want to set or read individual port bits with help as a link.

With Bascom-AVR I can link with alias, but C/C++ I haven't found out yet.

 

BASCOM-AVR Example:

 

LED1 Alias PORTA.7

LED2 Alias PORTA.6

 

Taster1 Alias PINB.0

Taster2 Alias PINB.1

Taster3 Alias PINB.2

 

....

 

Main:

  

   do

      if Taste1 = 0 then

         LED1 = 1

      elseif Taste2 = 0 then

         LED2 = 1

      elseif Taste3 = 0 then

         LED1 = 0

         LED2 = 0

      end if

   loop

end

 

Wie muss ich das machen das ich in C/C++ einzelne Ports und Pins verknüpfe.

--------------------- ENGLISH ----------------------------

How do I have to link individual ports and pins in C/C++.

 

C/C++

//....

#define LED1 (Link to PORTA.7)
#define LED2 (Link to PORTA.6)

#define Taster1 (Link to PINB.0)
#define Taster2 (Link to PINB.1)
#define Taster3 (Link to PINB.2)

//....

void main(void)
{
    while(1)
    {
        if(Taster1 == 0)
        {
            LED1 = 1;
        }
        else if(Taster2 == 0)
        {
            LED2 = 1;
        }
        else if(Taster3 == 0)
        {
            LED1 = 0;
            LED2 = 0;
        }
    }
}

Grüße / Greetings

Patrick Dreger

Last Edited: Thu. Jun 2, 2022 - 09:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Microchip do support this access method, but only for PIC devices where much like 8051 you could say it is a de-facto standard.

 

The header files required to make this work are absolutely huge as a result and indeed you could copy the method used on PIC devices onto AVR but it would be a really big job of work. If you did this just for PORTx registers it would be manageable though.

 

I did this PORTS only work myself on one project but discovered that it was a lot of work for essentially little gain. I now universally use hardware abstraction macros thus:

// LED OUTPUTS
#define GREENLED_ON()  PORTB &= ~(1<<1)
#define GREENLED_OFF() PORTB |=  (1<<1)

// KEY INPUTS
#define IS_KEYPRESSED() (!(PINE & (1<<3)))

 

Edit:

Here's an old thread on the subject. Try to avoid the "tabs vs spaces" noise.

https://www.avrfreaks.net/forum/register-definition-mcu-header-file-pic-vs-avr

 

Last Edited: Thu. Jun 2, 2022 - 10:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It would be pretty easy in C++

nearly impossible in plain C, I think, but “setio (Led1, 1)” would be ok.