Brad Geltz EGRE631 – Embedded Systems April 15th, 2009 Introduction • Project motivations • Commercial products Sound • Transmission/Reception • Signals/Harmonics Guitars • Music theory • Signal generation (pickups) Tuner • • • • PIC18F4550 Microcontroller Features used Program outline Results Commercial product comparison Conclusion Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 2 Project motivations • Instrument tuning not a trivial problem • Personal interest Commercial products • Inexpensive • Accurate • More functionality • E.g. Korg GA-30 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 3 Transmission • Signal converted to wave • Transmit Electrical signal applied to coil Creates a electromagnetic field Magnet and coil interact Begin oscillations Basic Loudspeaker Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 4 Reception • Wave converted to signal • Wave causes oscillations in material of receiver (e.g. human ear, microphone) • Microphones (condenser) Vibrations cause charging and discharging of capacitors Generate signal Microphone Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 5 Signals (sound wave) • Properties of waves apply • Amplitude Effects how loud/soft the sound is • Frequency Effects how high/low the pitch is Example sound waves 440Hz to 880Hz Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 6 Music theory • Standard guitar 6 strings Each string tuned to a different note (frequency) Tuning – How close the frequency of the signal generated by the instrument is to its defined value All notes have a defined value in terms of Hertz Equal temperament – Every adjacent note has an identical frequency ration between them Based on Concert A at 440 Hz (key #49) Calculations based on 12 tone equal temperament (12-TET) Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 7 Frequencies • Standard 6 string guitar 1 - E4 – 329.6 Hz 2 – B3 – 246.9 Hz 3 – G3 – 195.9 Hz 4 – D3 – 146.8 Hz 5 – A2 – 110.0 Hz 6 – E2 – 82.4 Hz Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 8 Harmonics • For a given sound wave, the harmonic is an integer multiple of that wave’s frequency 440 Hz A4 (Concert A) 880 Hz A5 (Harmonic) • Problem Harmonics present when base note played Typically not recognized as separate notes “Noise” introduced to signal Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 9 Acoustic • Guitarist plucks note • String vibrates • Transferred to the body (insides) Bridge (where string connects to body) • Projected through sound hole • Specifically engineered to maximize resonance Maintaining maximum amplitude at a particular frequency • No way to pick up sound wave for amplification… Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 10 Electric • Pickups (transducers) Captures or “picks up” the vibration of the string and converts it to electrical signal Wire coiled around magnet Vibrations modulate magnetic flux and thus AC Signal weird to output jack on body of guitar Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 11 Signal (voltage) • Varies Type of pickup Guitar model Etc. • Typically between 100mV and 1V • Multiple sources of noise Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 12 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 13 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 14 Problem • How to determine frequency of analog signal of varying amplitude with a microcontroller? Frequency counter Analog comparator Timing algorithms Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 15 Microcontroller • PIC18F4550 8MHz +/- 2% internal oscillator (up to 48MHz externally) Numerous other oscillator modes (31 KHz, 2MHz, 4MHz, etc.) 2 analog comparators 4 timers (1-8 bit, 3-16 bit) 35 I/O pins USB Programmer (PICkit2) 40-pin DIP 32K flash (program) memory 2K bytes SRAM, 256 bytes of EEPROM (data memory) 10 bit, 13 channel A/D converter Wide Operating Voltage Range (2.0V to 5.5V) Numerous books/guides for C programming Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 16 Microcontroller • PIC18F4550 RISC architecture (8051 is CISC) Large number of registers Small stack 95% of instructions executed in 1 clock cycle Load/store versus memory manipulation of data One instruction cycle consists of 4 oscillator periods Instructions execute at CLKSPD/4 2MHz based on 8MHz clock Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 17 IDE • MPLAB IDE Direct from Microchip Full debugging utilities • MPLAB C18 Free for academic use In-line assembly Included and user created libraries I2C ADC Delays Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 18 PICkit 2 • Wire to PIC • Program direct from MPLAB • Provides debugging • Power for PIC Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 19 Features used • I/O ports LCD screen Newhaven NHD-0216BZRN-YBW Push button • Voltage reference for comparator • Analog comparator • Priority interrupts Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 20 Analog comparator • Set threshold voltage (VIN-) Between VDD and VSS • Set analog source (VIN+) • Output is low until signal crosses threshold voltage Raised to VDD Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 21 Threshold Voltage • Initially with voltage divider 2 resistors of equal size yields VDD / 2 • Voltage reference generated by PIC Setup to use source or external voltages (X/24) * VDD X between 0 and 24 .208V Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 22 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 23 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 24 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 25 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 26 Frequency counter • Solutions Comparator drives external interrupt Too complex Utilize interrupt triggered by output change on comparator Have to differentiate between rising and falling edge Flag set in comparator configuration register Increment variable on interrupt Report counted Hz after a specified amount of time Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 27 Determine tuning • Compare counted frequency versus stored value for particular note • If the counted frequency is less than the store frequency the note currently being played is know as “flat” String needs to be tightened to increase frequency • Frequency greater than the stored value is known as “sharp” String needs to be loosened to decrease frequency Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 28 Algorithm • Main() Wait for change in frequency variable Output to LCD if change Slider Difference of observed value from stored value (Z) Left if flat (Z < 0) Flash “PERFECT PITCH” if in tune (Z = 0) Right if sharp (Z > 0) If button press, advance to next note Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 29 Algorithm • Interrupts Comparator Output Checks to see if positive transition If so, increment frequency counter Timer 0 (2 Hz) Multiplies frequency count by 2 Updates LCD frequency display variable Resets frequency count variable Configured in priority fashion Two interrupt vector locations Each interrupt can be configured as high or low priority Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 30 /* Guitar Tuner Author: Brad Geltz */ #include <p18f4550.h> #include <string.h> #pragma config WDT = OFF, PBADEN = OFF, BOR = OFF #pragma config DEBUG = OFF, MCLRE = OFF, LVP = OFF, FOSC = INTOSCIO_EC #define button PORTCbits.RC6; void chk_isr(void); void chk_low_isr(void); void T0_ISR(void); void CM0_ISR(void); Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 31 //Set code for high and low priority interrupt #pragma code My_HiPrio_Int=0x0008 void My_HiPrio_Int (void){ chk_isr(); } #pragma code My_LoPrio_Int=0x0018 void My_LoPrio_Int (void){ chk_low_isr(); } //Set ISR for high priority interrupts #pragma interrupt chk_isr void chk_isr (void){ if (PIR2bits.CMIF == 1){CM0_ISR();} } //Set ISR for low priority interrupts #pragma interruptlow chk_low_isr void chk_low_isr (void){ if (INTCONbits.TMR0IF == 1){T0_ISR();} } Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 32 //Timer 0 ISR - Interrupts at 2Hz //Multiplies freq_count by 2 and stores it in frequency //Resets frequency count //If the button is not pressed, write the pitch indicator void T0_ISR(void){ TMR0H=-0xF2; TMR0L=-0x24; INTCONbits.TMR0IF=0; frequency = 2 * freq_count; freq_count = 0; if (!PORTCbits.RC6){ set_cursor_loc(0x40); write_string(dashes); set_cursor_loc(0x40); write_pitch_indicator(frequency - freqs[note_index]); } //DEBUG - Uncomment to see frequency //set_cursor_loc(0x40); //Set cursor to beginning of next line //write_int(frequency); } //Comparator 0 ISR - Interrupts on output transition //If it was a positive transition, increment the frequency count void CM0_ISR(){ if (CMCONbits.C1OUT){freq_count++;} PIR2bits.CMIF = 0; } Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 33 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 34 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 35 Error • Priority interrupts increase accuracy • Off by 2 Hz in the worst case Signal running at 31 Hz 15 Hz / 16 Hz Bounce between 30 and 31 Hz • 8 MHz only accurate to +/- 2% • Signal noise False comparator interrupt Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 36 Results • Successfully determines pitch Very briefly Intense vibrations • Not perfectly accurate • Cost ($17) PIC - $5 ¼” Jack - $3 LCD - $9 Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 37 Future iterations • Timer 0 counts between comparator interrupts No need to wait for frequency count for valid output Requires accurate system clock • Band pass filters MAX263 Pin programmable 1Hz to 57 KHz Eliminate harmonic noise • Decrease VDD to decrease threshold voltage Increase valid tuning time Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 38 Commercial products • Korg GA-30 Auto ranging Supports multiple instruments Multiple string configurations Built-in speaker Reference pitch Microphone/Line input Inexpensive (~$15) Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 39 Conclusion • Microcontroller successfully tunes guitar • Future iterations can greatly improve functionality Algorithm tweaks Filters • Add features similar to Korg tuner Brad Geltz | VCU | EGRE 631 - Embedded Systems 4/15/2009 40