Skip to content

Instantly share code, notes, and snippets.

@karlramberg
Created January 31, 2018 00:14
Show Gist options
  • Save karlramberg/65c96e5e219bcbbc56e981bf2731190d to your computer and use it in GitHub Desktop.
Save karlramberg/65c96e5e219bcbbc56e981bf2731190d to your computer and use it in GitHub Desktop.
Music (Jacklin Engineering Camp)
/*
* File: Switch.c
* Author: youngerr
*
* Created on June 1, 2015, 2:22 PM
*/
/***************************************************************
* The #pragma config statements set up the microcontroller's
* configuration registers. Changing these settings could make
* the PIC Simon Board inoperable. DO NOT CHANGE unless you
* know what you are doing and are sure the changes do not
* affect the operation of the PIC Simon Board.
***************************************************************/
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC
// Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF
// Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON
// Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = ON
// MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF
// Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF
// Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON
// Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF
// Clock Out Enable (CLKOUT function is disabled. )
#pragma config IESO = ON
// Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON
// Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF
// Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF
// Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
#pragma config PLLEN = OFF
// PLL Disable
#pragma config STVREN = ON
//Stack Overflow/Underflow Reset(Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO
// Brown-out Reset Voltage Selection (low trip point selected.)
#pragma config LVP = ON
// Low-Voltage Programming Enable (Low-voltage programming enabled)
#include <xc.h>
#include "Simon_Board.h"
void Note_B4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*31; //B4 Freq./16 = 30.8
LEDS_ON(LED2);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<169;index++); //83333.33 / B4 Freq. = 168.7
}
LEDS_OFF(LED2);
}
void Note_B3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*16; //B4 Freq./16 = 30.8
LEDS_ON(LED2);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<338;index++); //83333.33 / B4 Freq. = 168.7
}
LEDS_OFF(LED2);
}
void Note_D4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*37; // Note Freq./16 D4=293.67/16=18.35
LEDS_ON(LED4);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<142;index++); // 83333.33/293.67=283.76
}
LEDS_OFF(LED4);
}
void Note_G4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*24; // Note Freq./16 G4=392.00/16=24.5
LEDS_ON(LED7);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<213;index++); // 83333.33/392.00=212.59
}
LEDS_OFF(LED7);
}
void Note_G3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*12; // Note Freq./16 G4=392.00/16=24.5
LEDS_ON(LED7);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<426;index++); // 83333.33/392.00=212.59
}
LEDS_OFF(LED7);
}
void Note_G3Sh(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*13; // Note Freq./16 G4=392.00/16=24.5
LEDS_ON(LED7);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker();
for(index=0;index<401;index++); // 83333.33/392.00=212.59
}
LEDS_OFF(LED7);
}
void Note_A4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*28; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED1);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<189;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED1);
}
void Note_E5(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*42; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED5);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<127;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED5);
}
void Note_E4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*21; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED5);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<253;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED5);
}
void Note_E3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*11; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED5);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<506;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED5);
}
void Note_F4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*22; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED6);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<239;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED6);
}
void Note_F3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*11; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED6);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<478;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED6);
}
void Note_D4Sh(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*19; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED4);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<268;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED4);
}
void Note_C4(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*16; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED3);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<319;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED3);
}
void Note_C3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*8; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED3);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<638;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED3);
}
void Note_A3(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*14; // Note Freq./16 A4=440.00/16=27.5
LEDS_ON(LED1);
for(index2=0;index2<stop;index2++)
{
Toggle_Speaker(); // 83333.33333/Note Freq.
for(index=0;index<379;index++); // 83333.33/440.00=189.39
}
LEDS_OFF(LED1);
}
void Rest(unsigned int duration)
{
unsigned int index,index2,stop;
stop=duration*28;
for(index2=0;index2<stop;index2++)
{
for(index=0;index<189;index++);
}
}
void Breath(void)
{
unsigned int index,index2;
for(index2=0;index2<4;index2++)
{
for(index=0;index<379;index++);
}
LEDS_OFF(LED6);
}
void main(void) {
unsigned char temp;
unsigned int index,index2,temp16;
//Initialization
LATA=0x3D; // LED5 off, Aux LED off, speaker '1'
ANSELA=0x00; // All pins set to be digital i/o
TRISA=0xEE; // PA7-PA5, and PA3-PA1 inputs, PA4 and PA0 are outputs
LATB=0xFF; // Set for switch inputs (active low)
ANSELB=0x00; // All pins set to be digital i/o
OPTION_REG=0x7F; // Enables weak pull-ups
TRISB=0xFF; // All pins set as inputs
LATC=0xFF; // All pins high (all LEDs off)
TRISC=0x00; // PC7 to PC0 are outputs
OSCCON=0x70; // PLL disabled, 8MHz clock selected
//Main Loop
while(1)
{
temp16=Read_Switches();
if((temp16&SW1)==SW1) // Plays song when SW1 is pressed
{
/* MARY HAD A LITTLE LAMB */
//Line 1
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(32);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_A4(32);
Breath();
//Measure 4
Note_B4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(32);
//Line 2
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_B4(16);
Breath();
Note_A4(16);
Breath();
//Measure 4
Note_G4(64);
Breath();
//Line 3
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(32);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_A4(32);
Breath();
//Measure 4
Note_B4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(32);
Breath();
//Line 4
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_B4(16);
Breath();
Note_A4(16);
Breath();
//Measure 4
Note_G4(48);
Breath();
Note_D4(16);
Breath();
//Line 5
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(32);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_A4(32);
Breath();
//Measure 4
Note_B4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(16);
Breath();
//Line 6
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_B4(16);
Breath();
Note_A4(16);
Breath();
//Measure 4
Note_G4(48);
Breath();
Note_D4(16);
Breath();
//Line 7
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(32);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_A4(32);
Breath();
//Measure 4
Note_B4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(16);
Breath();
Note_D4(16);
Breath();
//Line 8
//Measure 1
Note_B4(16);
Breath();
Note_A4(16);
Breath();
Note_G4(16);
Breath();
Note_A4(16);
Breath();
//Measure 2
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
Note_B4(16);
Breath();
//Measure 3
Note_A4(16);
Breath();
Note_A4(16);
Breath();
Note_B4(16);
Breath();
Note_A4(16);
Breath();
//Measure 4
Note_G4(64);
Breath();
}
if((temp16&SW2)==SW2) // Plays song when SW1 is pressed
{
/* FUR ELISE */
//Line 1
//Measure 1
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
//Measure 2
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Measure 3
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
Breath();
//Measure 4
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_G3Sh(8);
Breath();
Note_B3(8);
Breath();
//Measure 5
Note_C4(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_E4(8);
Note_D4Sh(8);
//Line 2
//Measure 1
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Measure 2
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
Breath();
//Measure 3
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_C4(8);
Breath();
Note_B3(8);
Breath();
//Measure 4
Note_A3(16);
Breath();
Rest(48);
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
//Measure 5
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Line 3
//Measure 1
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
Breath();
//Measure 2
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_G3Sh(8);
Breath();
Note_B3(8);
Breath();
//Measure 3
Note_C4(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
//Measure 4
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Measure 5
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
Breath();
//Line 4
//Measure 1
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_C4(8);
Breath();
Note_B3(8);
Breath();
//Measure 2
Note_A3(8);
Breath();
Rest(24);
Note_B3(8);
Breath();
Note_C4(8);
Breath();
Note_D4(8);
Breath();
//Measure 3
Note_E4(16);
Breath();
Rest(24);
Note_G3(8);
Breath();
Note_F4(8);
Breath();
Note_E4(8);
Breath();
//Measure 4
Note_D4(16);
Breath();
Rest(24);
Note_F3(8);
Breath();
Note_E4(8);
Breath();
Note_D4(8);
Breath();
//Measure 5
Note_C4(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Line 5
//Measure 1
Note_B3(16);
Breath();
Note_E3(8);
Breath();
Note_E3(8);
Breath();
Note_E4(8);
Breath();
Note_E3(8);
Breath();
//Measure 2
Note_E3(8);
Breath();
Note_E3(8);
Breath();
Note_E5(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
//Measure 3
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
//Measure 4
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
//Measure 5
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_G3Sh(8);
Breath();
Note_B3(8);
Breath();
//Line 6
//Measure 1
Note_C4(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
//Measure 2
Note_E4(8);
Breath();
Note_D4Sh(8);
Breath();
Note_E4(8);
Breath();
Note_B3(8);
Breath();
Note_D4(8);
Breath();
Note_C4(8);
Breath();
//Measure 3
Note_A3(16);
Breath();
Rest(24);
Note_C3(8);
Breath();
Note_E3(8);
Breath();
Note_A3(8);
Breath();
//Measure 4
Note_B3(16);
Breath();
Rest(24);
Note_E3(8);
Breath();
Note_C4(8);
Breath();
Note_B3(8);
Breath();
//Measure 5
Note_A3(16);
Breath();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment