Trying things out as I learn C code, I wanted to test something. It worked as expected, but throws the warning
Warning 1 assignment from incompatible pointer type [enabled by default]
The code is simple. All I am doing here is toggling PIN B7 on an atmega2560. I have a LED hooked to it and I can see it blinking, so I know it works as expected.
Can anyone explain why I am seeing this error, even though it is executed as expected? The code is as follows:
#include <avr/io.h>
#include <util/delay.h>
void main(void) {
int *ptr;
ptr = &PORTB; // This line throws the warning
DDRB = (1 << 7);
while(1) {
*ptr = (1 << 7);
_delay_ms(1000);
*ptr = (0 << 7);
_delay_ms(1000);
}
}
PORTB is an 8bit register that has a bit per pin to control whether that pin is HIGH or LOW.
For now, I am glad it works. But these warnings annoy me.
int *ptr;
ptr = &PORTB; // This line throws the warning
PORTB
is a volatile unsigned char
defined with something like this:
*(volatile unsigned char *) 0xBEEF
Change your ptr
declaration to a volatile unsigned char *
:
volatile unsigned char *ptr;
ptr = &PORTB;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With