It works!
This commit is contained in:
parent
1e7089feae
commit
e9f0377b1c
1341
lab3/piano.asm
1341
lab3/piano.asm
File diff suppressed because it is too large
Load Diff
128
lab3/piano.c
128
lab3/piano.c
|
@ -1,5 +1,6 @@
|
||||||
#include "msp430.h"
|
#include "msp430.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define c 3822
|
#define c 3822
|
||||||
#define dflat 3608
|
#define dflat 3608
|
||||||
|
@ -16,91 +17,83 @@
|
||||||
#define cc 1911
|
#define cc 1911
|
||||||
#define n 0
|
#define n 0
|
||||||
|
|
||||||
unsigned int hbd[30] = {
|
#define LED1 BIT0
|
||||||
c, n, c,
|
#define BUTTON BIT3
|
||||||
d, c, f,
|
#define OUTPUT BIT6
|
||||||
e, c, n, c,
|
|
||||||
d, c, g,
|
// CSINT should be 256^2 / 10 000 = 65 536 / 10 000 = 6
|
||||||
f, c, n, c,
|
// USINT should be 60 000
|
||||||
cc, a, f,
|
#define CSUS 10000 // CSUS microseconds (us) in a centisecond (cs)
|
||||||
e, d, bflat, n, bflat,
|
#define CSINT 6 // centiseconds storable in int of microseconds
|
||||||
a, f, g,
|
#define USINT 60000 // CSINT in microseconds
|
||||||
f, n
|
|
||||||
};
|
#define MAXNOTES 64 // record at most 64 notes (arbitrary)
|
||||||
unsigned int hbd_lengths[30] = {
|
|
||||||
1, 1, 1,
|
volatile bool recording = true;
|
||||||
3, 3, 3,
|
volatile unsigned int us = 0;
|
||||||
6, 1, 1, 1,
|
volatile unsigned int cs = 0;
|
||||||
3, 3, 3,
|
|
||||||
6, 1, 1, 1,
|
|
||||||
3, 3, 3,
|
|
||||||
3, 6, 1, 1, 1,
|
|
||||||
3, 3, 3,
|
|
||||||
6, 3
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int scale[13] = {
|
unsigned int scale[13] = {
|
||||||
c, dflat, d, eflat, e, f, gflat, g, aflat, a, bflat, b, cc
|
c, dflat, d, eflat, e, f, gflat, g, aflat, a, bflat, b, cc
|
||||||
};
|
};
|
||||||
unsigned int scale_lengths[13] = {
|
|
||||||
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int whole[8] = {
|
unsigned int whole[8] = {
|
||||||
c, d, e, f, g, a, b, cc
|
c, d, e, f, g, a, b, cc
|
||||||
};
|
};
|
||||||
|
|
||||||
void play(unsigned int note) {
|
void play(unsigned int note) {
|
||||||
if (note != 0) {
|
|
||||||
CCR0 = note;
|
CCR0 = note;
|
||||||
CCR1 = 100;
|
CCR1 = note == n ? 0 : 100;
|
||||||
} else {
|
|
||||||
CCR1 = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sing(unsigned int* song, unsigned int* song_lengths, unsigned int length) {
|
void sing(unsigned int* song, unsigned int* lengths, unsigned int length) {
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
play(song[i]);
|
play(song[i]);
|
||||||
for (int j = 0; j < song_lengths[i]; j++) {
|
for (int j = 0; j < lengths[i]; j++) {
|
||||||
__delay_cycles(0x200);
|
__delay_cycles(CSUS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int us = 0;
|
unsigned int record(unsigned int* song, unsigned int* lengths) {
|
||||||
unsigned int cs = 0;
|
unsigned int pos = 1;
|
||||||
|
unsigned int note;
|
||||||
|
TAR = 0;
|
||||||
|
P1OUT |= LED1;
|
||||||
|
while (recording) {
|
||||||
|
note = whole[P2IN & 0xF];
|
||||||
|
if (note != song[pos - 1]) {
|
||||||
|
lengths[pos - 1] = cs + (us + TAR)/CSUS;
|
||||||
|
song[pos] = note;
|
||||||
|
play(note);
|
||||||
|
cs = us = TAR = 0;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
__delay_cycles(0x200);
|
||||||
|
}
|
||||||
|
P1OUT &= ~LED1;
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
|
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
|
||||||
|
|
||||||
P1DIR = BIT6; // set P1.6 to output
|
P2DIR = 0; // P2 all input
|
||||||
P1SEL |= BIT6; // P1.6 to TA0.1
|
P1DIR = OUTPUT | LED1; // P1.6, P1.0 output (P1.3 input)
|
||||||
|
P1SEL |= OUTPUT; // P1.6 to TA0.1
|
||||||
|
P1REN = BUTTON; // button resistor pullup/pulldown
|
||||||
|
P1IE = BUTTON; // enable button interrupt
|
||||||
|
|
||||||
CCTL1 = OUTMOD_7; // CCR1 reset/set
|
CCTL1 = OUTMOD_7; // CCR1 reset/set
|
||||||
TACTL = TASSEL_2 | MC_1; // SMCLK, up to CCR0
|
TACTL = TASSEL_2 | MC_1; // SMCLK (1 MHz) count up to CCRO
|
||||||
TACCTL0 = CCIE; // enable interrupt
|
TACCTL0 = CCIE; // enable clock interrupt
|
||||||
|
|
||||||
__enable_interrupt();
|
__enable_interrupt();
|
||||||
|
|
||||||
unsigned int rec[10] = {n, b, b, b, b, b, b, b, b, b};
|
unsigned int rec[MAXNOTES];
|
||||||
unsigned int rec_len[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
|
unsigned int len[MAXNOTES];
|
||||||
unsigned int position = 1;
|
unsigned int length = record(rec, len);
|
||||||
TAR = 0;
|
sing(rec, len, length);
|
||||||
while (position < 10) {
|
|
||||||
if (whole[P1IN & 0xF] != rec[position - 1]) {
|
|
||||||
rec_len[position - 1] = cs + (us + TAR)/10000;
|
|
||||||
rec[position] = whole[P1IN & 0xF];
|
|
||||||
play(rec[position]);
|
|
||||||
cs = us = TAR = 0;
|
|
||||||
position++;
|
|
||||||
}
|
|
||||||
__delay_cycles(0x200);
|
|
||||||
}
|
|
||||||
|
|
||||||
sing(rec, rec_len, 10);
|
|
||||||
//clear();
|
|
||||||
//sing(hbd, hbd_lengths, 30);
|
|
||||||
//sing(scale, scale_lengths, 13);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__TI_COMPILER_VERSION__)
|
#if defined(__TI_COMPILER_VERSION__)
|
||||||
|
@ -110,12 +103,25 @@ __interrupt void timer0_a0_isr(void)
|
||||||
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) timer0_a0_isr (void)
|
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) timer0_a0_isr (void)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
unsigned int us_left = 60000 - us; // count up to 60 000 microseconds in us
|
unsigned int us_left = USINT - us; // count up to USINT microseconds in us
|
||||||
if (us_left > TACCR0) {
|
if (us_left < TACCR0) {
|
||||||
cs += 6; // add six centiseconds to count
|
cs += CSINT; // add centiseconds to count
|
||||||
us = TACCR0 - us_left; // save overflow
|
us = TACCR0 - us_left; // save overflow
|
||||||
} else {
|
} else {
|
||||||
us += TACCR0;
|
us += TACCR0;
|
||||||
}
|
}
|
||||||
TACCTL0 &= ~CCIFG; // set interrupt flag to 0
|
TACCTL0 &= ~CCIFG; // set interrupt flag to 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__TI_COMPILER_VERSION__)
|
||||||
|
#pragma vector=PORT1_VECTOR
|
||||||
|
__interrupt void port1_isr(void)
|
||||||
|
#else
|
||||||
|
void __attribute__ ((interrupt(PORT1_VECTOR))) port1_isr (void)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
recording = false; // stop recording
|
||||||
|
P1OUT ^= BIT0;
|
||||||
|
P1IFG &= ~BIT3; // set interrupt flag to 0
|
||||||
|
P1IE &= ~BIT3; // disable interrupts for button
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue