Skip to main content

Posts

JUKEBOX: Source code for Jukebox using Timer0 interrupts as time signature control and PWM for note generation. Source Code:   /*Program Name: Jukebox Author: TY Lew This program generates a tune at the piezo speaker of PICDEM2 Plus. It uses PWM to generate each note and timer0 interrupt to execute the time signature of each note. Thus, the PR2 is updated with the value upon each inetrrupt. Whereas, the CCPR1L simple updated with PR2/2 for 50-50 mark-space ratio. The CCP1CON<5:4> is ignore as we do not need such accuracy for the tones. The 'note' array determines the tone frequecy of the notes that will go into PR2. The number is the MIDI notation. The 'tmsig' array determines the duration of each note and will be used to update timer registers. The larger the value the longer is the duration. In general the sytem goes into the ISR for every tone played. */ /* Pressing RA4 button toggles the sound tune on and off */ #include <xc.h> #include <stdlib.h>
Recent posts

Why "PORTB=~PORTB" sometime does not work?

When performinging READ-MODIFY-WRITE action, some target board setup may encounter problems. Don't know what is this? Simple. Let's look at the following example: PORTB=~PORTB; The statement required the PORTB to be read first then invert then assign back to PORTB, when executed. This is called READ-MODIFY-WRITE. Some students (not all) encounter problem whereby the execution does not lead to the toggling port PORTB. The remedy is to change the statement to: PORTB=~LATB. Why? All the I/O ports in the PIC18F are buffered when they are configured as output ports. In this context, all the 8 bits of PORTB are directed from the output sides of the buffer. The output devices (e.g. LEDs) may have loaded heavily and causing the voltage to drop far below the VDD value. When the READ action takes place the logic states might have been read wrongly. Consider that the PORTB LEDs are currently lighted up by logic 1s. If the READ-MODIFY-WRIRE statement mentioned above is used to t

Generating Monophonic Melody using PWM of PIC18

/*Program Name: Jukebox Author: TY Lew This program generates a tune at the piezo speaker of PICDEM2 Plus. It uses PWM to generate each note and timer0 interrupt to execute the time signature of each note. Thus, the PR2 is updated with the value upon each inetrrupt. Whereas, the CCPR1L simple updated with PR2/2 for 50-50 mark-space ratio. The CCP1CON<5:4> is ignore as we do not need such accuracy for the tones. The 'note' array determines the tone frequecy of the notes that will go into PR2. The number is the MIDI notation. The 'tmsig' array determines the duration of each note and will be used to update timer registers. The larger the value the longer is the duration. In general the sytem goes into the ISR for every tone played. */ /* Pressing RA4 button toggles the sound tune on and off */ #include < xc.h > #include < stdlib.h > const char note[30]={72,74,76,78,78,79,83,81,78,81,79,78,79,76,78,74,76,78,78,79,83,81,78,81,79,78,79,76,74,0}; const char