/* Blink Turns on an LED on for one second, then off for one second, repeatedly. The LED on the Blue Pill is connected to PC13 */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin PC13 as an output. pinMode(PC13, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } /* Timer Interrupt Using timer 2 to give a flag when an overflow occured. Use polling this flag to time programs */ uint16_t timer_value = 0x8FFF; int prescalefactor = 400; volatile bool timer_tick = false;//timer_tick is changed in the Interrupt Service Routine HardwareTimer timer(2); void overflowISR(); void setup() { pinMode(PC13, OUTPUT);// here is the internal LED attached to timer.pause(); timer.setPrescaleFactor(prescalefactor); timer.setOverflow(timer_value); timer.refresh(); timer.attachInterrupt(4, overflowISR); } void loop() { timer.resume();//start timer2 while(1){ while(!timer_tick){} //poll for timer_tick timer_tick=false; //reset timer_tick digitalWrite(PC13, !digitalRead(PC13));//invert LED port } } void overflowISR() { timer_tick=true;//here is the timer flag set } /* MIDI using the TX1 of UART1 at pin PA9 */ uint8_t channel = 10;//I used the percussion channel 10 uint8_t velocity = 64;// half max value // plays a midi note on TX1 @ PA9 //check in calling routine for max cmd, pitch and velocity void noteOn(char cmd, char pitch, char velocity) { Serial1.print(cmd); Serial1.print(pitch); Serial1.print(velocity); } void setup(void) { pinMode(PA9, OUTPUT);// this is the TX1 pin // Set MIDI baud rate to 31250 Bps: Serial1.begin(31250); } void loop(void) { while (1){ // Play notes from F#-0 (0x1E) to F#-5 (0x5A): for (uint8_t note = 0x1E; note < 0x5A; note++) { noteOn((0x90+channel), note, velocity); delay(10); //noteOff can be this: noteOn((0x90+channel), note, 0x00);//velocity = 0x00 means noteOff //or use for noteOff: //noteOn((0x80+channel), note, velocity); delay(100); } } } /* Using SPI */ #include #include #define SPI_CS0 PA2 // three chip-select lines, but can be more #define SPI_CS1 PA3 #define SPI_CS2 PA4 #define SPI_CLK PA5 #define SPI_MOSI PA7 #define SPI_MISO PA6 void setup() { pinMode(SPI_CS0, OUTPUT); pinMode(SPI_CS1, OUTPUT); pinMode(SPI_CS2, OUTPUT); SPI.begin(); //here set your spi freq and mode SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE3)); } void loop() { delay(10); // writing to the SPI // take the SS (using CS0) pin low to select the chip: digitalWrite(SPI_CS0, LOW); // send in the address and value via SPI: SPI.transfer(0x3F);//some data to be send SPI.transfer(0xAA); SPI.transfer(0x55); // take the SS pin high to de-select the chip: digitalWrite(SPI_CS0, HIGH);//done }