/**************************************************************************
Base sistema diagnostico per Arduino Uno
pinchange interrupt test
**************************************************************************/
#include <Wire.h> // protocollo i2c per settare ad esempio la velocità
#include <Adafruit_GFX.h> // font
#include <Adafruit_SSD1306.h>
#include <stdio.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
volatile Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// parola di stato dei 6 LSB bit della porta B
volatile byte port_state=B00000000;
volatile int least_one=0;
volatile unsigned int num;
char *outstr;
char ch,outstr1[30];
void setup() {
int i;
outstr=&(outstr1[0]); // esercizio sui puntatori
PCICR = B00000000; // No Change pin interrupt
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Clear the buffer
display.clearDisplay();
display.setCursor(0,0);
display.write("START");
display.setCursor(32,16);
display.write("OUTPUT TEST");
display.display();
for(i=2;i<18;i++) pinMode(i, OUTPUT);
PORTB &= B11011111;
for(i=0;i<3;i++)
{ PORTB |= B00011111;
PORTC |= B00001111;
PORTD |= B11111100;
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(12,32);
display.write("oooooo oooooo oooo");
display.display();
delay(300);
PORTB &= B11000000;
PORTC &= B11110000;
PORTD &= B00000011;
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(12,32);
display.write("oooooo oooooo oooo");
display.display();
delay(600);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(6,0);
display.setTextSize(1);
port_state=B00000000;
delay(500);
for(i=2;i<13;i++) pinMode(i, INPUT);
//delay(500);
PCICR |= B00000001; //Bit0 = 1 -> "PCIE0" enabeled (PCINT08 to PCINT13)
//PCICR |= B00000010; //Bit1 = 1 -> "PCIE1" enabeled (PCINT14 to PCINT19)
PCICR |= B00000100; //Bit2 = 1 -> "PCIE2" enabeled (PCINT01 to PCINT07)
PCMSK0 |= B00011111; // portB mask (da 15 a 08)
//PCMSK1 |= B11111100; // portC mask (da 14 a 19)
PCMSK2 |= B11111100; // portD mask (da 7 a 0)
// https://www.electrosoftcloud.com/en/pcint-interrupts-on-arduino/
}
ISR (PCINT0_vect)
{
port_state = PINB & B00011111;
num=(unsigned int )port_state; //int(log10(port_state)/log10(2)+0.01);
if (port_state>0)
{ /* la porta B ha cambiato stato
portando almeno un bit a uno
*/
least_one=1;
PORTC |= B00001111;
}
}
ISR (PCINT2_vect)
{
port_state = PIND & B11111100;
num=(unsigned int )port_state; //int(log10(port_state>>2)/log10(2)+0.01);
if (port_state>0)
{ /* la porta D ha cambiato stato
portando almeno un bit a uno
*/
least_one=1;
PORTC |= B00001111;
}
}
void loop()
{
if(least_one>0)
{ least_one=0;
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(12,32);
sprintf(outstr,"%3d ",num);
display.print(outstr);
display.display();
delay(1000);
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(12,32);
display.print(outstr);
display.display();
PORTC &= B11110000;
}
}