Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
dac.h File Reference
#include "dac_types.h"
#include "types.h"
#include <stdbool.h>

Go to the source code of this file.

Functions

void DAC_BUZInit (u8 timer_a, u8 timer_b, u8 int_priority)
 Initializes the DAC peripheral.
 
void DAC_BUZDeinit (void)
 
void DAC_BUZPlay (DAC_Tone tone, u16 bpm)
 Plays the given note for the given duration.
 
void DAC_BUZSetVolume (u8 volume)
 Sets the volume of the buzzer.
 
bool DAC_BUZIsPlaying (void)
 
void DAC_BUZStop (void)
 

Function Documentation

◆ DAC_BUZDeinit()

void DAC_BUZDeinit ( void  )

Definition at line 76 of file dac.c.

77{
78 LPC_PINCON->PINSEL1 &= ~(3 << 20);
79 LPC_GPIO0->FIODIR &= ~(1 << 26);
80
84}
_PRIVATE TIMER DAC_SecondsTimer
Definition dac.c:9
_PRIVATE TIMER DAC_SamplesTimer
Definition dac.c:8
void DAC_BUZStop(void)
Definition dac.c:119
void TIMER_Deinit(TIMER timer)
Deconfigures a TIMER peripheral (also match registers).
Definition timer.c:124

◆ DAC_BUZInit()

void DAC_BUZInit ( u8  timer_a,
u8  timer_b,
u8  int_priority 
)

Initializes the DAC peripheral.

Parameters
int_priorityThe interrupt priority of the timer. If set to INT_PRIO_DEF, the default interrupt priority will be used.
Note
This implementation requires the use of 2 timers to feed the DAC with samples and to play the note for the given duration. By giving these parameters to this function, you ensure that those timers are not and won't be used for other purposes.

Definition at line 66 of file dac.c.

67{
68 // Enable Analog OUT (function 10) on PIN on P0.26, located in PINSEL1
69 LPC_PINCON->PINSEL1 |= (2 << 20);
70 LPC_GPIO0->FIODIR |= (1 << 26);
71
72 TIMER_Init(&DAC_SamplesTimer, timer_a, NO_PRESCALER, int_priority);
73 TIMER_Init(&DAC_SecondsTimer, timer_b, NO_PRESCALER, int_priority);
74}
void TIMER_Init(TIMER *timer, u8 which, u32 prescaler, u8 int_priority)
Initializes a TIMER peripheral.
Definition timer.c:62
#define NO_PRESCALER
Definition types.h:40

◆ DAC_BUZIsPlaying()

bool DAC_BUZIsPlaying ( void  )

Definition at line 114 of file dac.c.

115{
117}
bool TIMER_IsEnabled(TIMER timer)
Checks if a TIMER peripheral is enabled.
Definition timer.c:248

◆ DAC_BUZPlay()

void DAC_BUZPlay ( DAC_Tone  tone,
u16  bpm 
)

Plays the given note for the given duration.

Parameters
toneThe tone to be played
bpmThe tempo of the note (i.e. 60, 120, 240, etc.)

Definition at line 91 of file dac.c.

92{
93 // How many samples need to be sent to the DAC in a second in order to play the tone
96 .actions = TIM_MR_INT | TIM_MR_RES,
97 .match = get_tone_timing(tone.note, tone.octave)});
98
99 // Since a tone has a type (i.e. a duration which depends on the BPM), the duration of the tone
100 // needs to be controlled by another timer, which, on match, will stop & clear the samples timer.
101 // The duration (in seconds) of the tone is calculated by the formula: (60 * type) / bpm. Since
102 // each second the seconds timer ticks 25 million times, we need to multiply that by the duration
103 const u32 tone_duration = DAC_CLK_HZ * ((60 * tone.type) / bpm);
106 .actions = TIM_MR_INT | TIM_MR_RES | TIM_MR_STP,
107 .match = tone_duration});
108
109 // Starts the timers (i.e. plays)
112}
_PRIVATE void next_sample(void)
Definition dac.c:48
#define DAC_CLK_HZ
Definition dac.c:5
u8 octave
Definition dac_types.h:50
u16 note
Definition dac_types.h:49
u8 which
Definition timer_types.h:44
void TIMER_Enable(TIMER timer)
Enables a TIMER peripheral.
Definition timer.c:203
void TIMER_SetMatch(TIMER timer, TIMER_MatchRegister match_reg)
Sets the match value for a match register of a TIMER peripheral.
Definition timer.c:162
void TIMER_SetInterruptHandler(TIMER timer, u8 source, TIMER_InterruptHandler handler)
Sets the interrupt handler for a TIMER peripheral, on a specific source between the 4 match registers...
Definition timer_irq.c:60
@ TIM_MR0
Definition timer_types.h:20
@ TIM_MR_INT
Definition timer_types.h:30
@ TIM_MR_RES
Definition timer_types.h:31
@ TIM_MR_STP
Definition timer_types.h:32
@ TIM_INT_SRC_MR0
Definition timer_types.h:55
uint32_t u32
Definition types.h:6

◆ DAC_BUZSetVolume()

void DAC_BUZSetVolume ( u8  volume)

Sets the volume of the buzzer.

Parameters
volumeThe volume of the buzzer (between 0 and 10).

Definition at line 86 of file dac.c.

87{
88 DAC_SinVolume = (IS_BETWEEN_EQ(volume, 0, 10)) ? volume : DAC_VOL_DEFAULT;
89}
_PRIVATE u8 DAC_SinVolume
Definition dac.c:11
#define DAC_VOL_DEFAULT
Definition dac_types.h:55
#define IS_BETWEEN_EQ(value, low, hi)
Definition types.h:23

◆ DAC_BUZStop()

void DAC_BUZStop ( void  )

Definition at line 119 of file dac.c.

120{
123 LPC_DAC->DACR &= ~(0x3FF << 6);
124
127
130}
_PRIVATE u32 DAC_SinTableIndex
Definition dac.c:12
void TIMER_Reset(TIMER timer)
Resets a TIMER peripheral without deconfiguring it.
Definition timer.c:265
void TIMER_Disable(TIMER timer)
Disables a TIMER peripheral.
Definition timer.c:229