Lab 3 grading rubric:

B functionality: Write a C program which generates tones according to the musical scale from the octave starting at (440hz - 831hz) up 1 octaves to (880 - 1760hz).

Freq / Init (dec) / Init (hex)
440 / 114 / 0x72
466 / 122 / 0x7A
494 / 129 / 0x81
523 / 136 / 0x88
554 / 143 / 0x8F
587 / 150 / 0x96
622 / 156 / 0x9C
659 / 161 / 0xA1
698 / 166 / 0xA6
740 / 172 / 0xAC
784 / 176 / 0xB0
831 / 181 / 0xB5

Pressing and then releasing the upper/lower button should move the tone to the next higher/lower note.

Pressing both buttons should cause the generated tone to stop playing until a button is pressed and released.

Store only 12 notes, one octave, in an array.

Change prescalar on change in octave. Prescalar = 6 for 880-1760. Prescalar = 7 for 440-831.

Notes:

Write T0PS when you change the prescalar variable (style)

Checking “note < 0” requires signed variable (typing)

Don’t perform spurious computations that could be done statically “255-note[i]” (efficiency)

Don’t use 16-bit variable when 8-bits could suffice (efficiency)

Don’t use meaningless variable names (e.x. “j” instead of “octave”) (maintain)

Missing academic integrity statement (style)

A functionality:

You will need to use a second timer (running in 16-bit mode) to generate the correct duration for the notes. Your song should have at least 4 distinct notes.

Notes:

Long run of calls to subroutine (use a loop and array) (maintain/efficiency)

Do not include magic numbers in your code. (maintain)

No subroutines (maintain/efficiency)

Wrong datatypes (assigning negative numbers with unsigned chars) (correctness)

Use proper tab indenting for nested control structures (maintain)

No floats or doubles in this class, ever. No exceptions (efficiency)

Pseudo code for song

int8 note[] = {B5, A6, G5, … // Note and octave, defined earlier

int16 noteDuration[] = {qn, hn, fn, … // constants for correct note duration

void main(void) {

for (i=0; i<sizeof(note); i++) play_note(note[i], noteDuration[i]);

} // end main

void play_note(int8 half_period8, int16 duration) {

int16 half_period;

half_period = (int16) half_period8;

WriteTimer0(noteDuration[i]);

INTCON0bits.T0IF = 0;

while(INTCON0bits.T0IF == 0) {

WriteTimer1(half_period);

T1CONbits.T1IF = 0;

while(T1CONbits.T1IF == 0);

LATCbits.LATC1 ^= 1;

} // end while note duration

} // end play_note