Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Goal : To become familiar with using Buzzer on Dragon12-Light boards, the timer

ID: 3845251 • Letter: G

Question

Goal: To become familiar with using Buzzer on Dragon12-Light boards, the timer output compare function, and interrupt method, as well as to improve your programming skills.

Project: Write a C program to do the following:

Write your own program to turn the Buzzer on and off in exactly one millisecond frequency by using the timer output compare and interrupt method.

The Buzzer is connected to PT5 of the port T. In your main program, you need to write codes to configure PT5 and timer channel 5 so that interrupt for channel 5 output compare is enabled, and PT5 is configured in toggle mode. You need to write an interrupt service routine for timer channel 5 to provide a one millisecond delay (or interrupt).

In EVB (or Debug) mode, the clock frequency for instructions is 24MHz. Use a variable called “ClockCycles” that provides the correct number of clock cycles for the service routine to generate exactly one millisecond delay. This variable is similar to the variable “Period” in the example program Buzzer.txt.

Here is the example program Buzzer

//*****************************************************************
//   Include derivative-specific definitions   
//*****************************************************************   
//The microcontroller chip used by Dragon12-Light boards
#include <mc9s12dg256.h> /* derivative information */


//*****************************************************************
// Gloable Variables
//*****************************************************************

//*****************************************************************
// Function Prototype
//*****************************************************************
void MSDelay(unsigned int, unsigned in);

//*****************************************************************
// Main program section   
//*****************************************************************
void main(void)
{

DDRT = DDRT | 0b00100000; //PTT5 as output

for (;;) {
PTT = PTT | 0x20; //make PT5=1
MSDelay(70, 70); //change the delay size to see what happens
PTT = PTT & 0xDF; //Make PT5=0
MSDelay(70, 70); //change delay size....

}

}

//*****************************************************************
// Subroutine: MSDelay   
//*****************************************************************
void MSDelay(unsigned int itime, unsigned int jtime) //msec delay
{
unsigned int i; unsigned int j;
for(i=0;i<itime;i++)
for(j=0;j<jtime;j++);
}

//*****************************************************************
// Interrupt Service Routines
//*****************************************************************

Explanation / Answer

#define F_CPU 8000000UL #include #include #include /* global variables for music player Play_tune: set to 1 to play tune. Music routine resets to zero when done. *tune_array = pointer to tune array consisting of byte pairs [duration, note]. duration = 8 for whole note, 4 for half note, etc. duration = 0 terminates tune. note = timer constant corresponding to predefined note value taken from table defined below. tempo = (time in milliseconds for whole note)/8 In this version, the zeroth entry of the tune data array contains (tempo)/8 the address of the first entry is passed to the music routine */ unsigned char play_tune,*tune_array; unsigned int tempo; unsigned int ms_tick=0; //general purpose millisecond timer tick, incremented by TIMER2 /* TIMER 2 OCR2A interrupt service routine This routine is called when TIMER2 count (TCNT2) = OCR2A, which is set on the fly for each note. Operation is to simply toggle the buzzer line, producing frequency = 1/(2*timer period) */ ISR(TIMER2_COMPA_vect) { PORTB ^= 1; //toggle buzzer output line } /* TIMER 0 OCR0A interrupt service routine This routine is called every millisecond, when TCNT0 = OCR0A (125) Operation: -increment general purpose timer tick at 1 ms intervals -if tempo>0, continue playing current tune, pointed to by *tune_array -signals "done playing tune" by setting global variable play_tune->0 */ ISR(TIMER0_COMPA_vect) { static unsigned int duration=0; //internal timer for current note volatile unsigned char dur,note; ms_tick++; //increment timer tick at 1 ms intervals if(play_tune) { //are we playing a tune? if(duration>0) duration--; //yes, count down this note's timer else { //done with this note, get next [duration,note] pair dur=*tune_array++; duration = tempo*dur; //calculate time in ms to play this note if(dur==0) play_tune=0; //duration entry=0, so signal done playing tune note=*tune_array++; if (note>0) { //check for note or silence to play OCR2A = note; //set timer interrupt period and point to next entry in tune table DDRB |= 1; //turn on buzzer output line } else { OCR2A=255; //silence, reduce interrupt load DDRB &=~(1); //and turn off buzzer } } } } /* Set up buzzer to play tones. Timer/Counter 2 is used in CTC mode, clear timer on compare/match Clocked at 125 kHz = 8MHz/64 OCR2A overflow buzzer value freq freq 255 488 244 (Hz) 128 976 488 64 1953 976 32 3906 1953 For other CPU clock or timer ratios, multiply or divide accordingly */ void init_buzzer(void) { DDRB |= 1; //set data direction reg bit 0 to output PORTB &=~(1); //set buzzer output low /* initialize timer 0 */ TCCR0A = 0; //tmr0 Clear Timer on Compare Match TCCR0B = _BV(WGM01)|_BV(CS01)|_BV(CS00); //CTC mode with CPU clock/64 = 125 kHz OCR0A = 125; //set for 1 ms interrupt TCNT0=0; TIMSK0 = (1
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote