Author Topic: Passing a PORT to a class -- Solved  (Read 1444 times)

0 Members and 1 Guest are viewing this topic.

Offline dan3460Topic starter

  • Frequent Contributor
  • **
  • Posts: 326
  • Country: us
Passing a PORT to a class -- Solved
« on: February 28, 2017, 01:25:37 am »
I have been reading about passing a port to a function, and seems simple enough. Now i'm trying to expand this concept to a class. To test I created a simple LED class:

Code: [Select]
class LED
{
//variables
public:
protected:
private:
volatile uint8_t *ledPort;
uint8_t ledPin;
//functions
public:
LED();
~LED();
void initLED(volatile uint8_t *port, volatile uint8_t *dirport, uint8_t pin); //Setup port and pin for LED use
void on(void);
void off(void);

protected:
private:
LED( const LED &c );
LED& operator=( const LED &c );
And here is the code for the class:
Code: [Select]
#include "LED.h"

// default constructor
LED::LED()
{
} //LED

void LED::initLED(volatile uint8_t *port, volatile uint8_t *dirport, uint8_t pin)
{
*ledPort=*port;
ledPin=pin;
*dirport |= (1<<ledPin);
}

void LED::on(void)
{
*ledPort |= (1<<ledPin);

}

void LED::off(void)
{
*ledPort &= (1<<ledPin);
}

// default destructor
LED::~LED()
{
} //~LED

Here i'm trying to keep in the function the address of PORTB so I can use it on the led::on and led::off members. The led::iniLED is called from the main function.

Is this correct?

EDIT:

The blinking was not working, but by changing: *ledPort=*port; to ledPort=port; on the initLED function the blinker works.
« Last Edit: February 28, 2017, 01:32:21 am by dan3460 »
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3024
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Passing a PORT to a class
« Reply #1 on: February 28, 2017, 01:35:34 am »
Edit: you solved it between me starting to type and posting the reply, will leave here as an explanation anyway :)

> *ledPort=*port;

In english, ' Copy the data stored at the memory address pointed to by "port", and put it into the memory address pointed to by "ledPort" '

I don't think that is what you intended to do, which is probably just "ledPort = port;" - point ledPort to the same memory address as port is presently pointing to.

Other than that nothing jumps out at me.

~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 
The following users thanked this post: dan3460

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: Passing a PORT to a class -- Solved
« Reply #2 on: February 28, 2017, 07:11:16 am »
A port could be class too....   >:D
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf