These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /******************** (C) COPYRIGHT 2010 NXPSemiconductors ************
frank26080115 0:bf7b9fba3924 2 * @file dac_wave_generate.c
frank26080115 0:bf7b9fba3924 3 * @purpose This example describes how to use DAC to generate a sine wave,
frank26080115 0:bf7b9fba3924 4 * triangle wave or escalator wave
frank26080115 0:bf7b9fba3924 5 * @version 1.0
frank26080115 0:bf7b9fba3924 6 * @date 16. July. 2010
frank26080115 0:bf7b9fba3924 7 * @author NXP MCU SW Application Team
frank26080115 0:bf7b9fba3924 8 *---------------------------------------------------------------------
frank26080115 0:bf7b9fba3924 9 * Software that is described herein is for illustrative purposes only
frank26080115 0:bf7b9fba3924 10 * which provides customers with programming information regarding the
frank26080115 0:bf7b9fba3924 11 * products. This software is supplied "AS IS" without any warranties.
frank26080115 0:bf7b9fba3924 12 * NXP Semiconductors assumes no responsibility or liability for the
frank26080115 0:bf7b9fba3924 13 * use of the software, conveys no license or title under any patent,
frank26080115 0:bf7b9fba3924 14 * copyright, or mask work right to the product. NXP Semiconductors
frank26080115 0:bf7b9fba3924 15 * reserves the right to make changes in the software without
frank26080115 0:bf7b9fba3924 16 * notification. NXP Semiconductors also make no representation or
frank26080115 0:bf7b9fba3924 17 * warranty that such application will be suitable for the specified
frank26080115 0:bf7b9fba3924 18 * use without further testing or modification.
frank26080115 0:bf7b9fba3924 19 **********************************************************************/
frank26080115 0:bf7b9fba3924 20 #include "lpc17xx_dac.h"
frank26080115 0:bf7b9fba3924 21 #include "lpc17xx_libcfg.h"
frank26080115 0:bf7b9fba3924 22 #include "lpc17xx_pinsel.h"
frank26080115 0:bf7b9fba3924 23 #include "lpc17xx_gpdma.h"
frank26080115 0:bf7b9fba3924 24 #include "debug_frmwrk.h"
frank26080115 0:bf7b9fba3924 25
frank26080115 0:bf7b9fba3924 26 /* Example group ----------------------------------------------------------- */
frank26080115 0:bf7b9fba3924 27 /** @defgroup DAC_WaveGenerate WaveGenerate
frank26080115 0:bf7b9fba3924 28 * @ingroup DAC_Examples
frank26080115 0:bf7b9fba3924 29 * @{
frank26080115 0:bf7b9fba3924 30 */
frank26080115 0:bf7b9fba3924 31
frank26080115 0:bf7b9fba3924 32 /************************** PRIVATE MACROS *************************/
frank26080115 0:bf7b9fba3924 33 /** DMA size of transfer */
frank26080115 0:bf7b9fba3924 34 #define DMA_SIZE_SINE 60
frank26080115 0:bf7b9fba3924 35 #define NUM_SAMPLE_SINE 60
frank26080115 0:bf7b9fba3924 36 #define DMA_SIZE 64
frank26080115 0:bf7b9fba3924 37 #define NUM_SAMPLE 64
frank26080115 0:bf7b9fba3924 38
frank26080115 0:bf7b9fba3924 39 #define SIGNAL_FREQ_IN_HZ 60
frank26080115 0:bf7b9fba3924 40 #define PCLK_DAC_IN_MHZ 25 //CCLK divided by 4
frank26080115 0:bf7b9fba3924 41
frank26080115 0:bf7b9fba3924 42 #define DAC_GENERATE_SINE 1
frank26080115 0:bf7b9fba3924 43 #define DAC_GENERATE_TRIANGLE 2
frank26080115 0:bf7b9fba3924 44 #define DAC_GENERATE_ESCALATOR 3
frank26080115 0:bf7b9fba3924 45 #define DAC_GENERATE_NONE 0
frank26080115 0:bf7b9fba3924 46
frank26080115 0:bf7b9fba3924 47 /************************** PRIVATE VARIABLES *************************/
frank26080115 0:bf7b9fba3924 48 uint8_t menu1[] =
frank26080115 0:bf7b9fba3924 49 "********************************************************************************\n\r"
frank26080115 0:bf7b9fba3924 50 "Hello NXP Semiconductors \n\r"
frank26080115 0:bf7b9fba3924 51 " DAC generate signals demo \n\r"
frank26080115 0:bf7b9fba3924 52 "\t - MCU: LPC17xx \n\r"
frank26080115 0:bf7b9fba3924 53 "\t - Core: ARM Cortex-M3 \n\r"
frank26080115 0:bf7b9fba3924 54 "\t - Communicate via: UART0 - 115200 kbps \n\r"
frank26080115 0:bf7b9fba3924 55 " Use DAC to generate sine, triangle, escalator wave, frequency adjustable\n\r"
frank26080115 0:bf7b9fba3924 56 " Signal samples are transmitted to DAC by DMA memory to peripheral\n\r"
frank26080115 0:bf7b9fba3924 57 "********************************************************************************\n\r";
frank26080115 0:bf7b9fba3924 58
frank26080115 0:bf7b9fba3924 59 /************************** PRIVATE FUNCTION *************************/
frank26080115 0:bf7b9fba3924 60 void print_menu(void);
frank26080115 0:bf7b9fba3924 61
frank26080115 0:bf7b9fba3924 62 /*-------------------------PRIVATE FUNCTIONS------------------------------*/
frank26080115 0:bf7b9fba3924 63 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 64 * @brief Print menu
frank26080115 0:bf7b9fba3924 65 * @param[in] None
frank26080115 0:bf7b9fba3924 66 * @return None
frank26080115 0:bf7b9fba3924 67 **********************************************************************/
frank26080115 0:bf7b9fba3924 68 void print_menu(void)
frank26080115 0:bf7b9fba3924 69 {
frank26080115 0:bf7b9fba3924 70 _DBG(menu1);
frank26080115 0:bf7b9fba3924 71 }
frank26080115 0:bf7b9fba3924 72
frank26080115 0:bf7b9fba3924 73 /*-------------------------MAIN FUNCTION------------------------------*/
frank26080115 0:bf7b9fba3924 74 /*********************************************************************//**
frank26080115 0:bf7b9fba3924 75 * @brief c_entry: Main DAC program body
frank26080115 0:bf7b9fba3924 76 * @param[in] None
frank26080115 0:bf7b9fba3924 77 * @return int
frank26080115 0:bf7b9fba3924 78 **********************************************************************/
frank26080115 0:bf7b9fba3924 79 int c_entry(void)
frank26080115 0:bf7b9fba3924 80 {
frank26080115 0:bf7b9fba3924 81 PINSEL_CFG_Type PinCfg;
frank26080115 0:bf7b9fba3924 82 DAC_CONVERTER_CFG_Type DAC_ConverterConfigStruct;
frank26080115 0:bf7b9fba3924 83 GPDMA_Channel_CFG_Type GPDMACfg;
frank26080115 0:bf7b9fba3924 84 GPDMA_LLI_Type DMA_LLI_Struct;
frank26080115 0:bf7b9fba3924 85 uint32_t tmp;
frank26080115 0:bf7b9fba3924 86 uint8_t i,option;
frank26080115 0:bf7b9fba3924 87 uint32_t sin_0_to_90_16_samples[16]={\
frank26080115 0:bf7b9fba3924 88 0,1045,2079,3090,4067,\
frank26080115 0:bf7b9fba3924 89 5000,5877,6691,7431,8090,\
frank26080115 0:bf7b9fba3924 90 8660,9135,9510,9781,9945,10000\
frank26080115 0:bf7b9fba3924 91 };
frank26080115 0:bf7b9fba3924 92 uint32_t dac_lut[NUM_SAMPLE];
frank26080115 0:bf7b9fba3924 93
frank26080115 0:bf7b9fba3924 94 /*
frank26080115 0:bf7b9fba3924 95 * Init DAC pin connect
frank26080115 0:bf7b9fba3924 96 * AOUT on P0.26
frank26080115 0:bf7b9fba3924 97 */
frank26080115 0:bf7b9fba3924 98 PinCfg.Funcnum = 2;
frank26080115 0:bf7b9fba3924 99 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 100 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 101 PinCfg.Pinnum = 26;
frank26080115 0:bf7b9fba3924 102 PinCfg.Portnum = 0;
frank26080115 0:bf7b9fba3924 103 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 104
frank26080115 0:bf7b9fba3924 105 /* Initialize debug via UART0
frank26080115 0:bf7b9fba3924 106 * – 115200bps
frank26080115 0:bf7b9fba3924 107 * – 8 data bit
frank26080115 0:bf7b9fba3924 108 * – No parity
frank26080115 0:bf7b9fba3924 109 * – 1 stop bit
frank26080115 0:bf7b9fba3924 110 * – No flow control
frank26080115 0:bf7b9fba3924 111 */
frank26080115 0:bf7b9fba3924 112 debug_frmwrk_init();
frank26080115 0:bf7b9fba3924 113
frank26080115 0:bf7b9fba3924 114 // print welcome screen
frank26080115 0:bf7b9fba3924 115 print_menu();
frank26080115 0:bf7b9fba3924 116
frank26080115 0:bf7b9fba3924 117 while(1)
frank26080115 0:bf7b9fba3924 118 {
frank26080115 0:bf7b9fba3924 119 //Select option
frank26080115 0:bf7b9fba3924 120 _DBG("Please choose signal type:\n\r");
frank26080115 0:bf7b9fba3924 121 _DBG("\t1) Sine wave.\n\r");
frank26080115 0:bf7b9fba3924 122 _DBG("\t2) Triangle wave.\n\r");
frank26080115 0:bf7b9fba3924 123 _DBG("\t3) Escalator wave.\n\r");
frank26080115 0:bf7b9fba3924 124 option = DAC_GENERATE_NONE;
frank26080115 0:bf7b9fba3924 125 while(option == DAC_GENERATE_NONE)
frank26080115 0:bf7b9fba3924 126 {
frank26080115 0:bf7b9fba3924 127 switch(_DG)
frank26080115 0:bf7b9fba3924 128 {
frank26080115 0:bf7b9fba3924 129 case '1': option = DAC_GENERATE_SINE; break;
frank26080115 0:bf7b9fba3924 130 case '2': option = DAC_GENERATE_TRIANGLE; break;
frank26080115 0:bf7b9fba3924 131 case '3': option = DAC_GENERATE_ESCALATOR; break;
frank26080115 0:bf7b9fba3924 132 default:
frank26080115 0:bf7b9fba3924 133 _DBG("Wrong choice! Please select 1 or 2 or 3 only!\n\r");
frank26080115 0:bf7b9fba3924 134 option = DAC_GENERATE_NONE;
frank26080115 0:bf7b9fba3924 135 break;
frank26080115 0:bf7b9fba3924 136 }
frank26080115 0:bf7b9fba3924 137 }
frank26080115 0:bf7b9fba3924 138
frank26080115 0:bf7b9fba3924 139 //Prepare DAC look up table
frank26080115 0:bf7b9fba3924 140 switch(option)
frank26080115 0:bf7b9fba3924 141 {
frank26080115 0:bf7b9fba3924 142 case DAC_GENERATE_SINE:
frank26080115 0:bf7b9fba3924 143 for(i=0;i<NUM_SAMPLE_SINE;i++)
frank26080115 0:bf7b9fba3924 144 {
frank26080115 0:bf7b9fba3924 145 if(i<=15)
frank26080115 0:bf7b9fba3924 146 {
frank26080115 0:bf7b9fba3924 147 dac_lut[i] = 512 + 512*sin_0_to_90_16_samples[i]/10000;
frank26080115 0:bf7b9fba3924 148 if(i==15) dac_lut[i]= 1023;
frank26080115 0:bf7b9fba3924 149 }
frank26080115 0:bf7b9fba3924 150 else if(i<=30)
frank26080115 0:bf7b9fba3924 151 {
frank26080115 0:bf7b9fba3924 152 dac_lut[i] = 512 + 512*sin_0_to_90_16_samples[30-i]/10000;
frank26080115 0:bf7b9fba3924 153 }
frank26080115 0:bf7b9fba3924 154 else if(i<=45)
frank26080115 0:bf7b9fba3924 155 {
frank26080115 0:bf7b9fba3924 156 dac_lut[i] = 512 - 512*sin_0_to_90_16_samples[i-30]/10000;
frank26080115 0:bf7b9fba3924 157 }
frank26080115 0:bf7b9fba3924 158 else
frank26080115 0:bf7b9fba3924 159 {
frank26080115 0:bf7b9fba3924 160 dac_lut[i] = 512 - 512*sin_0_to_90_16_samples[60-i]/10000;
frank26080115 0:bf7b9fba3924 161 }
frank26080115 0:bf7b9fba3924 162 dac_lut[i] = (dac_lut[i]<<6);
frank26080115 0:bf7b9fba3924 163 }
frank26080115 0:bf7b9fba3924 164 break;
frank26080115 0:bf7b9fba3924 165 case DAC_GENERATE_TRIANGLE:
frank26080115 0:bf7b9fba3924 166 for(i=0;i<NUM_SAMPLE;i++)
frank26080115 0:bf7b9fba3924 167 {
frank26080115 0:bf7b9fba3924 168 if(i<32) dac_lut[i]= 32*i;
frank26080115 0:bf7b9fba3924 169 else if (i==32) dac_lut[i]= 1023;
frank26080115 0:bf7b9fba3924 170 else dac_lut[i] = 32*(NUM_SAMPLE-i);
frank26080115 0:bf7b9fba3924 171 dac_lut[i] = (dac_lut[i]<<6);
frank26080115 0:bf7b9fba3924 172 }
frank26080115 0:bf7b9fba3924 173 break;
frank26080115 0:bf7b9fba3924 174 case DAC_GENERATE_ESCALATOR:
frank26080115 0:bf7b9fba3924 175 for(i=0;i<NUM_SAMPLE;i++)
frank26080115 0:bf7b9fba3924 176 {
frank26080115 0:bf7b9fba3924 177 dac_lut[i] = (1023/3)*(i/16);
frank26080115 0:bf7b9fba3924 178 dac_lut[i] = (dac_lut[i]<<6);
frank26080115 0:bf7b9fba3924 179 }
frank26080115 0:bf7b9fba3924 180 break;
frank26080115 0:bf7b9fba3924 181 default: break;
frank26080115 0:bf7b9fba3924 182 }
frank26080115 0:bf7b9fba3924 183
frank26080115 0:bf7b9fba3924 184 //Prepare DMA link list item structure
frank26080115 0:bf7b9fba3924 185 DMA_LLI_Struct.SrcAddr= (uint32_t)dac_lut;
frank26080115 0:bf7b9fba3924 186 DMA_LLI_Struct.DstAddr= (uint32_t)&(LPC_DAC->DACR);
frank26080115 0:bf7b9fba3924 187 DMA_LLI_Struct.NextLLI= (uint32_t)&DMA_LLI_Struct;
frank26080115 0:bf7b9fba3924 188 DMA_LLI_Struct.Control= ((option==DAC_GENERATE_SINE)?DMA_SIZE_SINE:DMA_SIZE)
frank26080115 0:bf7b9fba3924 189 | (2<<18) //source width 32 bit
frank26080115 0:bf7b9fba3924 190 | (2<<21) //dest. width 32 bit
frank26080115 0:bf7b9fba3924 191 | (1<<26) //source increment
frank26080115 0:bf7b9fba3924 192 ;
frank26080115 0:bf7b9fba3924 193
frank26080115 0:bf7b9fba3924 194
frank26080115 0:bf7b9fba3924 195 /* GPDMA block section -------------------------------------------- */
frank26080115 0:bf7b9fba3924 196 /* Initialize GPDMA controller */
frank26080115 0:bf7b9fba3924 197 GPDMA_Init();
frank26080115 0:bf7b9fba3924 198
frank26080115 0:bf7b9fba3924 199 // Setup GPDMA channel --------------------------------
frank26080115 0:bf7b9fba3924 200 // channel 0
frank26080115 0:bf7b9fba3924 201 GPDMACfg.ChannelNum = 0;
frank26080115 0:bf7b9fba3924 202 // Source memory
frank26080115 0:bf7b9fba3924 203 GPDMACfg.SrcMemAddr = (uint32_t)(dac_lut);
frank26080115 0:bf7b9fba3924 204 // Destination memory - unused
frank26080115 0:bf7b9fba3924 205 GPDMACfg.DstMemAddr = 0;
frank26080115 0:bf7b9fba3924 206 // Transfer size
frank26080115 0:bf7b9fba3924 207 GPDMACfg.TransferSize = ((option==DAC_GENERATE_SINE)?DMA_SIZE_SINE:DMA_SIZE);
frank26080115 0:bf7b9fba3924 208 // Transfer width - unused
frank26080115 0:bf7b9fba3924 209 GPDMACfg.TransferWidth = 0;
frank26080115 0:bf7b9fba3924 210 // Transfer type
frank26080115 0:bf7b9fba3924 211 GPDMACfg.TransferType = GPDMA_TRANSFERTYPE_M2P;
frank26080115 0:bf7b9fba3924 212 // Source connection - unused
frank26080115 0:bf7b9fba3924 213 GPDMACfg.SrcConn = 0;
frank26080115 0:bf7b9fba3924 214 // Destination connection
frank26080115 0:bf7b9fba3924 215 GPDMACfg.DstConn = GPDMA_CONN_DAC;
frank26080115 0:bf7b9fba3924 216 // Linker List Item - unused
frank26080115 0:bf7b9fba3924 217 GPDMACfg.DMALLI = (uint32_t)&DMA_LLI_Struct;
frank26080115 0:bf7b9fba3924 218 // Setup channel with given parameter
frank26080115 0:bf7b9fba3924 219 GPDMA_Setup(&GPDMACfg);
frank26080115 0:bf7b9fba3924 220
frank26080115 0:bf7b9fba3924 221 DAC_ConverterConfigStruct.CNT_ENA =SET;
frank26080115 0:bf7b9fba3924 222 DAC_ConverterConfigStruct.DMA_ENA = SET;
frank26080115 0:bf7b9fba3924 223 DAC_Init(LPC_DAC);
frank26080115 0:bf7b9fba3924 224 /* set time out for DAC*/
frank26080115 0:bf7b9fba3924 225 tmp = (PCLK_DAC_IN_MHZ*1000000)/(SIGNAL_FREQ_IN_HZ*((option==DAC_GENERATE_SINE)?NUM_SAMPLE_SINE:NUM_SAMPLE));
frank26080115 0:bf7b9fba3924 226 DAC_SetDMATimeOut(LPC_DAC,tmp);
frank26080115 0:bf7b9fba3924 227 DAC_ConfigDAConverterControl(LPC_DAC, &DAC_ConverterConfigStruct);
frank26080115 0:bf7b9fba3924 228
frank26080115 0:bf7b9fba3924 229 //Start the demo
frank26080115 0:bf7b9fba3924 230 switch(option)
frank26080115 0:bf7b9fba3924 231 {
frank26080115 0:bf7b9fba3924 232 case DAC_GENERATE_SINE:
frank26080115 0:bf7b9fba3924 233 _DBG("\n\rDAC is generating 60Hz sine wave...");
frank26080115 0:bf7b9fba3924 234 break;
frank26080115 0:bf7b9fba3924 235 case DAC_GENERATE_TRIANGLE:
frank26080115 0:bf7b9fba3924 236 _DBG("\n\rDAC is generating 60Hz triangle wave...");
frank26080115 0:bf7b9fba3924 237 break;
frank26080115 0:bf7b9fba3924 238 case DAC_GENERATE_ESCALATOR:
frank26080115 0:bf7b9fba3924 239 _DBG("\n\rDAC is generating 60Hz escalator wave...");
frank26080115 0:bf7b9fba3924 240 break;
frank26080115 0:bf7b9fba3924 241 default: break;
frank26080115 0:bf7b9fba3924 242 }
frank26080115 0:bf7b9fba3924 243
frank26080115 0:bf7b9fba3924 244 // Enable GPDMA channel 0
frank26080115 0:bf7b9fba3924 245 GPDMA_ChannelCmd(0, ENABLE);
frank26080115 0:bf7b9fba3924 246
frank26080115 0:bf7b9fba3924 247 _DBG_("\n\rPreass ESC if you want to terminate!");
frank26080115 0:bf7b9fba3924 248 while(_DG!=27);
frank26080115 0:bf7b9fba3924 249
frank26080115 0:bf7b9fba3924 250 // Disable GPDMA channel 0
frank26080115 0:bf7b9fba3924 251 GPDMA_ChannelCmd(0, DISABLE);
frank26080115 0:bf7b9fba3924 252
frank26080115 0:bf7b9fba3924 253 }
frank26080115 0:bf7b9fba3924 254 return 1;
frank26080115 0:bf7b9fba3924 255 }
frank26080115 0:bf7b9fba3924 256 /* With ARM and GHS toolsets, the entry point is main() - this will
frank26080115 0:bf7b9fba3924 257 allow the linker to generate wrapper code to setup stacks, allocate
frank26080115 0:bf7b9fba3924 258 heap area, and initialize and copy code and data segments. For GNU
frank26080115 0:bf7b9fba3924 259 toolsets, the entry point is through __start() in the crt0_gnu.asm
frank26080115 0:bf7b9fba3924 260 file, and that startup code will setup stacks and data */
frank26080115 0:bf7b9fba3924 261 int main(void)
frank26080115 0:bf7b9fba3924 262 {
frank26080115 0:bf7b9fba3924 263 return c_entry();
frank26080115 0:bf7b9fba3924 264 }
frank26080115 0:bf7b9fba3924 265
frank26080115 0:bf7b9fba3924 266 #ifdef DEBUG
frank26080115 0:bf7b9fba3924 267 /*******************************************************************************
frank26080115 0:bf7b9fba3924 268 * @brief Reports the name of the source file and the source line number
frank26080115 0:bf7b9fba3924 269 * where the CHECK_PARAM error has occurred.
frank26080115 0:bf7b9fba3924 270 * @param[in] file Pointer to the source file name
frank26080115 0:bf7b9fba3924 271 * @param[in] line assert_param error line source number
frank26080115 0:bf7b9fba3924 272 * @return None
frank26080115 0:bf7b9fba3924 273 *******************************************************************************/
frank26080115 0:bf7b9fba3924 274 void check_failed(uint8_t *file, uint32_t line)
frank26080115 0:bf7b9fba3924 275 {
frank26080115 0:bf7b9fba3924 276 /* User can add his own implementation to report the file name and line number,
frank26080115 0:bf7b9fba3924 277 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
frank26080115 0:bf7b9fba3924 278
frank26080115 0:bf7b9fba3924 279 /* Infinite loop */
frank26080115 0:bf7b9fba3924 280 while(1);
frank26080115 0:bf7b9fba3924 281 }
frank26080115 0:bf7b9fba3924 282 #endif
frank26080115 0:bf7b9fba3924 283 /*
frank26080115 0:bf7b9fba3924 284 * @}
frank26080115 0:bf7b9fba3924 285 */