Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A video demonstration of this project can be found here: https://bit.ly/2AWLzZC
- /*
- The parts that were used to make this project consist of the following:
- [1] PIC18F4520
- [1] 5v 1a power supply
- [11] standard push buttons
- [1] 5v active buzzer
- [1] 10k potentiometer
- [11] 5k resistors
- [50+] jumper wires
- */
- #include <xc.h>
- #include <pic18f4520.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include "Keyboard.h"
- #define BUTTON_ONE PORTAbits.RA1
- #define BUTTON_TWO PORTAbits.RA2
- #define BUTTON_THREE PORTAbits.RA3
- #define BUTTON_FOUR PORTAbits.RA4
- #define BUTTON_FIVE PORTAbits.RA5
- #define BUTTON_SIX PORTEbits.RE0
- #define BUTTON_SEVEN PORTEbits.RE1
- #define BUTTON_EIGHT PORTEbits.RE2
- #define BUTTON_OCTAVE PORTDbits.RD3
- #define BUTTON_SAVE PORTCbits.RC4
- #define ON 255
- #define OFF 0
- void __interrupt() HIGH_ISR(void);
- void debounceInterrupt(void);
- void PWM_Set(uint16_t duty);
- int EEPROM_Read(int address);
- void EEPROM_Write(int address, int data);
- void bitSetupLED(void);
- void bitSetupPWM(void);
- void bitSetupADC(void);
- void bitSetupOscillator(void);
- void bitSetupButton(void);
- void bitSetupInterrupts(void);
- void initPot(void);
- void handleOctaveChange(void);
- void playBuzzer(int octave, int note);
- void handleSaveOption(void);
- void saveTones(void);
- void initSavedTones(void);
- _Bool programmingMode = false;
- int toneValue = 0;
- int octaveState = 0;
- int debounce = 0;
- int buttonPressed;
- int specificButtonTone[8];
- int specificButtonOctave[8];
- int specificToneAddr[8] = {0,2,4,6,8,10,12,14};
- int specificOctaveAddr[8] = {1,3,5,7,9,11,13,15};
- void main(void)
- {
- bitSetupLED();
- bitSetupPWM();
- bitSetupADC();
- bitSetupOscillator();
- bitSetupButton();
- bitSetupInterrupts();
- initSavedTones();
- while(1)
- {
- if(programmingMode == 1)
- {
- initPot();
- handleOctaveChange();
- if(BUTTON_ONE == 0)
- {
- specificButtonTone[0] = toneValue;
- specificButtonOctave[0] = octaveState;
- }
- else if(BUTTON_TWO == 0)
- {
- specificButtonTone[1] = toneValue;
- specificButtonOctave[1] = octaveState;
- }
- else if(BUTTON_THREE == 0)
- {
- specificButtonTone[2] = toneValue;
- specificButtonOctave[2] = octaveState;
- }
- else if(BUTTON_FOUR == 0)
- {
- specificButtonTone[3] = toneValue;
- specificButtonOctave[3] = octaveState;
- }
- else if(BUTTON_FIVE == 0)
- {
- specificButtonTone[4] = toneValue;
- specificButtonOctave[4] = octaveState;
- }
- else if(BUTTON_SIX == 0)
- {
- specificButtonTone[5] = toneValue;
- specificButtonOctave[5] = octaveState;
- }
- else if(BUTTON_SEVEN == 0)
- {
- specificButtonTone[6] = toneValue;
- specificButtonOctave[6] = octaveState;
- }
- else if(BUTTON_EIGHT == 0)
- {
- specificButtonTone[7] = toneValue;
- specificButtonOctave[7] = octaveState;
- }
- }
- else
- {
- handleSaveOption();
- if(BUTTON_ONE == 0)
- {
- playBuzzer(specificButtonOctave[0], specificButtonTone[0]);
- }
- else if(BUTTON_TWO == 0)
- {
- playBuzzer(specificButtonOctave[1], specificButtonTone[1]);
- }
- else if(BUTTON_THREE == 0)
- {
- playBuzzer(specificButtonOctave[2], specificButtonTone[2]);
- }
- else if(BUTTON_FOUR == 0)
- {
- playBuzzer(specificButtonOctave[3], specificButtonTone[3]);
- }
- else if(BUTTON_FIVE == 0)
- {
- playBuzzer(specificButtonOctave[4], specificButtonTone[4]);
- }
- else if(BUTTON_SIX == 0)
- {
- playBuzzer(specificButtonOctave[5], specificButtonTone[5]);
- }
- else if(BUTTON_SEVEN == 0)
- {
- playBuzzer(specificButtonOctave[6], specificButtonTone[6]);
- }
- else if(BUTTON_EIGHT == 0)
- {
- playBuzzer(specificButtonOctave[7], specificButtonTone[7]);
- }
- else
- {
- PWM_Set(OFF); //turns buzzer off
- }
- }
- debounceInterrupt();
- }
- }
- void __interrupt() HIGH_ISR()
- {
- INTCONbits.GIEH = 0;
- if(INTCONbits.INT0IF)
- {
- if(debounce == 0)
- {
- programmingMode = !programmingMode;
- debounce = 1;
- }
- INTCONbits.INT0IF = 0;
- }
- INTCONbits.GIEH = 1;
- }
- void debounceInterrupt()
- {
- if(PORTBbits.RB0 == 1)
- {
- debounce = 0;
- }
- }
- void PWM_Set(uint16_t duty)
- {
- CCP2CONbits.DC2B = (uint8_t)(duty & 0x0003);
- CCPR2L = (uint8_t)(duty >> 2);
- }
- int EEPROM_Read(int address)
- {
- EEADR = address;
- EECON1bits.EEPGD = 0;
- EECON1bits.CFGS = 0;
- EECON1bits.RD = 1;
- while(EECON1bits.RD);
- return EEDATA;
- }
- void EEPROM_Write(int address, int data)
- {
- EEADR = address;
- EEDATA = data;
- EECON1bits.EEPGD = 0;
- EECON1bits.CFGS = 0;
- EECON1bits.WREN = 1;
- INTCONbits.GIE = 0;
- EECON2 = 0x55;
- EECON2 = 0xAA;
- EECON1bits.WR = 1;
- while(EECON1bits.WR);
- EECON1bits.WREN = 0;
- INTCONbits.GIE = 1;
- }
- void bitSetupInterrupts()
- {
- RCONbits.IPEN = 1;
- INTCONbits.INT0IE = 1;
- INTCON2bits.INTEDG0 = 1;
- INTCONbits.GIEH = 1;
- }
- void bitSetupLED()
- {
- TRISDbits.RD2 = 0;
- LATDbits.LD2 = 0;
- }
- void bitSetupButton()
- {
- TRISBbits.RB0 = 1;
- TRISDbits.RD3 = 1;
- TRISAbits.RA1 = 1;
- TRISAbits.RA2 = 1;
- TRISAbits.RA3 = 1;
- TRISAbits.RA4 = 1;
- TRISAbits.RA5 = 1;
- TRISCbits.RC4 = 1;
- TRISEbits.RE0 = 1;
- TRISEbits.RE1 = 1;
- TRISEbits.RE2 = 1;
- }
- void bitSetupOscillator()
- {
- OSCCONbits.IRCF = 0b110;
- OSCCONbits.SCS = 0b10;
- while(OSCCONbits.IOFS != 1);
- }
- void bitSetupADC()
- {
- TRISAbits.RA0 = 1;
- ADCON0bits.CHS = 0;
- ADCON0bits.ADON = 1;
- ADCON1bits.VCFG0 = 0;
- ADCON1bits.VCFG1 = 0;
- ADCON1bits.PCFG = 0b1110;
- ADCON2bits.ADFM = 1;
- ADCON2bits.ACQT = 0x04;
- ADCON2bits.ADCS = 0x04;
- }
- void bitSetupPWM()
- {
- TRISCbits.RC1 = 0;
- T2CONbits.T2OUTPS = 0x00;
- T2CONbits.T2CKPS = 0b10;
- PR2 = 250;
- CCP2CONbits.DC2B = 0;
- CCP2CONbits.CCP2M = 0x0C;
- CCPR2L = 0;
- T2CONbits.TMR2ON = 1;
- }
- void initPot()
- {
- ADCON0bits.GODONE = 1;
- while(ADCON0bits.GODONE);
- toneValue = (ADRESH << 8) | (ADRESL);
- toneValue = toneValue * 0.249; // map the analog value (0-1024) to acceptable PR2 values (0-255)
- PR2 = toneValue;
- PWM_Set(ON);
- }
- void handleOctaveChange()
- {
- if(BUTTON_OCTAVE == 0)
- {
- buttonPressed = 1;
- }
- while(BUTTON_OCTAVE == 1 && buttonPressed == 1)
- {
- octaveState++;
- buttonPressed = 0;
- }
- if(octaveState == 0)
- {
- OSCCONbits.IRCF = 0b101;
- }
- else if(octaveState == 1)
- {
- OSCCONbits.IRCF = 0b110;
- }
- else if(octaveState == 2)
- {
- OSCCONbits.IRCF = 0b111;
- }
- else
- {
- octaveState = 0;
- }
- }
- void playBuzzer(int octave, int note)
- {
- if(octave == 0)
- {
- OSCCONbits.IRCF = 0b101;
- }
- else if(octave == 1)
- {
- OSCCONbits.IRCF = 0b110;
- }
- else if(octave == 2)
- {
- OSCCONbits.IRCF = 0b111;
- }
- PR2 = note;
- PWM_Set(ON);
- }
- void handleSaveOption()
- {
- if(BUTTON_SAVE == 0)
- {
- buttonPressed = 1;
- }
- while(BUTTON_SAVE == 1 && buttonPressed == 1)
- {
- saveTones();
- buttonPressed = 0;
- }
- }
- void saveTones()
- {
- for(int t = 0; t < 8; t++)
- {
- EEPROM_Write(specificToneAddr[t], specificButtonTone[t]);
- }
- for(int o = 0; o < 8; o++)
- {
- EEPROM_Write(specificOctaveAddr[o], specificButtonOctave[o]);
- }
- }
- void initSavedTones()
- {
- for(int t = 0; t < 8; t++)
- {
- specificButtonTone[t] = EEPROM_Read(specificToneAddr[t]);
- }
- for(int o = 0; o < 8; o++)
- {
- specificButtonOctave[o] = EEPROM_Read(specificOctaveAddr[o]);
- }
- }
Add Comment
Please, Sign In to add comment