Re: program modification
Posted: Wed Jan 27, 2021 3:24 pm
I want to scan I2C2, which is pins PB10 and PB11
Everything relating to using STM32 boards with the Arduino IDE and alternatives
https://www.stm32duino.com/
Code: Select all
Adafruit_SSD1306 Display (128, 64, &Wire2, OLED_RESET);
Code: Select all
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire_slave.h>
//use IIC2
//TwoWire WIRE2 (2,I2C_FAST_MODE);
//#define Wire WIRE2
//////////////////
#define OLED_RESET -1 // use -1 if no reset pin on OLED board
// use second I2C bus on STM32F103
// TwoWire Wire2(SDA2, SCL2);
TwoWire Wire2(PB11, PB10);
Adafruit_SSD1306 Display
// below is for a 128x64 OLED. Other option is 128x32 for smaller OLED
Adafruit_SSD1306 Display (128, 64, &Wire2, OLED_RESET);
//////////////////
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
nDevices++;
}
else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found");
else
Serial.println("done");
delay(5000); // wait 5 seconds for next scan
}
/*
////////////////////
Display1.begin(SSD1306_SWITCHCAPVCC, 0x3C); // if nothing is displayed using 0x3C, try 0x3D
// Display1
Display1.clearDisplay();
Display1.setTextSize(2);
Display1.setTextColor(WHITE, BLACK);
Display1.setCursor(0, 0 );
Display1.println("Display");
Display1.println("I2C #2 @ 3C");
Display1.display();
////////////////////
*/
Code: Select all
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x3F, 16, 2);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(" I2C LCD with ");
lcd.setCursor(0,1);
lcd.print(" STM32F103C8T6 ");
}
void loop()
{
// Do nothing hereā¦
}
Code: Select all
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
// TwoWire Wire2(SDA2, SCL2);
TwoWire Wire2(PB11, PB10);
void setup()
{
Wire2.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire2.beginTransmission(address);
error = Wire2.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Code: Select all
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
//#if (SSD1306_LCDHEIGHT != 64)
#if (SSD1306_LCDHEIGHT != 32)
///#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
int sensorValue = 0;
int voltage = 0;
const int analogInPin = PA7;
//display.invertDisplay(true); // invert the colours of led display
//display.invertDisplay(false);
//display.drawPixel(10, 10, WHITE);//to highlight a particular pixel
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize I2C addr to 0x3C ( for 128x64 Display )
display.clearDisplay(); // clear the display before starting the program to avoid adafruit splashscreen ( *we can also skip it by modifing header file )
// display.drawBitmap(x-axis position, y-axis position, bitmap data, bitmap width, bitmap height, color)
display.drawPixel(100, 15, WHITE);//to highlight a particular pixel
}
void loop()
{
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(WHITE);
sensorValue = analogRead(analogInPin);
voltage = map(sensorValue, 0, 4096, 0, 3300);
display.setCursor(0, 0);
display.print(voltage);
display.display();
}