I'm trying to set a library in C++ for AVR. The idea is to have an easy way of configuring what pins you use on each device. This is the library:
class PINS{
public:
//ATTRIBUTES
uint8_t* DDRaddr;
uint8_t* PORTaddr;
uint8_t* PINaddr;
int pinnum;
//METHODS
void usepin(volatile uint8_t *pin, int num);};
void PINS::usepin(volatile uint8_t *pin, int num){
this->pinnum=num;
if(pin==&PORTB){
this->DDRaddr=&DDRB;
this->PINaddr=&PINB;
this->PORTaddr=&PORTB;}
if(pin==&PORTC){
this->DDRaddr=&DDRC;
this->PINaddr=&PINC;
this->PORTaddr=&PORTC;}
if(pin==&PORTD){
this->DDRaddr=&DDRD;
this->PINaddr=&PIND;
this->PORTaddr=&PORTD;}
return;}
And this is what calls for it:
PINS RS;
RS.usepin(&PORTC, 0);
Now this is how I thought it would work:
When I try to build it in Atmel Studio I have the following error for every line like this: "this->DDRaddr=&DDRB" The error says:
Error 1 invalid conversion from volatile uint8_t* to uint8_t*
It used to work before I made uint8_t* DDRaddr a class member. I can't understand what is the problem, i couldn't make a conclusion from similar - but yet not quite the same - questions. Is there anyone who could have an idea of what is wrong with this specific code?
Apparently DDRB
is of type volatile uint8_t
, but you're trying to assign its address into a uint8_t*
. That's not allowed - volatile
follows the same rules as const
in this regard.
If you intend to use the members like this, you'll have to declare them as pointers to volatile
:
class PINS{
public:
//ATTRIBUTES
volatile uint8_t* DDRaddr;
volatile uint8_t* PORTaddr;
volatile uint8_t* PINaddr;
// ... rest as before