I have a small project which is expected to count interrupts coming in on PB12 and PB13 of a Blue Pill board.
The incoming signals are formatted externally in hardware in pulses of about 9ms period toggling between 0 and 5V. These signals are confirmed on an oscilloscope. The data is then sent to an ST7920 where the count is updated every 2 seconds.
The code is rather simple but obviously I am missing something because the display stays stuck at 0000. Here is the code:
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
const int ledPin = PC13;
const int cyl1 = PB12;
const int cyl2 = PB13;
//const int cyl3 = PB14;
//const int cyl4 = PB15;
volatile unsigned int cyl1Counter = 0;
volatile unsigned int cyl2Counter = 0;
unsigned int cyl1CounterCopy;
unsigned int cyl2CounterCopy;
char buffer[30];
U8G2_ST7920_128X64_F_HW_SPI u8g2(U8G2_R0, /* CS/RS=*/ PA4, /* reset/RST=*/ PA0);
void setup() {
// put your setup code here, to run once:
u8g2.begin();
u8g2.clearBuffer();
u8g2.drawXBM(0,0, u8g_logo_width, u8g_logo_height, u8g_logo_bits );
u8g2.sendBuffer();
u8g2.setFont(u8g2_font_ncenB10_tr);
pinMode(ledPin, OUTPUT);
pinMode(cyl1, INPUT);
attachInterrupt(digitalPinToInterrupt(cyl1),cyl1_ISR, FALLING);
pinMode(cyl2, INPUT);
attachInterrupt(digitalPinToInterrupt(cyl2),cyl2_ISR, FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(ledPin, LOW);
// block interrupts to ensure no copy errors whilst parsing the variables
noInterrupts();
cyl1CounterCopy = cyl1Counter;
cyl2CounterCopy = cyl2Counter;
interrupts();
u8g2.clearBuffer();
u8g2.drawStr(0,13, "Cyl 1 -");
sprintf(buffer,"%04u",cyl1CounterCopy);
u8g2.setCursor(50,13);
u8g2.printf(buffer);
u8g2.drawStr(0,29, "Cyl 2 -");
sprintf(buffer,"%04u",cyl2CounterCopy);
u8g2.setCursor(50,29);
u8g2.printf(buffer);
u8g2.drawStr(0,45, "Cyl 3 -");
u8g2.drawStr(50,45, "buffer");
u8g2.drawStr(0,61, "Cyl 4 -");
u8g2.sendBuffer();
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
}
//interrupt service routines
void cyl1_ISR() {
cyl1Counter++;
}
void cyl2_ISR() {
cyl2Counter++;
}
any suggestions please?