phillip_bourdon234

KeyboardMain.c

May 29th, 2020 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.70 KB | None | 0 0
  1. // A video demonstration of this project can be found here: https://bit.ly/2AWLzZC  
  2.  
  3. /*
  4.     The parts that were used to make this project consist of the following:
  5.     [1] PIC18F4520
  6.     [1] 5v 1a power supply
  7.     [11] standard push buttons
  8.     [1] 5v active buzzer  
  9.     [1] 10k potentiometer
  10.     [11] 5k resistors
  11.     [50+] jumper wires    
  12. */
  13. #include <xc.h>
  14. #include <pic18f4520.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include "Keyboard.h"
  20.  
  21. #define BUTTON_ONE PORTAbits.RA1
  22. #define BUTTON_TWO PORTAbits.RA2
  23. #define BUTTON_THREE PORTAbits.RA3
  24. #define BUTTON_FOUR PORTAbits.RA4
  25. #define BUTTON_FIVE PORTAbits.RA5
  26. #define BUTTON_SIX PORTEbits.RE0
  27. #define BUTTON_SEVEN PORTEbits.RE1
  28. #define BUTTON_EIGHT PORTEbits.RE2
  29. #define BUTTON_OCTAVE PORTDbits.RD3
  30. #define BUTTON_SAVE PORTCbits.RC4
  31.  
  32. #define ON 255
  33. #define OFF 0
  34.  
  35. void __interrupt() HIGH_ISR(void);
  36. void debounceInterrupt(void);
  37.  
  38. void PWM_Set(uint16_t duty);
  39.  
  40. int EEPROM_Read(int address);
  41. void EEPROM_Write(int address, int data);
  42.  
  43. void bitSetupLED(void);
  44. void bitSetupPWM(void);
  45. void bitSetupADC(void);
  46. void bitSetupOscillator(void);
  47. void bitSetupButton(void);
  48. void bitSetupInterrupts(void);
  49.  
  50. void initPot(void);
  51. void handleOctaveChange(void);
  52. void playBuzzer(int octave, int note);
  53. void handleSaveOption(void);
  54. void saveTones(void);
  55. void initSavedTones(void);
  56.  
  57. _Bool programmingMode = false;
  58.  
  59. int toneValue = 0;
  60. int octaveState = 0;
  61. int debounce = 0;
  62. int buttonPressed;
  63.  
  64. int specificButtonTone[8];
  65. int specificButtonOctave[8];
  66.  
  67. int specificToneAddr[8] = {0,2,4,6,8,10,12,14};
  68. int specificOctaveAddr[8] = {1,3,5,7,9,11,13,15};
  69.  
  70. void main(void)
  71. {
  72.     bitSetupLED();
  73.     bitSetupPWM();
  74.     bitSetupADC();
  75.     bitSetupOscillator();
  76.     bitSetupButton();
  77.     bitSetupInterrupts();
  78.    
  79.     initSavedTones();
  80.  
  81.     while(1)
  82.     {
  83.         if(programmingMode == 1)
  84.         {
  85.             initPot();
  86.             handleOctaveChange();
  87.            
  88.             if(BUTTON_ONE == 0)
  89.             {
  90.                 specificButtonTone[0] = toneValue;
  91.                 specificButtonOctave[0] = octaveState;
  92.             }
  93.             else if(BUTTON_TWO == 0)
  94.             {
  95.                 specificButtonTone[1] = toneValue;
  96.                 specificButtonOctave[1] = octaveState;
  97.             }
  98.             else if(BUTTON_THREE == 0)
  99.             {
  100.                 specificButtonTone[2] = toneValue;
  101.                 specificButtonOctave[2] = octaveState;
  102.             }
  103.             else if(BUTTON_FOUR == 0)
  104.             {
  105.                 specificButtonTone[3] = toneValue;
  106.                 specificButtonOctave[3] = octaveState;
  107.             }
  108.             else if(BUTTON_FIVE == 0)
  109.             {
  110.                 specificButtonTone[4] = toneValue;
  111.                 specificButtonOctave[4] = octaveState;
  112.             }
  113.             else if(BUTTON_SIX == 0)
  114.             {
  115.                 specificButtonTone[5] = toneValue;
  116.                 specificButtonOctave[5] = octaveState;
  117.             }
  118.             else if(BUTTON_SEVEN == 0)
  119.             {
  120.                 specificButtonTone[6] = toneValue;
  121.                 specificButtonOctave[6] = octaveState;
  122.             }
  123.             else if(BUTTON_EIGHT == 0)
  124.             {
  125.                 specificButtonTone[7] = toneValue;
  126.                 specificButtonOctave[7] = octaveState;
  127.             }
  128.         }
  129.         else
  130.         {
  131.             handleSaveOption();
  132.            
  133.             if(BUTTON_ONE == 0)
  134.             {
  135.                 playBuzzer(specificButtonOctave[0], specificButtonTone[0]);
  136.             }
  137.             else if(BUTTON_TWO == 0)
  138.             {
  139.                 playBuzzer(specificButtonOctave[1], specificButtonTone[1]);
  140.             }
  141.             else if(BUTTON_THREE == 0)
  142.             {
  143.                 playBuzzer(specificButtonOctave[2], specificButtonTone[2]);
  144.             }
  145.             else if(BUTTON_FOUR == 0)
  146.             {
  147.                 playBuzzer(specificButtonOctave[3], specificButtonTone[3]);
  148.             }
  149.             else if(BUTTON_FIVE == 0)
  150.             {
  151.                 playBuzzer(specificButtonOctave[4], specificButtonTone[4]);
  152.             }
  153.             else if(BUTTON_SIX == 0)
  154.             {
  155.                 playBuzzer(specificButtonOctave[5], specificButtonTone[5]);
  156.             }
  157.             else if(BUTTON_SEVEN == 0)
  158.             {
  159.                 playBuzzer(specificButtonOctave[6], specificButtonTone[6]);
  160.             }
  161.             else if(BUTTON_EIGHT == 0)
  162.             {
  163.                 playBuzzer(specificButtonOctave[7], specificButtonTone[7]);
  164.             }
  165.             else
  166.             {
  167.                 PWM_Set(OFF); //turns buzzer off
  168.             }
  169.         }        
  170.         debounceInterrupt();
  171.     }    
  172. }
  173.  
  174. void __interrupt() HIGH_ISR()
  175. {
  176.     INTCONbits.GIEH = 0;
  177.    
  178.     if(INTCONbits.INT0IF)
  179.     {
  180.         if(debounce == 0)
  181.         {
  182.             programmingMode = !programmingMode;
  183.             debounce = 1;
  184.         }
  185.         INTCONbits.INT0IF = 0;
  186.     }
  187.  
  188.     INTCONbits.GIEH = 1;  
  189. }
  190.  
  191. void debounceInterrupt()
  192. {
  193.     if(PORTBbits.RB0 == 1)
  194.     {
  195.         debounce = 0;
  196.     }
  197. }
  198.  
  199. void PWM_Set(uint16_t duty)
  200. {
  201.     CCP2CONbits.DC2B = (uint8_t)(duty & 0x0003);
  202.     CCPR2L = (uint8_t)(duty >> 2);
  203. }
  204.  
  205. int EEPROM_Read(int address)
  206. {
  207.     EEADR = address;
  208.     EECON1bits.EEPGD = 0;
  209.     EECON1bits.CFGS = 0;
  210.     EECON1bits.RD = 1;
  211.     while(EECON1bits.RD);
  212.     return EEDATA;
  213. }
  214.  
  215. void EEPROM_Write(int address, int data)
  216. {
  217.     EEADR = address;
  218.     EEDATA = data;
  219.     EECON1bits.EEPGD = 0;
  220.     EECON1bits.CFGS = 0;
  221.     EECON1bits.WREN = 1;
  222.     INTCONbits.GIE = 0;
  223.    
  224.     EECON2 = 0x55;
  225.     EECON2 = 0xAA;
  226.    
  227.     EECON1bits.WR = 1;
  228.     while(EECON1bits.WR);
  229.    
  230.     EECON1bits.WREN = 0;
  231.     INTCONbits.GIE = 1;  
  232. }
  233.  
  234. void bitSetupInterrupts()
  235. {
  236.     RCONbits.IPEN = 1;
  237.     INTCONbits.INT0IE = 1;
  238.     INTCON2bits.INTEDG0 = 1;
  239.     INTCONbits.GIEH = 1;
  240. }
  241.  
  242. void bitSetupLED()
  243. {
  244.     TRISDbits.RD2 = 0;
  245.     LATDbits.LD2 = 0;
  246. }
  247.  
  248. void bitSetupButton()
  249. {
  250.     TRISBbits.RB0 = 1;
  251.     TRISDbits.RD3 = 1;
  252.     TRISAbits.RA1 = 1;
  253.     TRISAbits.RA2 = 1;
  254.     TRISAbits.RA3 = 1;
  255.     TRISAbits.RA4 = 1;
  256.     TRISAbits.RA5 = 1;
  257.     TRISCbits.RC4 = 1;
  258.     TRISEbits.RE0 = 1;
  259.     TRISEbits.RE1 = 1;
  260.     TRISEbits.RE2 = 1;
  261. }
  262.  
  263. void bitSetupOscillator()
  264. {
  265.     OSCCONbits.IRCF = 0b110;
  266.     OSCCONbits.SCS = 0b10;
  267.     while(OSCCONbits.IOFS != 1);
  268. }
  269.  
  270. void bitSetupADC()
  271. {
  272.     TRISAbits.RA0 = 1;
  273.     ADCON0bits.CHS = 0;
  274.     ADCON0bits.ADON = 1;
  275.     ADCON1bits.VCFG0 = 0;
  276.     ADCON1bits.VCFG1 = 0;
  277.     ADCON1bits.PCFG = 0b1110;
  278.     ADCON2bits.ADFM = 1;
  279.     ADCON2bits.ACQT = 0x04;
  280.     ADCON2bits.ADCS = 0x04;
  281. }
  282.  
  283. void bitSetupPWM()
  284. {
  285.     TRISCbits.RC1 = 0;
  286.     T2CONbits.T2OUTPS = 0x00;
  287.     T2CONbits.T2CKPS = 0b10;
  288.     PR2 = 250;
  289.     CCP2CONbits.DC2B = 0;
  290.     CCP2CONbits.CCP2M = 0x0C;
  291.     CCPR2L = 0;
  292.     T2CONbits.TMR2ON = 1;
  293. }
  294.  
  295. void initPot()
  296. {
  297.     ADCON0bits.GODONE = 1;
  298.     while(ADCON0bits.GODONE);
  299.        
  300.     toneValue = (ADRESH << 8) | (ADRESL);
  301.        
  302.     toneValue = toneValue * 0.249; // map the analog value (0-1024) to acceptable PR2 values (0-255)
  303.      
  304.     PR2 = toneValue;
  305.    
  306.     PWM_Set(ON);
  307. }
  308.  
  309. void handleOctaveChange()
  310. {
  311.     if(BUTTON_OCTAVE == 0)
  312.     {
  313.         buttonPressed = 1;
  314.     }
  315.     while(BUTTON_OCTAVE == 1 && buttonPressed == 1)
  316.     {
  317.         octaveState++;
  318.         buttonPressed = 0;
  319.     }    
  320.     if(octaveState == 0)
  321.     {
  322.         OSCCONbits.IRCF = 0b101;
  323.     }
  324.     else if(octaveState == 1)
  325.     {
  326.         OSCCONbits.IRCF = 0b110;
  327.     }
  328.     else if(octaveState == 2)
  329.     {
  330.         OSCCONbits.IRCF = 0b111;
  331.     }
  332.     else
  333.     {
  334.         octaveState = 0;
  335.     }
  336. }
  337.  
  338. void playBuzzer(int octave, int note)
  339. {
  340.     if(octave == 0)
  341.     {
  342.         OSCCONbits.IRCF = 0b101;
  343.     }
  344.     else if(octave == 1)
  345.     {
  346.         OSCCONbits.IRCF = 0b110;
  347.     }
  348.     else if(octave == 2)
  349.     {
  350.         OSCCONbits.IRCF = 0b111;
  351.     }
  352.     PR2 = note;
  353.     PWM_Set(ON);
  354. }
  355.  
  356. void handleSaveOption()
  357. {
  358.     if(BUTTON_SAVE == 0)
  359.     {
  360.         buttonPressed = 1;
  361.     }
  362.     while(BUTTON_SAVE == 1 && buttonPressed == 1)
  363.     {
  364.         saveTones();
  365.         buttonPressed = 0;
  366.     }      
  367. }
  368.  
  369. void saveTones()
  370. {
  371.     for(int t = 0; t < 8; t++)
  372.     {
  373.         EEPROM_Write(specificToneAddr[t], specificButtonTone[t]);
  374.     }
  375.     for(int o = 0; o < 8; o++)
  376.     {
  377.         EEPROM_Write(specificOctaveAddr[o], specificButtonOctave[o]);
  378.     }
  379. }
  380.  
  381. void initSavedTones()
  382. {
  383.     for(int t = 0; t < 8; t++)
  384.     {
  385.         specificButtonTone[t] = EEPROM_Read(specificToneAddr[t]);
  386.     }
  387.     for(int o = 0; o < 8; o++)
  388.     {
  389.         specificButtonOctave[o] = EEPROM_Read(specificOctaveAddr[o]);
  390.     }
  391. }
Add Comment
Please, Sign In to add comment