Hello,
In the past i have used some i2c based displays(ssd1306 driver) with my Nucleo F303K8. but when i tried to make a little project the other day, i discovered that there’s something wrong with my code. When using the following code:
#include "mbed.h"
#include "Adafruit_SSD1306.h"
DigitalOut myled(LED1);
// an I2C sub-class that provides a constructed default
class I2CPreInit : public I2C
{
public:
I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
{
frequency(400000);
start();
};
};
I2CPreInit gI2C(D4,D5);
Adafruit_SSD1306_I2c scherm(gI2C,D13);
int main()
{
int x = 0;
scherm.clearDisplay();
while(1)
{
myled = !myled;
scherm.printf("%d\r",x);
scherm.display();
x++;
wait(1.0);
}
}
Everything works fine,
But when i rewrite the code so that the display is ‘controlled’ by a void outside of the main it suddenly doesn’t work anymore. The rewritten code:
#include "mbed.h"
#include "Adafruit_SSD1306.h"
Ticker plussen;
DigitalOut led(LED1);
// an I2C sub-class that provides a constructed default
class I2CPreInit : public I2C
{
public:
I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
{
frequency(400000);
start();
};
};
I2CPreInit gI2C(D4,D5);
Adafruit_SSD1306_I2c scherm(gI2C,D13);
int x;
void plus(){
x++;
scherm.clearDisplay();
scherm.setTextCursor(1,1);
scherm.printf("%d", x);
scherm.display();
}
int main()
{
scherm.clearDisplay();
plussen.attach(&plus, 1);
while(true){
led = !led;
wait_ms(500);
}
}
As far as i could test with my knowledge of programming there is something wrong with the .display(); With timers i discovered that it takes about 2 minutes to pass the display(); funtion. When lowering the frequency it also takes longer to pass the .display(); and when the frequency is higher it is a little faster. I also checked if it isn’t a hardware probleme by using other microcontrollers and other displays but the results stay the same. So am i using the library wrong wich creates this fault or is it because of something which isn’t my fault. I hope to hear from you,
Wouter de Gries