0% found this document useful (0 votes)
76 views2 pages

Original Lab DAC

This document describes code for a 4-key digital piano that uses SysTick interrupts to implement the tones. Port B bits 3-0 control the 4-bit DAC for sound output and Port E bits 3-0 are connected to the 4 piano keys as inputs. The code initializes the SysTick timer, DAC, and piano keys. It then continuously reads the piano keys and plays the corresponding tone using the DAC if a new key is pressed.

Uploaded by

Anet Augustin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Original Lab DAC

This document describes code for a 4-key digital piano that uses SysTick interrupts to implement the tones. Port B bits 3-0 control the 4-bit DAC for sound output and Port E bits 3-0 are connected to the 4 piano keys as inputs. The code initializes the SysTick timer, DAC, and piano keys. It then continuously reads the piano keys and plays the corresponding tone using the DAC if a new key is pressed.

Uploaded by

Anet Augustin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//

//
//
//
//
//
//
//

Lab13.c
Runs on LM4F120 or TM4C123
Use SysTick interrupts to implement a 4-key digital piano
edX Lab 13
Daniel Valvano, Jonathan Valvano
December 29, 2014
Port B bits 3-0 have the 4-bit DAC
Port E bits 3-0 have 4 piano keys

#include
#include
#include
#include

"..//tm4c123gh6pm.h"
"Sound.h"
"Piano.h"
"TExaS.h"

#define GPIO_PORTB_DATA_R
#define GPIO_PORTB_DIR_R
#define GPIO_PORTB_AFSEL_R
#define GPIO_PORTB_PUR_R
#define GPIO_PORTB_DEN_R
#define GPIO_PORTB_LOCK_R
#define GPIO_PORTB_CR_R
#define GPIO_PORTB_AMSEL_R
#define GPIO_PORTB_PCTL_R
#define GPIO_PORTB_DR8R_R
// Port E:
#define GPIO_PORTE_DATA_R
#define GPIO_PORTE_DIR_R
#define GPIO_PORTE_AFSEL_R
#define GPIO_PORTE_PUR_R
#define GPIO_PORTE_DEN_R
#define GPIO_PORTE_LOCK_R
#define GPIO_PORTE_CR_R
#define GPIO_PORTE_AMSEL_R
#define GPIO_PORTE_PCTL_R
// Clock:
#define SYSCTL_RCGC2_R
// SysTick Timer:
#define NVIC_ST_CTRL_R
#define NVIC_ST_RELOAD_R
#define NVIC_ST_CURRENT_R

(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile

unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned

long
long
long
long
long
long
long
long
long
long

*)0x400053FC))
*)0x40005400))
*)0x40005420))
*)0x40005510))
*)0x4000551C))
*)0x40005520))
*)0x40005524))
*)0x40005528))
*)0x4000552C))
*)0x40005508))

(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile
(*((volatile

unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned
unsigned

long
long
long
long
long
long
long
long
long

*)0x400243FC))
*)0x40024400))
*)0x40024420))
*)0x40024510))
*)0x4002451C))
*)0x40024520))
*)0x40024524))
*)0x40024528))
*)0x4002452C))

(*((volatile unsigned long *)0x400FE108))


(*((volatile unsigned long *)0xE000E010))
(*((volatile unsigned long *)0xE000E014))
(*((volatile unsigned long *)0xE000E018))

// basic functions defined at end of startup.s


void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void); // Enable interrupts
void delay(unsigned long msec);
int piano;
int prevPiano;
int main(void){ // Real Lab13
// for the real board grader to work
// you must connect PD3 to your DAC output
TExaS_Init(SW_PIN_PE3210, DAC_PIN_PB3210,ScopeOn); // activate grader and set
system clock to 80 MHz
// PortE used for piano keys, PortB used for DAC
Sound_Init(); // initialize SysTick timer and DAC
Piano_Init();
EnableInterrupts(); // enable after all initialization are done
while(1){

// input from keys to select tone


piano = Piano_In();
if(piano != prevPiano){ // only react if piano keys pressed
if(piano == 0x00)
Sound_Off();
else if(piano == 0x01)
Sound_Tone(2389);
else if(piano == 0x02)
Sound_Tone(2128);
else if(piano == 0x04)
Sound_Tone(1896);
else if(piano == 0x08)
Sound_Tone(1594);
delay(10);
}
prevPiano = piano;
}
}
// Inputs: Number of msec to delay
// Outputs: None
void delay(unsigned long msec){
unsigned long count;
while(msec > 0 ) { // repeat while there are still delay
count = 16000;
// about 1ms
while (count > 0) {
count--;
} // This while loop takes approximately 3 cycles
msec--;
}
}

You might also like