Laffin

Untitled

Jan 6th, 2021
688
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PIC18F4520 Configuration Bit Settings
  2. #pragma config OSC = XT // Oscillator Selection bits (XT oscillator)
  3. #pragma config LVP = OFF // Single-Supply ICSP Enable
  4. #pragma config WDT = OFF // Watchdog Timer Enable bit
  5. #include <xc.h>
  6. #include <stdlib.h>
  7. #define _XTAL_FREQ 4000000
  8.  
  9.  
  10. /*
  11.  KEY A = 440.00Hz
  12.  T2CON = 0b00000111;
  13.  PR2 = 140 With prescale of 16*
  14.  CCPR1L = 0b01000110;
  15.  CCP1CON = 0b00101111;
  16.  
  17.  KEY B = 493.88Hz
  18.  T2CON = 0b00000111;
  19.  PR2 = 125 with prescale of 16
  20.  CCPR1L = 0b00111111;
  21.  CCPR1CON = 0b00001111;
  22.  
  23.  KEY G = 392.00Hz
  24.  T2CON = 0b00000111;
  25.  PR2 = 158 with prescale of 16
  26.  CCPR1L = 0b01001111;
  27.  CCP1CON = 0b00101111;
  28.  
  29.  KEY E = 329.63Hz
  30.  T2CON = 0b00000111;
  31.  PR2 = 188 with prescale of 16
  32.  CCPR1L = 0b01011110;
  33.  CCP1CON = 0b00101111;
  34.  
  35.  
  36.  KEY F sharp = 369.99Hz
  37.  T2CON = 0b00000111;
  38.  PR2 = 167
  39.  CCPR1L = 0b01010100;
  40.  CCP1CON = 0b00101111;
  41.  
  42.  Key E5 = 659.25Hz
  43.  T2CON = 0b00000111;
  44.  PR2 = 94
  45.  CCPR1L = 0b00101111;
  46.  CCP1CON = 0b00101111;
  47.  
  48.  Key D5 = 587.33Hz
  49.  T2CON = 0b00000111;
  50.  PR2 = 105
  51.  CCPR1L = 0b00110101;
  52.  CCP1CON = 0b00001111;
  53.  
  54.  Key B5 = 987.77Hz
  55.  T2CON = 0b00000111;
  56.  PR2 = 62
  57.  CCPR1L = 0b00011111;
  58.  CCP1CON = 0b00101111;
  59.  
  60.  Key A5 = 880Hz
  61.  T2CON = 0b00000111;
  62.  PR2 = 69;
  63.  CCPR1L = 0b00100011;
  64.  CCP1CON = 0b00001111;
  65.  
  66.  Key F#5 = 739.99Hz
  67.  T2CON = 0b00000111;
  68.  PR2 = 83;
  69.  CCPR1L = 0b00101010;
  70.  CCP1CON = 0b00001111;
  71.  
  72.  Key E5 = 659.25Hz
  73.  T2CON = 0b00000111;
  74.  PR2 = 93;
  75.  CCPR1L = 0b00101111;
  76.  CCP1CON = 0b00001111;
  77.  
  78.  Key G5 = 789.99Hz
  79.  T2CON = 0b00000111;
  80.  PR2 = 78;
  81.  CCPR1L = 0b00100111;
  82.  CCP1CON = 0b00101111;
  83.  
  84.  Key F5 =  698.46Hz
  85.  T2CON = 0b00000111;
  86.  PR2 = 88;
  87.  CCPR1L = 0b00101100;
  88.  CCP1CON = 0b00101111;
  89.  
  90.  
  91.  */
  92.  
  93. void interrupt highISR(void);
  94. void delay (unsigned int len);
  95.  
  96. unsigned char pr2 [27] = {140, 125, 140, 158, 188, 167, 158, 140, 94, 105, 125, 62, 69, 78, 83, 93,
  97.                             69, 78, 83, 62, 69, 78, 83, 93, 83, 93, 78};
  98. unsigned char dutyCycleCCPR1L [27] = {0b01000110, 0b00111111, 0b01000110, 0b01001111, 0b01011110, 0b01010100,
  99.                                       0b01001111, 0b01000110, 0b00101111, 0b00110101, 0b00111111, 0b00011111,
  100.                                       0b00100011, 0b00100111, 0b00101010, 0b00101111, 0b00100011, 0b00100111,
  101.                                       0b00101010, 0b00011111, 0b00100011, 0b00100111, 0b00101010, 0b00101111,
  102.                                       0b00101010, 0b00101111, 0b00100111};
  103.  
  104. unsigned char dutyCycleCCP1CON[27] = {0b00101111, 0b00001111, 0b00101111, 0b00101111, 0b00101111, 0b00101111, 0b00101111,
  105.                                       0b00101111, 0b00101111, 0b00001111, 0b00001111, 0b00101111, 0b00001111, 0b00101111,
  106.                                       0b00001111, 0b00001111, 0b00001111, 0b00101111, 0b00001111, 0b00101111, 0b00001111,
  107.                                       0b00101111, 0b00001111, 0b00001111, 0b00001111, 0b00001111, 0b00101111};
  108.  
  109. unsigned int delayTimings[27] = {300, 300, 500, 700, 1000, 1000, 1000, 1000, 1000, 1000, 1400/*32PRESCALE*/, 250, 250, 300, 250, 700,
  110.                                   250, 250, 700, 500, 300, 300, 700, 500, 500, 500, 1000};
  111.  
  112. unsigned char tmr0HByte[27] = {0xB6, 0xB6, 0x85, 0x55, 0x0B,0x0B,0x0B,0x0B,0x0B,0x0B, 0x55/*32PRESCALE*/,
  113.                                0xC2,0xC2,0xB6, 0xC2,0x55,0xC2,0xC2,0x55,0x85,0xB6,0xB6,0x55,0x85,0x85,0x85,
  114.                                 0x0B};
  115. unsigned char tmr0LByte[27] = {0xC2, 0xC2, 0xEE, 0x1A, 0xDC,0xDC,0xDC,0xDC,0xDC,0xDC, 0x1A/*32PRESCALE*/,
  116.                                0xF7,0xF7, 0xC2,0xF7,0x1A,0xF7,0xF7,0x1A,0xEE,0xC2,0xC2,0x1A,0xEE,0xEE,0xEE,
  117.                                0xDC};
  118.  
  119. unsigned char tmr0CON[27] = {0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000100,
  120.                              0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,0b10000011,
  121.                              0b10000011,0b10000011,0b10000011,0b10000011,0b10000011};
  122.  
  123. unsigned int i = 0;
  124. unsigned int x = 0;
  125.  
  126. void main ()
  127. {
  128. ADCON1 = 0x0F;  
  129. INTCONbits.GIEH = 0; // disables all interrupts
  130. RCONbits.IPEN = 1; // IPEN
  131. INTCON2bits.TMR0IP = 1; // high priority
  132. TMR0H = 0x00;
  133. TMR0L = 0x00;
  134. TRISC = 0b11111011; // Configure RC2 (CCP1) as output
  135.  
  136.     TMR0H = tmr0HByte[i];
  137.     TMR0L = tmr0LByte[i];
  138.     T0CON = tmr0CON[i];
  139.     INTCONbits.TMR0IF = 0;
  140.     INTCONbits.TMR0IE = 1;
  141.     INTCONbits.GIEH = 1;
  142.     while(x<27); // Do nothing, until all tones played
  143.  
  144. }
  145.  
  146. void interrupt highISR(void){
  147.     if(INTCONbits.TMR0IF)
  148.     {
  149.         T2CON = 0b00000111;   //But
  150.         PR2 = pr2[x];
  151.         CCPR1L = dutyCycleCCPR1L[x]; // in array
  152.         CCP1CON =  dutyCycleCCP1CON[x];
  153.         INTCONbits.TMR0IF = 0;
  154.         x++;
  155.     }
  156. }
RAW Paste Data