Skip to content
Permalink
efee4431d3
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
227 lines (209 sloc) 7.89 KB
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: DAC Sine Example
* for ModusToolbox. This example demonstrate two of five DAC
* configurations. It generates an analog sine wave on DAC0
* Output and a static analog value on DAC1 Output. The frequency
* and time duration of the sine waves are adjusted to play a melody
* out of the DAC0 output.
*
* Related Document: See README.md
*
******************************************************************************
*
* Copyright (c) 2015-2022, Infineon Technologies AG
* All rights reserved.
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
#include "cybsp.h"
#include "cy_utils.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Defines
*******************************************************************************/
#define R (20000U) /* Frequency outside the audible range */
#define ARRAY_SIZE (48U) /* Size of the arrays for melody frequency and beat size */
#define TICKS_PER_SECOND (100U) /* Systick ticks per second */
#define SINE_CHANNEL (0U) /*DAC channel for sine output*/
#define STATIC_CHANNEL (1U) /*DAC channel for static output*/
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/* Define macro to set the loop count before printing debug messages */
#if ENABLE_XMC_DEBUG_PRINT
static bool DAC_updated = false;
static bool loop_entered = false;
#endif
/*******************************************************************************
* Global Variables
*******************************************************************************/
/*
* The values in melody[index] defines the new frequency. Frequencies defined by R
* are outside audible range and act as pauses in the melody being played.
*/
const uint16_t melody[ARRAY_SIZE] =
{
660U, R, 660U, R, 660U, R, 510U, R,
660U, R, 770U, R, 380U, R, 510U, R,
380U, R, 320U, R, 440U, R, 480U, R,
450U, R, 430U, R, 380U, R, 660U, R,
760U, R, 860U, R, 700U, R, 760U, R,
660U, R, 520U, R, 580U, R, 480U, R
};
/*
* The value in beats[index] defines the number of function calls
* after which the frequency will be updated. This defines the duration
* and pause times in the melody being played
*/
const uint16_t beats[ARRAY_SIZE] =
{
10U, 15U, 10U, 30U, 10U, 30U, 10U, 10U,
10U, 30U, 10U, 55U, 10U, 57U, 10U, 45U,
10U, 40U, 10U, 50U, 10U, 30U, 8U, 33U,
10U, 15U, 10U, 30U, 10U, 20U, 8U, 20U,
5U, 15U, 10U, 30U, 8U, 15U, 5U, 35U,
8U, 30U, 8U, 15U, 8U, 15U, 8U, 50U
};
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
void DacUpdate(void);
/*******************************************************************************
* Function Name: SysTick_Handler
********************************************************************************
* Summary:
* Handler for Systick interrupt. To change the frequency of the sine wave,
* function DAC_Update() is called periodically. The sine wave generation is
* taken care by the DAC module and do not require any periodic function calls.
* DAC_Update() has to be called only to change frequency of basic sine wave
* generated.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void SysTick_Handler(void)
{
DacUpdate();
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary
* This is the main function. Initializes both DAC channels. Sets DAC0 in pattern
* mode and DAC1 in single value mode. Systick timer is also configured, which will
* take care of the frequency update of the pattern being generated.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
cy_retarget_io_init(CYBSP_DEBUG_UART_HW);
#if ENABLE_XMC_DEBUG_PRINT
printf("Initialization done\r\n");
#endif
/* System timer configuration */
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
while (1)
{
#if ENABLE_XMC_DEBUG_PRINT
if(DAC_updated)
{
if(!loop_entered)
{
printf("DAC Updated\r\n");
loop_entered = true;
}
}
#endif
}
}
/*******************************************************************************
* Function Name: DacUpdate
********************************************************************************
* Summary:
* Updates the DAC frequency after beats[index] function calls.
* New frequency value is melody[index]. Each frequncy value in melody[index] denote
* a musical note to be played. Value in beats[index] determines the duration for which
* each note is being played.
*
* e.g.:
* index=0, beats[index]= 10, melody[index] =660 and Dac_Update is called after 10ms:
* Sine wave with 660Hz will be displayed for 100ms.
*
*
* If melody[index] =R, frequency is shifted out of audible area. Corresponding values
* in beats[index] determines the pause duration in the melody being played .
*
* Parameters:
* none
*
* Return:
* none
*******************************************************************************/
void DacUpdate(void)
{
/* Variable to track function call limit */
static uint8_t function_calls = 0;
/* Index variable to track and update frequency */
static uint8_t index = 0;
/* Increment function call count */
function_calls++;
/* check if function_call limit is reached.*/
if (function_calls >= beats[index])
{
function_calls = 0;
/* Increment index value for new frequency */
index++;
/* avoid array overflow.*/
if (index >= ARRAY_SIZE)
{
index = 0;
}
/* update frequency */
XMC_DAC_CH_SetPatternFrequency(XMC_DAC0, SINE_CHANNEL, melody[index]);
#if ENABLE_XMC_DEBUG_PRINT
DAC_updated = true;
#endif
}
}
/* [] END OF FILE */