
Originally Posted by
PaulStoffregen
Again, you've shown pieces of code, but no single complete program which I or others can copy into Arduino and run on a Teensy to see the problem. This time, I'm not going to write a program or try to guess how to put these pieces together (even if that'd be trivial). Please, follow our "Forum Rule". This type of conversation is perfectly good, but you *also* need put a complete copy of the test program in your message.
Hi Paul,
thanks, heres the code in full. I'm just trying to get the interrupt to write a pin high/low, siiliar if you were polling with digitialReadFast and digitalWriteFast. I assume the avr headers are only relvant to the 8-bit teensy's, but it's in the documentation section on interupts.
Code:
#include <MIDI.h>
#include <midi_UsbTransport.h>
#include <avr/io.h>
#include <avr/interrupt.h>
template<class T> inline Print &operator <<(Print &obj, T arg) {
obj.print(arg);
return obj;
}
#define LED 13 // variable for LED pin
#define K1_IN 11 // variable for K1 in pin
#define K2_IN 12 // variable for K2 in pin
#define K3_IN 24 // variable for K3 in pin
#define K4_IN 25 // variable for K4 in pin
#define MASTER_CK_IN 26 // variable for Master 1mhz clock in pin
#define SYNC_CK_IN 27 // variable for 54us Sync clock in pin
#define USI_POLY_OUT 28 // variable for Serial Poly Out pin
#define SI_ORGAN_OUT 29 // variable for Serial Organ Out pin
#define SYNC_CK_OUT 32 // variable for Serial Organ Out pin
const int PWMpin1 = 3; //FTM1 timer
volatile unsigned int globalFiredFlag = 0;
boolean pwmduration;
float clock_Array[128];
void setup() {
Serial.begin(9600);
for (byte i = 0; i < 128; i++) {
float divider = (i * 2) + 60;
clock_Array [i] = 60000000 / divider;
}
pwmduration = true;
attachInterrupt(digitalPinToInterrupt(SYNC_CK_IN), ISR_FUNCTION, RISING);
usbMIDI.setHandleControlChange(OnCC); // set handle for MIDI continuous controller messages
pinMode(PWMpin1, OUTPUT);
pinMode(LED, OUTPUT); // set LED pin to output
pinMode(K1_IN, INPUT);
pinMode(K2_IN, INPUT);
pinMode(K3_IN, INPUT);
pinMode(K4_IN, INPUT);
pinMode(MASTER_CK_IN, INPUT);
pinMode(SYNC_CK_IN, INPUT);
pinMode(USI_POLY_OUT, OUTPUT);
pinMode(SI_ORGAN_OUT, OUTPUT);
pinMode(SYNC_CK_OUT, OUTPUT);
analogWrite(PWMpin1, 0);
analogWriteFrequency(PWMpin1, 1000000); // set freq to 1mhz
analogWrite(PWMpin1, 128);
}
void loop() {
usbMIDI.read(); // read the USB MIDI bus every loop
if (globalFiredFlag == 1) {
digitalWriteFast(SYNC_CK_OUT, HIGH);
globalFiredFlag = 0;
}
else{
digitalWriteFast(SYNC_CK_OUT, LOW);
}
}
void ISR_FUNCTION() {
globalFiredFlag = 1;
}
void OnCC(byte channel, byte controller, byte value) {
if (controller == 1) {
pwmduration = false;
analogWrite(PWMpin1, 0);
analogWriteFrequency(PWMpin1, clock_Array[value]); //
analogWrite(PWMpin1, 128);
pwmduration = true;
}
}