#include <Arduino.h>
volatile int i = 0; // Make datatype volatile if it is changed by ISR or hardware
hw_timer_t *myTimer = NULL; // Initialize the timer variable
// Pin definitions
const int SevSegA = 19;
const int SevSegB = 18;
const int SevSegC = 17;
const int SevSegD = 16;
const int SevSegE = 23;
const int SevSegF = 22;
const int SevSegG = 1; // (TX Pin)
const int SevSegSwitch = 26;
const int SevSegSwitch2 = 27;
int firstDig = 0;
int secondDig = 0;
// Digit codes for 7-segment display (common anode)
const byte Digits[10] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
// Interrupt Service Routine (ISR)
void IRAM_ATTR ISRTMR() {
i++;
if (i > 99) {
i = 0;
}
}
// Setup function runs once when you press reset or power the board
void setup() {
// Initialize the digital pins as outputs
pinMode(SevSegA, OUTPUT);
pinMode(SevSegB, OUTPUT);
pinMode(SevSegC, OUTPUT);
pinMode(SevSegD, OUTPUT);
pinMode(SevSegE, OUTPUT);
pinMode(SevSegF, OUTPUT);
pinMode(SevSegG, OUTPUT);
pinMode(SevSegSwitch, OUTPUT);
pinMode(SevSegSwitch2, OUTPUT);
myTimer = timerBegin(1); // Initialize timer
// 0 means we start counting from 0
// 80 is prescaler (Clock is 80MHz) so its 1MHz now, true means count up
timerAttachInterrupt (myTimer, &ISRTMR); // Attach ISR to timer
// myTimer is our timer, ISRTMR is the ISR, true means on edge interrupt
timerWrite(myTimer, 1000000); // Set alarm
// it means myTimer will count 1 million times (1 second) and true means it should restart counting each time
timerStart(myTimer); // Enable the timer alarm
}
// Loop function runs over and over again forever
void loop() {
firstDig = i % 10;
secondDig = i / 10; // i modulo 10 gives the first digit and div by 10 gives the second
// Display first digit
digitalWrite(SevSegSwitch, LOW); // Enable the first digit (common anode)
digitalWrite(SevSegSwitch2, HIGH); // Disable the second digit
displayDigit(Digits[firstDig]);
delay(10);
// Display second digit
digitalWrite(SevSegSwitch, HIGH); // Disable the first digit
digitalWrite(SevSegSwitch2, LOW); // Enable the second digit (common anode)
displayDigit(Digits[secondDig]);
delay(10); // Adjust this value to control the overall display refresh rate
}
// The function below takes SegCode as a parameter which is a byte that contains which segments of the 7-seg are on (1) and off (0)
void displayDigit(byte SegCode) {
digitalWrite(SevSegA, !bitRead(SegCode, 0));
digitalWrite(SevSegB, !bitRead(SegCode, 1));
digitalWrite(SevSegC, !bitRead(SegCode, 2));
digitalWrite(SevSegD, !bitRead(SegCode, 3));
digitalWrite(SevSegE, !bitRead(SegCode, 4));
digitalWrite(SevSegF, !bitRead(SegCode, 5));
digitalWrite(SevSegG, !bitRead(SegCode, 6));
}
This is my code, I know it may be very badly written, however I just need to know how to configure the timer and interrupt properly! I am also aware of the comments that are badly written haha.
EDIT: I think that the frequency I was supposed to put in as a parameter to timerBegin() was in Hz? not MHz, now my two 7Segments light up and stay at 0.