Go Down

Topic: OLED display problem - Direct Port manipulation issue (Read 870 times) previous topic - next topic


I am having a problem getting my Arduino Micro Pro to work with an OLED screen.
I am using this library
I am using hardware SPI

Everything works fine if I use the pins specified in the example....

#define OLED_DC 6
#define OLED_CS 7
#define OLED_RESET 8

Unfortunately I am already using these pins for something else.....and specifically the interrupt functionality on these pins so I cannot change them. The only pins that are spare on my board are the Analogue pins A0-A4.

If I use.....

#define OLED_DC 18
#define OLED_CS 19
#define OLED_RESET 20

...which supposedly maps to the analogue pins it doesnt work.

When I looked into the library code I noticed that for speed digitalWrite() is not used to toggle the pins. Instead the ports are accessed directly like this....

csport = portOutputRegister(digitalPinToPort(cs));
cspinmask = digitalPinToBitMask(cs);
dcport = portOutputRegister(digitalPinToPort(dc));
dcpinmask = digitalPinToBitMask(dc);

portOutputRegister(), digitalPinToPort() and digitalPinToBitMask() are arduino functions but it seems they are having trouble accessing Port F (the analogue port)

I cant see any reason why this should be a problem?

Any help greatly appreciated



Try using the names like A0, A1... instead of numbers. The numbers are different on different platforms. A0 is 14 on an UNO but on a Leonardo it's something like 18.  The names are automatically declared.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp



A0 should have worked for one of the pins. I hope you didn't use A0 for more than one pin.

When you said "having trouble accessing Port F", what exactly did you mean?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


"having trouble accessing Port F"

I just meant because it works using pins 6,7,8 perhaps the direct register access functions were not addressing Port F (the analogue port) properly for some reason.

It was my best guess

I was trying to just move OLED_DC onto the analogue pins to start with - keeping pins 6 and 7 the same

I tried 18, 14(just in case it was mapping to a different board for some reason) and A0

None of these work

When I get home I will try using digitalWrite() and try toggling an LED just to see if the problem really is with the direct addressing.

I probably should have done this before posting on here to be fair. I just thought someone may have encountered this before.

Just for interest - when you supply "A0" to digitalWrite(), does it pass the HEX value 160 as an int?


If you supply "A0" yes, but you should not, you should pass A0 which is a macro for the right pin ;)
Use fricking code tags!!!!

NEW Library to make fading leds a piece of cake


Update - cant write to any of the Analogue pins even using digitalwrite()
They are all always at 3.3V no matter what?


No.  That would be 0xA0.  
The variable named A0 is declared to be a constant value of the index into the lookup tables for the pin named A0.  On the Arduino UNO that would be 14.  The 15th entry (number 14) in the tables contain the various details like the register address and bit mask.

From hardware/arduino/avr/variants/standard/pins_arduino.h:
Code: [Select]
#define PIN_A0   (14)
#define PIN_A1   (15)
#define PIN_A2   (16)
#define PIN_A3   (17)
#define PIN_A4   (18)
#define PIN_A5   (19)
#define PIN_A6   (20)
#define PIN_A7   (21)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up